Commit a5e55a18 authored by michael lundquist's avatar michael lundquist
Browse files

Removed weight from Step because I wasn't using it. Got dfs working with a...

Removed weight from Step because I wasn't using it. Got dfs working with a do-while loop, but should probably clean it up a bit. Initializing minPath to null. Making bfs and dfs public. Changing dfs to do...while.
parent a49ca6fa
......@@ -108,6 +108,7 @@ public class UndirectedGraph<E> {
/**
* Breadth First Search uses a queue
* works
*
* @param src The node you're leaving
* @param dst The node you're traveling to
......@@ -119,14 +120,14 @@ public class UndirectedGraph<E> {
ArrayList<E> path = new ArrayList<>();
path.add(src);
ArrayList<E> minPath = path;
ArrayList<E> minPath = null;
int pathWeight = 0; //distance to src
int minPathWeight = Integer.MAX_VALUE; //dummy distance to dst
LinkedList<Step> adjacencies = new LinkedList<>();
//change this to FIFO queue for bfs
Step lastStep = new Step(null, src, 0);
Step lastStep = new Step(null, src);
//adjacencies.add(lastStep);
......@@ -140,7 +141,7 @@ public class UndirectedGraph<E> {
!visited.containsKey(node) ||
(pathWeight < visited.get(node) && pathWeight < minPathWeight)
){
Step curStep = new Step(lastStep.dst, node, stepWeight);
Step curStep = new Step(lastStep.dst, node);
//If the node hasn't been visited, it visits it and traverses it.
/*skips nodes that a faster (or equally fast) path to the node
or the destination was already found
......@@ -187,26 +188,31 @@ public class UndirectedGraph<E> {
* @param dst the last node in the path
* @param visited the nodes you've already visited
*/
private ArrayList<E> dfs(E src,E dst){
public ArrayList<E> dfs(E src,E dst){
Map<E, Integer> visited = new HashMap<>();
visited.put(src, 0);
ArrayList<E> path = new ArrayList<>();
path.add(src);
ArrayList<E> minPath = path;
ArrayList<E> minPath = null;
int pathWeight = 0; //distance to src
int minPathWeight = Integer.MAX_VALUE; //dummy distance to dst
Stack<Step> adjacencies = new Stack<>();
//change this to FIFO queue for bfs
Step lastStep = new Step(null, src, 0);
Step lastStep = new Step(null, src);
adjacencies.push(lastStep);
/*
This is commented out in bfs.
It's an important distinction because in bfs, it results in an extra source,
but in dfs it's required to prevent an empty stack before you're done traversing
*/
//keeps going while there's nodes on the stack
while (!adjacencies.isEmpty()){
do{
//traverses the nodes adjacent to lastNode
for(E node: getAdjacentVertices(lastStep.dst)){
int stepWeight = weightBetween(lastStep.dst, node);
......@@ -215,7 +221,7 @@ public class UndirectedGraph<E> {
!visited.containsKey(node) ||
(pathWeight < visited.get(node) && pathWeight < minPathWeight)
){
Step curStep = new Step(lastStep.dst, node, stepWeight);
Step curStep = new Step(lastStep.dst, node);
//If the node hasn't been visited, it visits it and traverses it.
/*skips nodes that a faster (or equally fast) path to the node
or the destination was already found
......@@ -250,7 +256,8 @@ public class UndirectedGraph<E> {
lastStep = nextStep;
path.add(lastStep.dst);
}
}while (!adjacencies.isEmpty());
return minPath;
}
......@@ -260,11 +267,9 @@ public class UndirectedGraph<E> {
private final class Step{
final E src;
final E dst;//This dst is only 1 step
final int weight;
public Step(E src, E dst, int weight){
public Step(E src, E dst){
this.src = src;
this.dst = dst;
this.weight = weight;
}
}
......
......@@ -5,7 +5,6 @@ import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
......@@ -61,7 +60,7 @@ public class SearchTest {
public void shortestPathTester(){
String[] shortestPath = {"A", "B", "D"};
ArrayList<String> expected = new ArrayList<>(Arrays.asList(shortestPath));
ArrayList<String> actual = createGraph().bfs("A", "D");
ArrayList<String> actual = createGraph().dfs("A", "D");
assertEquals(expected, actual);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment