MCPcopy Index your code
hub / github.com/TheAlgorithms/Go / HasCycle

Method HasCycle

graph/cycle.go:10–28  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

8package graph
9
10func (g *Graph) HasCycle() bool {
11 //this implimetation referred as 3-color too
12 all := map[int]struct{}{}
13 visiting := map[int]struct{}{}
14 visited := map[int]struct{}{}
15
16 for v := range g.edges {
17 all[v] = struct{}{}
18 }
19
20 for current := range all {
21 if g.hasCycleHelper(current, all, visiting, visited) {
22 return true
23 }
24 }
25
26 return false
27
28}
29
30func (g Graph) hasCycleHelper(v int, all, visiting, visited map[int]struct{}) bool {
31 delete(all, v)

Callers 1

TestHasCycleFunction · 0.95

Calls 1

hasCycleHelperMethod · 0.95

Tested by 1

TestHasCycleFunction · 0.76