csc 331: algorithm analysis paths in graphs. the dfs algorithm we gave is recursive. dfs generates a...

19
CSC 331: Algorithm Analysis Paths in Graphs

Upload: scarlett-pierce

Post on 17-Dec-2015

216 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: CSC 331: Algorithm Analysis Paths in Graphs. The DFS algorithm we gave is recursive. DFS generates a search tree showing paths from one vertex to all

CS

C 3

31

: A

lgori

thm

An

aly

sis

Paths in Graphs

Page 2: CSC 331: Algorithm Analysis Paths in Graphs. The DFS algorithm we gave is recursive. DFS generates a search tree showing paths from one vertex to all

The DFS algorithm we gave is recursive.

DFS generates a search tree showing paths from one vertex to all other connected vertices.

We can re-write using a stack:

Page 3: CSC 331: Algorithm Analysis Paths in Graphs. The DFS algorithm we gave is recursive. DFS generates a search tree showing paths from one vertex to all

procedure explore(G, s)Input: Graph G=(V, E); s ∈ V

for all u in V:visited(u) = false

Stack = [s] (stack containing just s)while Stack is not empty:

u = pop(Stack)visited(u) = true

for all edges (u, v) ∈ E:if visited(v) = false:push(Stack, v)

Page 4: CSC 331: Algorithm Analysis Paths in Graphs. The DFS algorithm we gave is recursive. DFS generates a search tree showing paths from one vertex to all

Does DFS give the shortest path from S to all vertices?

EE

DD

SS

CC

AA

BB

SS

CC

BB

AA DD

EE

DFS generates a search tree showing paths from one vertex to all other connected vertices.

Page 5: CSC 331: Algorithm Analysis Paths in Graphs. The DFS algorithm we gave is recursive. DFS generates a search tree showing paths from one vertex to all

The distance between two vertices is the length of the shortest path between them.

Page 6: CSC 331: Algorithm Analysis Paths in Graphs. The DFS algorithm we gave is recursive. DFS generates a search tree showing paths from one vertex to all

EE

DD

SS

CC

AA

BB

SS

CC

BB

AA DD EE

A convenient way to compute distances from s to other vertices is to proceed layer by layer.

Page 7: CSC 331: Algorithm Analysis Paths in Graphs. The DFS algorithm we gave is recursive. DFS generates a search tree showing paths from one vertex to all

procedure bfs(G, s)Input: Graph G=(V, E); s ∈ VOutput: For all vertices u reachable from s, dist(u) is set to the distance from s to u

for all u in V:dist(u) = ∞

dist(s) = 0Q = [s] (queue containing just s)while Q is not empty:

u = dequeue(Q)for all edges (u, v) ∈ E:

if dist(v) = ∞:enqueue(Q, v)dist(v) = dist(u) + 1

Page 8: CSC 331: Algorithm Analysis Paths in Graphs. The DFS algorithm we gave is recursive. DFS generates a search tree showing paths from one vertex to all

EE

DD

SS

CC

AA

BB

SS

CC

BB

AA DD EE

What is the overall running time of BFS?

O(|V| + |E|)

Page 9: CSC 331: Algorithm Analysis Paths in Graphs. The DFS algorithm we gave is recursive. DFS generates a search tree showing paths from one vertex to all

DFS makes deep incursions into a graph, retreating only when it runs out of new vertices to visit.

BFS makes sure to visit vertices in increasing order of their distances from the starting point.

Page 10: CSC 331: Algorithm Analysis Paths in Graphs. The DFS algorithm we gave is recursive. DFS generates a search tree showing paths from one vertex to all

Suppose you are taking a train from San Francisco to Las Vegas, and want to find the quickest route.

3

53

2

Page 11: CSC 331: Algorithm Analysis Paths in Graphs. The DFS algorithm we gave is recursive. DFS generates a search tree showing paths from one vertex to all

The weights on an edge can denote time, cost, or any other quantity that we would like to conserve.

BFS finds shortest paths in any graph whose edges have unit length.

Can we adapt it to a more general graph G = (V, E) whose edge lengths le are positive integers?

Page 12: CSC 331: Algorithm Analysis Paths in Graphs. The DFS algorithm we gave is recursive. DFS generates a search tree showing paths from one vertex to all

Dijkstra’s algorithm is similar to BFS, except that it uses a priority queue to choose vertices in a way that takes the lengths into account.

Page 13: CSC 331: Algorithm Analysis Paths in Graphs. The DFS algorithm we gave is recursive. DFS generates a search tree showing paths from one vertex to all

DD

CC

AA

BB

EE

4

2

1 3

2

1

5

4

3

A: 0 D: ∞

B: 4 E: ∞

C: 2

Q = {C, B}

Page 14: CSC 331: Algorithm Analysis Paths in Graphs. The DFS algorithm we gave is recursive. DFS generates a search tree showing paths from one vertex to all

DD

CC

AA

BB

EE

4

2

1 3

2

1

5

4

3

A: 0 D: 6

B: 3 E: 7

C: 2

Q = {B, D, E}

Page 15: CSC 331: Algorithm Analysis Paths in Graphs. The DFS algorithm we gave is recursive. DFS generates a search tree showing paths from one vertex to all

DD

CC

AA

BB

EE

4

2

1 3

2

1

5

4

3

A: 0 D: 5

B: 3 E: 6

C: 2

Q = {D, E}

Page 16: CSC 331: Algorithm Analysis Paths in Graphs. The DFS algorithm we gave is recursive. DFS generates a search tree showing paths from one vertex to all

DD

CC

AA

BB

EE

4

2

1 3

2

1

5

4

3

A: 0 D: 5

B: 3 E: 6

C: 2

Q = {E}

Page 17: CSC 331: Algorithm Analysis Paths in Graphs. The DFS algorithm we gave is recursive. DFS generates a search tree showing paths from one vertex to all

DD

CC

AA

BB

EE

2

1

2

3

Page 18: CSC 331: Algorithm Analysis Paths in Graphs. The DFS algorithm we gave is recursive. DFS generates a search tree showing paths from one vertex to all

procedure dijkstra(G, l, s)Input: Graph G=(V, E); positive edge lengths {le: ∈ E}; vertex s in VOutput: For all vertices u reachable from s, dist(u) is set to the distance from s to u

for all u in V:dist(u) = ∞prev(u) = nil

dist(s) = 0

H = makequeue(V) (using dist-values as keys)while H is not empty:

u = deletemin(H)for all edges (u,v) ∈ E:

if dist(v) > dist(u) + l(u, v):dist(v) = dist(u) + l(u, v)prev(v) = udecreaseKey(H, v)

Page 19: CSC 331: Algorithm Analysis Paths in Graphs. The DFS algorithm we gave is recursive. DFS generates a search tree showing paths from one vertex to all

What is the running time for Dijkstra’s?

O((|V| + |E|) log |V|)

If the priority queue is implemented using a binary heap.