shortest paths (1/11)

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

Upload: kirk

Post on 31-Jan-2016

53 views

Category:

Documents


2 download

DESCRIPTION

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

TRANSCRIPT

Page 1: Shortest Paths (1/11)

Shortest Paths (1/11)Shortest Paths (1/11) In this section, we shall study the path problems In this section, we shall study the path problems

such likesuch like Is there a path from city A to city B?Is there a path from city A to city B? If there is more than one path from A to B, which path is If there is more than one path from A to B, which path is

the shortest?the shortest?

Page 2: Shortest Paths (1/11)

Shortest Paths (2/11)Shortest Paths (2/11) Single source/All destinationsSingle source/All destinations: :

nonnegative edge costnonnegative edge cost Problem:Problem: given a directed graph given a directed graph GG = ( = (VV, , EE), a length funct), a length funct

ion ion lengthlength((ii, , jj), ), lengthlength((ii, , jj) ) 0, for the edges of 0, for the edges of GG, and a so, and a source vertex urce vertex vv..

Need to solve:Need to solve: determine a shortest determine a shortest path from path from vv to each of the remaining to each of the remaining vertices of vertices of GG..

Let S denote the set of verticesLet S denote the set of vertices distdist[[ww]: the length of shortest path ]: the length of shortest path

starting from starting from vv, going through only , going through only the vertices that are in the vertices that are in SS, ending at , ending at ww..

V0 V4V1

V5V3V2

50 10

20 10 15 20 35 30

15 3

45

path length1) v0 v2 102) v0 v2 v3 253) v0 v2 v3 v1 454) v0 v4 45

Page 3: Shortest Paths (1/11)

Shortest Paths (3/11)Shortest Paths (3/11) Dijkastra’s AlgorithmDijkastra’s Algorithm

Find the min cost of the path from a given source nodFind the min cost of the path from a given source node to every other node.e to every other node.

Given: the cost Given: the cost ee((vvii, , vvjj) of all edges; ) of all edges; vv00 is the source n is the source n

ode; ode; ee((vvii, , vvjj) = ) = , if , if vvii and and vvjj are not adjacent are not adjacent

SS { {vv00};};

distdist[[vv00] ] 0; 0;

for each for each vv in in V V - {- {vv00} do } do distdist[[vv] ] ee((vv00, , vv););

while while SSVV do dochoose a vertex choose a vertex ww in in VV--SS such that such that distdist[[ww] is a minimum;] is a minimum;

add add ww to to SS;;

for each for each vv in in VV--SS do dodistdist[[vv] ] min( min(distdist[[vv], ], distdist[[ww]+]+ee((ww, , vv));));

Page 4: Shortest Paths (1/11)

Shortest Paths (4/11)Shortest Paths (4/11) Declarations for the Shortest Path AlgorithmDeclarations 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; V0 V4V1

V5V3V2

50 10

20 10 15 20 35 30

15 3

45

Page 5: Shortest Paths (1/11)

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

Page 6: Shortest Paths (1/11)

Single Source Shortest Paths Program (v=0)Single Source Shortest Paths Program (v=0)

[0] [1] [2] [3] [4] [5]

visited:

distance:

cost:

i:u:w:

0 50 10 1000 45 1000

0

2

0123

25

45

1

3

45

2

1

3

4

V0 V4V1

V5V3V2

50 10

20 10 15 20 35 30

15 3

45

Page 7: Shortest Paths (1/11)

Shortest Paths (7/11)Shortest Paths (7/11)

All Pairs Shortest PathsAll Pairs Shortest Paths we could solve this problem using we could solve this problem using shortestpathshortestpath with e with e

ach of the vertices in ach of the vertices in VV((GG) as the source. (O() as the source. (O(nn33)))) we can obtain a conceptually simpler algorithm that wwe can obtain a conceptually simpler algorithm that w

orks correctly even if some edges in orks correctly even if some edges in GG have negative have negative weights, require weights, require GG has no cycles with a negative lengt has no cycles with a negative length (still O(h (still O(nn33))))

