-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDijkstra.java
155 lines (136 loc) · 5.03 KB
/
Dijkstra.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import java.util.PriorityQueue;
import java.util.HashMap;
import java.util.Random;
class Dijkstra {
public static void main(String[] args) {
DijGraph graph = DijGraph.buildGraph(4);
graph.printVertices();
graph.printEdges();
graph.printDijkstra("3");
}
}
class DijGraph {
HashMap<String, Vertex> vertices;
private DijGraph() {
vertices = new HashMap<String, Vertex>();
}
private void addVertex(String id){
vertices.put(id, new Vertex(id));
}
private void addEdge(String from, String to, int weight) {
Vertex fromVertex = vertices.get(from);
Vertex toVertex = vertices.get(to);
if (fromVertex == null || toVertex == null) {
System.err.printf(" %s : %s",
"DijGraph.addEdge", "could not find both nodes\n");
return;
}
if (fromVertex == toVertex) {
System.out.println("Adds self-loop");
}
fromVertex.addEdge(toVertex, weight);
}
public static DijGraph buildDirGraph(int size) {
DijGraph g = new DijGraph();
Random r = new Random();
for (int i=0; i<size; i++) {
g.addVertex(""+i);
}
for (int i=0; i<2*size; i++) {
int from = r.nextInt(size);
int to = r.nextInt(size);
while (from == to) {
to = r.nextInt(size);
}
g.addEdge(""+from, ""+to, r.nextInt(10));
}
return g;
}
public static DijGraph buildGraph(int size) {
DijGraph g = new DijGraph();
Random r = new Random();
for (int i=0; i<size; i++) {
g.addVertex(""+i);
}
for (int i=0; i<2*size; i++) {
int from = r.nextInt(size);
int to = r.nextInt(size);
int weight = r.nextInt(10);
while (from == to) {
to = r.nextInt(size);
}
g.addEdge(""+from, ""+to, weight);
g.addEdge(""+to, ""+from, weight);
}
return g;
}
public void printEdges() {
for (Vertex n : vertices.values()) {
for (Vertex neigh : n.neighbours.keySet()) {
System.out.println("(" + n.id + ", " + neigh.id + ")" + " weight: " + n.neighbours.get(neigh));
}
}
}
public void printVertices() {
for (Vertex n : vertices.values()) {
String str = "";
str += n + "\n Neighbours: ";
for (Vertex neigh : n.neighbours.keySet()) {
str += neigh.id + " ";
}
System.out.println(str);
System.out.println("");
}
}
// DIJKSTRA
// Works on weighted graphs with positive weights.
// distance = Integer.MAX_VALUE for unconnected vertices
public void printDijkstra(String startId) {
Vertex start = vertices.get(startId);
for (Vertex n : vertices.values()) {
n.distance = Integer.MAX_VALUE;
}
start.distance = 0;
PriorityQueue<Vertex> queue = new PriorityQueue<Vertex>(vertices.values());
while (!queue.isEmpty()) {
Vertex v = queue.poll();
for (Vertex n : v.neighbours.keySet()) {
if (queue.contains(n)) {
if (n.distance > v.distance + v.neighbours.get(n)) {
n.distance = v.distance + v.neighbours.get(n);
queue.remove(n);
queue.add(n);
}
}
}
}
System.out.println("Start node:" + start);
for (Vertex n : vertices.values()) {
System.out.println(n + ", distance from start: " + n.distance);
}
}
class Vertex implements Comparable<Vertex> {
String id;
HashMap<Vertex, Integer> neighbours;
int distance;
Vertex(String id){
this.id = id;
neighbours = new HashMap<Vertex, Integer>();
}
private void addEdge(Vertex to, int weight){
neighbours.put(to, weight);
}
public String toString(){
return " Vertex id: " + id;
}
public int compareTo(Vertex other) {
if (other.distance > distance) {
return -1;
}
else if (other.distance < distance) {
return 1;
}
return 0;
}
}
}