-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dijkstra.pde
99 lines (91 loc) · 3.75 KB
/
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
public class Dijkstra extends InteractiveFrame implements Solver {
Graph graph;
EventManager eventManager;
int currentLine;
String[] pseudocode = {
"choose some starting vertex x",
"add x to Priority Queue PQ",
"mark x",
"while PQ nonempty",
" choose vertex u from PQ and remove it",
" if current distance is worst continue",
" for each edge in adjacency list of u",
" if min_dist u + weight is less than min_dist v",
" mark v",
" add v to Priority Queue PQ"
};
public Dijkstra( Scene scene, Graph graph, EventManager eventManager ) {
super(scene);
this.graph = graph;
this.eventManager = eventManager;
currentLine = -1;
setShape("display");
}
public void setCurrentLine( int x ) {
currentLine = x;
}
void display(PGraphics pg) {
pg.pushStyle();
pg.background(200);
pg.strokeWeight(1);
pg.stroke(082E00);
int side = 16;
pg.textSize(12);
for(int i = 0; i < pseudocode.length; i++) {
if( i == currentLine ) pg.fill(Utility.ON_MOUSE_COLOR);
else pg.fill(255);
pg.rect(-200, i*side-60-side+2, 400, side);
pg.fill(0);
pg.text(pseudocode[i],-195,i*side-60);
}
pg.popStyle();
}
public void solve( Node source ) {
TreeMap<Node,Integer> minDistance = new TreeMap<Node, Integer>();
Heap<State> q = new Heap<State>();
minDistance.put(source, 0);
q.put(new State(minDistance.get(source),source));
source.minDistance = 0;
eventManager.addEvent(new Event(0,EventType.CODE));
eventManager.addEvent(new Event(source, EventType.QUEUED));
eventManager.addEvent(new Event(1,EventType.CODE));
eventManager.addEvent(new Event(minDistance.get(source),source,EventType.ADD_STATE));
eventManager.addEvent(new Event(2,EventType.CODE));
while(q.size() > 0) {
eventManager.addEvent(new Event(3,EventType.CODE));
int distance = q.getTop().cost;
Node current = q.getTop().node;
q.remove();
eventManager.addEvent(new Event(4,EventType.CODE));
eventManager.addEvent(new Event(current,EventType.PROCESSING));
eventManager.addEvent(new Event(current,EventType.REMOVE_NODE));
eventManager.addEvent(new Event(5,EventType.CODE));
if( distance == minDistance.get(current) ) {
eventManager.addEvent(new Event(current,EventType.SHOW_RESULT));
for(Integer edgeId : graph.adjacencyList.get(current)) {
eventManager.addEvent(new Event(6,EventType.CODE));
eventManager.addEvent(new Event(graph.edges.get(edgeId), EventType.PROCESSING));
Edge edge = graph.edges.get(edgeId);
Node neighbor = edge.getFrom();
if(neighbor.compareTo(current) == 0) {
neighbor = edge.getTo();
}
eventManager.addEvent(new Event(7,EventType.CODE));
if(minDistance.containsKey(neighbor) == false || minDistance.get(current)+edge.getWeight() < minDistance.get(neighbor) ) {
minDistance.put(neighbor, minDistance.get(current)+edge.getWeight() );
q.put(new State(minDistance.get(neighbor), neighbor));
neighbor.minDistance = minDistance.get(neighbor);
eventManager.addEvent(new Event(8,EventType.CODE));
eventManager.addEvent(new Event(neighbor, EventType.QUEUED));
eventManager.addEvent(new Event(9,EventType.CODE));
eventManager.addEvent(new Event(minDistance.get(neighbor),neighbor,EventType.ADD_STATE));
}
eventManager.addEvent(new Event(graph.edges.get(edgeId), EventType.DEFAULT_EDGE));
}
}
eventManager.addEvent(new Event(current,EventType.VISITED));
}
eventManager.addEvent(new Event(3,EventType.CODE));
eventManager.addEvent(new Event(-1,EventType.CODE));
}
}