/** * Weighted digraph test program. * * @param object IDigraph $g The weighted digraph to test. */ public static function weightedDigraphTest(IDigraph $g) { printf("Weighted digraph test program.\n"); Application11::weightedGraphTest($g); printf("Dijkstra's Algorithm\n"); $g2 = Algorithms::dijkstrasAlgorithm($g, 0); printf("%s\n", str($g2)); printf("Floyd's Algorithm\n"); $g2 = Algorithms::floydsAlgorithm($g); printf("%s\n", str($g2)); }
/** * Computes the critical path in an event-node graph. * * @param object IDigraph $g An edge-weighted, directed acylic graph. * It is assumed that the edge weights are <code>Int</code>s * @return object IDigraph A vertex-weighted, directed graph. * The events (vertices) on the critical path have zero weight. * The one edge that emantes from a vertex connects that vertex * to its predecessor on the critical path. */ public static function criticalPathAnalysis(IDigraph $g) { $n = $g->getNumberOfVertices(); $earliestTime = new BasicArray($n); $earliestTime[0] = 0; $g->topologicalOrderTraversal(new EarliestTimeVisitor($earliestTime)); $latestTime = new BasicArray($n); $latestTime[$n - 1] = $earliestTime[$n - 1]; $g->depthFirstTraversal(new PostOrder(new LatestTimeVisitor($latestTime)), 0); $slackGraph = new DigraphAsLists($n); for ($v = 0; $v < $n; ++$v) { $slackGraph->addVertex($v); } foreach ($g->getEdges() as $edge) { $n0 = $edge->getV0()->getNumber(); $n1 = $edge->getV1()->getNumber(); $wt = $edge->getWeight(); $slack = $latestTime[$n1] - $earliestTime[$n0] - unbox($wt); $slackGraph->addEdge($n0, $n1, box($slack)); } return Algorithms::dijkstrasAlgorithm($slackGraph, 0); }