Class GraphWithPayload<V,P>

Object
GraphWithPayload<V,P>
Type Parameters:
V - vertex-type
P - edge payload-type

public class GraphWithPayload<V,P> extends Object
A graph, either directed or undirected, with edges containing a payload of type E.
Author:
Owen Feehan
  • Constructor Details

    • GraphWithPayload

      public GraphWithPayload(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

    • shallowCopy

      public GraphWithPayload<V,P> shallowCopy()
      Creates a new graph with identical elements and structure, reusing the existing vertice and edge data objects.
      Returns:
      a newly created graph, with newly created vertices and edges, but reusing the data-objects of tyoe V and E.
    • 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).
    • 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
    • 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, P edgePayload)
      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.

      Parameters:
      from - the vertex the edge joins from.
      to - the vertex the edge joins to.
      edgePayload - the payload for the 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.
    • edgesUnique

      public Set<TypedEdge<V,P>> edgesUnique()
      The edges in the graph, all of them, without any duplicates.

      This operation is more expensive than edgesMaybeDuplicates() but guarantees that no edge is repeated.

      Returns:
      a newly created set containing the edges of the graph.
    • edgesMaybeDuplicates

      public Collection<TypedEdge<V,P>> edgesMaybeDuplicates()
      The edges in the graph, all of them, but with some edges possibly duplicated.

      This operation is more cheaper than edgesUnique() but edges may exist twice.

      Returns:
      the collection (as exists internally) of edges in the graph, with some edges may exist twice.
    • 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:
      a newly created List with 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
    • outgoingEdgesFor

      public Collection<TypedEdge<V,P>> outgoingEdgesFor(V vertex)
      All outgoing edges for a given vertex.
      Parameters:
      vertex - the vertex
      Returns:
      a collection (based on an internal data structure, unduplicated) of outgoing edges for the vertex.
    • toString

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

      public String describe(boolean includeEdgePayload)
      Describes the graph in a multi-line string.
      Parameters:
      includeEdgePayload - whether to show the payload of each edge
      Returns:
      a multi-line string describing the graph