Skip to content

Commit

Permalink
Merge pull request #17 from sanjaybhat2004/sanjaybhat2004-patch-issue#5
Browse files Browse the repository at this point in the history
Updated AIDijkstra.class.st
  • Loading branch information
jordanmontt authored Mar 24, 2023
2 parents 39cbe00 + 9dc7e95 commit f8c72fc
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 22 deletions.
19 changes: 17 additions & 2 deletions src/AI-Algorithms-Graph-Components/AIPathDistanceNode.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ Class {
'previousNode',
'visited',
'outgoingEdges',
'pathDistance'
'pathDistance',
'priority'
],
#category : #'AI-Algorithms-Graph-Components-Nodes'
}
Expand All @@ -19,7 +20,9 @@ AIPathDistanceNode >> initialize [
super initialize.
outgoingEdges := OrderedCollection new.
pathDistance := Float infinity.
visited := false
visited := false.
priority := Float infinity.

]

{ #category : #accessing }
Expand Down Expand Up @@ -61,6 +64,18 @@ AIPathDistanceNode >> printOn: aStream [
nextPutAll: pathDistance asString
]

{ #category : #accessing }
AIPathDistanceNode >> priority [

^ priority
]

{ #category : #accessing }
AIPathDistanceNode >> priority: anInteger [

priority := anInteger.
]

{ #category : #accessing }
AIPathDistanceNode >> to: aNode edge: anEdge [

Expand Down
39 changes: 19 additions & 20 deletions src/AI-Algorithms-Graph/AIDijkstra.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ AIDijkstra >> end: endModel [
{ #category : #actions }
AIDijkstra >> newPriorityQueue [

"This is the naive implementation of the data structure."
"We use the Heap object defined in the SequenceableCollections package."

^ OrderedCollection new
^ Heap new
]

{ #category : #configuration }
Expand Down Expand Up @@ -70,11 +70,7 @@ AIDijkstra >> reconstructPath [
{ #category : #actions }
AIDijkstra >> removeMostPromisingPair: aPriorityQueue [

"This is the naive implementation of the data structure."

| minValue |
minValue := aPriorityQueue detectMin: [ :assoc | assoc value ].
^ aPriorityQueue remove: minValue
^ aPriorityQueue removeFirst
]

{ #category : #initialization }
Expand All @@ -92,28 +88,31 @@ AIDijkstra >> run [

| pq |
pq := self newPriorityQueue.
pq add: start -> 0.

[ pq isNotEmpty ] whileTrue: [
| assoc node minWeight |
assoc := self removeMostPromisingPair: pq.
node := assoc key.
minWeight := assoc value.
pq sortBlock: [ :element1 :element2 | (element1 priority ) <= (element2 priority )].
start priority: 0.
pq add: start.

[ pq isNotEmpty ] whileTrue: [
| node minWeight |
node := self removeMostPromisingPair: pq.
minWeight := node priority.
node visited: true.

"Skip if the path weight is less than the one obtained from the pq.
This is an optimization for not processing unnecessary nodes."
node pathDistance < minWeight ifFalse: [
node outgoingEdges do: [ :edge |
edge to visited ifFalse: [
node pathDistance < minWeight ifFalse: [
node outgoingEdges do: [ :edge |
edge to visited ifFalse: [
| newDistance |
newDistance := node pathDistance + edge weight.

newDistance < edge to pathDistance ifTrue: [
newDistance < edge to pathDistance ifTrue: [
self updateDistance: newDistance of: edge to previousNode: node.
pq add: edge to -> newDistance ] ] ] ] ]
edge to priority: newDistance.
pq add: edge to] ] ] ] ]
]


{ #category : #running }
AIDijkstra >> runFrom: startModel [

Expand Down

0 comments on commit f8c72fc

Please sign in to comment.