@ 2018-06-28T17:50:58Z <?php
class Graph{
private $v;
private $adjList;
public function __construct($vertices){
$this->v=$vertices;
$this->initAdjList();
}
//@suppressWarnings("unchecked")
private function initAdjList(){
$this->adjList = array($this->v);
for($i=0; $i<$this->v;$i++){
$this->adjList[$i]=array();
}
}
public function addEdge($u, $v){
array_push($this->adjList[$u],$v);
}
public function printAllPaths($s, $d){
$isVisted=array();
$isVisted=array_fill(0,$this->v,false);
$pathList=array();
array_push($pathList,$s);
$this->printAllPathsUtil($s, $d, $isVisted,$pathList);
}
private function printAllPathsUtil($u,$d,$isVisted,$localPathList){
$isVisted[intval($u)]=true;
if(intval($u)==intval($d)){
// echo $localPathList;
print_r($localPathList);
}
foreach ($this->adjList as $i){
if(!$isVisted[intval($i)]){
array_push($localPathList,$i);
$this->printAllPathsUtil($i, $d, $isVisted, $localPathList);
array_splice($localPathList,intval($i),1);
}
}
$isVisted[intval($u)]=false;
}
}
$g=new Graph(5);
$g->addEdge(0,1);
$g->addEdge(0,2);
$g->addEdge(0,3);
$g->addEdge(2,0);
$g->addEdge(2,1);
$g->addEdge(1,3);
$g->addEdge(3,1);
$g->addEdge(3,2);
$g->addEdge(4,3);
$g->addEdge(3,4);
$s=0;
$d=4;
$g->printAllPaths($s,$d);
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for 7.1.0 - 7.1.25 , 7.2.0 - 7.2.33 , 7.3.0 - 7.3.33 , 7.4.0 - 7.4.33 , 8.0.0 - 8.0.30 , 8.1.0 - 8.1.28 , 8.2.0 - 8.2.18 , 8.3.0 - 8.3.4 , 8.3.6 Output for 8.3.5 Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
preferences:dark mode live preview
159.83 ms | 402 KiB | 172 Q