MCPcopy Create free account
hub / github.com/TheAlgorithms/Go / DepthFirstSearchHelper

Function DepthFirstSearchHelper

graph/depthfirstsearch.go:27–57  ·  view source on GitHub ↗
(start, end int, nodes []int, edges [][]bool, showroute bool)

Source from the content-addressed store, hash-verified

25}
26
27func DepthFirstSearchHelper(start, end int, nodes []int, edges [][]bool, showroute bool) ([]int, bool) {
28 var route []int
29 var stack []int
30 startIdx := GetIdx(start, nodes)
31 stack = append(stack, startIdx)
32 for len(stack) > 0 {
33 now := stack[len(stack)-1]
34 route = append(route, nodes[now])
35 if len(stack) > 1 {
36 stack = stack[:len(stack)-1]
37 } else {
38 stack = stack[:len(stack)-1]
39 }
40 for i := 0; i < len(edges[now]); i++ {
41 if edges[now][i] && NotExist(i, stack) {
42 stack = append(stack, i)
43 }
44 edges[now][i] = false
45 edges[i][now] = false
46 }
47 if route[len(route)-1] == end {
48 return route, true
49 }
50 }
51
52 if showroute {
53 return route, false
54 } else {
55 return nil, false
56 }
57}
58
59func DepthFirstSearch(start, end int, nodes []int, edges [][]bool) ([]int, bool) {
60 return DepthFirstSearchHelper(start, end, nodes, edges, false)

Callers 2

DepthFirstSearchFunction · 0.85
TopologicalFunction · 0.85

Calls 2

GetIdxFunction · 0.85
NotExistFunction · 0.85

Tested by

no test coverage detected