- Download the
TestGraph.zip
file from this repository. - Import the zip into a Java IDE - IntelliJ IDE.
- From the menu bar you can click 'Run'-> 'Run 'GraphMani'.
- You can also run the whole project with 'mvn package'. Since there is support for the surefire plugin this command runs the tests as well.
- To run all the tests separately you can 'Run GraphManiTest'.
void parseGraph(String filePath)
- Imports a directed graph from a DOT file.public String toString()
- Gets the information about the graph like the number of nodes & edges and converts the data into string output.void outputGraph(String filePath)
- Puts the information about the graph into a text file format.
public void addNode(String label)
- Adds a new node with the given label into the graph if not already present.public void addNodes(String[] label)
- Adds an array of nodes into the graph if not present already.
public void addEdge(String srcLabel,String dstLabel)
- Adds a new edge with the given source and destination nodes if not already present.
public void outputDOTGraph(String path)
- Outputs graph in a dot graph format in the given path.public void outputGraphics(String path, String format)
- Outputs graph in png format in the given path with graph visualization.
public Graph<String, DefaultEdge> getGraph()
- Returns the current graph object.
- Create a new graph object:
GraphMani f = new GraphMani();
- To parse the graph and print it:
f.parseGraph("src/main/sample.DOT");
System.out.println(f.toString());
f.outputGraph("src/main/outputGraph.txt");
- To add node / nodes :
f.addNode("f");
String[] add_Nodes = {"e","g","a","h"};
f.addNodes(add_Nodes);
- To add edge:
f.addEdge("e","f");
- To output the graph in DOT format:
f.outputDOTGraph("src/main/outGraph.DOT");
- To output the graph in PNG format:
f.outputGraphics("src/main/newGraph.png","PNG");
- Remove node:
f.removeNode("e");
- Remove nodes:
String[] nodesToRemove = {"h","g"};
f.removeNodes(nodesToRemove);
To test removal of edges. I am adding few edges to the graph.
f.addEdge("e","g");
f.addEdge("d","e");
- Remove edge:
f.removeEdge("a","b");
I have made a new DOT file (sample2.DOT).
GraphMani new_f = new GraphMani();
new_f.parseGraph("src/main/sample2.DOT");
- For BFS Traversal:
Path result = new_f.GraphSearch("a","e", algo.BFS);
System.out.println(result.toString());
- For DFS Traversal:
Path result2 = new_f.GraphSearch("a","e", algo.DFS);
System.out.println(result2.toString());
- The 6 refactors were:
-Magic strings like "PNG" is replaced with constants to improve code maintainability
-Made variable declaration location consistent and clear at the top of the code
-Separate the graph parsing logic from the graph file reading in the parseGraph method. This allows for better error handling and more meaningful error messages.
-Simplified the construction of the edges section in the toString method using the forEach loop for clarity and simplicity.
-Improved the logic of toString() in Path class by making it concise and improving readability.
-Refactored the Path class to use the List interface and used contains in containsNode function, made it concise and simplified it.
- For Template Pattern:
GraphMani new_f = new GraphMani();
new_f.parseGraph("src/main/sample2.DOT");
Path result = new_f.GraphSearch("a","e", algo.BFS);
System.out.println("BFS: "+ result.toString());
Path result2 = new_f.GraphSearch("a","e", algo.DFS);
System.out.println("DFS: " +result2.toString());
- For Strategy Pattern:
GraphMani BFS_Sp = new GraphMani();
BFS_Sp.parseGraph("src/main/sample2.DOT");
Path resBFS_Sp = BFS_Sp.GraphSearch("a", "e", algo.BFS);
System.out.println("BFS: "+ resBFS_Sp.toString());
GraphMani DFS_Sp = new GraphMani();
DFS_Sp.parseGraph("src/main/sample2.DOT");
Path resDFS_Sp = DFS_Sp.GraphSearch("a", "e", algo.DFS);
System.out.println("DFS: "+ resDFS_Sp.toString());
- For Random Walk:
GraphMani RWS_Sp = new GraphMani();
RWS_Sp.parseGraph("src/main/input2.dot");
System.out.println("Random Walk: ");
Path resRWS_Sp = RWS_Sp.GraphSearch("a", "c", algo.RWS);
System.out.println();
System.out.println("RWS: "+ resRWS_Sp.toString());
In this project, I have learned about:
- manipulating DOT files
- function overriding
- git commands
- maven
- git branching and merging concepts
- coding a theoretical algorithm on a DOT file (BFS and DFS) using JGraphT
- implementing a new algorithm based on previous knowledge (Random Forest using BFS)
- refactoring patterns like strategy patterns and template patterns
- abstract classes
- API building
- concepts of access modifiers
- continuous integration
- writing tests that cover broad possibilities using JUnit