topsort.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Copyright 2019 The sortutil Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package sortutil // import "modernc.org/sortutil"
  5. // TopologicalSortNode represents a node of a graph for TopologicalSort.
  6. // Implementations of TopologicalSortNode must be comparable.
  7. type TopologicalSortNode interface {
  8. // Edges return the list of nodes this node points to.
  9. Edges() []TopologicalSortNode
  10. }
  11. // TopologicalSort returns a reversed topological ordering of a directed
  12. // acyclic graph or nil if graph is not a DAG.
  13. //
  14. // It implements the Depth-first search algorithm:
  15. //
  16. // https://en.wikipedia.org/wiki/Topological_sorting#Depth-first_search
  17. func TopologicalSort(graph []TopologicalSortNode) []TopologicalSortNode {
  18. l := make([]TopologicalSortNode, 0, len(graph))
  19. noPermanentMark := make(map[TopologicalSortNode]struct{}, len(graph))
  20. for _, n := range graph {
  21. noPermanentMark[n] = struct{}{}
  22. }
  23. temporaryMark := make(map[TopologicalSortNode]struct{}, len(graph))
  24. var visit func(TopologicalSortNode) bool
  25. visit = func(n TopologicalSortNode) bool {
  26. if _, ok := noPermanentMark[n]; !ok {
  27. return true
  28. }
  29. if _, ok := temporaryMark[n]; ok {
  30. return false
  31. }
  32. temporaryMark[n] = struct{}{}
  33. for _, m := range n.Edges() {
  34. visit(m)
  35. }
  36. delete(temporaryMark, n)
  37. delete(noPermanentMark, n)
  38. l = append(l, n)
  39. return true
  40. }
  41. for len(noPermanentMark) != 0 {
  42. for n := range noPermanentMark {
  43. if !visit(n) {
  44. return nil // Not a DAG
  45. }
  46. }
  47. }
  48. return l
  49. }