Interface UndirectedGraph<V,E>

All Known Implementing Classes:
UndirectedGraphImpl

public interface UndirectedGraph<V,E>
An undirected graph implementation. Note that there is no guarantee that the indices of the vertex or edges are contiguous. To iterate over the vertices or the edges, it is recommended to use getVertices() and getEdges().
Author:
Geoffroy Jamgotchian <geoffroy.jamgotchian at rte-france.com>
  • Method Details

    • addVertex

      int addVertex()
      Create a new vertex and notify the UndirectedGraphListeners.
      Returns:
      the index of the new vertex.
    • addVertexIfNotPresent

      default void addVertexIfNotPresent(int v)
      If the specified vertex does not exist or is null, create it and notify the UndirectedGraphListener
    • vertexExists

      default boolean vertexExists(int v)
      Check if a specified vertex exists. This method throws a PowsyblException if the vertex index is invalid (negative).
    • removeVertex

      V removeVertex(int v)
      Remove the specified vertex and notify the UndirectedGraphListeners. This method throws a PowsyblException if the vertex doesn't exist or if an edge is connected to this vertex.
      Parameters:
      v - the vertex index to remove.
      Returns:
      the value attached to the vertex.
    • getVertexCount

      int getVertexCount()
      Return the number of non-null vertices. As the contiguity of vertices is not mandatory, the number of vertices can be less than the highest vertex index.
      Returns:
      the number of vertices.
    • addEdge

      int addEdge(int v1, int v2, E obj)
      Create an edge between the two specified vertices and notify the UndirectedGraphListeners. This method throws a PowsyblException if one of the vertices doesn't exist.
      Parameters:
      v1 - the first end of the edge.
      v2 - the second end of the edge.
      obj - the value attached to the edge.
      Returns:
      the index of the new edge.
    • removeEdge

      E removeEdge(int e)
      Remove the specified edge and notify the UndirectedGraphListeners. This method thows a PowsyblException if the edge doesn't exist.
      Parameters:
      e - the edge index to remove.
      Returns:
      the value attached to the edge.
    • removeAllEdges

      void removeAllEdges()
      Remove all the edges and notify the UndirectedGraphListeners.
    • getEdgeCount

      int getEdgeCount()
      Return the number of edges.
      Returns:
      the number of edges.
    • getEdges

      int[] getEdges()
      Return the indices of the edges.
      Returns:
      the indices of the edges.
    • getVertices

      int[] getVertices()
      Return the indices of the vertices.
      Returns:
      the indices of the vertices.
    • getMaxVertex

      @Deprecated(since="2.5.0") default int getMaxVertex()
      Deprecated.
      Use getVertexCapacity() instead.
      Return the maximum number of vertices that this graph can contain. The vertex indices are in the range [0, getVertexCapacity[ As the contiguity of the vertices is not mandatory, do not use this method to iterate over the vertices. Use getVertices() instead. To get the number of vertices in this graph, use getVertexCount().
      Returns:
      the maximum number of vertices contained in this graph.
    • getVertexCapacity

      default int getVertexCapacity()
      Return the maximum number of vertices that this graph can contain. The vertex indices are in the range [0, getVertexCapacity[ As the contiguity of the vertices is not mandatory, do not use this method to iterate over the vertices. Use getVertices() instead. To get the number of vertices in this graph, use getVertexCount().
      Returns:
      the maximum number of vertices contained in this graph.
    • getVerticesObj

      Iterable<V> getVerticesObj()
      Return an Iterable to iterate over the values attached to the vertices.
      Returns:
      an Iterable to iterate over the values attached to the vertices.
    • getVertexObjectStream

      Stream<V> getVertexObjectStream()
      Return a Stream to iterate over the values attached to the vertices.
      Returns:
      a Stream to iterate over the values attached to the vertices.
    • getVertexObject

      V getVertexObject(int v)
      Return the value attached to the specified vertex. This method throws a PowsyblException if the vertex doesn't exist.
      Parameters:
      v - the vertex index
      Returns:
      the value attached to the specified vertex.
    • setVertexObject

      void setVertexObject(int v, V obj)
      Set the value attached to the specified vertex. This method throws a PowsyblException if the vertex doesn't exist.
      Parameters:
      v - the vertex index.
      obj - the value to attach to the vertex.
    • getEdgeVertex1

      int getEdgeVertex1(int e)
      Return the index of the first vertex that the specified edge is connected to. This method throws a PowsyblException if the edge doesn't exist.
      Parameters:
      e - the edge index.
      Returns:
      the index of the first vertex that the specified edge is connected to.
    • getEdgeObjectsConnectedToVertex

      List<E> getEdgeObjectsConnectedToVertex(int v)
      Return the edge objects connected to the specified vertex. This method throws a PowsyblException if the vertex doesn't exist.
      Parameters:
      v - the vertex index.
      Returns:
      an iterable of the edge objects
    • getEdgeObjectConnectedToVertexStream

      Stream<E> getEdgeObjectConnectedToVertexStream(int v)
      Return the edge objects connected to the specified vertex. This method throws a PowsyblException if the vertex doesn't exist.
      Parameters:
      v - the vertex index.
      Returns:
      a stream of the edge objects
    • getEdgesConnectedToVertex

      List<Integer> getEdgesConnectedToVertex(int v)
      Return the indices of the edges connected to the specified vertex. This method throws a PowsyblException if the vertex doesn't exist.
      Parameters:
      v - the vertex index.
      Returns:
      an iterable of the edge indices
    • getEdgeConnectedToVertexStream

      IntStream getEdgeConnectedToVertexStream(int v)
      Return the indices of the edges connected to the specified vertex. This method throws a PowsyblException if the vertex doesn't exist.
      Parameters:
      v - the vertex index.
      Returns:
      a stream of the edge indices
    • getEdgeVertex2

      int getEdgeVertex2(int e)
      Return the index of the second vertex that the specified edge is connected to. This method throws a PowsyblException if the edge doesn't exist.
      Parameters:
      e - the edge index.
      Returns:
      the index of the second vertex that the specified edge is connected to.
    • removeAllVertices

      void removeAllVertices()
      Remove all the vertices of this graph. This method throws a PowsyblException if edges exist.
    • getEdgesObject

      Iterable<E> getEdgesObject()
      Return an Iterable to iterate over the values attached to the edges.
      Returns:
      an Iterable to iterate over the values attached to the edges.
    • getEdgeObjectStream

      Stream<E> getEdgeObjectStream()
      Return a Stream to iterate over the values attached to the edges.
      Returns:
      a Stream to iterate over the values attached to the edges.
    • getEdgeObject

      E getEdgeObject(int e)
      Return the value attached to the specified edge.
      Parameters:
      e - the index of an edge.
      Returns:
      the value attached to the specified edge.
    • getEdgeObjects

      List<E> getEdgeObjects(int v1, int v2)
      Return a List containing the values attached to the edges between the vertices v1 and v2.
      Returns:
      a List containing the values attached to the edges between the vertices v1 and v2.
    • traverse

      boolean traverse(int v, TraversalType traversalType, Traverser traverser, boolean[] encountered)
      Traverse the entire graph, starting at the specified vertex v. This method relies on a Traverser instance to know if the traverse of the graph should continue or stop. This method throws a PowsyblException if the encountered table size is less than the maximum vertex index. At the end of the method, the encountered array contains true for all the traversed vertices, false otherwise.
      Parameters:
      v - the vertex index where the traverse has to start.
      traversalType - the type of traversal (breadth-first or depth-first)
      traverser - the Traverser instance to use to know if the traverse should continue or stop.
      encountered - the list of traversed vertices.
      Returns:
      false if the whole traversing has to stop, meaning that a TraverseResult.TERMINATE_TRAVERSER has been returned from the traverser, true otherwise
    • traverse

      boolean traverse(int v, TraversalType traversalType, Traverser traverser)
      Traverse the entire graph, starting at the specified vertex v. This method allocates a boolean array and calls traverse(int, TraversalType, Traverser, boolean[]).
      Parameters:
      v - the vertex index where the traverse has to start.
      traversalType - the type of traversal (breadth-first or depth-first)
      traverser - the Traverser instance to use to know if the traverse should continue or stop.
      Returns:
      false if the whole traversing has to stop, meaning that a TraverseResult.TERMINATE_TRAVERSER has been returned from the traverser, true otherwise
    • traverse

      boolean traverse(int[] v, TraversalType traversalType, Traverser traverser)
      Traverse the entire graph, starting at each vertex index of the specified vertices array v. This method allocates a boolean array and calls traverse(int, TraversalType, Traverser, boolean[]) for each entry of the array.
      Parameters:
      v - the array of vertex indices where the traverse has to start.
      traversalType - the type of traversal (breadth-first or depth-first)
      traverser - the Traverser instance to use to know if the traverse should continue or stop.
      Returns:
      false if the whole traversing has to stop, meaning that a TraverseResult.TERMINATE_TRAVERSER has been returned from the traverser, true otherwise
    • findAllPaths

      List<gnu.trove.list.array.TIntArrayList> findAllPaths(int from, Predicate<V> pathComplete, Predicate<? super E> pathCancelled)
      Find all paths from the specified vertex. This method relies on two functions to stop the traverse when the target vertex is found or when an edge must not be traversed.
      Parameters:
      from - the vertex index where the traverse has to start.
      pathComplete - a function that returns true when the target vertex is found.
      pathCancelled - a function that returns true when the edge must not be traversed.
      Returns:
      a list that contains the index of the traversed edges.
    • findAllPaths

      List<gnu.trove.list.array.TIntArrayList> findAllPaths(int from, Predicate<V> pathComplete, Predicate<? super E> pathCancelled, Comparator<gnu.trove.list.array.TIntArrayList> comparator)
      Find all paths from the specified vertex. This method relies on two functions to stop the traverse when the target vertex is found or when an edge must not be traversed.
      Parameters:
      from - the vertex index where the traverse has to start.
      pathComplete - a function that returns true when the target vertex is found.
      pathCancelled - a function that returns true when the edge must not be traversed.
      comparator - a comparator used to sort the paths
      Returns:
      a list that contains the index of the traversed edges.
    • addListener

      void addListener(UndirectedGraphListener<V,E> l)
      Add a UndirectedGraphListener to get notified when the graph changes.
      Parameters:
      l - the listener to add.
    • removeListener

      void removeListener(UndirectedGraphListener<V,E> l)
      Remove a UndirectedGraphListener to stop listening the graph changes.
      Parameters:
      l - the listener to remove.
    • print

      void print(PrintStream out, Function<V,String> vertexToString, Function<E,String> edgeToString)
      Prints the entire graph to the specified PrintStream. The printing relies on the two specified functions to render the values attached to a vertex or an edge.
      Parameters:
      out - the output stream.
      vertexToString - the function to use to render the values of vertices.
      edgeToString - the function to use to render the values of edges.
    • removeIsolatedVertices

      void removeIsolatedVertices()
      Remove from the vertices which are not connected to any edge, and which have no associated object.