-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdfs_test.go
More file actions
199 lines (158 loc) · 4.05 KB
/
dfs_test.go
File metadata and controls
199 lines (158 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package dfs
import (
"fmt"
"testing"
)
func TestDFSRecursive_CoreTraversal(t *testing.T) {
t.Parallel()
g := NewGraph()
g.AddEdge(1, 2)
g.AddEdge(1, 3)
g.AddEdge(2, 4)
g.AddEdge(2, 5)
g.AddEdge(3, 6)
got := DFSRecursive(g, 1)
if len(got) != 6 {
t.Fatalf("DFSRecursive: expected to visit 6 nodes, got %d (%v)", len(got), got)
}
if got[0] != 1 {
t.Fatalf("DFSRecursive: expected first node to be start=1, got %d (%v)", got[0], got)
}
// If you rely on deterministic adjacency order, you can enforce expected ordering.
// Otherwise, validate properties only.
}
func TestDFSIterative_SameAsRecursive(t *testing.T) {
t.Parallel()
g := NewGraph()
g.AddEdge(1, 2)
g.AddEdge(1, 3)
g.AddEdge(2, 4)
rec := DFSRecursive(g, 1)
iter := DFSIterative(g, 1)
// This requires both implementations to produce the same order.
// If your iterative stack pushes neighbors in a different sequence, adjust implementation or relax the test.
assertIntSliceEqual(t, rec, iter)
}
func TestDFSRecursive_LinearGraph(t *testing.T) {
t.Parallel()
g := NewGraph()
g.AddEdge(1, 2)
g.AddEdge(2, 3)
g.AddEdge(3, 4)
got := DFSRecursive(g, 1)
want := []int{1, 2, 3, 4}
assertIntSliceEqual(t, want, got)
}
func TestHasCycle_NoCycle(t *testing.T) {
t.Parallel()
g := NewGraph()
g.AddEdge(1, 2)
g.AddEdge(2, 3)
g.AddEdge(1, 3)
if HasCycle(g) {
t.Fatalf("HasCycle: expected false, got true")
}
}
func TestHasCycle_WithCycle(t *testing.T) {
t.Parallel()
g := NewGraph()
g.AddEdge(1, 2)
g.AddEdge(2, 3)
g.AddEdge(3, 1)
if !HasCycle(g) {
t.Fatalf("HasCycle: expected true, got false")
}
}
func TestHasCycle_SelfLoop(t *testing.T) {
t.Parallel()
g := NewGraph()
g.AddEdge(1, 1)
if !HasCycle(g) {
t.Fatalf("HasCycle: expected true for self-loop, got false")
}
}
func TestFindAllPaths_MultiplePaths(t *testing.T) {
t.Parallel()
g := NewGraph()
g.AddEdge(0, 1)
g.AddEdge(0, 2)
g.AddEdge(1, 3)
g.AddEdge(2, 3)
got := FindAllPaths(g, 0, 3)
// Two valid paths; order not specified.
want := [][]int{{0, 1, 3}, {0, 2, 3}}
assertPathSetEqual(t, want, got)
}
func TestFindAllPaths_NoPath(t *testing.T) {
t.Parallel()
g := NewGraph()
g.AddEdge(1, 2)
g.AddEdge(3, 4)
got := FindAllPaths(g, 1, 4)
if len(got) != 0 {
t.Fatalf("FindAllPaths: expected 0 paths, got %d (%v)", len(got), got)
}
}
func TestFindAllPaths_SinglePath(t *testing.T) {
t.Parallel()
g := NewGraph()
g.AddEdge(1, 2)
g.AddEdge(2, 3)
got := FindAllPaths(g, 1, 3)
want := [][]int{{1, 2, 3}}
assertPathSetEqual(t, want, got)
}
// --- helpers ---
func assertIntSliceEqual(t *testing.T, want, got []int) {
t.Helper()
if len(want) != len(got) {
t.Fatalf("slice length mismatch: want=%d got=%d\nwant=%v\ngot=%v", len(want), len(got), want, got)
}
for i := range want {
if want[i] != got[i] {
t.Fatalf("slice[%d] mismatch: want=%d got=%d\nwant=%v\ngot=%v", i, want[i], got[i], want, got)
}
}
}
func assertPathSetEqual(t *testing.T, want, got [][]int) {
t.Helper()
toKey := func(p []int) string {
s := ""
for i, v := range p {
if i > 0 {
s += ","
}
s += fmt.Sprintf("%d", v)
}
return s
}
wm := map[string]int{}
for _, p := range want {
wm[toKey(p)]++
}
gm := map[string]int{}
for _, p := range got {
gm[toKey(p)]++
}
if len(wm) != len(gm) {
t.Fatalf("path set size mismatch:\nwant=%v\ngot=%v", want, got)
}
for k, v := range wm {
if gm[k] != v {
t.Fatalf("missing path %q (want count=%d got=%d)\nwant=%v\ngot=%v", k, v, gm[k], want, got)
}
}
}
// TODO (edge cases you should implement yourself):
// - DFS traversal on cyclic graph should terminate (visited).
// - Disconnected graph: only reachable nodes returned.
// - Deep graph (1000+ depth): iterative DFS avoids recursion overflow.
// - HasCycle for undirected graphs (different logic).
// - FindAllPaths with cycles: define "simple path only" and prevent revisits in current path.
// - Start == target: FindAllPaths returns [[start]].
// - Empty graph / nil graph: define policy.
//
// Bonus TODOs:
// - DFS preorder/postorder explicitly.
// - SCC (Tarjan/Kosaraju).
// - DFS for topological sorting.