Page 8: Shortest Paths (1/11)

Shortest Paths (8/11)Shortest Paths (8/11)

Another solutionAnother solution Use dynamic programming method.Use dynamic programming method. Represent the graph Represent the graph GG by its cost adjacency matrix wi by its cost adjacency matrix wi

th cost[th cost[ii][][jj].]. IfIf i i = = jj, , costcost[[ii][][jj] = 0.] = 0. If <If <ii, , jj> is not in > is not in GG, , costcost[[ii][][jj] is set to some sufficiently large nu] is set to some sufficiently large nu

mber.mber.

Let Let AAkk[[ii][][jj] be the cost of shortest path from ] be the cost of shortest path from ii to to jj, using , using only those intermediate vertices with an index only those intermediate vertices with an index kk.. The shortest path from The shortest path from ii to to j j is is AAn-1n-1[[ii][][jj] as no vertex in] as no vertex in G G has a has a

n index greater than n index greater than n n - 1.- 1.

Page 9: Shortest Paths (1/11)

Shortest Paths (9/11)Shortest Paths (9/11)

Algorithm conceptAlgorithm concept The basic idea in the all pairs algorithm is begin with tThe basic idea in the all pairs algorithm is begin with t

he matrix he matrix AA-1-1 and successively generated the matrices and successively generated the matrices AA-1-1, , AA00, , AA22, …, , …, AAnn

AA-1-1[[ii][][jj]=cost[]=cost[ii][][jj]] Calculate the ACalculate the A00, A, A11, A, A22, ..., A, ..., Ann-1-1 from A from A-1-1 iteratively iteratively AAkk[[ii][][jj]=min{A]=min{Akk-1-1[[ii][][jj], A], Akk-1-1[[ii][][kk]+A]+Akk-1-1[[kk][][jj]}, ]}, kk>=0>=0

Page 10: Shortest Paths (1/11)

Shortest Paths (10/11)Shortest Paths (10/11)

Graph with Negative CycleGraph with Negative Cycle The length of the shortest path from vertex 0 to vertex

2 is -

0, 1, 0, 1, 0, 1, …, 0, 1, 2

0 1 2

-2

1 1

(a) Directed graph (b) A-1

0

102

10

Page 11: Shortest Paths (1/11)

Shortest Paths (11/11)Shortest Paths (11/11) All pairs shortest paths programAll pairs shortest paths program

V0

V2

V1

6

4 3 11 2

0 4 11

6 0 2

3 1000 0

cost:

final distance:0 4 6

5 0 2

3 7 0

Page 12: Shortest Paths (1/11)

Topological Sorts (1/19)Topological Sorts (1/19) Activity on vertex (AOV) networksActivity on vertex (AOV) networks

All but the simplest of projects can be divided into several All but the simplest of projects can be divided into several subprojects called subprojects called activitiesactivities..

The successful completion of these activities results in the The successful completion of these activities results in the completion of completion of the entire projectthe entire project

Example: Example: A student working A student working toward a degree in toward a degree in computer science computer science must complete must complete several courses several courses successfullysuccessfully

Page 13: Shortest Paths (1/11)

Topological Sorts (2/19)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

Page 14: Shortest Paths (1/11)

Topological Sorts (3/19)Topological Sorts (3/19) Definition: Topological orderDefinition: Topological order

linear ordering of vertices of a graphlinear ordering of vertices of a graph ii, , jj if if ii is a predecessor of is a predecessor of jj, then , then ii precedes precedes jj in the linear in the linear

orderingordering

C4, C5, C2, C1, C6, C3, C8, C15, C7, C9, C10, C11, C13, C12, C14

C1, C2, C4, C5, C3, C6, C8, C7, C10, C13, C12, C14, C15, C11, C9

Page 15: Shortest Paths (1/11)

Topological Sorts (4/19)Topological Sorts (4/19) Topological sort AlgorithmTopological sort Algorithm

