Skip to content

Commit e33c6f9

Browse files
authored
Merge pull request #31 from virenvarma007/Search_using_dictionary
Test added and findNode fixed with O(1) complexity
2 parents 232cacb + 2461a18 commit e33c6f9

File tree

2 files changed

+89
-6
lines changed

2 files changed

+89
-6
lines changed

src/AI-Algorithms-Graph-Tests/AIGraphAlgorithmTest.class.st

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,71 @@ AIGraphAlgorithmTest >> testAddNodesFromChildrenBlock [
2929
WideSymbol }.
3030
self assert: (builder findNode: ByteSymbol) adjacentNodes isEmpty
3131
]
32+
33+
{ #category : #'as yet unclassified' }
34+
AIGraphAlgorithmTest >> testingAddingNodes [
35+
36+
|graphType graph dijkstra|
37+
graphType := AICyclicWeightedSimpleFixture new.
38+
graph :=graphType aseCircuitWeightedGraph.
39+
dijkstra := AIDijkstra new.
40+
dijkstra nodes: graph nodes.
41+
dijkstra
42+
edges: graph edges
43+
from: #first
44+
to: #second
45+
weight: #third.
46+
47+
self assert: 8 equals: (dijkstra nodes) size.
48+
dijkstra addNodeFor: $i.
49+
self assert: 9 equals: (dijkstra nodes) size.
50+
self assert: Float infinity equals: (dijkstra findNode: $i) pathDistance.
51+
52+
]
53+
54+
{ #category : #'as yet unclassified' }
55+
AIGraphAlgorithmTest >> testingFindingNodes [
56+
57+
|graphType graph dijkstra|
58+
graphType := AICyclicWeightedSimpleFixture new.
59+
graph :=graphType aseCircuitWeightedGraph.
60+
dijkstra := AIDijkstra new.
61+
dijkstra nodes: graph nodes.
62+
dijkstra
63+
edges: graph edges
64+
from: #first
65+
to: #second
66+
weight: #third.
67+
68+
self assert: Float infinity equals: (dijkstra findNode: $g) pathDistance.
69+
]
70+
71+
{ #category : #'as yet unclassified' }
72+
AIGraphAlgorithmTest >> testingNodeDictionary [
73+
74+
|graphType graph dijkstra|
75+
graphType := AICyclicWeightedSimpleFixture new.
76+
graph :=graphType aseCircuitWeightedGraph.
77+
dijkstra := AIDijkstra new.
78+
dijkstra nodes: graph nodes.
79+
dijkstra
80+
edges: graph edges
81+
from: #first
82+
to: #second
83+
weight: #third.
84+
dijkstra runFrom: $a.
85+
self assert: 1 equals: (dijkstra findNode: $b) pathDistance.
86+
self assert: 8 equals: (dijkstra nodes) size.
87+
88+
dijkstra addNodeFor: $i.
89+
self assert: 9 equals: (dijkstra nodes) size.
90+
self assert: Float infinity equals: (dijkstra findNode: $i) pathDistance.
91+
92+
dijkstra reset.
93+
dijkstra runFrom: $c.
94+
self assert: 2 equals: (dijkstra findNode: $b) pathDistance.
95+
96+
dijkstra reset.
97+
dijkstra runFrom: $h.
98+
self assert: Float infinity equals: (dijkstra findNode: $a) pathDistance
99+
]

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

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ Class {
1010
#instVars : [
1111
'nodes',
1212
'edges',
13-
'sortingBlock'
13+
'sortingBlock',
14+
'nodeDictionary'
1415
],
1516
#category : #'AI-Algorithms-Graph-Core'
1617
}
@@ -45,9 +46,14 @@ AIGraphAlgorithm >> addEdge: edgeModel from: sourceBlock to: targetBlock [
4546
{ #category : #'building - graph' }
4647
AIGraphAlgorithm >> addNodeFor: aModel [
4748

49+
| newNode |
4850
^ self
4951
findNode: aModel
50-
ifAbsent: [ nodes add: (self nodeClass with: aModel) ]
52+
ifAbsent: [
53+
newNode := (self nodeClass with: aModel).
54+
nodes add: newNode .
55+
nodeDictionary at: aModel put: newNode .
56+
]
5157
]
5258

5359
{ #category : #'building - graph' }
@@ -168,15 +174,21 @@ AIGraphAlgorithm >> findNode: aModel [
168174

169175
"^ nodes findBinary: (self findBinaryBlock: aModel)."
170176

171-
^ nodes detect: [ :node | node model = aModel ]
177+
"^ nodes detect: [ :node | node model = aModel ]"
178+
179+
(nodeDictionary includesKey: aModel) ifTrue: [ ^ nodeDictionary at: aModel ].
180+
Error signal: ('No Element in Graph : ',(aModel asString) ).
181+
172182
]
173183

174184
{ #category : #accessing }
175185
AIGraphAlgorithm >> findNode: aModel ifAbsent: aBlock [
176186

177187
"^ nodes findBinary: (self findBinaryBlock: aModel) ifNone: aBlock"
178188

179-
^ nodes detect: [ :node | node model = aModel ] ifNone: aBlock
189+
"^ nodes detect: [ :node | node model = aModel ] ifNone: aBlock"
190+
191+
^nodeDictionary at: aModel ifAbsent: aBlock.
180192
]
181193

182194
{ #category : #accessing }
@@ -187,7 +199,9 @@ AIGraphAlgorithm >> findNode: aModel ifFound: aBlock [
187199
do: aBlock
188200
ifNone: [ ]"
189201

190-
^ nodes detect: [ :aNode | aNode model = aModel ] ifFound: aBlock
202+
"^ nodes detect: [ :aNode | aNode model = aModel ] ifFound: aBlock"
203+
204+
^nodeDictionary at: aModel ifPresent: aBlock.
191205
]
192206

193207
{ #category : #accessing }
@@ -201,7 +215,8 @@ AIGraphAlgorithm >> initialize [
201215

202216
sortingBlock := [ :a :b | a model hash <= b model hash ].
203217
nodes := SortedCollection sortUsing: sortingBlock.
204-
edges := SortedCollection sortUsing: sortingBlock
218+
edges := SortedCollection sortUsing: sortingBlock.
219+
nodeDictionary := Dictionary new.
205220
]
206221

207222
{ #category : #configuration }

0 commit comments

Comments
 (0)