Graph.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace Drupal\Component\Graph;
  3. /**
  4. * Directed acyclic graph manipulation.
  5. */
  6. class Graph {
  7. /**
  8. * Holds the directed acyclic graph.
  9. */
  10. protected $graph;
  11. /**
  12. * Instantiates the depth first search object.
  13. *
  14. * @param $graph
  15. * A three dimensional associated array, with the first keys being the names
  16. * of the vertices, these can be strings or numbers. The second key is
  17. * 'edges' and the third one are again vertices, each such key representing
  18. * an edge. Values of array elements are copied over.
  19. *
  20. * Example:
  21. * @code
  22. * $graph[1]['edges'][2] = 1;
  23. * $graph[2]['edges'][3] = 1;
  24. * $graph[2]['edges'][4] = 1;
  25. * $graph[3]['edges'][4] = 1;
  26. * @endcode
  27. *
  28. * On return you will also have:
  29. * @code
  30. * $graph[1]['paths'][2] = 1;
  31. * $graph[1]['paths'][3] = 1;
  32. * $graph[2]['reverse_paths'][1] = 1;
  33. * $graph[3]['reverse_paths'][1] = 1;
  34. * @endcode
  35. */
  36. public function __construct($graph) {
  37. $this->graph = $graph;
  38. }
  39. /**
  40. * Performs a depth-first search and sort on the directed acyclic graph.
  41. *
  42. * @return
  43. * The given $graph with more secondary keys filled in:
  44. * - 'paths': Contains a list of vertices than can be reached on a path from
  45. * this vertex.
  46. * - 'reverse_paths': Contains a list of vertices that has a path from them
  47. * to this vertex.
  48. * - 'weight': If there is a path from a vertex to another then the weight of
  49. * the latter is higher.
  50. * - 'component': Vertices in the same component have the same component
  51. * identifier.
  52. */
  53. public function searchAndSort() {
  54. $state = [
  55. // The order of last visit of the depth first search. This is the reverse
  56. // of the topological order if the graph is acyclic.
  57. 'last_visit_order' => [],
  58. // The components of the graph.
  59. 'components' => [],
  60. ];
  61. // Perform the actual search.
  62. foreach ($this->graph as $start => $data) {
  63. $this->depthFirstSearch($state, $start);
  64. }
  65. // We do such a numbering that every component starts with 0. This is useful
  66. // for module installs as we can install every 0 weighted module in one
  67. // request, and then every 1 weighted etc.
  68. $component_weights = [];
  69. foreach ($state['last_visit_order'] as $vertex) {
  70. $component = $this->graph[$vertex]['component'];
  71. if (!isset($component_weights[$component])) {
  72. $component_weights[$component] = 0;
  73. }
  74. $this->graph[$vertex]['weight'] = $component_weights[$component]--;
  75. }
  76. return $this->graph;
  77. }
  78. /**
  79. * Performs a depth-first search on a graph.
  80. *
  81. * @param $state
  82. * An associative array. The key 'last_visit_order' stores a list of the
  83. * vertices visited. The key components stores list of vertices belonging
  84. * to the same the component.
  85. * @param $start
  86. * An arbitrary vertex where we started traversing the graph.
  87. * @param $component
  88. * The component of the last vertex.
  89. *
  90. * @see \Drupal\Component\Graph\Graph::searchAndSort()
  91. */
  92. protected function depthFirstSearch(&$state, $start, &$component = NULL) {
  93. // Assign new component for each new vertex, i.e. when not called recursively.
  94. if (!isset($component)) {
  95. $component = $start;
  96. }
  97. // Nothing to do, if we already visited this vertex.
  98. if (isset($this->graph[$start]['paths'])) {
  99. return;
  100. }
  101. // Mark $start as visited.
  102. $this->graph[$start]['paths'] = [];
  103. // Assign $start to the current component.
  104. $this->graph[$start]['component'] = $component;
  105. $state['components'][$component][] = $start;
  106. // Visit edges of $start.
  107. if (isset($this->graph[$start]['edges'])) {
  108. foreach ($this->graph[$start]['edges'] as $end => $v) {
  109. // Mark that $start can reach $end.
  110. $this->graph[$start]['paths'][$end] = $v;
  111. if (isset($this->graph[$end]['component']) && $component != $this->graph[$end]['component']) {
  112. // This vertex already has a component, use that from now on and
  113. // reassign all the previously explored vertices.
  114. $new_component = $this->graph[$end]['component'];
  115. foreach ($state['components'][$component] as $vertex) {
  116. $this->graph[$vertex]['component'] = $new_component;
  117. $state['components'][$new_component][] = $vertex;
  118. }
  119. unset($state['components'][$component]);
  120. $component = $new_component;
  121. }
  122. // Only visit existing vertices.
  123. if (isset($this->graph[$end])) {
  124. // Visit the connected vertex.
  125. $this->depthFirstSearch($state, $end, $component);
  126. // All vertices reachable by $end are also reachable by $start.
  127. $this->graph[$start]['paths'] += $this->graph[$end]['paths'];
  128. }
  129. }
  130. }
  131. // Now that any other subgraph has been explored, add $start to all reverse
  132. // paths.
  133. foreach ($this->graph[$start]['paths'] as $end => $v) {
  134. if (isset($this->graph[$end])) {
  135. $this->graph[$end]['reverse_paths'][$start] = $v;
  136. }
  137. }
  138. // Record the order of the last visit. This is the reverse of the
  139. // topological order if the graph is acyclic.
  140. $state['last_visit_order'][] = $start;
  141. }
  142. }