- Download the
GraphManager.zipfile from this repository - Run
mvn package - This should run all tests for the project
- This command will build the project in the
targetfolder as well - Alternatively, unzip
GraphManager.zipand then open the GraphManager folder in IntelliJ
void parseGraph(String filePath)- import a directed graph in a dot fileString toString()- Graph information like number of nodes, edges and their directionsvoid outputGraph(String filePath)- Write the graph information to a fileint nodeSize()- Returns number of nodes in the graphint edgeSize()- Returns the number of edges in the graphboolean containsNode(String label)- Returns true if the graph contains the node and false otherwiseboolean containsEdge(String srcLabel, String dstLabel)- Returns true if edge exists in graph and false otherwisevoid addNode(String label)- Adds a new node to the graph with the given label if it does not existboolean removeNode(String label)- Returns false if node does not exist or returns true if node is removedvoid addNodes(String[] labels)- Add multiple nodes to the graphboolean removeNodes(String[] labels)- Returns true if all nodes are removed successfully otherwise returns false if even one node existsboolean addEdge(String srcLabel, String dstLabel)- Returns true if edge is added otherwise returns false if edge existsboolean removeEdge(String srcLabel, String dstLabel)- Returns true if edge is removed successfully otherwise returns false if edge does not exist in the graphvoid outputDOTGraph(String filePath)- Outputs the modified graph in DOT format to the specified filevoid outputGraphics(String filePath)- Output the modified graph to a PNG file (Graph Visualization)Path GraphSearch(String src, String dst, Algorithm algo)- Find a path fromsrctodstnode using BFS or DFS or Random Walk algorithm depending on enum specified. Possible values of the enum can beAlgorithm.BFS,Algorithm.DFSorAlgorithm.RandomWalkSearch
- Creating a new GraphManager object
GraphManager g = new GraphManager();- Parsing the graph and printing information
g.parseGraph("src/test.dot");
System.out.println(g.toString());
g.outputGraph("src/graphinfo.txt");- Adding and Removing nodes
g.addNode("e");
g.removeNode("e");
String[] nodesToAdd = {"e","f","g"};
g.addNodes(nodesToAdd);
String[] nodesToRemove = {"e","g"};
g.removeNodes(nodesToRemove);- Adding and removing edges
g.addEdge("a","f");
g.removeEdge("a","b");- Output graph as DOT and PNG
g.outputDOTGraph("src/modified.dot");
g.outputGraphics("src/modified.png");- Find a path from one node to another using BFS, DFS or Random Walk algorithm
Path bfs = g.GraphSearch("a", "c", Algorithm.BFS);
Path dfs = g.GraphSearch("c", "d", Algorithm.DFS);
Path rws = g.GraphSearch("a","c", Algorithm.RandomWalkSearch);- The
toString()methods of thePathclass will print the path in the formata -> b -> c - If no path if found, the
GraphSearchAPI returnsnull, so we need to check if the returned path is not null - The
Pathclass also exposes the methodgetNodeList()which returns an ArrayList containing the searched nodes in the order that they were visited
if (bfs != null) System.out.println(bfs.toString());
ArrayList<String> nodes = bfs.getNodeList();