Class GraphWithoutPayload<V>

Object
GraphWithoutPayload<V>
Type Parameters:
V - vertex-type

public class GraphWithoutPayload<V> extends Object
A graph, either directed or undirected, where edges contain no payload.
Author:
Owen Feehan
  • Constructor Details

    • GraphWithoutPayload

      public GraphWithoutPayload(boolean undirected)
      Creates the graph.
      Parameters:
      undirected - true if it should be an undirected graph (an edge applies in both directions), false if it should be a directed graph (an edge applies in one direction only).
  • Method Details

    • containsVertex

      public boolean containsVertex(V vertex)
      Does the graph contain a particular vertex?
      Parameters:
      vertex - the vertex to check if it is contained
      Returns:
      true iff the graph contains the vertex
    • containsEdge

      public boolean containsEdge(V from, V to)
      Does the graph contain a particular edge?
      Parameters:
      from - the vertex the edge emanates from.
      to - the vertex the edge is connected to.
      Returns:
      true iff an edge exists from from to to.
    • addVertex

      public void addVertex(V vertex)
      Adds a vertex.
      Parameters:
      vertex - the vertex to add
    • removeVertex

      public void removeVertex(V vertex) throws OperationFailedException
      Removes a vertex and any edges connected to it.
      Parameters:
      vertex - the vertex to remove
      Throws:
      OperationFailedException - if the vertex doesn't exist in the graph.
    • addEdge

      public void addEdge(V from, V to)
      Add an edge between two vertices.

      For an undirected graph, the directionality is irrelevant, and will achieve the same effect, whatever the order of from and to.

      No error is reported if an edge already exists.

      Parameters:
      from - the vertex the edge joins from.
      to - the vertex the edge joins to.
    • addEdges

      public void addEdges(V from, Collection<V> toCollection)
      Adds edge(s) from the vertex from to each element in toCollection.

      This creates as many edges as exist elements in toCollection, unless some such edges already exist.

      No error is reported if an edge already exists.

      Parameters:
      from - the vertex the edge joins from.
      toCollection - the vertex the edge joins to, for each intended edge.
    • removeEdge

      public void removeEdge(V from, V to)
      Remove an edge between two vertices.

      For an undirected graph, the directionality is irrelevant, and will achieve the same effect, whatever the order of from and to.

      Parameters:
      from - the vertex the edge joins from.
      to - the vertex the edge joins to.
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • adjacentVerticesOutgoing

      public List<V> adjacentVerticesOutgoing(V vertex)
      The vertices that are connected to a particular vertex by an outgoing edge.
      Parameters:
      vertex - the vertex to find adjacent vertices for.
      Returns:
      all vertices to which an outgoing edge exists from vertex.
    • adjacentVerticesOutgoingStream

      public Stream<V> adjacentVerticesOutgoingStream(V vertex)
      Like adjacentVerticesOutgoing(V) but returns a Stream instead of a Set.
      Parameters:
      vertex - the vertex to find adjacent vertices for.
      Returns:
      all vertices to which an outgoing edge exists from vertex.
    • mergeVertices

      public void mergeVertices(V element1, V element2, V merged)
      Merges two existing vertices together.

      The two existing vertices are replaced with merged.

      Existing incoming and outgoing edges for the two vertices are then connected instead to merged.

      Parameters:
      element1 - the first element to merge.
      element2 - the second element to merge.
      merged - the merged element that replaces element1 and element2.
    • numberVertices

      public int numberVertices()
      The number of vertices in the graph.
      Returns:
      the number of vertices
    • numberEdges

      public int numberEdges()
      The number of edges in the graph.
      Returns:
      the number of edges
    • vertices

      public Set<V> vertices()
      The set of all vertices in the graph.
      Returns:
      the set (as is used internally within the class, without any duplication).
    • describe

      public String describe()
      Describes the graph in a multi-line string.
      Returns:
      a multi-line string describing the graph