Class Graph

  • Direct Known Subclasses:
    LSVMGridGraph, Mesh2D, SpectralGraph

    public class Graph
    extends java.lang.Object
    The class implements the graph structure (via adjacency lists) and simple algorithms for processing the graph. The vertices of the graph should be enumerated from 1 to N (not from 0 to N). The list of implemented algorithms: - Breadth-First Search - Bellman-Ford single source graph search algorithm ( complexity O(V*E) ) - Dijkstra single source graph search algorithm (complexity O(V*V); priority queue is used, for large graphs it is lower ) - Ford-Fulkerson maximum flow search - Multicommodity Flow search with fractional flows (search for the feasible solution) - Multicommodity FLow search with fractional flows (with minimization of the maximum d_i / f_i - e.g. time needed to transfer d_i Bytes with f_i bandwidth) - Low Stretch Spanning tree composition
    Author:
    Dmitry Moor
    • Constructor Summary

      Constructors 
      Constructor Description
      Graph()
      The method constructs am empty graph
      Graph​(java.util.List<Vertex> vertices)
      The method constructs
      Graph​(java.util.List<Vertex> vertices, java.util.List<java.util.List<VertexCell>> adjLsts)  
      Graph​(java.util.List<Vertex> vertices, java.util.List<VertexCell>... adjLst)
      Constructor
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addAdjacencyList​(java.util.List<VertexCell> adjLst)
      The method adds an adjacency list
      void addEdge​(int sourceId, int sinkId)
      The method adds one edge to the graph
      void addListOfVertices​(java.util.List<Vertex> vrts)
      The method updates the list of vertices of the graph
      double BallCut​(Vertex x, double rad, double delta)  
      boolean BellmanFord​(Vertex sourceID, int idx)
      The method implements the Bellman-Ford graph search algorithm (the complexity is O(V*E) )
      void BFS​(Vertex s)
      The method implements the Breadth-First-Search in the graph given the source vertex
      Graph buildResidualNetwork()
      The method builds the residual network for this graph
      double computeCost​(java.util.List<Edge> edges)
      The method computes the cost of a set of edges i.e.
      double ConeCut​(Vertex center, double lambda, double lambda1, java.util.List<Vertex> S)  
      java.util.List<java.util.List<Vertex>> coneDecomp​(java.util.List<Vertex> S, double delta, java.util.List<Vertex> coneVertices)
      The method implements the cone decomposition of the graph.
      void constructFlowTables​(int numberOfFlows)
      The method constructs flow tables from a solution of MCF problem.
      void Dijkstra​(Vertex source, int idx)  
      java.util.Set<java.util.Set<Vertex>> findAllPaths​(Vertex source, Vertex destination)
      The method implements the dfs algorithm to find all paths between two vertices.
      void FordFulkerson​(Vertex s, Vertex t)
      The method implements Ford-Fulkerson maximum flow search method given the source vertex and the sink vertex
      java.util.List<java.util.List<VertexCell>> getAdjacencyLists()
      The method returns adjacency lists of the graph
      Graph getBall​(Vertex center, double radius, boolean recompute)
      The method returns a subgraph induced by a subset of vertices that are at most at distance=radius from the center vertex
      java.util.List<Vertex> getBallShell​(Vertex center, double radius, boolean recompute)
      The method returns a ball shell of a graph with a center in 'center' and with the given radius
      java.util.List<Edge> getBoundary​(java.util.List<Vertex> vertices)  
      java.util.List<Vertex> getCone​(Vertex center, double radius, java.util.List<Vertex> S)
      The method returns the cone with a vertex in 'center' and a given radius in respect to the set of vertices S
      java.util.List<Edge> getEdges()  
      double[][][] getFlowTables()
      The method returns flow tables obtained after MCF
      long getInputDegree​(int vertexId)
      The method returns the input degree of a given vertex
      int getNumberOfEdges()
      The method returns the number of edges of the graph
      int getOutputDegree​(int vertexId)
      The method returns the output degree of a given vertex
      java.util.List<VertexCell> getPath​(Vertex v, Vertex u)
      The method returns a path between two vertices i.e.
      double getRadius​(Vertex v, boolean recompute)
      The method returns the radius of the graph i.e.
      int getVertexPredecessor​(int vertexID)  
      java.util.List<Vertex> getVertices()
      The method returns the list of vertices of the graph
      Graph induceGraph​(java.util.List<Vertex> vertices)
      The method returns the graph induced from the current one by a set of vertices Complexity O(n*m) because of adjacency lists.
      boolean isAdjacent​(Vertex u, Vertex v)
      The method returns true if there is an edge from the vertex U to the vertex V and false otherwise
      java.util.List<Graph> lowStretchTree​(Vertex center, double betta, java.util.List<Edge> bridges)
      The method generate a Low Stretch Spanning Tree for the graph
      void removeEdge​(int vertexId, int edgeIdx)
      The method removes one edge from the graph
      java.util.List<java.util.List<Vertex>> starDecomp​(Vertex x0, double delta, double epsilon, java.util.List<Vertex> y, java.util.List<Vertex> coneVertices)
      The method returns the Star-decomposition of the graph
      java.lang.String toString()
      (non-Javadoc)
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • _adjacencyLists

        protected java.util.List<java.util.List<VertexCell>> _adjacencyLists
      • _vertices

        protected java.util.List<Vertex> _vertices
    • Constructor Detail

      • Graph

        public Graph​(java.util.List<Vertex> vertices,
                     java.util.List<VertexCell>... adjLst)
        Constructor
        Parameters:
        vertices - - a list of vertices of the graph
        adjLst - - a set of adjacency lists. The 1st list should correspond to the vertex with id=1, the 2nd list - to the vertex with id=2, etc...
      • Graph

        public Graph​(java.util.List<Vertex> vertices,
                     java.util.List<java.util.List<VertexCell>> adjLsts)
        Parameters:
        vertices - - a list of vertices
        adjLsts - - a set of adjacency lists. The 1st list should correspond to the vertex with id=1, the 2nd list - to the vertex with id=2, etc...
      • Graph

        public Graph​(java.util.List<Vertex> vertices)
        The method constructs
        Parameters:
        vertices - - a list of vertices
      • Graph

        public Graph()
        The method constructs am empty graph
    • Method Detail

      • getNumberOfEdges

        public int getNumberOfEdges()
        The method returns the number of edges of the graph
        Returns:
        the number of edges of the graph
      • getVertices

        public java.util.List<Vertex> getVertices()
        The method returns the list of vertices of the graph
        Returns:
        the list of vertices of the graph
      • getAdjacencyLists

        public java.util.List<java.util.List<VertexCell>> getAdjacencyLists()
        The method returns adjacency lists of the graph
        Returns:
        a list of adjacency lists of the graph
      • getEdges

        public java.util.List<Edge> getEdges()
      • addListOfVertices

        public void addListOfVertices​(java.util.List<Vertex> vrts)
        The method updates the list of vertices of the graph
        Parameters:
        vrts - new list of vertices of the graph
      • addAdjacencyList

        public void addAdjacencyList​(java.util.List<VertexCell> adjLst)
        The method adds an adjacency list
        Parameters:
        adjLst - - a new adjacency list
      • toString

        public java.lang.String toString()
        (non-Javadoc)
        Overrides:
        toString in class java.lang.Object
        See Also:
        Object.toString()
      • getOutputDegree

        public int getOutputDegree​(int vertexId)
        The method returns the output degree of a given vertex
        Parameters:
        vertexId - - the id of the vertex for which the output degree should be computed
        Returns:
        the output degree of the given vertex
      • getInputDegree

        public long getInputDegree​(int vertexId)
        The method returns the input degree of a given vertex
        Parameters:
        vertexId - - the id of the vertex for which the input degree should be computed
        Returns:
        the input degree of the given vertex
      • getVertexPredecessor

        public int getVertexPredecessor​(int vertexID)
        Parameters:
        vertexID - the id of a vertex
        Returns:
        the predecessor of the vertex (obtained e.g. by Dijkstra or BFS)
      • getPath

        public java.util.List<VertexCell> getPath​(Vertex v,
                                                  Vertex u)
        The method returns a path between two vertices i.e. a list of vertices obtained by analyzing predecessors for each vertex (based for example on prior Dijkstra or BFS method calls)
        Parameters:
        v - - the first vertex in the path
        u - - the last vertex of the path
        Returns:
        the path between u and v
      • getFlowTables

        public double[][][] getFlowTables()
        The method returns flow tables obtained after MCF
        Returns:
        flow tables
      • getRadius

        public double getRadius​(Vertex v,
                                boolean recompute)
        The method returns the radius of the graph i.e. the smallest r s.t. every vertex in the graph is within distance at most r from the specified vertex
      • getBall

        public Graph getBall​(Vertex center,
                             double radius,
                             boolean recompute)
        The method returns a subgraph induced by a subset of vertices that are at most at distance=radius from the center vertex
        Parameters:
        center - - the center vertex of the ball
        radius - - the radius of the ball
        recompute - - the boolean value indicating if it is necessary to recompute shortest paths estimations
        Returns:
        a ball subgraph
      • getBallShell

        public java.util.List<Vertex> getBallShell​(Vertex center,
                                                   double radius,
                                                   boolean recompute)
        The method returns a ball shell of a graph with a center in 'center' and with the given radius
        Parameters:
        center - - the center of the corresponding ball
        radius - - radius of the ball
        recompute - - a boolean value indicating if it is needed to recompute the shortest path estimations
        Returns:
        a list of vertices distributed over the ball shell (sphere)
      • getBoundary

        public java.util.List<Edge> getBoundary​(java.util.List<Vertex> vertices)
      • getCone

        public java.util.List<Vertex> getCone​(Vertex center,
                                              double radius,
                                              java.util.List<Vertex> S)
        The method returns the cone with a vertex in 'center' and a given radius in respect to the set of vertices S
        Parameters:
        center - - the vertex of the cone
        radius - - the radius ot the cone (i.e. max distance)
        S - - the set in respect to which the cone should be defined
        Returns:
        the cone
      • computeCost

        public double computeCost​(java.util.List<Edge> edges)
        The method computes the cost of a set of edges i.e. the sum of 1/w_i for i=1...numberOfEdges (w_i) is weights of edges
        Parameters:
        edges - - a list of edges for which the cost should be computed
        Returns:
        the cost of the given set of edges
      • BallCut

        public double BallCut​(Vertex x,
                              double rad,
                              double delta)
      • ConeCut

        public double ConeCut​(Vertex center,
                              double lambda,
                              double lambda1,
                              java.util.List<Vertex> S)
      • coneDecomp

        public java.util.List<java.util.List<Vertex>> coneDecomp​(java.util.List<Vertex> S,
                                                                 double delta,
                                                                 java.util.List<Vertex> coneVertices)
        The method implements the cone decomposition of the graph.
        Parameters:
        S - - the list of cone vertices
        delta - - delta parameter of the algorithm
        coneVertices - - a list of cone vertices (used as an additional output parameter of the method)
        Returns:
        a list of cones of the decomposition
      • starDecomp

        public java.util.List<java.util.List<Vertex>> starDecomp​(Vertex x0,
                                                                 double delta,
                                                                 double epsilon,
                                                                 java.util.List<Vertex> y,
                                                                 java.util.List<Vertex> coneVertices)
        The method returns the Star-decomposition of the graph
        Parameters:
        x0 - - the center of the star
        delta - - delta parameter of the algorithm ( =0.333)
        epsilon - - epsilon parameter of the algorithm (precision)
        y - - a list of vertices within the center-ball of the star that are the closest ones to the cone vertices (used as an additional return value)
        Returns:
        list of cones and balls of the star decomposition
      • induceGraph

        public Graph induceGraph​(java.util.List<Vertex> vertices)
        The method returns the graph induced from the current one by a set of vertices Complexity O(n*m) because of adjacency lists. If to use matrix O( n ) - just remove the missing rows and columns TODO WARNING: the indexes of vertices in _vertices are not continuous anymore!!! WARNING: the graph contains copies of the vertices and adjacency lists of the original graph, but not the originals!!!
        Parameters:
        vertices - - the list of vertices that should be present in the induced graph (NOTE: the vertices objects should belong to THIS object as they refer to the adjacency lists of THIS graph)
        Returns:
        an induced graph
      • lowStretchTree

        public java.util.List<Graph> lowStretchTree​(Vertex center,
                                                    double betta,
                                                    java.util.List<Edge> bridges)
        The method generate a Low Stretch Spanning Tree for the graph
        Parameters:
        center - - the center (the root) of the tree
        betta - - betta parameter for the algorithm
        bridges - - edges x(i)-y(i) that are the bridges between different cones and balls
        Returns:
        a low stretch spanning tree
      • isAdjacent

        public boolean isAdjacent​(Vertex u,
                                  Vertex v)
        The method returns true if there is an edge from the vertex U to the vertex V and false otherwise
        Parameters:
        u - - id of the 1st vertex
        v - - id of the 2nd vertex
        Returns:
        true if these vertices are adjacent and false otherwise
      • BFS

        public void BFS​(Vertex s)
        The method implements the Breadth-First-Search in the graph given the source vertex
        Parameters:
        s - - the source vertex in the graph
      • FordFulkerson

        public void FordFulkerson​(Vertex s,
                                  Vertex t)
        The method implements Ford-Fulkerson maximum flow search method given the source vertex and the sink vertex
        Parameters:
        s - - the source vertex
        t - - the sink vertex
      • BellmanFord

        public boolean BellmanFord​(Vertex sourceID,
                                   int idx)
        The method implements the Bellman-Ford graph search algorithm (the complexity is O(V*E) )
        Parameters:
        sourceID - - the id of the source vertex
        Returns:
        true if there exist shortest paths and false otherwise. The shortest path itself i.e. predecessors for each vertex and the weights of paths may be traced by printing the vertices
      • Dijkstra

        public void Dijkstra​(Vertex source,
                             int idx)
      • findAllPaths

        public java.util.Set<java.util.Set<Vertex>> findAllPaths​(Vertex source,
                                                                 Vertex destination)
        The method implements the dfs algorithm to find all paths between two vertices. Code adjusted from: http://introcs.cs.princeton.edu/java/45graph/AllPaths.java.html by Robert Sedgewick and Kevin Wayne.
      • buildResidualNetwork

        public Graph buildResidualNetwork()
        The method builds the residual network for this graph
      • removeEdge

        public void removeEdge​(int vertexId,
                               int edgeIdx)
        The method removes one edge from the graph
        Parameters:
        vertexId - - an ide of a source vertex of the edge
        edgeIdx - - an index of the edge to be removed
      • addEdge

        public void addEdge​(int sourceId,
                            int sinkId)
        The method adds one edge to the graph
        Parameters:
        sourceId - - an id of a source vertex of the edge
        sinkId - - an index of the edge to be removed
      • constructFlowTables

        public void constructFlowTables​(int numberOfFlows)
        The method constructs flow tables from a solution of MCF problem.
        Parameters:
        numberOfFlows - - the number of flows in the graph