Package edu.upc.dama.dex.algorithms

Provides classes in order to run algorithms among graphs.

See:
          Description

Interface Summary
Algorithm Common interface for all the algorithm implementations.
 

Class Summary
ConnectedComponents This class contains the results processed on a Connectivity algorithm.
Connectivity Any class implementing this abstract class can be used to solve a problem related to graph connectivity as finding the strongly connected components or finding the weakly connected components.
ShortestPath Any class implementing this abstract class can be used to solve the single pair shortest path problem, single-source pair shortest path problem, single-destination shortest path problem or all-pairs shortest path problem.
SinglePairShortestPath Any class implementing this abstract class can be used to solve the problem of finding a path between two nodes with the condition that the sum of the weights of the edges which belong to the path is minimized.
SinglePairShortestPathBFS This class can be used to solve the single-pair shortest path problem in an unweighted graph.
SinglePairShortestPathDijkstra This class can be used to solve the single-pair shortest path problem in a weighted graph.
StrongConnectivity Any class implementing this abstract class can be used to solve the problem of finding strongly connected components in a directed graph or in an undirected graph which will be considered as a directed one.
StrongConnectivityGabow This class can be used to solve the problem of finding strongly connected components in a directed graph.
Traversal Any class implementing this abstract class can be used to traverse a graph.
TraversalBFS This class can be used to traverse the graph using the breadth-first search algorithm (BFS).
TraversalDFS This class can be used to traverse the graph using the depth-first search algorithm (DFS).
WeakConnectivity Any class implementing this abstract class can be used to solve the problem of finding weakly connected components in an undirected graph or in a directed graph which will be considered as an undirected one.
WeakConnectivityDFS This class can be used to solve the problem of finding weakly connected components in an undirected graph or in a directed graph which will be considered as an undirected one.
 

Package edu.upc.dama.dex.algorithms Description

Provides classes in order to run algorithms among graphs. Specifically, this package contains the following classes:

Table of Contents

USAGE

SinglePairShortestPathBFS

The following example shows how to use the SinglePairShortestPathBFS class:

              // Opening the graph
              DEX dex = new DEX();
              GraphPool gp = dex.open("./data.dex");
              Session sess = gp.newSession();
              DbGraph graph = sess.getDbGraph();

              // Creating a new instance and setting its parameters
              int nodetype = graph.findType("city");
              long idsource = graph.findObj(nodetype, new Value("Barcelona"));
              long iddestination = graph.findObj(nodetype, new Value("Rome"));
              SinglePairShortestPathBFS sp = new SinglePairShortestPathBFS(graph, idsource, iddestination);

              // Adding the allowed edge types for traversing the graph
              int edgetype1 = graph.findType("street");
              sp.addEdge(edgetype1, Algorithm.NAVIGATION_FORWARD);
              int edgetype2 = graph.findType("road");
              sp.addEdge(edgetype2, Algorithm.NAVIGATION_UNDIRECTED);

              // Setting the maximum hops
              sp.setMaximumHops(7);

              // Running the algorithm
              sp.run();

              // Retrieving the generated results
              long[] spAsNodes;
              long[] spAsEdges;
              int hopsDone;
              if (sp.existsShortestPath()) {
                  spAsNodes = sp.getPathAsNodes();
                  spAsEdges = sp.getPathAsEdges();
                  hopsDone = sp.getCost();
              }

              // Closing the instance
              sp.close();

              // Closing the graph
              sess.close();
              gp.close();
              dex.close();
        

SinglePairShortestPathDijkstra

The following example shows how to use the SinglePairShortestPathDijkstra class:

              // Opening the graph
              DEX dex = new DEX();
              GraphPool gp = dex.open("./data.dex");
              Session sess = gp.newSession();
              DbGraph graph = sess.getDbGraph();

              // Creating a new instance and setting its parameters
              int nodetype = graph.findType("city");
              long idsource = graph.findObj(nodetype, new Value("Barcelona"));
              long iddestination = graph.findObj(nodetype, new Value("Rome"));
              SinglePairShortestPathDijkstra sp = new SinglePairShortestPathDijkstra(graph, idsource, iddestination);

              // Adding the allowed edge types for traversing the graph
              int edgetype1 = graph.findType("street");
              long weightedattr1 = graph.findAttribute(edgetype1, "weight");
              sp.addWeightedEdge(edgetype1, Algorithm.NAVIGATION_FORWARD, weightedattr1);
              int edgetype2 = graph.findType("road");
              long weightedattr2 = graph.findAttribute(edgetype2, "weight");
              sp.addWeightedEdge(edgetype2, Algorithm.NAVIGATION_UNDIRECTED, weightedattr1);

              // Setting no maximum hops (not calling the method)

              // Running the algorithm
              sp.run();

              // Retrieving the generated results
              long[] spAsNodes;
              long[] spAsEdges;
              double cost;
              if (sp.existsShortestPath()) {    
                  spAsNodes = sp.getPathAsNodes();
                  spAsEdges = sp.getPathAsEdges();
                  cost = sp.getCost();
              }

              // Closing the instance
              sp.close();

              // Closing the graph
              sess.close();
              gp.close();
              dex.close();
        

