The implementation of the shortest path finding algorithm (dixstra algorithm) in php

Keywords: Python PHP

Refer to Chapter 7 of algorithm diagram for details. The original book was written in python, and I wrote it again in PHP, which was slightly improved

Test these three exercises in the book


I found another "difficult" question on the Internet


Upper Code:

class ShortPath{
    protected $graph=[];//A graph can be represented only by storing the neighbors corresponding to each node

    protected $parents=[];//Save the parent relationship, and finally backtrack the shortest path through this array

    protected $costs;//Cost from start to each node

    protected $infinity=999999;//Use a large number to represent a positive infinity

    protected $processed=[];//key of processed node

    public function __construct($graph)

        //Initialize the parent array, that is, only the neighbors of start are known, that is, the parent of start
        foreach ($this->graph['start'] as $k=>$v) {

        //Initialize the cost array, that is, only the cost of the neighbor node of start is known, and all other nodes are positive infinite
        foreach ($this->graph as $k=>$v) {

    public function find(){
        while ($nodeKey){//As long as there are unprocessed nodes
            $neighbors=$this->graph[$nodeKey] ?? [];//Take neighbors
            //Traversing neighbors
            foreach ($neighbors as $k=> $v) {
                if($this->costs[$k] > $newCost){
                    $this->costs[$k]=$newCost;//Update lower overhead

            //Debug output
            echo 'Current node:' .$nodeKey . PHP_EOL;
            echo 'Neighbours:' . PHP_EOL;
            echo 'Expenses:' . PHP_EOL;

            //Now that all neighbors of the node have been processed, mark the node as processed
            //Find the next node to be processed

    //Find the shortest one among the unprocessed nodes
    protected function findLowestCostNode(){
        $lowestCost=$this->infinity;//Set positive infinity here so that when you first enter the loop, it must be larger than the first element
        $lowestCostKey=false;//The key of the shortest path node to return
        foreach ($this->costs as $k=>$v) {
            if($v<$lowestCost && !in_array($k,$this->processed)){
        return $lowestCostKey;

    //Beautify display processing results
    protected function showResult(){
        //From the end, find the parent and trace back the shortest route
        while (isset($this->parents[$key])){
        echo 'The shortest path length is:' . $this->costs['finish'] . PHP_EOL;
        echo 'The route is:start->' . implode('->',$path) . PHP_EOL;


//Corresponding exercise A

//Corresponding Exercise B

//Corresponding exercise C. It can be seen that the algorithm is not suitable for the case with negative weight edges

//Corresponding to the last question, change v0 in the figure to start and v8 to finish

$obj=new ShortPath($graph4);

Output of the last question


Finally, the pdf version of the book "algorithm diagram" is attached

Link: password: ij0r

Published 18 original articles, won praise 15, visited 40000+
Private letter follow

Posted by Pioden on Wed, 05 Feb 2020 07:13:55 -0800