php 拓扑排序

拓扑排序li z

<?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 (!empty($S)) {
        $L[] = $id = array_shift($S);
        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));

参考文献:

http://codepad.org/thpzCOyn

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值