You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
2.8KB

  1. # util/topological.py
  2. # Copyright (C) 2005-2021 the SQLAlchemy authors and contributors
  3. # <see AUTHORS file>
  4. #
  5. # This module is part of SQLAlchemy and is released under
  6. # the MIT License: http://www.opensource.org/licenses/mit-license.php
  7. """Topological sorting algorithms."""
  8. from .. import util
  9. from ..exc import CircularDependencyError
  10. __all__ = ["sort", "sort_as_subsets", "find_cycles"]
  11. def sort_as_subsets(tuples, allitems):
  12. edges = util.defaultdict(set)
  13. for parent, child in tuples:
  14. edges[child].add(parent)
  15. todo = list(allitems)
  16. todo_set = set(allitems)
  17. while todo_set:
  18. output = []
  19. for node in todo:
  20. if todo_set.isdisjoint(edges[node]):
  21. output.append(node)
  22. if not output:
  23. raise CircularDependencyError(
  24. "Circular dependency detected.",
  25. find_cycles(tuples, allitems),
  26. _gen_edges(edges),
  27. )
  28. todo_set.difference_update(output)
  29. todo = [t for t in todo if t in todo_set]
  30. yield output
  31. def sort(tuples, allitems, deterministic_order=True):
  32. """sort the given list of items by dependency.
  33. 'tuples' is a list of tuples representing a partial ordering.
  34. deterministic_order is no longer used, the order is now always
  35. deterministic given the order of "allitems". the flag is there
  36. for backwards compatibility with Alembic.
  37. """
  38. for set_ in sort_as_subsets(tuples, allitems):
  39. for s in set_:
  40. yield s
  41. def find_cycles(tuples, allitems):
  42. # adapted from:
  43. # http://neopythonic.blogspot.com/2009/01/detecting-cycles-in-directed-graph.html
  44. edges = util.defaultdict(set)
  45. for parent, child in tuples:
  46. edges[parent].add(child)
  47. nodes_to_test = set(edges)
  48. output = set()
  49. # we'd like to find all nodes that are
  50. # involved in cycles, so we do the full
  51. # pass through the whole thing for each
  52. # node in the original list.
  53. # we can go just through parent edge nodes.
  54. # if a node is only a child and never a parent,
  55. # by definition it can't be part of a cycle. same
  56. # if it's not in the edges at all.
  57. for node in nodes_to_test:
  58. stack = [node]
  59. todo = nodes_to_test.difference(stack)
  60. while stack:
  61. top = stack[-1]
  62. for node in edges[top]:
  63. if node in stack:
  64. cyc = stack[stack.index(node) :]
  65. todo.difference_update(cyc)
  66. output.update(cyc)
  67. if node in todo:
  68. stack.append(node)
  69. todo.remove(node)
  70. break
  71. else:
  72. node = stack.pop()
  73. return output
  74. def _gen_edges(edges):
  75. return set([(right, left) for left in edges for right in edges[left]])