-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBreadth First Search.java
More file actions
137 lines (107 loc) · 4.34 KB
/
Copy pathBreadth First Search.java
File metadata and controls
137 lines (107 loc) · 4.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
This class is written by Rami Achkoudir, using implementations from using https://algs4.cs.princeton.edu/
BreadthFirst is an algorithm that traverses a graph data structure by starting at the root node
and going through all adjacent vertices before moving on to the next depth level
*/
package edu.princeton.cs.algs4;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class BreadthFirst{
private boolean marked[]; // boolean array to check if marked
private int[] edgeTo; // array to store last vertex used
private int source; // declare source
public BreadthFirst(Graph G, int s){
marked = new boolean[G.V()]; // mark this vertex
edgeTo = new int[G.V()]; // the last vertex before chosen vertex
this.source = s; // source input
bfp(G,s); // do bfs traversing
}
//Creates a queue to store integers in FIFO order
//marks the source vertex. While queue is not empty dequeue last int and look for adjacent
//vertices. Add adjacent vertices to marked[] = true and to edgeTo[] with the last vertex.
//iterate through all adjacent vertex
private void bfp(Graph graph, int s) {
Queue<Integer> queue = new Queue<>();
queue.enqueue(s);
marked[s] = true;
while (!queue.isEmpty()) {
int v = queue.dequeue();
for (int w : graph.adj(v)) {
if (!marked[w]) {
queue.enqueue(w);
marked[w] = true;
edgeTo[w] = v;
}
}
}
}
//checks if there is a path between destination and source vertex
//uses EdgeTo array to see previous vertex and iterates through the array until we reach
//the source
public boolean hasPathTo(int v)
{ return marked[v]; }
public Iterable<Integer> PathTo(int v){
if (!hasPathTo(v)) {
System.out.println("Not found");
System.exit(0);}
Stack<Integer> path = new Stack<Integer>();
for (int x = v; x != source; x = edgeTo[x])
path.push(x);
path.push(source);
return path;
}
public static void main(String[] args) throws FileNotFoundException {
File x = new File("C:\\Users\\usr-ramach01\\eclipse-workspace\\labb4\\src\\edu\\princeton\\cs\\algs4\\graph.txt");
Scanner sc = new Scanner(x);
String key = "";
int vertex = 0;
//Two symbol tables, both takes in every node, one is reversed
ST<String, Integer> st1 = new ST<>();
ST<Integer, String> st2 = new ST<>();
// pushing the key-pair onto separate stacks
Stack<String> stack1 = new Stack<>();
Stack<String> stack2 = new Stack<>();
//if key not in table, add key and vertex
while(sc.hasNext()){
key = sc.next();
if(st1.get(key) == null){
st1.put(key, vertex);
st2.put(vertex, key);
vertex++;
}
//push first key of pair
stack1.push(key);
key = sc.next();
if(st1.get(key) == null){
st1.put(key, vertex);
st2.put(vertex, key);
vertex++;
}
//push next key of the pair
stack2.push(key);
}
// create graph where size is number of vertex
Graph graph = new Graph(vertex);
sc.close();
// iterate through stack and pop them, this will add an edge between indices of table1
while(!stack1.isEmpty()){
graph.addEdge(st1.get(stack1.pop()), st1.get(stack2.pop()));
}
System.out.println("Enter two cities to find a path: " );
//not case sensitive
Scanner sc2 = new Scanner(System.in);
String X = sc2.next().toUpperCase();
String Y = sc2.next().toUpperCase();
sc2.close();
//calls on PathTo method which generates a stack with integers from source to destination
//table 2 will receive a stack and return the country-state corresponding with each index
BreadthFirst bfp = new BreadthFirst(graph,st1.get(X));
String str = "";
//use Table2 as a "translation" from indices to Strings
for (int i : bfp.PathTo(st1.get(Y))){
str += st2.get(i) +"->";
}
System.out.println(str);
}
}