for (i = 0; i <n; i++) {for (i = 0; i <n; i++) {

if every vertex has a predecessor {if every vertex has a predecessor {

fprintf (stderr, “Network has a cycle. \n “ );fprintf (stderr, “Network has a cycle. \n “ );

exit(1);exit(1);

}}

pick a vertex v that has no predecessors;pick a vertex v that has no predecessors;

output v;output v;

delete v and all edges leading out of v from the network;delete v and all edges leading out of v from the network;

}}

V0

V1

V2

V3

V4

V5

Topological order generated: v0 v3 v2 v5 v1 v4

Page 16: Shortest Paths (1/11)

Topological Topological Sorts (5/19)Sorts (5/19)

Declarations used Declarations used in in topsorttopsort

data representationdata representation

decide whether a vertex has any predecessors

decide a vertex together with all its incident edges

V0

V1

V2

V3

V4

V5

the in-degree of that vertex

Page 17: Shortest Paths (1/11)

Topological Topological sort Programsort Program

Time complexity: O(n+e)

V5V4V3V2V1V0

count

link

1

2

3

4 4

5

5

4

0 1 1 1 3 2

headnode

node

output:

v0 v3 v2 v5 v1 v4

top:

-1

0-1 j: 0 k:

ptr

1

0-1

1 2

01

2 3

02

3 32 5

1

4

2

21

1

5

01

5 51 1-1 4

0-1

4-1

V0

V1

V2

V3

V4

V5

4

Page 18: Shortest Paths (1/11)

Topological Sorts (7/19)Topological Sorts (7/19) Activity on Edge (AOE) NetworksActivity on Edge (AOE) Networks

AOE is related to the AOV network.AOE is related to the AOV network.

AOE networks have proved very useful for AOE networks have proved very useful for evaluating the performance of many types of evaluating the performance of many types of projectsprojects.. What is the least amount of time in which the project What is the least amount of time in which the project

may be complete (assuming there are no cycle in the may be complete (assuming there are no cycle in the network)?network)?

Which activities should be speeded to reduce project Which activities should be speeded to reduce project length?length?

Page 19: Shortest Paths (1/11)

Topological Sorts (8/19)Topological Sorts (8/19) An AOE networkAn AOE network

directed edgedirected edge: tasks : tasks or activities to be or activities to be performedperformed

vertexvertex: events which : events which signal the completion signal the completion of certain activitiesof certain activities

numbernumber: associated : associated with each edge with each edge (activity) is the time (activity) is the time required to required to perform the activityperform the activity

Page 20: Shortest Paths (1/11)

Topological Sorts (9/19)Topological Sorts (9/19) Application of AOE NetworkApplication of AOE Network

Evaluate performance minimum amount of time activity whose duration

time should be shortened …

Critical path a path that has the

longest length minimum time required

to complete the project

v0, v1, v4, v6, v8 or v0, v1, v4, v7, v8 (18)

Page 21: Shortest Paths (1/11)

Topological Sorts (10/19)Topological Sorts (10/19)

Critical-path analysisCritical-path analysis The purpose of critical-path analysis is to The purpose of critical-path analysis is to identify criticidentify critic

al activitiesal activities so that so that resource may be concentrated on tresource may be concentrated on these activitieshese activities in an attempt to reduce a project finish in an attempt to reduce a project finish time.time.

Critical-path analysis can also be carried out with AOCritical-path analysis can also be carried out with AOV networkV network

Determine Critical PathsDetermine Critical Paths Delete all noncritical activitiesDelete all noncritical activities Generate all the paths from the start to finish vertexGenerate all the paths from the start to finish vertex

Page 22: Shortest Paths (1/11)

Topological Sorts (11/19)Topological Sorts (11/19) Various FactorsVarious Factors

The The earliest timeearliest time of an event 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 i the earliest start time for all activities responded by edges leaving vi

early(6) = early(7)=7

late(i): latest event occurrence (latest activity) time of i the latest time the

activity may start without increasing the project duration

early(5)=5, late(5)=8 early(7)=7, late(7)=7

=18-2-4-4

Page 23: Shortest Paths (1/11)

Topological Sorts (12/19)Topological Sorts (12/19) Various Factors (cont’d)Various Factors (cont’d)

