Skip to content

Commit

Permalink
refactor: Add comments in multiple functions
Browse files Browse the repository at this point in the history
This commit introduces comments at various parts of the codebase to
explain written code better.
  • Loading branch information
theViz343 committed Nov 29, 2023
1 parent 390f17a commit 4478848
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/main/java/GraphData.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public String toString() {
graphString+="Node labels: "+graphObject.vertexSet()+"\n";
graphString+="Number of edges: "+graphObject.edgeSet().size()+"\n";
StringBuilder edges = new StringBuilder();

// Iterate over edges to form edge string
for (DefaultEdge e: graphObject.edgeSet()) {
edges.append(e.toString().replace(":", "->"));
edges.append(", ");
Expand All @@ -65,6 +67,7 @@ public String toString() {
}

public boolean outputGraph(String filepath) {
// Use function defined earlier
String graphString = toString();
try {
Files.writeString(Paths.get(filepath), graphString, StandardCharsets.ISO_8859_1);
Expand All @@ -77,6 +80,7 @@ public boolean outputGraph(String filepath) {
}

public boolean addNode(String label) {
// Check if node already exists
boolean existing = graphObject.vertexSet().stream().anyMatch(v -> Objects.equals(v, label));

if (existing) {
Expand All @@ -92,12 +96,14 @@ public boolean addNode(String label) {
public boolean addNodes(String[] labels) {
boolean result = true;
for(String label: labels) {
// Store the logical AND for the boolean values returned by the function
result = result && addNode(label);
}
return result;
}

public void removeNode(String label) throws Exception {
// Check if node already exists
boolean existing = graphObject.vertexSet().stream().anyMatch(v -> Objects.equals(v, label));

if (existing) {
Expand Down Expand Up @@ -140,6 +146,7 @@ public Path GraphSearch(String src, String dst, Algorithm algo) {
HashMap<String, Boolean> visited = new HashMap<>();
HashMap<String, String> parent = new HashMap<>();

// Select algorithm and apply it
switch(algo) {
case Algorithm.BFS: {
System.out.println("Using BFS");
Expand Down Expand Up @@ -206,6 +213,7 @@ public Path GraphSearch(String src, String dst, Algorithm algo) {
}
public void removeEdge(String srcLabel, String dstLabel) throws Exception {
DefaultEdge edgeexisting = graphObject.getEdge(srcLabel, dstLabel);
// Check if edge exists
if (edgeexisting==null) {
throw new Exception("Edge does not exist!");
} else {
Expand Down

0 comments on commit 4478848

Please sign in to comment.