[ create a new paste ] login | about

Link: http://codepad.org/cYnqNfLk    [ raw code | output | fork ]

PHP, pasted on Aug 14:
<?php

function topological_sort($nodeids, $edges) {
    $L = $S = $nodes = array();
    foreach($nodeids as $id) {
        $nodes[$id] = array('in'=>array(), 'out'=>array());
        foreach($edges as $e) {
            if ($id==$e[0]) { $nodes[$id]['out'][]=$e[1]; }
            if ($id==$e[1]) { $nodes[$id]['in'][]=$e[0]; }
        }
    }
    foreach ($nodes as $id=>$n) { if (empty($n['in'])) $S[]=$id; }
    while ($id = array_shift($S)) {
        if (!in_array($id, $L)) {
            $L[] = $id;
            foreach($nodes[$id]['out'] as $m) {
                $nodes[$m]['in'] = array_diff($nodes[$m]['in'], array($id));
                if (empty($nodes[$m]['in'])) { $S[] = $m; }
            }
            $nodes[$id]['out'] = array();
        }
    }
    foreach($nodes as $n) {
        if (!empty($n['in']) or !empty($n['out'])) {
            return null; // not sortable as graph is cyclic
        }
    }
    return $L;
}



$nodes = array (
  0 => 'nominal',
  1 => 'subtotal',
  2 => 'shipping',
  3 => 'grand_total',
  4 => 'msrp',
  5 => 'freeshipping',
  6 => 'discount',
  7 => 'tax_subtotal',
  8 => 'tax_shipping',
  9 => 'tax',
  10 => 'weee',
  11 => 'customerbalance',
  12 => 'giftcardaccount',
  13 => 'giftwrapping',
  14 => 'tax_giftwrapping',
  15 => 'bonuspoints',
);

$edges = array (
  0 => 
  array (
    0 => 'nominal',
    1 => 'subtotal',
  ),
  1 => 
  array (
    0 => 'subtotal',
    1 => 'grand_total',
  ),
  2 => 
  array (
    0 => 'nominal',
    1 => 'subtotal',
  ),
  3 => 
  array (
    0 => 'shipping',
    1 => 'grand_total',
  ),
  4 => 
  array (
    0 => 'subtotal',
    1 => 'shipping',
  ),
  5 => 
  array (
    0 => 'freeshipping',
    1 => 'shipping',
  ),
  6 => 
  array (
    0 => 'tax_subtotal',
    1 => 'shipping',
  ),
  7 => 
  array (
    0 => 'subtotal',
    1 => 'grand_total',
  ),
  8 => 
  array (
    0 => 'freeshipping',
    1 => 'tax_subtotal',
  ),
  9 => 
  array (
    0 => 'freeshipping',
    1 => 'shipping',
  ),
  10 => 
  array (
    0 => 'subtotal',
    1 => 'freeshipping',
  ),
  11 => 
  array (
    0 => 'discount',
    1 => 'grand_total',
  ),
  12 => 
  array (
    0 => 'subtotal',
    1 => 'discount',
  ),
  13 => 
  array (
    0 => 'shipping',
    1 => 'discount',
  ),
  14 => 
  array (
    0 => 'tax_subtotal',
    1 => 'tax',
  ),
  15 => 
  array (
    0 => 'tax_subtotal',
    1 => 'discount',
  ),
  16 => 
  array (
    0 => 'freeshipping',
    1 => 'tax_subtotal',
  ),
  17 => 
  array (
    0 => 'tax_shipping',
    1 => 'tax',
  ),
  18 => 
  array (
    0 => 'tax_shipping',
    1 => 'discount',
  ),
  19 => 
  array (
    0 => 'shipping',
    1 => 'tax_shipping',
  ),
  20 => 
  array (
    0 => 'tax',
    1 => 'grand_total',
  ),
  21 => 
  array (
    0 => 'subtotal',
    1 => 'tax',
  ),
  22 => 
  array (
    0 => 'shipping',
    1 => 'tax',
  ),
  23 => 
  array (
    0 => 'discount',
    1 => 'tax',
  ),
  24 => 
  array (
    0 => 'weee',
    1 => 'tax',
  ),
  25 => 
  array (
    0 => 'weee',
    1 => 'discount',
  ),
  26 => 
  array (
    0 => 'subtotal',
    1 => 'weee',
  ),
  27 => 
  array (
    0 => 'tax_subtotal',
    1 => 'weee',
  ),
  28 => 
  array (
    0 => 'weee',
    1 => 'customerbalance',
  ),
  29 => 
  array (
    0 => 'discount',
    1 => 'customerbalance',
  ),
  30 => 
  array (
    0 => 'tax',
    1 => 'customerbalance',
  ),
  31 => 
  array (
    0 => 'tax_subtotal',
    1 => 'customerbalance',
  ),
  32 => 
  array (
    0 => 'grand_total',
    1 => 'customerbalance',
  ),
  33 => 
  array (
    0 => 'giftcardaccount',
    1 => 'customerbalance',
  ),
  34 => 
  array (
    0 => 'giftcardaccount',
    1 => 'customerbalance',
  ),
  35 => 
  array (
    0 => 'weee',
    1 => 'giftcardaccount',
  ),
  36 => 
  array (
    0 => 'discount',
    1 => 'giftcardaccount',
  ),
  37 => 
  array (
    0 => 'tax',
    1 => 'giftcardaccount',
  ),
  38 => 
  array (
    0 => 'tax_subtotal',
    1 => 'giftcardaccount',
  ),
  39 => 
  array (
    0 => 'grand_total',
    1 => 'giftcardaccount',
  ),
  40 => 
  array (
    0 => 'subtotal',
    1 => 'giftwrapping',
  ),
  41 => 
  array (
    0 => 'tax_giftwrapping',
    1 => 'grand_total',
  ),
  42 => 
  array (
    0 => 'tax',
    1 => 'tax_giftwrapping',
  ),
  43 => 
  array (
    0 => 'bonuspoints',
    1 => 'tax',
  ),
  44 => 
  array (
    0 => 'subtotal',
    1 => 'bonuspoints',
  ),
);

print_r(topological_sort($nodes, $edges));


Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Array
(
    [0] => nominal
    [1] => msrp
    [2] => subtotal
    [3] => freeshipping
    [4] => giftwrapping
    [5] => bonuspoints
    [6] => tax_subtotal
    [7] => shipping
    [8] => weee
    [9] => tax_shipping
    [10] => discount
    [11] => tax
    [12] => tax_giftwrapping
    [13] => grand_total
    [14] => giftcardaccount
    [15] => customerbalance
)


Create a new paste based on this one


Comments: