1 9.3 shortest path algorithms improvement –use a queue to keep all vertices whose shortest...

27
1 9.3 Shortest Path Algorithms • Improvement Use a queue to keep all vertices whose shortest distance to s is known. Initialize d v to for all vertices, except s Set d s to 0 and enqueue s. While the queue is not empty •remove the first vertex in the queue. •enqueue all vertices which are adjacent to the vertex, and have a distance of infinity from s.

Upload: dustin-gordon

Post on 17-Dec-2015

222 views

Category:

Documents


3 download

TRANSCRIPT

1

9.3 Shortest Path Algorithms

• Improvement– Use a queue to keep all vertices whose

shortest distance to s is known.

– Initialize dv to for all vertices, except s

– Set ds to 0 and enqueue s.

– While the queue is not empty• remove the first vertex in the queue.• enqueue all vertices which are adjacent to the

vertex, and have a distance of infinity from s.

2

9.3 Shortest Path Algorithms– known not used

– Running time is (|E|+|V|).

/* Fig. 9.18 Unweighted shortest path algorithm */void Unweighted (Table T) /* Assume T is initialized */{ Queue Q; Vertex V, W;

Q = CreateQueue (NumVertex);

3

9.3 Shortest Path Algorithms MakeEmpty (Q);

/* Enqueue the start vertex S */

Enqueue (S, Q);

while (!IsEmpty (Q))

{ V = Dequeue {Q);

T [V].Known = True; /* Not really needed */

4

9.3 Shortest Path Algorithms for each W adjacent to V

if (T [W].Dist == Infinity)

{ T [W].Dist = T [V].Dist + 1;

T [W].Path = V;

Enqueue (W, Q);

}

}

DisposeQueue (Q); /* Free the memory */

}

5

9.3 Shortest Path Algorithms

• Result for Fig. 9.20, with weight=1 & start = v3Initial State v3 Dequeued

v Known dv pv v Known dv pv

v1 0 0 v1 0 1 v3

v2 0 0 v2 0 0v3 0 0 0 v3 1 0 0v4 0 0 v4 0 0v5 0 0 v5 0 0v6 0 0 v6 0 1 v3

v7 0 0 v7 0 0Q v3 Q v1, v6

v1 Dequeued v6 Dequeuedv Known dv pv v Known dv pv

v1 1 1 v3 v1 1 1 v3

v2 0 2 v1 v2 0 2 v1

v3 1 0 0 v3 1 0 0v4 0 2 v1 v4 0 2 v1

v5 0 0 v5 0 0v6 0 1 v3 v6 1 1 v3

v7 0 0 v7 0 0Q v6, v2, v4 Q v2, v4

6

9.3 Shortest Path Algorithmsv2 Dequeued v4 Dequeued

v Known dv pv v Known dv pv

v1 1 1 v3 v1 1 1 v3

v2 1 2 v1 v2 1 2 v1

v3 1 0 0 v3 1 0 0v4 0 2 v1 v4 1 2 v1

v5 0 3 v2 v5 0 3 v2

v6 1 1 v3 v6 1 1 v3

v7 0 0 v7 0 3 v4

Q v4, v5 Q v5, v7

v5 Dequeued v7 Dequeuedv Known dv pv v Known dv pv

v1 1 1 v3 v1 1 1 v3

v2 1 2 v1 v2 1 2 v1

v3 1 0 0 v3 1 0 0v4 1 2 v1 v4 1 2 v1

v5 1 3 v2 v5 1 3 v2

v6 1 1 v3 v6 1 1 v3

v7 0 3 v4 v7 1 3 v4

Q v7 Q empty

7

9.3 Shortest Path AlgorithmsDijkstra’s Algorithm (for single-source

weighted graphs)

• A greedy algorithm, solving a problem by stages by doing what appears to be the best thing at each stage.

• Select a vertex v, which has the smallest dv among all the unknown vertices, and declare that the shortest path from s to v is known.

8

9.3 Shortest Path Algorithms• For each adjacent vertex, w, update dw = dv

+ cv,w if this new value for dw is an improvement. 2

v1 v2

4 1 3 10

v3 2 v4 2 v5

8 4 6 5 v6 1 v7

Figure 9.20 The directed graph G (again)

9

9.3 Shortest Path Algorithms

v Known dv pv v Known dv pv

v1 0 0 0 v1 1 0 0v2 0 0 v2 0 2 v1

v3 0 0 v3 0 0v4 0 0 v4 0 1 v1

v5 0 0 v5 0 0v6 0 0 v6 0 0v7 0 0 v7 0 0

v Known dv pv v Known dv pv

v1 1 0 0 v1 1 0 0v2 0 2 v1 v2 1 2 v1

v3 0 3 v4 v3 0 3 v4

