3 namespace Drupal\Component\Graph;
6 * Directed acyclic graph manipulation.
11 * Holds the directed acyclic graph.
16 * Instantiates the depth first search object.
19 * A three dimensional associated array, with the first keys being the names
20 * of the vertices, these can be strings or numbers. The second key is
21 * 'edges' and the third one are again vertices, each such key representing
22 * an edge. Values of array elements are copied over.
26 * $graph[1]['edges'][2] = 1;
27 * $graph[2]['edges'][3] = 1;
28 * $graph[2]['edges'][4] = 1;
29 * $graph[3]['edges'][4] = 1;
32 * On return you will also have:
34 * $graph[1]['paths'][2] = 1;
35 * $graph[1]['paths'][3] = 1;
36 * $graph[2]['reverse_paths'][1] = 1;
37 * $graph[3]['reverse_paths'][1] = 1;
40 public function __construct($graph) {
41 $this->graph = $graph;
45 * Performs a depth-first search and sort on the directed acyclic graph.
48 * The given $graph with more secondary keys filled in:
49 * - 'paths': Contains a list of vertices than can be reached on a path from
51 * - 'reverse_paths': Contains a list of vertices that has a path from them
53 * - 'weight': If there is a path from a vertex to another then the weight of
54 * the latter is higher.
55 * - 'component': Vertices in the same component have the same component
58 public function searchAndSort() {
60 // The order of last visit of the depth first search. This is the reverse
61 // of the topological order if the graph is acyclic.
62 'last_visit_order' => [],
63 // The components of the graph.
66 // Perform the actual search.
67 foreach ($this->graph as $start => $data) {
68 $this->depthFirstSearch($state, $start);
71 // We do such a numbering that every component starts with 0. This is useful
72 // for module installs as we can install every 0 weighted module in one
73 // request, and then every 1 weighted etc.
74 $component_weights = [];
76 foreach ($state['last_visit_order'] as $vertex) {
77 $component = $this->graph[$vertex]['component'];
78 if (!isset($component_weights[$component])) {
79 $component_weights[$component] = 0;
81 $this->graph[$vertex]['weight'] = $component_weights[$component]--;
88 * Performs a depth-first search on a graph.
91 * An associative array. The key 'last_visit_order' stores a list of the
92 * vertices visited. The key components stores list of vertices belonging
93 * to the same the component.
95 * An arbitrary vertex where we started traversing the graph.
97 * The component of the last vertex.
99 * @see \Drupal\Component\Graph\Graph::searchAndSort()
101 protected function depthFirstSearch(&$state, $start, &$component = NULL) {
102 // Assign new component for each new vertex, i.e. when not called recursively.
103 if (!isset($component)) {
106 // Nothing to do, if we already visited this vertex.
107 if (isset($this->graph[$start]['paths'])) {
110 // Mark $start as visited.
111 $this->graph[$start]['paths'] = [];
113 // Assign $start to the current component.
114 $this->graph[$start]['component'] = $component;
115 $state['components'][$component][] = $start;
117 // Visit edges of $start.
118 if (isset($this->graph[$start]['edges'])) {
119 foreach ($this->graph[$start]['edges'] as $end => $v) {
120 // Mark that $start can reach $end.
121 $this->graph[$start]['paths'][$end] = $v;
123 if (isset($this->graph[$end]['component']) && $component != $this->graph[$end]['component']) {
124 // This vertex already has a component, use that from now on and
125 // reassign all the previously explored vertices.
126 $new_component = $this->graph[$end]['component'];
127 foreach ($state['components'][$component] as $vertex) {
128 $this->graph[$vertex]['component'] = $new_component;
129 $state['components'][$new_component][] = $vertex;
131 unset($state['components'][$component]);
132 $component = $new_component;
134 // Only visit existing vertices.
135 if (isset($this->graph[$end])) {
136 // Visit the connected vertex.
137 $this->depthFirstSearch($state, $end, $component);
139 // All vertices reachable by $end are also reachable by $start.
140 $this->graph[$start]['paths'] += $this->graph[$end]['paths'];
145 // Now that any other subgraph has been explored, add $start to all reverse
147 foreach ($this->graph[$start]['paths'] as $end => $v) {
148 if (isset($this->graph[$end])) {
149 $this->graph[$end]['reverse_paths'][$start] = $v;
153 // Record the order of the last visit. This is the reverse of the
154 // topological order if the graph is acyclic.
155 $state['last_visit_order'][] = $start;