Download presentation
Presentation is loading. Please wait.
Published byScarlett Pierce Modified over 9 years ago
1
CSC 331: Algorithm Analysis Paths in Graphs
2
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:
3
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 )
4
Does DFS give the shortest path from S to all vertices? E D S C A B S C B AD E DFS generates a search tree showing paths from one vertex to all other connected vertices.
5
The distance between two vertices is the length of the shortest path between them.
6
E D S C A B S C B ADE A convenient way to compute distances from s to other vertices is to proceed layer by layer.
7
procedure bfs( G, s ) Input: Graph G=(V, E) ; s ∈ V Output: 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 ) = 0 Q = [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
8
E D S C A B S C B ADE What is the overall running time of BFS? O(|V| + |E|)
9
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.
10
Suppose you are taking a train from San Francisco to Las Vegas, and want to find the quickest route. 3 5 3 2
11
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 l e are positive integers?
12
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.
13
D C A B E 4 2 13 2 1 5 4 3 A: 0D: ∞ B: 4E: ∞ C: 2 Q = {C, B}
14
D C A B E 4 2 13 2 1 5 4 3 A: 0D: 6 B: 3E: 7 C: 2 Q = {B, D, E}
15
D C A B E 4 2 13 2 1 5 4 3 A: 0D: 5 B: 3E: 6 C: 2 Q = {D, E}
16
D C A B E 4 2 13 2 1 5 4 3 A: 0D: 5 B: 3E: 6 C: 2 Q = {E}
17
D C A B E 2 1 2 3
18
procedure dijkstra(G, l, s) Input: Graph G=(V, E); positive edge lengths {l e : ∈ E}; vertex s in V Output: 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) = u decreaseKey(H, v)
19
What is the running time for Dijkstra’s? O((|V| + |E|) log |V|) If the priority queue is implemented using a binary heap.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.