v4 1 1 v1 v4 1 1 v1

v5 0 3 v4 v5 0 3 v4

v6 0 9 v4 v6 0 9 v4

v7 0 5 v4 v7 0 5 v4

Application of Dijkstra’s algorithm for Fig. 9.20

10

9.3 Shortest Path Algorithmsv Known dv pv v Known dv pv

v1 1 0 0 v1 1 0 0v2 1 2 v1 v2 1 2 v1

v3 1 3 v4 v3 1 3 v4

v4 1 1 v1 v4 1 1 v1

v5 1 3 v4 v5 1 3 v4

v6 0 8 v3 v6 0 6 v7

v7 0 5 v4 v7 1 5 v4

v Known dv pv

v1 1 0 0v2 1 2 v1

v3 1 3 v4

v4 1 1 v1

v5 1 3 v4

v6 1 6 v7

v7 1 5 v4

Details on Fig. 9.28

11

9.3 Shortest Path Algorithms

/* Fig. 9.29 Declarations for Dijkstra’s Algorithm

typedef int Vertex;

struct TableEntry

{ List Header; /* Adjacency list */

int Known;

DistType Dist;

Vertex Path;

}

#defin NotAVertex (-1)

tyepdef struct TableEntry Table [NumVertex];

12

9.3 Shortest Path Algorithms/* Fig. 9.30 Table initialization routine */

void InitTable (Vertex Start, Graph G, Table T)

{ int i;

ReadGraph (G, T);

for (i=0; i<NumVertex; i++)

{ T [i].Known = False; T [i].Dist = Infinity;

T [i].Path = NotAVertex;

}

T [Start].Dist = 0;

}

13

9.3 Shortest Path Algorithms/* Fig. 9.32 Pseudocode - Dijkstra’s Algorithm */

void Dijkstra (Table T)

{ Vertex V, W;

for (;;;)

{ V = smallest unknown distance vertex;

if (V == NotAVertex)

break;

T [V].Known = True;

for each W adjacent to V

14

9.3 Shortest Path Algorithms if (!T [W].Known)

if (T [V].Dist + Cvw < T [W].Dist)

{ /* update W */

Decrease (T [W].Dist to T [V].Dist +

Cvw);

T [W].Path = V;

}

}

}

15

9.3 Shortest Path Algorithms/* Fig. 9.31 Routine to print actual shortest path */

/* Assume the path exists */

void PrintPath (Vertex V, Table T)

{ if (T [V].Path != NotAVertex)

{ PrintPath (T [V].Path, T)

printf (" to");

}

printf ("%v", V); /* %v is pseudocode */

}

16

9.3 Shortest Path Algorithms• Total of the running time to find the

minimum dv is (|V|2).

• Running time for updating dw is constant per update, for a total of (|E|).

• Total running time is (|E|+|V|2).

• For a sparse graph, |E| = (|V|), the algorithm is slow. It’s best to keep the distances in a priority queue.

17

9.3 Shortest Path AlgorithmsCritical Path Analysis (An Acyclic Graph)

• Given an activity-node graph

• The edges represent precedence relationships: (v, w) means that activity v must be completed before activity w may begin.

• Activities independent of each other can be performed in parallel.

18

9.3 Shortest Path Algorithms

C(3)

A(3) F(3)

start D(2) H(11) finish

B(2) G(2)

E(1) K(4)

Figure 9.34 Activity-node graph

19

9.3 Shortest Path Algorithms• Convert the activity-node graph to an

event-node graph.

• Each event corresponds to the completion of an activity and all its dependent activities.

• Events reachable from a node v in the event-node graph may not commence until after the event v is completed.

20

9.3 Shortest Path Algorithms• Dummy edges and nodes may need to be

inserted in the case where an activity depends on several others, to avoid introducing false dependencies (or false lack of dependencies).

21

9.3 Shortest Path Algorithms

22

9.3 Shortest Path Algorithms• Earliest completion time

– Length of the longest path from the first event to the last event

– Let ECi be the earliest completion time for node i

– Compute for all vertices by their topological order.

)(max

0

,

1

),(wvvw cECEC

EC

Ewv

23

9.3 Shortest Path Algorithms

24

9.3 Shortest Path Algorithms• Latest starting time

– Latest starting time

– Let LCi be the latest time event i must be completed without affecting the final completion time.

– Computed by reverse topological order.

)(min ,),(

wvwEwv

v

nn

cLCLC

ECLC

25

9.3 Shortest Path Algorithms

26

9.3 Shortest Path Algorithms

• Slack time for each edge is the amount of time that the completion of the corresponding activity can be delayed without affecting the overall completion.

Slack (v,w) = LCw - ECv - cv,w

27

9.3 Shortest Path Algorithms

• A critical path consists entirely of zero-slack edges.