-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdriver.java
More file actions
101 lines (77 loc) · 2.97 KB
/
Copy pathdriver.java
File metadata and controls
101 lines (77 loc) · 2.97 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
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Scanner;
public class driver {
public static void main(String[] args) throws IOException {
//start hash table
HashMap<String, vertex> graph = new HashMap<>();
String start = "";
//read data in
//File file = new File("graph24-6.txt");
File file = new File("WarAndPeace.txt_11765_90758_graph.txt");
Scanner sc = new Scanner(file);
String st;
String[] connections;
while (sc.hasNextLine()) {
connections = sc.nextLine().split("\\s+");
//get first entry
if(graph.size() == 0){
start = connections[0];
}
//if new entry
if (!graph.containsKey(connections[0])) {
vertex temp = new vertex(0.0,null,connections[0]);
for (int i = 1; i < connections.length - 1; i = i + 2) {
//read data
String v = connections[i];
double w = Double.parseDouble(connections[i + 1]);
connect newConnect = new connect(v,w);
temp.addEdge(newConnect);
}
graph.put(connections[0],temp);
} else {
//take priority queue, add stuff, replace back into hashmap
vertex temp = graph.get(connections[0]);
for (int i = 1; i < connections.length - 1; i = i + 2) {
//read data
String v = connections[i];
double w = Double.parseDouble(connections[i + 1]);
connect newConnect = new connect(v,w);
temp.addEdge(newConnect);
}
graph.replace(connections[0],temp);
}
}
//print the data
for (HashMap.Entry<String,vertex> entry : graph.entrySet()){
//print name(key)
System.out.print(entry.getKey());
//print vertex data
vertex v = entry.getValue();
System.out.print(" pi:" + v.pi + " d:" + v.dist);
for(connect cc: v.edges){
System.out.print(" " + cc.vertex + ": " + cc.weight);
}
System.out.println();
}
DijkstraAlg alg = new DijkstraAlg(graph, start);
graph = alg.solve();
//print the data
for (HashMap.Entry<String,vertex> entry : graph.entrySet()){
//print name(key)
System.out.print(entry.getKey());
//print vertex data
vertex v = entry.getValue();
System.out.print(" pi:" + v.pi + " d:" + v.dist);
for(connect cc: v.edges){
System.out.print(" " + cc.vertex + ": " + cc.weight);
}
System.out.println();
}
}
}