Rename tempmark to visiting and mark to visited
Better variable names, per Eric's code review feedback.
@@ -10,34 +10,34 @@ import (
// DFS-based algorithm. The graph must be acyclic, otherwise this function will return an error.
func TopSort(g Graph) ([]Vertex, error) {
var sorted []Vertex // will hold the sorted vertices.
- tempmark := make(map[Vertex]bool) // temporary marks to detect cycles.
- mark := make(map[Vertex]bool) // marks that will avoid visiting the same node twice.
+ visiting := make(map[Vertex]bool) // temporary entries to detect cycles.
+ visited := make(map[Vertex]bool) // entries to avoid visiting the same node twice.
// Now enumerate the roots, topologically sorting their dependencies.
roots := g.Roots()
for _, r := range roots {
- if err := topvisit(r, &sorted, tempmark, mark); err != nil {
+ if err := topvisit(r, &sorted, visiting, visited); err != nil {
return sorted, err
}
}
return sorted, nil
}
-func topvisit(n Vertex, sorted *[]Vertex, tempmark map[Vertex]bool, mark map[Vertex]bool) error {
- if tempmark[n] {
+func topvisit(n Vertex, sorted *[]Vertex, visiting map[Vertex]bool, visited map[Vertex]bool) error {
+ if visiting[n] {
// This is not a DAG! Stop sorting right away, and issue an error.
// TODO: use a real error here; and also ideally give an error message that makes sense (w/ the full cycle).
return errors.New("Graph is not a DAG")
}
- if !mark[n] {
- tempmark[n] = true
+ if !visited[n] {
+ visiting[n] = true
for _, m := range n.Outs() {
- if err := topvisit(m.To(), sorted, tempmark, mark); err != nil {
+ if err := topvisit(m.To(), sorted, visiting, visited); err != nil {
return err
}
}
- mark[n] = true
- tempmark[n] = false
+ visited[n] = true
+ visiting[n] = false
*sorted = append(*sorted, n)
}
return nil
