HasCycles detects cycles in the graph
()
| 403 | |
| 404 | // HasCycles detects cycles in the graph |
| 405 | func (g *Graph) HasCycles() (bool, error) { |
| 406 | discovered := []string{} |
| 407 | finished := []string{} |
| 408 | |
| 409 | for _, vertex := range g.Vertices { |
| 410 | path := []string{ |
| 411 | vertex.Key, |
| 412 | } |
| 413 | if !slices.Contains(discovered, vertex.Key) && !slices.Contains(finished, vertex.Key) { |
| 414 | var err error |
| 415 | discovered, finished, err = g.visit(vertex.Key, path, discovered, finished) |
| 416 | if err != nil { |
| 417 | return true, err |
| 418 | } |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | return false, nil |
| 423 | } |
| 424 | |
| 425 | func (g *Graph) visit(key string, path []string, discovered []string, finished []string) ([]string, []string, error) { |
| 426 | discovered = append(discovered, key) |