TraversalBFS

The following example shows how to use the TraversalBFS class:

              // Opening the graph
              DEX dex = new DEX();
              GraphPool gp = dex.open("./data.dex");
              Session sess = gp.newSession();
              DbGraph graph = sess.getDbGraph();

              // Creating a new instance and setting its parameters
              int nodetype = graph.findType("city");
              long idsource = graph.findObj(nodetype, new Value("Barcelona"));
              TraversalBFS bfs = new TraversalBFS(graph, idsource);

              // Adding the allowed edge types for traversing the graph
              int edgetype1 = graph.findType("street");
              bfs.addEdge(edgetype1, Algorithm.NAVIGATION_FORWARD);
              int edgetype2 = graph.findType("road");
              bfs.addEdge(edgetype2, Algorithm.NAVIGATION_FORWARD);

              // Adding the allowed node types for traversing the graph
              bfs.addAllNodes();

              // Running the algorithm
              long idnode;
              while (bfs.hasNext()) {
                    idnode = bfs.next();
              }

              // Closing the instance
              bfs.close();

              // Closing the graph
              sess.close();
              gp.close();
              dex.close();
        

TraversalDFS

The following example shows how to use the TraversalDFS class:

              // Opening the graph
              DEX dex = new DEX();
              GraphPool gp = dex.open("./data.dex");
              Session sess = gp.newSession();
              DbGraph graph = sess.getDbGraph();

              // Creating a new instance and setting its parameters
              int nodetype = graph.findType("city");
              long idsource = graph.findObj(nodetype, new Value("Barcelona"));
              TraversalDFS dfs = new TraversalDFS(graph, idsource);

              // Adding the allowed edge types for traversing the graph
              dfs.addAllEdges(Algorithm.NAVIGATION_BACKWARD);

              // Adding the allowed node types for traversing the graph
              int nodetype1 = graph.findType("city");
              dfs.addNode(nodetype1);
              int nodetype2 = graph.findType("town");
              dfs.addNode(nodetype2);

              // Running the algorithm
              long idnode;
              while (dfs.hasNext()) {
                    idnode = dfs.next();
              }

              // Closing the instance
              dfs.close();

              // Closing the graph
              sess.close();
              gp.close();
              dex.close();
        

WeakConnectivityDFS

The following example shows how to use the WeakConnectivityDFS class:

              // Opening the graph
              DEX dex = new DEX();
              GraphPool gp = dex.open("./data.dex");
              Session sess = gp.newSession();
              DbGraph graph = sess.getDbGraph();

              // Creating a new instance and setting its parameters
              WeakConnectivityDFS wcc = new WeakConnectivityDFS(graph);

              // Adding the allowed edge types for traversing the graph
              wcc.addAllEdges();

              // Adding the allowed node types for traversing the graph
              int nodetype1 = graph.findType("city");
              wcc.addNode(nodetype1);
              int nodetype2 = graph.findType("town");
              wcc.addNode(nodetype2);

              // Running the algorithm
              wcc.run();

              // Getting the instance which has the results
              ConnectedComponents ccs = wcc.getConnectedComponents();

              // Closing the instance of the algorithm
              wcc.close();

              /*****  Retrieving information from the resulting instance *****/
              int nodetype = graph.findType("city");
              long idsource = graph.findObj(nodetype, new Value("Barcelona"));

              // Retrieving the connected component where the given node belongs to.
              long idComponent = ccs.getConnectedComponent(idsource);

              // Retrieving the collection of node contained in the given component.
              Objects nodes = ccs.getNodes(idComponent);

              // Retrieving the number of nodes contained in the given component.
              long nodes1size = ccs.getSize(idComponent);

              // Retrieving the number of connected components found.
              long totalCCS = ccs.getCount();

              // Closing the graph
              sess.close();
              gp.close();
              dex.close();
        

