@@ -35,9 +35,9 @@ AIDijkstra >> end: endModel [
35
35
{ #category : #actions }
36
36
AIDijkstra >> newPriorityQueue [
37
37
38
- " This is the naive implementation of the data structure ."
38
+ " We use the Heap object defined in the SequenceableCollections package ."
39
39
40
- ^ OrderedCollection new
40
+ ^ Heap new
41
41
]
42
42
43
43
{ #category : #configuration }
@@ -70,11 +70,7 @@ AIDijkstra >> reconstructPath [
70
70
{ #category : #actions }
71
71
AIDijkstra >> removeMostPromisingPair: aPriorityQueue [
72
72
73
- " This is the naive implementation of the data structure."
74
-
75
- | minValue |
76
- minValue := aPriorityQueue detectMin: [ :assoc | assoc value ].
77
- ^ aPriorityQueue remove: minValue
73
+ ^ aPriorityQueue removeFirst
78
74
]
79
75
80
76
{ #category : #initialization }
@@ -92,28 +88,31 @@ AIDijkstra >> run [
92
88
93
89
| pq |
94
90
pq := self newPriorityQueue.
95
- pq add: start - > 0 .
96
-
97
- [ pq isNotEmpty ] whileTrue: [
98
- | assoc node minWeight |
99
- assoc := self removeMostPromisingPair: pq.
100
- node := assoc key.
101
- minWeight := assoc value.
91
+ pq sortBlock: [ :element1 :element2 | (element1 priority ) <= (element2 priority )].
92
+ start priority: 0 .
93
+ pq add: start.
94
+
95
+ [ pq isNotEmpty ] whileTrue: [
96
+ | node minWeight |
97
+ node := self removeMostPromisingPair: pq.
98
+ minWeight := node priority.
102
99
node visited: true .
103
100
104
101
" Skip if the path weight is less than the one obtained from the pq.
105
102
This is an optimization for not processing unnecessary nodes."
106
- node pathDistance < minWeight ifFalse: [
107
- node outgoingEdges do: [ :edge |
108
- edge to visited ifFalse: [
103
+ node pathDistance < minWeight ifFalse: [
104
+ node outgoingEdges do: [ :edge |
105
+ edge to visited ifFalse: [
109
106
| newDistance |
110
107
newDistance := node pathDistance + edge weight.
111
-
112
- newDistance < edge to pathDistance ifTrue: [
108
+
109
+ newDistance < edge to pathDistance ifTrue: [
113
110
self updateDistance: newDistance of: edge to previousNode: node.
114
- pq add: edge to - > newDistance ] ] ] ] ]
111
+ edge to priority: newDistance.
112
+ pq add: edge to] ] ] ] ]
115
113
]
116
114
115
+
117
116
{ #category : #running }
118
117
AIDijkstra >> runFrom: startModel [
119
118
0 commit comments