(start, end int, nodes []int, edges [][]bool, showroute bool)
| 25 | } |
| 26 | |
| 27 | func 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 | |
| 59 | func DepthFirstSearch(start, end int, nodes []int, edges [][]bool) ([]int, bool) { |
| 60 | return DepthFirstSearchHelper(start, end, nodes, edges, false) |
no test coverage detected