-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShotestPathFinder.java
140 lines (120 loc) · 5.31 KB
/
ShotestPathFinder.java
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
138
139
140
/**
* @author Shanika Perera
*
*/
import java.io.InputStreamReader;
import java.util.Scanner;
public class ShotestPathFinder {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner = new Scanner(new InputStreamReader(System.in));
int inputSearchAlgo;
Character inputStartCity;
char inputContinue = 'Y';
//initializing cities
City A = new City(0,"Arad");
City B = new City(1,"Bucharest");
City C = new City(2,"Craiova");
City D = new City(3,"Drobeta");
City E = new City(4,"Eforie");
City F = new City(5,"Fagaras");
City G = new City(6,"Giurgiu");
City H = new City(7,"Hirsova");
City I = new City(8,"Iasi");
City L = new City(9,"Lugoj");
City M = new City(10,"Mehadia");
City N = new City(11,"Neamt");
City O = new City(12,"Oradea");
City P = new City(13,"Pitesti");
City R = new City(14,"Rimnicu");
City S = new City(15,"Sibiu");
City T = new City(16,"Timisoara");
City U = new City(17,"Urziceni");
City V = new City(18,"Vaslui");
City Z = new City(19,"Zerind");
//adding vertices to the graph
RoadMapGraph.addCity(A);
RoadMapGraph.addCity(B);
RoadMapGraph.addCity(C);
RoadMapGraph.addCity(D);
RoadMapGraph.addCity(E);
RoadMapGraph.addCity(F);
RoadMapGraph.addCity(G);
RoadMapGraph.addCity(H);
RoadMapGraph.addCity(I);
RoadMapGraph.addCity(L);
RoadMapGraph.addCity(M);
RoadMapGraph.addCity(N);
RoadMapGraph.addCity(O);
RoadMapGraph.addCity(P);
RoadMapGraph.addCity(R);
RoadMapGraph.addCity(S);
RoadMapGraph.addCity(T);
RoadMapGraph.addCity(U);
RoadMapGraph.addCity(V);
RoadMapGraph.addCity(Z);
// adding edges to the graph
RoadMapGraph.addRoad(A, S, true);
RoadMapGraph.addRoad(A, T, true);
RoadMapGraph.addRoad(A, Z, true);
RoadMapGraph.addRoad(S, F, true);
RoadMapGraph.addRoad(S, O, true);
RoadMapGraph.addRoad(S, R, true);
RoadMapGraph.addRoad(T, L, true);
RoadMapGraph.addRoad(Z, O, true);
RoadMapGraph.addRoad(F, B, true);
RoadMapGraph.addRoad(R, C, true);
RoadMapGraph.addRoad(R, P, true);
RoadMapGraph.addRoad(L, M, true);
RoadMapGraph.addRoad(B, G, true);
RoadMapGraph.addRoad(B, P, true);
RoadMapGraph.addRoad(B, U, true);
RoadMapGraph.addRoad(C, D, true);
RoadMapGraph.addRoad(C, P, true);
RoadMapGraph.addRoad(M, D, true);
RoadMapGraph.addRoad(U, H, true);
RoadMapGraph.addRoad(U, V, true);
RoadMapGraph.addRoad(H, E, true);
RoadMapGraph.addRoad(V, I, true);
RoadMapGraph.addRoad(I, N, true);
System.out.println("Welcome to Romania Problem Application!\n\nBelow are the graph representations of the Romania Map:\n");
System.out.println("Adjacency List of the graph \n----------------------------");
String adjListStr = RoadMapGraph.adjacencyList();
System.out.println(adjListStr);
System.out.println("Adjacency Matrix of the graph \n------------------------------");
String adjMatrixStr = RoadMapGraph.adjacencyMatrix();
System.out.println(adjMatrixStr);
while(inputContinue == 'Y' || inputContinue == 'y') {
System.out.println("Please select the Search Algorithm and press enter:\n|1| BFS |2| DFS |3| IDS");
inputSearchAlgo = scanner.nextInt();
System.out.println("\nPlease select the Start City(first character only) and press enter: \n"
+ "|A| Arad |Z| Zerind |O| Oradea |S| Sibiu |T| Timisoara |L| Lugoj |M| Mehadia |D| Drobeta |C| Craiova |R| Rimnicu\n"
+ "|F| Fagaras |P| Pitesti |G| Giurgiu |U| Urziceni |H| Hirsova |E| Eforie |V| Vaslui |I| Iasi |N| Neamt");
inputStartCity = Character.toUpperCase(scanner.next().charAt(0));
if (inputSearchAlgo == 1)
RoadMapGraph.bfs(RoadMapGraph.getCityByNameId(inputStartCity), B);
else if (inputSearchAlgo == 2)
RoadMapGraph.dfs(RoadMapGraph.getCityByNameId(inputStartCity), B);
else if (inputSearchAlgo == 3)
RoadMapGraph.ids(RoadMapGraph.getCityByNameId(inputStartCity), B);
else
System.out.println("Incorrect option!");
System.out.print("\nList of Explored/Visited cities: ");
for (int i = 0; i < RoadMapGraph.exploredCityList.size(); i++) {
System.out.print(RoadMapGraph.exploredCityList.get(i).getNameId() + " ");
}
System.out.print("\nPath from the Start to Destination: ");
for (int i = 0; i < RoadMapGraph.pathCityList.size(); i++) {
System.out.print(RoadMapGraph.pathCityList.get(i).getNameId() + " ");
}
System.out.println("\nTotal Path Cost: " + RoadMapGraph.pathCost);
System.out.println("\nDo you want to continue?(Y/N)");
inputContinue = scanner.next().charAt(0);
//restart cleanup
RoadMapGraph.exploredCityList.removeAll(RoadMapGraph.exploredCityList);
RoadMapGraph.pathCityList.removeAll(RoadMapGraph.pathCityList);
RoadMapGraph.pathCost = -1;
}
System.out.println("\nThank you!");
}
}