-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDepth First Search algorithm.java
More file actions
125 lines (99 loc) · 4.02 KB
/
Copy pathDepth First Search algorithm.java
File metadata and controls
125 lines (99 loc) · 4.02 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
/*
This class is written by Rami Achkoudir, using implementations from using https://algs4.cs.princeton.edu/
DepthFirst is an algorithm that traverses a graph data structure by starting at the root node and
traversing as far as possible using adjacent vertices before backtracking recursively
*/
package edu.princeton.cs.algs4;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class DepthFirst{
private boolean marked[]; // boolean array to check if marked
private int[] edgeTo; // array to store last vertex used
private int source; // declare source
public DepthFirst(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
dfs(G,s); // do dfs traversing
}
// mark the source vertex as true, look for next adjacent vertex (using bag from graph method)
//and call the method recursively with the new vertex as the source vertex.
//when adjacent node is marked, return to previous node using edgeTo.
private void dfs(Graph G, int v) {
marked[v] = true;
for(int w : G.adj(v)){
if(!marked[w]){
edgeTo[w] = v;
dfs(G,w);
}
}
}
//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: ");
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
DepthFirst dfp = new DepthFirst(graph,st1.get(X));
String str = "";
//use Table2 as a "translation" from indices to Strings
for (int i : dfp.PathTo(st1.get(Y))){
str += st2.get(i) +"->";
}
System.out.println(str);
}
}