forked from TheAlgorithms/Go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
depthfirstsearch.go
79 lines (73 loc) · 1.67 KB
/
depthfirstsearch.go
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
package graph
func GetIdx(target int, nodes []int) int {
for i := 0; i < len(nodes); i++ {
if nodes[i] == target {
return i
}
}
return -1
}
func NotExist(target int, slice []int) bool {
for i := 0; i < len(slice); i++ {
if slice[i] == target {
return false
}
}
return true
}
func DepthFirstSearchHelper(start, end int, nodes []int, edges [][]bool, showroute bool) ([]int, bool) {
var route []int
var stack []int
startIdx := GetIdx(start, nodes)
stack = append(stack, startIdx)
for len(stack) > 0 {
now := stack[len(stack)-1]
route = append(route, nodes[now])
if len(stack) > 1 {
stack = stack[:len(stack)-1]
} else {
stack = stack[:len(stack)-1]
}
for i := 0; i < len(edges[now]); i++ {
if edges[now][i] && NotExist(i, stack) {
stack = append(stack, i)
}
edges[now][i] = false
edges[i][now] = false
}
if route[len(route)-1] == end {
return route, true
}
}
if showroute {
return route, false
} else {
return nil, false
}
}
func DepthFirstSearch(start, end int, nodes []int, edges [][]bool) ([]int, bool) {
return DepthFirstSearchHelper(start, end, nodes, edges, false)
}
// func main() {
// nodes := []int{
// 1, 2, 3, 4, 5, 6,
// }
// /*
// sample graph
// ①-②
// | |
// ③-④-⑤-⑥
// */
// edges := [][]bool{
// {false, true, true, false, false, false},
// {true, false, false, true, false, false},
// {true, false, false, true, false, false},
// {false, true, true, false, true, false},
// {false, false, false, true, false, true},
// {false, false, false, false, true, false},
// }
// start := 1
// end := 6
// route, _ := dfs(start, end, nodes, edges)
// fmt.Println(route)
// }