late(i)-early(i) measure of how critical an activity is (ex. late(5)-early(5)=8-5=3)

Critical activity an activity for which early(i)=late(i) (ex. early(7)=late(7))

earliestearliest[[jj]: ]: earliest event occurrence time for all event earliest event occurrence time for all event jj in the network in the network

latestlatest[[jj]: ]: latest event occurrence time for all event latest event occurrence time for all event jj in the network in the network

If activity If activity aaii is represented by edge < is represented by edge <kk, , ii>> earlyearly((ii) = ) = earliestearliest[[kk]]

latelate((ii) =) = latest latest[[ii] - ] - duration of activity duration of activity aaii

We compute the times We compute the times earliestearliest[[jj] and ] and latestlatest[[jj] in two stages: ] in two stages: a forward stage and a backward stagea forward stage and a backward stage

vk viai

Page 24: Shortest Paths (1/11)

Topological Sorts (13/19)Topological Sorts (13/19) Calculation of Earliest TimesCalculation of Earliest Times

During the forwarding stage, we start with During the forwarding stage, we start with earliestearliest[0] = 0 [0] = 0 and compute the remaining start times using the formula:and compute the remaining start times using the formula:

Where Where PP((jj) is the set of immediate predecessors of ) is the set of immediate predecessors of jj We can easily obtain an algorithmWe can easily obtain an algorithm

that does this by inserting the that does this by inserting the following statement at the end of thefollowing statement at the end of theelseelse clause in clause in topsorttopsort

} , of duration][ {max][ )(

jiiearliestjearliestjpi

vi1

vi2

vin

.

.

.

.

vj

forward stage

if (earliest[k] < earliest[j] + ptr->duration) earliest[k] = earliest[j] + ptr->duration;

Page 25: Shortest Paths (1/11)

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;

}

Calculation of Earliest Calculation of Earliest Times (cont’d)Times (cont’d)

Page 26: Shortest Paths (1/11)

Topological Sorts (15/19)Topological Sorts (15/19) Calculation of latest timesCalculation of latest times

In the backward stage, we compute the values of In the backward stage, we compute the values of latestlatest[[ii] us] using a procedure analogous to that used in the forward stage.ing a procedure analogous to that used in the forward stage.

We start with We start with latestlatest[[nn-1] = -1] = earliestearliest[[nn-1] and use the formula:-1] and use the formula:

Where Where SS((jj) is the set of vertices adjacent from vertex ) is the set of vertices adjacent from vertex jj Use inverse adjacency listUse inverse adjacency list Insert the following statement at the Insert the following statement at the

end of the end of the elseelse clause in clause in topsorttopsort

backward stage

vi1

vi2

vin

.

.

.

vj

} , of duration-][ {min][ )(

ijilatestjlatestjSi

if (latest[k] > latest[j] - ptr->duration) latest[k] = latest[j] - ptr->duration;

Page 27: Shortest Paths (1/11)

Calculation of latest Calculation of latest Times (cont’d)Times (cont’d)

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;}

Page 28: Shortest Paths (1/11)

Calculation of latest Calculation of latest Times (cont’d)Times (cont’d)

Page 29: Shortest Paths (1/11)

Topological Sorts (18/19)Topological Sorts (18/19) Non-critical activities deletedNon-critical activities deleted

earliest and latest values earliest and latest values earlyearly((ii) and ) and latelate((ii) ) the degree of criticality for each task the degree of criticality for each task

Page 30: Shortest Paths (1/11)

Topological Sorts (19/19)Topological Sorts (19/19) We note that the topsort detects only directed cyWe note that the topsort detects only directed cy

cles in the network.cles in the network. There may be other flaws in the network, includiThere may be other flaws in the network, includi

ngng vertices that are not reachable from vertices that are not reachable from

the start vertexthe start vertex We can also use critical path We can also use critical path

analysis to detect this kind of analysis to detect this kind of fault in project planningfault in project planning

earliest[i] = 0

V0

V3

V1

V5

V2

V4

a0

a1

a2

a3

a5a6

a4