@@ -35,9 +35,9 @@ AIDijkstra >> end: endModel [
3535{ #category : #actions }
3636AIDijkstra >> newPriorityQueue [
3737
38- " This is the naive implementation of the data structure ."
38+ " We use the Heap object defined in the SequenceableCollections package ."
3939
40- ^ OrderedCollection new
40+ ^ Heap new
4141]
4242
4343{ #category : #configuration }
@@ -70,11 +70,7 @@ AIDijkstra >> reconstructPath [
7070{ #category : #actions }
7171AIDijkstra >> removeMostPromisingPair: aPriorityQueue [
7272
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
7874]
7975
8076{ #category : #initialization }
@@ -92,28 +88,31 @@ AIDijkstra >> run [
9288
9389 | pq |
9490 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.
10299 node visited: true .
103100
104101 " Skip if the path weight is less than the one obtained from the pq.
105102 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: [
109106 | newDistance |
110107 newDistance := node pathDistance + edge weight.
111-
112- newDistance < edge to pathDistance ifTrue: [
108+
109+ newDistance < edge to pathDistance ifTrue: [
113110 self updateDistance: newDistance of: edge to previousNode: node.
114- pq add: edge to - > newDistance ] ] ] ] ]
111+ edge to priority: newDistance.
112+ pq add: edge to] ] ] ] ]
115113]
116114
115+
117116{ #category : #running }
118117AIDijkstra >> runFrom: startModel [
119118
0 commit comments