Skip to content

Commit c27da26

Browse files
authored
Merge pull request #43 from Alokzh/dfs-algorithm-implementation
Implemented Depth-First Search (DFS) Algorithm & Added Tests
2 parents 6e902cf + 1452de1 commit c27da26

18 files changed

+491
-143
lines changed

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

+13-11
Original file line numberDiff line numberDiff line change
@@ -2,60 +2,62 @@
22
I am a node that is used in the BFS algorithm defined in the `AIBFS` class. I have an instance variable `previousNode` to track from which node I have been called and also an instance variable visited to see if I were visited or not.
33
"
44
Class {
5-
#name : #AIBFSNode,
6-
#superclass : #AIGraphNode,
5+
#name : 'AIBFSNode',
6+
#superclass : 'AIGraphNode',
77
#instVars : [
88
'previousNode',
99
'visited',
1010
'distance'
1111
],
12-
#category : #'AI-Algorithms-Graph-Components-Nodes'
12+
#category : 'AI-Algorithms-Graph-Components-Nodes',
13+
#package : 'AI-Algorithms-Graph-Components',
14+
#tag : 'Nodes'
1315
}
1416

15-
{ #category : #accessing }
17+
{ #category : 'accessing' }
1618
AIBFSNode >> distance [
1719

1820
^ distance
1921
]
2022

21-
{ #category : #accessing }
23+
{ #category : 'accessing' }
2224
AIBFSNode >> distance: anObject [
2325

2426
distance := anObject
2527
]
2628

27-
{ #category : #initialization }
29+
{ #category : 'initialization' }
2830
AIBFSNode >> initialize [
2931

3032
super initialize.
3133
visited := false
3234
]
3335

34-
{ #category : #accessing }
36+
{ #category : 'accessing' }
3537
AIBFSNode >> label [
3638

3739
^ 'BFS: '
3840
]
3941

40-
{ #category : #accessing }
42+
{ #category : 'accessing' }
4143
AIBFSNode >> previousNode [
4244

4345
^ previousNode
4446
]
4547

46-
{ #category : #accessing }
48+
{ #category : 'accessing' }
4749
AIBFSNode >> previousNode: aNode [
4850

4951
previousNode := aNode
5052
]
5153

52-
{ #category : #initialization }
54+
{ #category : 'initialization' }
5355
AIBFSNode >> visited [
5456

5557
^ visited
5658
]
5759

58-
{ #category : #initialization }
60+
{ #category : 'initialization' }
5961
AIBFSNode >> visited: aBoolean [
6062

6163
visited := aBoolean
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
"
2+
I am a node that is used in the DFS algorithm defined in the `AIDFS` class. I have an instance variable `previousNode` to track from which node I have been called and also an instance variable visited to see if I were visited or not.
3+
"
4+
Class {
5+
#name : 'AIDFSNode',
6+
#superclass : 'AIGraphNode',
7+
#instVars : [
8+
'previousNode',
9+
'visited'
10+
],
11+
#category : 'AI-Algorithms-Graph-Components-Nodes',
12+
#package : 'AI-Algorithms-Graph-Components',
13+
#tag : 'Nodes'
14+
}
15+
16+
{ #category : 'initialization' }
17+
AIDFSNode >> initialize [
18+
19+
super initialize.
20+
visited := false.
21+
]
22+
23+
{ #category : 'accessing' }
24+
AIDFSNode >> label [
25+
26+
^ 'DFS: '
27+
]
28+
29+
{ #category : 'accessing' }
30+
AIDFSNode >> previousNode [
31+
32+
^ previousNode
33+
]
34+
35+
{ #category : 'accessing' }
36+
AIDFSNode >> previousNode: aNode [
37+
38+
previousNode := aNode
39+
]
40+
41+
{ #category : 'accessing' }
42+
AIDFSNode >> visited [
43+
44+
^ visited
45+
]
46+
47+
{ #category : 'accessing' }
48+
AIDFSNode >> visited: aBoolean [
49+
50+
visited := aBoolean
51+
]

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

+11-9
Original file line numberDiff line numberDiff line change
@@ -3,48 +3,50 @@ This class represents a node in the Dinic's algorithm.
33
Each node has a level and a currentIndex property.
44
"
55
Class {
6-
#name : #AIDinicNode,
7-
#superclass : #AIGraphNode,
6+
#name : 'AIDinicNode',
7+
#superclass : 'AIGraphNode',
88
#instVars : [
99
'level',
1010
'currentIndex'
1111
],
12-
#category : #'AI-Algorithms-Graph-Components-Nodes'
12+
#category : 'AI-Algorithms-Graph-Components-Nodes',
13+
#package : 'AI-Algorithms-Graph-Components',
14+
#tag : 'Nodes'
1315
}
1416

15-
{ #category : #accessing }
17+
{ #category : 'accessing' }
1618
AIDinicNode >> currentIndex [
1719

1820
^ currentIndex
1921
]
2022

21-
{ #category : #setter }
23+
{ #category : 'setter' }
2224
AIDinicNode >> currentIndex: aValue [
2325

2426
currentIndex := aValue
2527
]
2628

27-
{ #category : #initialization }
29+
{ #category : 'initialization' }
2830
AIDinicNode >> initialize [
2931

3032
super initialize.
3133
level := -1.
3234
currentIndex := 1.
3335
]
3436

35-
{ #category : #accessing }
37+
{ #category : 'accessing' }
3638
AIDinicNode >> label [
3739

3840
^ 'Dinic: '
3941
]
4042

41-
{ #category : #accessing }
43+
{ #category : 'accessing' }
4244
AIDinicNode >> level [
4345

4446
^ level
4547
]
4648

47-
{ #category : #setter }
49+
{ #category : 'setter' }
4850
AIDinicNode >> level: aValue [
4951

5052
level := aValue

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

+12-10
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,17 @@ But, when you call the method `find` I path compress the nodes to make all the n
1313
I am used by the Kruskal's algorithm to find cycles in a graph with a constant time.
1414
"
1515
Class {
16-
#name : #AIDisjointSetNode,
17-
#superclass : #AIGraphNode,
16+
#name : 'AIDisjointSetNode',
17+
#superclass : 'AIGraphNode',
1818
#instVars : [
1919
'parent'
2020
],
21-
#category : #'AI-Algorithms-Graph-Components-Nodes'
21+
#category : 'AI-Algorithms-Graph-Components-Nodes',
22+
#package : 'AI-Algorithms-Graph-Components',
23+
#tag : 'Nodes'
2224
}
2325

24-
{ #category : #accessing }
26+
{ #category : 'accessing' }
2527
AIDisjointSetNode >> find [
2628

2729
| root next node |
@@ -41,38 +43,38 @@ AIDisjointSetNode >> find [
4143
^ root
4244
]
4345

44-
{ #category : #initialization }
46+
{ #category : 'initialization' }
4547
AIDisjointSetNode >> initialize [
4648

4749
super initialize.
4850
self makeSet
4951
]
5052

51-
{ #category : #accessing }
53+
{ #category : 'accessing' }
5254
AIDisjointSetNode >> label [
5355

5456
^ 'DSN '
5557
]
5658

57-
{ #category : #accessing }
59+
{ #category : 'accessing' }
5860
AIDisjointSetNode >> makeSet [
5961

6062
parent := self
6163
]
6264

63-
{ #category : #'private - accessing' }
65+
{ #category : 'private - accessing' }
6466
AIDisjointSetNode >> parent [
6567

6668
^ parent
6769
]
6870

69-
{ #category : #'private - accessing' }
71+
{ #category : 'private - accessing' }
7072
AIDisjointSetNode >> parent: anObject [
7173

7274
parent := anObject
7375
]
7476

75-
{ #category : #accessing }
77+
{ #category : 'accessing' }
7678
AIDisjointSetNode >> union: aDSNode [
7779

7880
| root1 root2 |

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

+15-13
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,25 @@ Instance Variables
1212
model: <Object>
1313
"
1414
Class {
15-
#name : #AIGraphEdge,
16-
#superclass : #Object,
15+
#name : 'AIGraphEdge',
16+
#superclass : 'Object',
1717
#instVars : [
1818
'model',
1919
'from',
2020
'to'
2121
],
22-
#category : #'AI-Algorithms-Graph-Components-Edges'
22+
#category : 'AI-Algorithms-Graph-Components-Edges',
23+
#package : 'AI-Algorithms-Graph-Components',
24+
#tag : 'Edges'
2325
}
2426

25-
{ #category : #'instance creation' }
27+
{ #category : 'instance creation' }
2628
AIGraphEdge class >> with: aModel [
2729

2830
^ self new model: aModel
2931
]
3032

31-
{ #category : #'instance creation' }
33+
{ #category : 'instance creation' }
3234
AIGraphEdge class >> with: aModel from: srcNode to: dstNode [
3335
| edge |
3436
edge := self new.
@@ -38,51 +40,51 @@ AIGraphEdge class >> with: aModel from: srcNode to: dstNode [
3840
^ edge
3941
]
4042

41-
{ #category : #accessing }
43+
{ #category : 'accessing' }
4244
AIGraphEdge >> asTuple [
4345

4446
^ {from model . to model}
4547
]
4648

47-
{ #category : #accessing }
49+
{ #category : 'accessing' }
4850
AIGraphEdge >> from [
4951

5052
^ from
5153
]
5254

53-
{ #category : #accessing }
55+
{ #category : 'accessing' }
5456
AIGraphEdge >> from: anObject [
5557

5658
from := anObject
5759
]
5860

59-
{ #category : #accessing }
61+
{ #category : 'accessing' }
6062
AIGraphEdge >> model [
6163

6264
^ model
6365
]
6466

65-
{ #category : #accessing }
67+
{ #category : 'accessing' }
6668
AIGraphEdge >> model: aModel [
6769

6870
model := aModel
6971
]
7072

71-
{ #category : #printing }
73+
{ #category : 'printing' }
7274
AIGraphEdge >> printOn: aStream [
7375

7476
self from printOn: aStream.
7577
aStream nextPutAll: ' -> '.
7678
self to printOn: aStream
7779
]
7880

79-
{ #category : #accessing }
81+
{ #category : 'accessing' }
8082
AIGraphEdge >> to [
8183

8284
^ to
8385
]
8486

85-
{ #category : #accessing }
87+
{ #category : 'accessing' }
8688
AIGraphEdge >> to: anObject [
8789

8890
to := anObject

0 commit comments

Comments
 (0)