-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSixDegreeDriver.java
More file actions
94 lines (78 loc) · 2.38 KB
/
SixDegreeDriver.java
File metadata and controls
94 lines (78 loc) · 2.38 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
/* Thanks for teaching me this semester Profressor David,
* I really enjoyed your class and you are an amazing person!:)
*/
/*
* The purpose of this class is a driver for
* runing the progrom
* @author Chris Li
*/
import java.util.*;
public class SixDegreeDriver {
public static void main(String[] args) {
// Checking if user passing the name of the file as a parameter
if (args.length != 1) {
System.out.println("No enough parameters");
System.exit(1);
}
// Data members
Graph graph = new Graph();
HashMap<String, Set<String>> map = graph.getMap();
Bfs search = new Bfs();
Scanner scan = new Scanner(System.in);
boolean flag = false;
// Passing the name of the file to Graph class
graph.readNames(args[0]);
/* while loop keeps looping to get input from user
* about the name of the firs and the second actor
*/
while (true) {
System.out.print("Actor 1 name: ");
String actor1 = scan.nextLine();
for (String s: map.keySet()) {
if (s.equalsIgnoreCase(actor1)) {
actor1 = s;
flag = true;
}
}
if(!flag){
System.out.println("No such actor.");
continue;
}
System.out.print("Actor 2 name: ");
String actor2 = scan.nextLine();
for (String s: map.keySet()) {
if (s.equalsIgnoreCase(actor2)) {
actor2 = s;
flag = true;
}
}
if(!flag){
System.out.println("No such actor.");
continue;
}
/* Using function bfs in Bfs class to find the shortest path between
* two actors and call printPath function to print it out
*/
LinkedList<String> path = search.bfs(actor1, actor2, map, map.size());
printPath(actor1, actor2,path);
}
}
/** Function for printing the shortest path between the first and the second
* actor.
* @param actor1: the name of the starting actor
* @param actor2: the name of the ending actor
* @param path: the linked list that contains the names of actors, which denotes the
* shortest path btween two actors
*/
public static void printPath(String actor1, String actor2, LinkedList<String> path) {
if (path == null) {
System.out.println("There is no such path between " + actor1 + "and " + actor2);
} else {
System.out.print("Path between " + actor1 + " and " + actor2 + "is: " + path.get(0));
for (int i = 1; i < path.size(); i++) {
System.out.print(" --> " + path.get(i));
}
System.out.println();
}
}
}