The following example shows how to use the WeakConnectivityDFS materializing its results:

              // Opening the graph
              DEX dex = new DEX();
              GraphPool gp = dex.open("./data.dex");
              Session sess = gp.newSession();
              DbGraph graph = sess.getDbGraph();

              // Creating a new instance and setting its parameters
              WeakConnectivityDFS wcc = new WeakConnectivityDFS(graph);

              // Adding the allowed edge types for traversing the graph
              wcc.addAllEdges();

              // Setting the common attribute type in order to materialize
              // the results
              wcc.setMaterializedAttribute("component_24_09_2009");

              // Adding the allowed node types for traversing the graph
              int nodetype1 = graph.findType("city");
              wcc.addNode(nodetype1);
              int nodetype2 = graph.findType("town");
              wcc.addNode(nodetype2);

              // Running the algorithm
              wcc.run();

              // Closing the instance of the algorithm
              wcc.close();

              // Closing the graph
              sess.close();
              gp.close();
              dex.close();

              // Opening the graph again
              dex = new DEX();
              gp = dex.open("./data.dex");
              sess = gp.newSession();
              graph = sess.getDbGraph();

              /*****  Retrieving information materialized in the graph *****/
              ConnectedComponents ccs = new ConnectedComponents(graph, "component_24_09_2009");
              int nodetype = graph.findType("city");
              long idsource = graph.findObj(nodetype, new Value("Barcelona"));

              // Retrieving the connected component where the given node belongs to.
              long idComponent = ccs.getConnectedComponent(idsource);

              // Retrieving the collection of node contained in the given component.
              Objects nodes = ccs.getNodes(idComponent);

              // Retrieving the number of nodes contained in the given component.
              long nodes1size = ccs.getSize(idComponent);

              // Retrieving the number of connected components found.
              long totalCCS = ccs.getCount();

              // Closing the graph
              sess.close();
              gp.close();
              dex.close();
        

StrongConnectivityGabow

The following example shows how to use the StrongConnectivityGabow class:

              // Opening the graph
              DEX dex = new DEX();
              GraphPool gp = dex.open("./data.dex");
              Session sess = gp.newSession();
              DbGraph graph = sess.getDbGraph();

              // Creating a new instance and setting its parameters
              StrongConnectivityGabow scc = new StrongConnectivityGabow(graph);

              // Adding the allowed edge types for traversing the graph
              scc.addAllEdges();

              // Adding the allowed node types for traversing the graph
              int nodetype1 = graph.findType("city");
              scc.addNode(nodetype1);
              int nodetype2 = graph.findType("town");
              scc.addNode(nodetype2);

              // Running the algorithm
              scc.run();

              // Getting the instance which has the results
              ConnectedComponents ccs = scc.getConnectedComponents();

              // Closing the instance of the algorithm
              scc.close();

              /*****  Retrieving information from the resulting instance *****/
              int nodetype = graph.findType("city");
              long idsource = graph.findObj(nodetype, new Value("Barcelona"));

              // Retrieving the connected component where the given node belongs to.
              long idComponent = ccs.getConnectedComponent(idsource);

              // Retrieving the collection of node contained in the given component.
              Objects nodes = ccs.getNodes(idComponent);

              // Retrieving the number of nodes contained in the given component.
              long nodes1size = ccs.getSize(idComponent);

              // Retrieving the number of connected components found.
              long totalCCS = ccs.getCount();

              // Closing the graph
              sess.close();
              gp.close();
              dex.close();
        

The following example shows how to use the StrongConnectivityGabow class materializing its results:

              // Opening the graph
              DEX dex = new DEX();
              GraphPool gp = dex.open("./data.dex");
              Session sess = gp.newSession();
              DbGraph graph = sess.getDbGraph();

              // Creating a new instance and setting its parameters
              StrongConnectivityGabow scc = new StrongConnectivityGabow(graph);

              // Adding the allowed edge types for traversing the graph
              scc.addAllEdges();

              // Setting the common attribute type in order to materialize
              // the results
              scc.setMaterializedAttribute("component_24_09_2009");

              // Adding the allowed node types for traversing the graph
              int nodetype1 = graph.findType("city");
              scc.addNode(nodetype1);
              int nodetype2 = graph.findType("town");
              scc.addNode(nodetype2);

              // Running the algorithm
              scc.run();

              // Closing the instance of the algorithm
              scc.close();

              // Closing the graph
              sess.close();
              gp.close();
              dex.close();

              // Opening the graph again
              dex = new DEX();
              gp = dex.open("./data.dex");
              sess = gp.newSession();
              graph = sess.getDbGraph();

              /*****  Retrieving information materialized in the graph *****/
              ConnectedComponents ccs = new ConnectedComponents(graph, "component_24_09_2009");
              int nodetype = graph.findType("city");
              long idsource = graph.findObj(nodetype, new Value("Barcelona"));

              // Retrieving the connected component where the given node belongs to.
              long idComponent = ccs.getConnectedComponent(idsource);

              // Retrieving the collection of node contained in the given component.
              Objects nodes = ccs.getNodes(idComponent);

              // Retrieving the number of nodes contained in the given component.
              long nodes1size = ccs.getSize(idComponent);

              // Retrieving the number of connected components found.
              long totalCCS = ccs.getCount();

              // Closing the graph
              sess.close();
              gp.close();
              dex.close();