123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- <?php
- function drupal_depth_first_search(&$graph) {
- $state = array(
-
-
- 'last_visit_order' => array(),
-
- 'components' => array(),
- );
-
- foreach ($graph as $start => $data) {
- _drupal_depth_first_search($graph, $state, $start);
- }
-
-
-
- $component_weights = array();
- foreach ($state['last_visit_order'] as $vertex) {
- $component = $graph[$vertex]['component'];
- if (!isset($component_weights[$component])) {
- $component_weights[$component] = 0;
- }
- $graph[$vertex]['weight'] = $component_weights[$component]--;
- }
- }
- function _drupal_depth_first_search(&$graph, &$state, $start, &$component = NULL) {
-
- if (!isset($component)) {
- $component = $start;
- }
-
- if (isset($graph[$start]['paths'])) {
- return;
- }
-
- $graph[$start]['paths'] = array();
-
- $graph[$start]['component'] = $component;
- $state['components'][$component][] = $start;
-
- if (isset($graph[$start]['edges'])) {
- foreach ($graph[$start]['edges'] as $end => $v) {
-
- $graph[$start]['paths'][$end] = $v;
- if (isset($graph[$end]['component']) && $component != $graph[$end]['component']) {
-
-
- $new_component = $graph[$end]['component'];
- foreach ($state['components'][$component] as $vertex) {
- $graph[$vertex]['component'] = $new_component;
- $state['components'][$new_component][] = $vertex;
- }
- unset($state['components'][$component]);
- $component = $new_component;
- }
-
- if (isset($graph[$end])) {
-
- _drupal_depth_first_search($graph, $state, $end, $component);
-
- $graph[$start]['paths'] += $graph[$end]['paths'];
- }
- }
- }
-
-
- foreach ($graph[$start]['paths'] as $end => $v) {
- if (isset($graph[$end])) {
- $graph[$end]['reverse_paths'][$start] = $v;
- }
- }
-
-
- $state['last_visit_order'][] = $start;
- }
|