Skip to content

Shortest path depends on vertex sequence #66

@integer

Description

@integer

I have code like this:

$graph = new \Fhaculty\Graph\Graph();
$v1 = $graph->createVertex(1);
$v2 = $graph->createVertex(2);
$v5 = $graph->createVertex(5);
$v6 = $graph->createVertex(6);

$v1->createEdge($v2)->setWeight(4);
$v2->createEdge($v5)->setWeight(4);
$v5->createEdge($v6)->setWeight(4);

$alg = new \Fhaculty\Graph\Algorithm\ShortestPath\Dijkstra($v1);
echo $graph;
var_dump($alg->getDistance($v6));

it has output as excepted:

graph G {
  1 -- 2 [label=4]
  2 -- 5 [label=4]
  5 -- 6 [label=4]
}
int(12)

but if I change sequence of creating vertices

$v1 = $graph->createVertex(1);
$v2 = $graph->createVertex(2);
$v6 = $graph->createVertex(6);  // this vertex was created as last
$v5 = $graph->createVertex(5);

// other code is the same

I get this output:

graph G {
  1 -- 2 [label=4]
  2 -- 5 [label=4]
  5 -- 6 [label=4]
}

And distance isn't count because maximum execution time exceeded.

Graph is the same in both cases, so result has to be same too.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions