shortest paths (1/11)

Download Shortest Paths (1/11)

Post on 31-Jan-2016




2 download

Embed Size (px)


Shortest Paths (1/11). In this section, we shall study the path problems such like Is there a path from city A to city B? If there is more than one path from A to B, which path is the shortest?. 45. 50 10. V 0. V 1. V 4. 20 10 15 20 35 30. 15 3. - PowerPoint PPT Presentation


  • Shortest Paths (1/11)In this section, we shall study the path problems such likeIs there a path from city A to city B?If there is more than one path from A to B, which path is the shortest?

  • Shortest Paths (2/11)Single source/All destinations: nonnegative edge costProblem: given a directed graph G = (V, E), a length function length(i, j), length(i, j) 0, for the edges of G, and a source vertex v.Need to solve: determine a shortest path from v to each of the remaining vertices of G.Let S denote the set of verticesdist[w]: the length of shortest path starting from v, going through only the vertices that are in S, ending at w. path length1) v0 v2 102) v0 v2 v3 253) v0 v2 v3 v1 454) v0 v4 45

  • Shortest Paths (3/11)Dijkastras AlgorithmFind the min cost of the path from a given source node to every other node.Given: the cost e(vi, vj) of all edges; v0 is the source node; e(vi, vj) = , if vi and vj are not adjacentS {v0};dist[v0] 0;for each v in V - {v0} do dist[v] e(v0, v);while SV dochoose a vertex w in V-S such that dist[w] is a minimum;add w to S;for each v in V-S dodist[v] min(dist[v], dist[w]+e(w, v));

  • Shortest Paths (4/11)Declarations for the Shortest Path Algorithm#define MAX_VERTICES 6int cost[ ][MAX_VERTICES]={{ 0, 50, 10, 1000, 45, 1000},{1000, 0, 15, 1000, 10, 1000},{ 20, 1000, 0, 15, 1000, 1000},{1000, 20, 1000, 0, 35, 1000},{1000, 1000, 30, 1000, 0, 1000},{1000, 1000, 1000, 3, 1000, 0}};int distance[MAX_VERTICES];short int found{MAX_VERTICES];int n = MAX_VERTICES;

  • Shortest Paths (5/11)Choosing the least cost edge

  • Single Source Shortest Paths Program (v=0)[0][1][2][3][4][5]visited:distance:cost:i:u:w:050101000451000020123254513452134

  • Shortest Paths (7/11)All Pairs Shortest Pathswe could solve this problem using shortestpath with each of the vertices in V(G) as the source. (O(n3))we can obtain a conceptually simpler algorithm that works correctly even if some edges in G have negative weights, require G has no cycles with a negative length (still O(n3))

  • Shortest Paths (8/11)Another solutionUse dynamic programming method.Represent the graph G by its cost adjacency matrix with cost[i][j].If i = j, cost[i][j] = 0.If is not in G, cost[i][j] is set to some sufficiently large number.Let Ak[i][j] be the cost of shortest path from i to j, using only those intermediate vertices with an index k.The shortest path from i to j is An-1[i][j] as no vertex in G has an index greater than n - 1.

  • Shortest Paths (9/11)Algorithm conceptThe basic idea in the all pairs algorithm is begin with the matrix A-1 and successively generated the matrices A-1, A0, A2, , AnA-1[i][j]=cost[i][j]Calculate the A0, A1, A2, ..., An-1 from A-1 iterativelyAk[i][j]=min{Ak-1[i][j], Ak-1[i][k]+Ak-1[k][j]}, k>=0

  • Shortest Paths (10/11)Graph with Negative CycleThe length of the shortest path from vertex 0 to vertex 2 is -0, 1, 0, 1, 0, 1, , 0, 1, 2(a) Directed graph (b) A-1

  • Shortest Paths (11/11)All pairs shortest paths programcost:final distance:


  • Topological Sorts (1/19)Activity on vertex (AOV) networksAll but the simplest of projects can be divided into several subprojects called activities.The successful completion of these activities results in the completion of the entire projectExample: A student working toward a degree in computer science must complete several courses successfully

  • Topological Sorts (2/19)Definition:Activity On Vertex (AOV) Network: a directed graph in which the vertices represent tasks or activities and the edges represent precedence relations between tasks.predecessor (successor): vertex i is a predecessor of vertex j iff there is a directed path from i to j. j is a successor of i.partial order: a precedence relation which is both transitive (i, j, k, i j & j k i k ) and irreflexive (x x x)acylic graph: a directed graph with no directed cycles

  • Topological Sorts (3/19)Definition: Topological orderlinear ordering of vertices of a graphi, j if i is a predecessor of j, then i precedes j in the linear orderingC4, C5, C2, C1, C6, C3, C8, C15, C7, C9, C10, C11, C13, C12, C14C1, C2, C4, C5, C3, C6, C8, C7, C10, C13, C12, C14, C15, C11, C9

  • Topological Sorts (4/19)Topological sort Algorithmfor (i = 0; i
  • Topological Sorts (5/19)Declarations used in topsortdata representationdecide whether a vertex has any predecessorsdecide a vertex together with all its incident edgesthe in-degree of that vertex

  • Topological sort ProgramTime complexity: O(n+e)V5V4V3V2V1V0countlink12344554011132headnodenodeoutput:v0v3v2v5v1v4top:-10-1j:0k:ptr10-11201230233251422115015511-140-14-14

  • Topological Sorts (7/19)Activity on Edge (AOE) NetworksAOE is related to the AOV network.AOE networks have proved very useful for evaluating the performance of many types of projects.What is the least amount of time in which the project may be complete (assuming there are no cycle in the network)?Which activities should be speeded to reduce project length?

  • Topological Sorts (8/19)An AOE networkdirected edge: tasks or activities to be performedvertex: events which signal the completion of certain activitiesnumber: associated with each edge (activity) is the time required to perform the activity

  • Topological Sorts (9/19)Application of AOE NetworkEvaluate performanceminimum amount of timeactivity whose duration time should be shortenedCritical patha path that has the longest lengthminimum time required to complete the projectv0, v1, v4, v6, v8 or v0, v1, v4, v7, v8 (18)

  • Topological Sorts (10/19)Critical-path analysisThe purpose of critical-path analysis is to identify critical activities so that resource may be concentrated on these activities in an attempt to reduce a project finish time.Critical-path analysis can also be carried out with AOV networkDetermine Critical PathsDelete all noncritical activitiesGenerate all the paths from the start to finish vertex

  • Topological Sorts (11/19)Various FactorsThe earliest time of an event vi the length of the longest path from v0 to vi (Ex. 7 for v4)early(i): earliest event occurrence (Earliest activity) time of ithe earliest start time for all activities responded by edges leaving viearly(6) = early(7)=7late(i): latest event occurrence (latest activity) time of ithe latest time the activity may start without increasing the project durationearly(5)=5, late(5)=8early(7)=7, late(7)=7=18-2-4-4

  • Topological Sorts (12/19)Various Factors (contd)late(i)-early(i)measure of how critical an activity is (ex. late(5)-early(5)=8-5=3)Critical activityan activity for which early(i)=late(i) (ex. early(7)=late(7))earliest[j]: earliest event occurrence time for all event j in the networklatest[j]: latest event occurrence time for all event j in the networkIf activity ai is represented by edge early(i) = earliest[k]late(i) = latest[i] - duration of activity aiWe compute the times earliest[j] and latest[j] in two stages: a forward stage and a backward stage

  • Topological Sorts (13/19)Calculation of Earliest TimesDuring the forwarding stage, we start with earliest[0] = 0 and compute the remaining start times using the formula: Where P(j) is the set of immediate predecessors of jWe can easily obtain an algorithm that does this by inserting the following statement at the end of the else clause in topsortforward stageif (earliest[k] < earliest[j] + ptr->duration) earliest[k] = earliest[j] + ptr->duration;

  • Calculation of Earliest Times (contd)for (ptr=graph[j].link;ptr;ptr=ptr->link){ k=ptr->vertex; graph[k].count--; if(!graph[k].count){ graph[k].count=top; top=k;} if(earliest[k]< earliest[j]+ptr->duration) earliest[k] = earliest[j]+ptr->duration;}

  • Topological Sorts (15/19)Calculation of latest timesIn the backward stage, we compute the values of latest[i] using a procedure analogous to that used in the forward stage.We start with latest[n-1] = earliest[n-1] and use the formula: Where S(j) is the set of vertices adjacent from vertex jUse inverse adjacency listInsert the following statement at the end of the else clause in topsortbackward stageif (latest[k] > latest[j] - ptr->duration) latest[k] = latest[j] - ptr->duration;

  • Calculation of latest Times (contd)for (ptr=graph[j].link;ptr; ptr=ptr->link){ k=ptr->vertex; graph[k].count--; if(!graph[k].count){ graph[k].count=top; top=k;} if(latest[k]> latest[j]-ptr->duration) latest[k] = latest[j]-ptr->duration;}

  • Calculation of latest Times (contd)

  • Topological Sorts (18/19)Non-critical activities deletedearliest and latest values early(i) and late(i) the degree of criticality for each task

  • Topological Sorts (19/19)We note that the topsort detects only directed cycles in the network.There may be other flaws in the network, includingvertices that are not reachable from the start vertexWe can also use critical path analysis to detect this kind of fault in project planningearliest[i] = 0a0a1a2a3a5a6a4


View more >