Skip to content

Commit f8c72fc

Browse files
authored
Merge pull request #17 from sanjaybhat2004/sanjaybhat2004-patch-issue#5
Updated AIDijkstra.class.st
2 parents 39cbe00 + 9dc7e95 commit f8c72fc

File tree

2 files changed

+36
-22
lines changed

2 files changed

+36
-22
lines changed

src/AI-Algorithms-Graph-Components/AIPathDistanceNode.class.st

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ Class {
88
'previousNode',
99
'visited',
1010
'outgoingEdges',
11-
'pathDistance'
11+
'pathDistance',
12+
'priority'
1213
],
1314
#category : #'AI-Algorithms-Graph-Components-Nodes'
1415
}
@@ -19,7 +20,9 @@ AIPathDistanceNode >> initialize [
1920
super initialize.
2021
outgoingEdges := OrderedCollection new.
2122
pathDistance := Float infinity.
22-
visited := false
23+
visited := false.
24+
priority := Float infinity.
25+
2326
]
2427

2528
{ #category : #accessing }
@@ -61,6 +64,18 @@ AIPathDistanceNode >> printOn: aStream [
6164
nextPutAll: pathDistance asString
6265
]
6366

67+
{ #category : #accessing }
68+
AIPathDistanceNode >> priority [
69+
70+
^ priority
71+
]
72+
73+
{ #category : #accessing }
74+
AIPathDistanceNode >> priority: anInteger [
75+
76+
priority := anInteger.
77+
]
78+
6479
{ #category : #accessing }
6580
AIPathDistanceNode >> to: aNode edge: anEdge [
6681

src/AI-Algorithms-Graph/AIDijkstra.class.st

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ AIDijkstra >> end: endModel [
3535
{ #category : #actions }
3636
AIDijkstra >> 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 }
7171
AIDijkstra >> 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 }
118117
AIDijkstra >> runFrom: startModel [
119118

0 commit comments

Comments
 (0)