Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 致命错误:已耗尽25165824字节的允许内存大小(尝试分配35字节)_Php_Algorithm_Memory - Fatal编程技术网

Php 致命错误:已耗尽25165824字节的允许内存大小(尝试分配35字节)

Php 致命错误:已耗尽25165824字节的允许内存大小(尝试分配35字节),php,algorithm,memory,Php,Algorithm,Memory,我在PHP中使用Dijkstra算法。但我犯了这个错误 Fatal error: Allowed memory size of 25165824 bytes exhausted (tried to allocate 35 bytes) in C:\AppServ\www\direction\dijkstra.php on line 100 如何优化此算法以减少内存消耗 <?php ini_set('memory_limit', '1M'); //Raise to 512 MB ini_

我在PHP中使用Dijkstra算法。但我犯了这个错误

Fatal error: Allowed memory size of 25165824 bytes exhausted (tried to allocate 35 bytes) in C:\AppServ\www\direction\dijkstra.php on line 100
如何优化此算法以减少内存消耗

<?php 
ini_set('memory_limit', '1M'); //Raise to 512 MB
ini_set('max_execution_time', '60'); //Raise to 512 MB
class Dijkstra { 

    var $visited = array(); 
    var $distance = array(); 
    var $previousNode = array(); 
    var $startnode =null; 
    var $map = array(); 
    var $infiniteDistance = 0; 
    var $numberOfNodes = 0; 
    var $bestPath = 0; 
    var $matrixWidth = 0; 

    function Dijkstra(&$ourMap, $infiniteDistance) { 
        $this -> infiniteDistance = $infiniteDistance; 
        $this -> map = &$ourMap; 
        $this -> numberOfNodes = count($ourMap); 
        $this -> bestPath = 0; 
    } 

    function findShortestPath($start,$to) { 
        $this -> startnode = $start; 
        for ($i=0;$i<$this -> numberOfNodes;$i++) { 
            if ($i == $this -> startnode) { 
                $this -> visited[$i] = true; 
                $this -> distance[$i] = 0; 
            } else { 
                $this -> visited[$i] = false; 
                $this -> distance[$i] = isset($this -> map[$this -> startnode][$i]) 
                    ? $this -> map[$this -> startnode][$i] 
                    : $this -> infiniteDistance; 
            } 
            $this -> previousNode[$i] = $this -> startnode; 
        } 

        $maxTries = $this -> numberOfNodes; 
        $tries = 0; 
        while (in_array(false,$this -> visited,true) && $tries <= $maxTries) {             
            $this -> bestPath = $this->findBestPath($this->distance,array_keys($this -> visited,false,true)); 
            if($to !== null && $this -> bestPath === $to) { 
                break; 
            } 
            $this -> updateDistanceAndPrevious($this -> bestPath);             
            $this -> visited[$this -> bestPath] = true; 
            $tries++; 
        } 
    } 

    function findBestPath($ourDistance, $ourNodesLeft) { 
        $bestPath = $this -> infiniteDistance; 
        $bestNode = 0; 
        for ($i = 0,$m=count($ourNodesLeft); $i < $m; $i++) { 
            if($ourDistance[$ourNodesLeft[$i]] < $bestPath) { 
                $bestPath = $ourDistance[$ourNodesLeft[$i]]; 
                $bestNode = $ourNodesLeft[$i]; 
            } 
        } 
        return $bestNode; 
    } 

    function updateDistanceAndPrevious($obp) {         
        for ($i=0;$i<$this -> numberOfNodes;$i++) { 
            if(     (isset($this->map[$obp][$i])) 
                &&    (!($this->map[$obp][$i] == $this->infiniteDistance) || ($this->map[$obp][$i] == 0 ))     
                &&    (($this->distance[$obp] + $this->map[$obp][$i]) < $this -> distance[$i]) 
            )      
            { 
                    $this -> distance[$i] = $this -> distance[$obp] + $this -> map[$obp][$i]; 
                    $this -> previousNode[$i] = $obp; 
            } 
        } 
    } 

    function printMap(&$map) { 
        $placeholder = ' %' . strlen($this -> infiniteDistance) .'d'; 
        $foo = ''; 
        for($i=0,$im=count($map);$i<$im;$i++) { 
            for ($k=0,$m=$im;$k<$m;$k++) { 
                $foo.= sprintf($placeholder, isset($map[$i][$k]) ? $map[$i][$k] : $this -> infiniteDistance); 
            } 
            $foo.= "\n"; 
        } 
        return $foo; 
    } 

    function getResults($to) { 
    if(trim($to)!="")
    {
        $ourShortestPath = array(); 
        $foo = ''; 
        for ($i = 0; $i < $this -> numberOfNodes; $i++) { 
            if($to !== null && $to !== $i) { 
                continue; 
            } 
            $ourShortestPath[$i] = array(); 
            $endNode = null; 
            $currNode = $i; 
            $ourShortestPath[$i][] = $i; 
            while ($endNode === null || $endNode != $this -> startnode) { 
                $ourShortestPath[$i][] = $this -> previousNode[$currNode]; 
                $endNode = $this -> previousNode[$currNode]; 
                $currNode = $this -> previousNode[$currNode]; 
            } 
            $ourShortestPath[$i] = array_reverse($ourShortestPath[$i]); 
            if ($to === null || $to === $i) { 
            if($this -> distance[$i] >= $this -> infiniteDistance) { 
                $foo .= sprintf("no route from %d to %d. \n",$this -> startnode,$i); 
            } else { 
                $foo .= sprintf(' - Distance  = %d (km) <br> - Walking time ~ %d (hrs)<br> - Running time ~ %d (hrs)<br> - Driving time ~ %d (hrs)<br> Nodes [%d] : %s'."\n" , 
                        $this -> distance[$i], round($this -> distance[$i]/5,2),$this -> distance[$i]/17.2,$this -> distance[$i]/50, 
                        count($ourShortestPath[$i]), 
                        implode('-',$ourShortestPath[$i])); 
            } 
                if ($to === $i) { 
                    break; 
                } 
            } 
        } 
        }
        else $foo="";
        return $foo; 
    } 
} // end class 
?>

看起来你的内存真的用完了,这正好是24mb的内存。 您可以通过修改PHP.ini中的
memory\u limit
来修改PHP的最大内存量


你能展示你的算法吗?

脚本使用了它有权使用的所有内存。尝试在php.ini或类似以下代码中提高内存限制:

ini_set('memory_limit', '512M'); //Raise to 512 MB

这不是问题。请您重新编辑并解释您不理解的内容。@hakre我已经编辑了question@adham第2行和第3行的评论不正确,是1MB和60秒…@adham,thx。这说明了一些情况。你从哪里得到这个算法的?同样重要的是,显示使用该类的代码。@hakre事实上,它不是我自己的,来自在线的某个地方