-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch_dijkstra.pde
163 lines (132 loc) · 3.45 KB
/
sketch_dijkstra.pde
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
156
157
158
159
160
161
162
163
import java.util.HashMap;
import java.util.Map;
// the graph
ArrayList<Node> nodes;
ArrayList<Arc> arcs;
// initial and final nodes
Node start;
Node target;
// data structures for the algorithm
int[] L; // shortest path from start to j know until current iteration
HashMap<Node, Node> pred; // predecessor of j in shortest path from s
// number of nodes
int n;
// number of iteration
int k;
// flag for finished alg
boolean solved;
void setup() {
size(900, 800);
n = 20;
solved = false;
nodes = new ArrayList<Node>(n);
arcs = new ArrayList<Arc>();
L = new int[n];
pred = new HashMap<Node, Node>(n);
// generate the nodes
while (nodes.size() < n) {
float randX = random(40, width-40);
float randY = random(40, height-40);
Node newNode = new Node(randX, randY, nodes.size());
boolean add = true;
for (int i = 0; i < nodes.size(); i++) {
Node n = nodes.get(i);
if (n.distanceFrom(newNode) < 40) {
add = false;
break;
}
}
if (nodes.size() == 0 || add) {
nodes.add(newNode);
}
}
// generate the arcs
for (int from = 0; from < nodes.size(); from++) {
for (int to = 0; to < nodes.size(); to++) {
if (from == to)
continue;
Arc newArc;
// add just a few arcs
if (random(0, 1) < 0.4)
newArc = new Arc(nodes.get(from), nodes.get(to));
else
// express the absense of an arc by plugging a very high cost fictitious one
newArc = new Arc(nodes.get(from), nodes.get(to), +9999999);
arcs.add(newArc);
}
}
// initialize nodes and data structures
start = nodes.get(0);
target = nodes.get(nodes.size()-1);
start.start = true;
target.target = true;
L[0] = 0;
for (int i = 1; i < n; i++) {
pred.put(nodes.get(i), start);
int arcIndex = arcs.indexOf(new Arc(start, nodes.get(i)));
L[i] = arcs.get(arcIndex).cost;
}
start.visited = true;
k = 1;
}
void draw() {
background(50);
// display the graph
for (int i = 0; i < arcs.size(); i++) {
arcs.get(i).display();
}
for (int i = 0; i < nodes.size(); i++) {
nodes.get(i).display();
}
// reached all nodes
if (k == n)
solved = true;
// 1 iteration
if (!solved) {
// find min
int minCost = +9999999;
Node h = new Node(0, 0, -1);
int index_h = -1;
for (int j = 0; j < n; j++) {
if (! nodes.get(j).visited && L[j] < minCost) {
minCost = L[j];
index_h = j;
h = nodes.get(j);
}
}
if (index_h == -1)
// no way to keep exploring and target not reached
println("no solution!");
else {
h.visited = true;
// update data structures
for (int j = 0; j < n; j++) {
int arcIndex = arcs.indexOf(new Arc(h, nodes.get(j)));
if (!nodes.get(j).visited && L[index_h] + arcs.get(arcIndex).cost < L[j]) {
L[j] = L[index_h] + arcs.get(arcIndex).cost;
pred.put(nodes.get(j), h);
}
}
// check if early finish
if (h.equals(target)) {
println("solved!");
solved = true;
// backtrack and highlight the path
Node current = target;
while (!current.equals(start)) {
println(current.index);
Node next = pred.get(current);
Arc a = new Arc(next, current);
arcs.get(arcs.indexOf(a)).solution = true;
current = next;
}
println(current.index);
}
k++;
}
}
noLoop();
}
void keyPressed() {
loop();
}