zoukankan      html  css  js  c++  java
  • 迪克斯特拉(Dijkstra)算法php 实现

    <?php
    class Graph{
    
        private $data = [];
        const INFINITY = -1;
    
        public function addDirection( $fromNode, $toNode,$cost ){
            if( !isset($this->data[$fromNode]) ){
                $this->data[$fromNode] = [];
            }
            $this->data[$fromNode][$toNode] = $cost;
        }
    
        public function getNeighters( $node ){
            return $this->data[$node] ?? [];
        }
    
        public function printData(){
            print_r( $this->data );
        }
    
    }
    
    class DK{
    
        public $graph = null;
        private $costs = [];
        private $parents = [];
        public $processedNodes = [];
    
        public function getGraph() : Graph{
            if( $this->graph == null ){
                $this->graph = new Graph();
            }
            return $this->graph;
        }
    
        public function printGraph(){
            return $this->getGraph()->printData();
        }
    
        public function addDirection($fromNode,$toNode,$cost){
            $this->getGraph()->addDirection( $fromNode,$toNode,$cost );
        }
    
        public function getCosts( $fromNode, $toNode ){
            $this->initCosts( $fromNode, $toNode );
            $node = $this->getLowestNode();
            while ( $node ){
                $cost = $this->getCost( $node );
                foreach ( $this->getGraph()->getNeighters($node) as $neighterNode => $neighterCost ){
                    $newCost = $cost + $neighterCost;
                    if( $this->costs[$neighterNode] == Graph::INFINITY || !isset($this->costs[$neighterNode]) || $newCost < $cost ){
                        $this->costs[$neighterNode] = $newCost;
                        $this->parents[$neighterNode] = $node;
                    }
                }
                array_push($this->processedNodes, $node);
                $node = $this->getLowestNode();
            }
            return $this->costs;
        }
    
        public function initCosts($fromNode, $toNode){
            $nodes = $this->getGraph()->getNeighters($fromNode);
            foreach ($nodes as $_node => $cost ){
                $this->costs[$_node] = $cost;
            }
            if( !isset( $this->costs[$toNode] ) ){
                $this->costs[$toNode] = Graph::INFINITY;
            }
        }
    
        public function getCost( $toNode ){
            return $this->costs[$toNode] ?? Graph::INFINITY;
        }
    
        private function getLowestNode(){
            $lowestNode = null;
            $lowestCost = Graph::INFINITY;
            foreach ( $this->costs as $node =>$cost ){
                if( $cost == Graph::INFINITY || in_array($node, $this->processedNodes) ){
                    continue;
                }
    
                if( $lowestCost == Graph::INFINITY || $cost < $lowestCost ){
                    $lowestCost = $cost;
                    $lowestNode = $node;
                }
            }
            return $lowestNode;
        }
    }
    
    $dk = new DK();
    $dk->addDirection('shenyang','liaoyang', 5 );
    $dk->addDirection('shenyang', 'anshan', 1);
    $dk->addDirection('anshan','dalian',5);
    $dk->addDirection('liaoyang','dalian', 2 );
    $dk->addDirection('dalian','panjin', 7 );
    
    $dk->printGraph();
    
    var_dump($dk->getCosts('shenyang','panjin'));
    ?>
    

      

  • 相关阅读:
    HDU4507 吉哥系列故事――恨7不成妻(数位dp)
    UCF Local Programming Contest 2017 G题(dp)
    ICPC Latin American Regional Contests 2019 I题
    UCF Local Programming Contest 2017 H题(区间dp)
    HDU2089 不要62
    AcWing1084 数字游戏II(数位dp)
    UCF Local Programming Contest 2017 F题(最短路)
    Google Code Jam 2019 Round 1A Pylons(爆搜+贪心)
    AcWing1083 Windy数(数位dp)
    Vue
  • 原文地址:https://www.cnblogs.com/glory-jzx/p/13590265.html
Copyright © 2011-2022 走看看