graph (ii) shortest path, minimum spanning tree gguy 19-3-2011

55
Graph (II) Shortest path, Minimum spanning tree GGuy 19-3-2011

Upload: jesse-oliver

Post on 27-Dec-2015

228 views

Category:

Documents


1 download

TRANSCRIPT

Graph (II)

Shortest path, Minimum spanning tree

GGuy19-3-2011

Review

DFS (Depth-First-Search)Graph traverseTopological sort

BFS (Breadth-First-Search)Finding shortest path

Shortest Path Problem

In a weighted graph GWe want to find a path from s to t,such that the sum of edge weight is minimum

Shortest Path

S

T

3

1

31

32

3

Shortest Path

S

T

3

1

31

32

3

Algorithms

Single source?All pairs?Negative weight edge?Negative cycles?

Dijkstra’s algorithmfor-each v, d[v] ← ∞Q.Insert(s,0)

while not Q.Empty() do(u, w) = Q.ExtractMin()if (visited[u]) continue;d[u] ← w for-each v where (u, v) in E

Q.Insert(v, d[u] + weightuv)

Dijkstra’s algorithm

S

T

3

1

31

32

3

Dijkstra’s algorithm

0

T

3

1

31

32

3

Dijkstra’s algorithm

0

2

T

3

1

31

32

3

Dijkstra’s algorithm

0

2

T

3

1

31

32

3

Dijkstra’s algorithm

0

3

2

T

3

1

31

32

3

Dijkstra’s algorithm

0

3

2

T

3

1

31

32

3

Dijkstra’s algorithm

0

3

3

2

T

3

1

31

32

3

Dijkstra’s algorithm

0

3

3

2

T

3

1

31

32

3

Dijkstra’s algorithm

0

3

3

2

T

3

1

31

32

3

Dijkstra’s algorithm

0

3

3

2

4

3

1

31

32

3

Dijkstra’s algorithm

0

3

3

2

4

3

1

31

32

3

Dijkstra’s algorithm

0

3

3

2

4

3

1

31

32

3

Dijkstra’s algorithm

Implement Q using binary heap (priority queue)At most E edges in the heap

Time complexity: O(E log (E)) = O(E log (V))Space complexity: O(V+E)

Bellman Fordfor-each v, d[v] ← ∞d[s] ← 0

Do V-1 timesfor each edge (u,v)

if (d[u] + weightuv < d[v])d[v] ← d[u] + weightuv

Bellman Ford’s algorithm

S

T

3

1

-21

42

3

Bellman Ford’s algorithm

S

4

3

2

T

3

1

-21

42

3

Bellman Ford’s algorithm

S

3

3

2

2

3

1

-21

42

3

Bellman Ford’s algorithm

S

3

3

2

1

3

1

-21

42

3

Bellman Ford’s algorithm

Assume the shortest path from s->t isP0 P1 P2 … Pm-1

where m < |V| and s is fixed

In the 1st iteration, d[P1] is shortest

In the 2nd iteration, d[P2] is shortest

…In the m-1 iteration, d[Pm-1] is shortest

Bellman Ford’s algorithm

After |V|-1 iterations,If there exists edge (u,v) where

d[u] + weightuv < d[v]

Negative cycle!

Bellman Ford’s algorithm

Time complexity: O(VE)Space complexity: O(V)

Floyd Warshall’s algorithmfor all pairs(i,j), d[i][j] ← ∞

for all edges(u,v), d[u][v] ← weightuv

for k=1 to Vfor i=1 to V

for j=1 to Vd[i][j] =

min(d[i][j], d[i][k]+d[k][j])

Floyd Warshall’s algorithmAssume the shortest path from s->t is

P0 P1 P2 … Pm-1

for any s,t

For example, 5->3->1->2->4->6

i=1: 5->1->6i=2: 5->1->2->6i=3: 5->3->1->2->6i=4: 5->3->1->2->4->6i=5: 5->3->1->2->4->6i=6: 5->3->1->2->4->6

We won’t miss any shortest path!

Floyd Warshall’s algorithm

Time complexity: O(V3)Space complexity: O(V2)

Summary

Negative edges?

Negative cycle?

Time complexity

Dijkstra Single source No No O(E log V)

Bellman Ford Single source Yes Yes O(VE)

Floyd Warshall All pairs Yes Maybe (modified)

O(V3)

Tree

What is a tree?

G is connected and acyclicG is connected and |E| = |V| - 1G is acyclic and |E| = |V| - 1For any pairs in G, there is a unique path

Spanning Tree

3

1

31

32

3

Minimum Spanning Tree

3

1

31

32

3

Kruskal’s algorithm

T ← empty set of edges

Sort the edges in increasing order

For each edge e (in increasing order)if T + e does not contain a cycleadd e to T

Kruskal’s algorithm

How to detect a cycle?

Disjoint Set

Kruskal’s algorithm

If we choose an edge (u,v)Union(u, v)

Kruskal’s algorithm

3

1

21

22

2

Kruskal’s algorithm

3

1

21

22

2

Kruskal’s algorithm

3

1

21

22

2

Kruskal’s algorithm

3

1

21

22

2

Kruskal’s algorithm

3

1

21

22

2

Kruskal’s algorithm

3

1

21

22

2

Kruskal’s algorithm

3

1

21

22

2

Kruskal’s algorithm

3

1

21

22

2

Krustal’s algorithm

Sorting: O(E log V)Select edge: O(E)Check union: O(α(V))

Overall: O(E log V + E α(V))

The Red Rule

The Red Rule states that for any cycle in G, the largest weight edge will NOT be contained in any Minimum Spanning Tree.

Prim’s algorithm

T ← node 1

while size of T < Vchoose a vertex u that is not in Vand the cost adding it to V is minimum

add u to V

Prim’s algorithm

3

1

21

22

2

Prim’s algorithm

3

1

21

22

2

Prim’s algorithm

3

1

21

22

2

Prim’s algorithm

3

1

21

22

2

Prim’s algorithm

3

1

21

22

2

Prim’s algorithm

Use a min heap to maintainDijkstra like implement

Time complexity: O(E log V)