Skip to content

Commit

Permalink
Fix Traversal Print. It was wrongly using the successor array and now…
Browse files Browse the repository at this point in the history
… uses a correct list
  • Loading branch information
LeonardoTPereira committed Jun 28, 2021
1 parent 5966d76 commit 47e6f35
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 14 deletions.
11 changes: 10 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@
<artifactId>svg-salamander</artifactId>
<version>1.1.2.2</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>


</dependencies>
Expand All @@ -39,5 +45,8 @@
<sonar.projectKey>LeonardoTPereira_Graphs</sonar.projectKey>
<sonar.organization>leonardotpereira</sonar.organization>
<sonar.host.url>https://sonarcloud.io</sonar.host.url>
<sonar.coverage.jacoco.xmlReportPaths>CoverageReport
</sonar.coverage.jacoco.xmlReportPaths>
</properties>
</project>
</project>

4 changes: 3 additions & 1 deletion src/main/java/graph/BreadthFirstTraversal.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public BreadthFirstTraversal(AbstractGraph graph)
public void traverseGraph(Vertex source)
{
int sourceIndex = getGraph().getVertices().indexOf(source);
addToPath(source);
markVertexAsVisited(sourceIndex);
setDistanceToVertex(sourceIndex, 0);
setPredecessorVertexIndex(sourceIndex, -1);
Expand Down Expand Up @@ -43,7 +44,7 @@ public void traverseGraph(Vertex source)
}
}
}
printPath(sourceIndex);
printPath();
printShortestPath(source, getGraph().getVertices().get(getGraph().getVertices().size()-1));
}

Expand All @@ -53,6 +54,7 @@ private void updateTraversalInfoForVertex(int newVertexIndex, int previousVertex
var oldVertex = getGraph().getVertices().get(previousVertexIndex);
float newDistance = getGraph().getDistance(oldVertex, newVertex);
float distance = getDistanceToVertex(previousVertexIndex) + newDistance;
addToPath(newVertex);
markVertexAsVisited(newVertexIndex);
setDistanceToVertex(newVertexIndex, distance);
setPredecessorVertexIndex(newVertexIndex, previousVertexIndex);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/graph/GraphController.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,6 @@ private void createTest()
g.addEdge(g.getVertices().get(5), g.getVertices().get(6), 1);
g.addEdge(g.getVertices().get(5), g.getVertices().get(0), 3);

traversalStrategy = new FloydWarshallTraversal(g);
traversalStrategy = new BreadthFirstTraversal(g);
}
}
29 changes: 18 additions & 11 deletions src/main/java/graph/TraversalStrategyInterface.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package graph;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Logger;

public abstract class TraversalStrategyInterface
Expand All @@ -11,6 +13,7 @@ public abstract class TraversalStrategyInterface
private float[] distanceToVertices;
private int[] predecessorVertexIndices;
private int[] successorVertexIndices;
private List<Vertex> traversalPath;

public void markVertexAsVisited(int vertexIndex)
{
Expand Down Expand Up @@ -63,6 +66,7 @@ protected TraversalStrategyInterface(AbstractGraph graph)
Arrays.fill(predecessorVertexIndices, -1);
successorVertexIndices = new int[graph.getNumberOfVertices()];
Arrays.fill(successorVertexIndices, -1);
traversalPath = new LinkedList<>();
}

abstract void traverseGraph(Vertex source);
Expand All @@ -77,18 +81,16 @@ public void setGraph(AbstractGraph graph)
this.graph = graph;
}

protected void printPath(int sourceIndex)
protected void printPath()
{
var visitedPath = new StringBuilder();
int currentIndex = sourceIndex;
do
for (Vertex vertex : traversalPath)
{
visitedPath.append(getGraph().getVertices().get(currentIndex)).append(' ').
append("Distance: ").append(getDistanceToVertex(currentIndex)).append(' ');
currentIndex = getSuccessorVertexIndex(currentIndex);
}while(currentIndex > 0);
var traversalPath = "\n"+ visitedPath +"\n";
LOGGER.info(traversalPath);
visitedPath.append(vertex).append(' ').
append("Distance: ").append(getDistanceToVertex(getGraph().getVertices().indexOf(vertex))).append(' ');
}
var traversalPathString = "\n"+ visitedPath +"\n";
LOGGER.info(traversalPathString);
}

protected void printShortestPath(Vertex source, Vertex destination)
Expand All @@ -103,8 +105,8 @@ protected void printShortestPath(Vertex source, Vertex destination)
currentIndex = getPredecessorVertexIndex(currentIndex);
}while(currentIndex != sourceIndex);
shortestPath.append(graph.getVertices().get(currentIndex));
var traversalPath = "\n"+ shortestPath +"\n";
LOGGER.info(traversalPath);
var shortestPathString = "\n"+ shortestPath +"\n";
LOGGER.info(shortestPathString);
}

protected void printDistances()
Expand All @@ -117,4 +119,9 @@ protected void printDistances()
var finalString = distanceString.toString();
LOGGER.info(finalString);
}

public void addToPath(Vertex vertex)
{
traversalPath.add(vertex);
}
}

0 comments on commit 47e6f35

Please sign in to comment.