Download presentation
Presentation is loading. Please wait.
1
CS200: Algorithm Analysis
2
BREADTH-FIRST SEARCH May be applied to directed or undirected graphs.
Show example Directed Graph and trace algorithm filling in discovery (distances) times. (Find the distance of each vertex from a source vertex that is chosen arbitrarily). Omit colors (unnecessary). Use a FIFO queue with front/rear pointers so Enqueue/Dequeue can be done in constant time.
3
BFS(source) for each u in V - {source} do d[u] = infinity; d[source] = 0; Q = {source}; while not empty Q do dequeue u from Q; for each v in ADJ[u] do if d[v] = infinity then d[v] = d[u] + 1; enqueue v on Q;
4
Trace BFS on G using s as source
Trace BFS on G using s as source. Visit adjacent vertices in alphabetical order.
6
Breadth-First Search
7
Breadth-First Search
8
Breadth-First Search
9
Breadth-First Search
10
Breadth-First Search
11
Breadth-First Search
12
Breadth-First Search
13
Breadth-First Search
14
Breadth-First Search
15
Breadth-First Search
16
Breadth-First Search
17
Breadth-First Search
18
Breadth-First Search
19
Breadth-First Search
20
Breadth-First Search
21
Breadth-First Search
22
Breadth-First Search
23
Breadth-First Search
24
Breadth-First Search
25
Breadth-First Search
26
Breadth-First Search
27
Breadth-First Search
28
Breadth-First Search
29
Breadth-First Search
30
Breadth-First Search
31
Breadth-First Search
32
Breadth-First Search
33
Breadth-First Search
35
Idea: when processing vertices at distance i, add all vertices at distance i+1 to queue, these will be processed when done with all vertices at distance i. Greedily expand a frontier (propagate a wave 1-edge distance at a time). Proof of correctness is delayed until we do Dijkstra’s algorithm. Unlike DFS, BFS may not reach all vertices. Why? Runtime is O(V+E) using same argument as used for DFS.
36
INTRO. SHORTEST PATHS IN GRAPH ALGORITHMS
These algorithms are a generalization of BFS, to handle the notion of weighted edge graphs. Interested in directed graphs, G = (V,E), weight function w: E–>R (BFS –>w(e) = 1 for all e in E). Example of type of graph; a street map with distances on roads between intersections. A communication network with probability of a connection failure or bandwidth load.
37
Weight of a path p = v0–>v1–>...–>vk is
w(p) = S k w(vi-1,vi) i=1 Shortest path in graph = path with minimum weight. Big Note: There is an optimal substructure to the problem of finding a shortest path=> we can use either a greedy or dynamic programming algorithm.
38
Lemma 24.1: Showing optimal substructure
Subpaths of shortest paths are also shortest paths. Sketch of proof with drawing. Definition: z(u,v) = weight of shortest path from u to v.
39
But this is a contradiction: if the sub-path were not a shortest path then we could substitute a shorter sub-path to create a shorter total path.
40
Generalization of Lemma 24.3 - Triangle Inequality:
z(u,v) <= z(u,x) + z(x,v) Proof using drawing. Shortest path u –> v can be no longer than any other path (in particular, the path that takes the shortest path from u–>x and then the shortest path from x–>v). Well-defined: If a negative-weight cycle exists in a graph => some shortest-path may not exist because we can always get a shorter path by going around the cycle again.
42
Example with shortest paths from s, blue lines showing precedence relation
43
The above points are only highlights of shortest paths theory.
BELLMAN-FORD ALGORITHM This is most basic single-source shortest paths algorithm. 1. finds the shortest path weights from source s to all v in V. 2. we won’t worry about constructing actual path, but it can be done easily.
44
BF(G,s) for each v in V do d[v] = infinity d[s] = 0 for i = 1 to |V| -1 do {|V|-1 times} for each edge (u,v) in E do {E times} if d[v] > d[u]+w(u,v) then d[v] = d[u]+w(u,v) {relax. step} for each edge(u,v) in E do if d[v] > d[u]+w(u,v) then {rules out} no solution{neg. edge weight cycles}
45
Example of Bellman Ford
46
Example of Bellman Ford
47
Example of Bellman Ford
48
Example of Bellman Ford
49
Example of Bellman Ford
50
Example of Bellman Ford
51
Example of Bellman Ford
52
Example of Bellman Ford
53
Example of Bellman Ford
54
Example of Bellman Ford
55
Example of Bellman Ford
56
Example of Bellman Ford
57
Example of Bellman Ford
58
Example of Bellman Ford
59
Example of Bellman Ford
60
Example of Bellman Ford
61
Example of Bellman Ford
62
Example of Bellman Ford
63
Example of Bellman Ford
64
Example of Bellman Ford
65
Example of Bellman Ford
66
Each of the d[u] times will converge to the shortest path value z via the relaxation phase which is performed at most |V| -1 times for each edge in G. Final for loop tests to see if a solution is produced (no negative weight- edge cycles). Trace algorithm on example graph. Show trace in suggested order first then in lexical order. These traces show that a different ordering will cause convergence times to differ. Runtime of algorithm is O(VE).
67
Example from the text, do the relaxation in order (t,x), (t,y), (t,z), (x,t), (y,x) (y,z), (z,x), (z,s), (s,t), (s,y):
68
order (t,x), (t,y), (t,z), (x,t), (y,x) (y,z), (z,x), (z,s), (s,t), (s,y):
69
Correctness Proof
70
Correctness Proof cont.
71
Correctness Proof cont.
Terminates in |V|-1 passes because: If there are no negative weight cycles, every shortest path is simple (no cycles) and the longest simple path has |V|-1 edges => all d[v] converge in |V|-1 passes. If there exists a neg-weight edge cycle then d[v] fails to converge after |V|-1 passes.
72
DAG SHORTEST PATHS Informal discussion.
Common problem : find shortest paths in directed acyclic graph (DAG). How fast can this be done? Think about a topological sort. Think about Bellman-Ford when edges are processed in lexical order => if vertices are processed on each shortest path from left to right, how many passes are required to get d values to converge? ________________ Show an example.
74
If the vertices are topologically sorted and the vertices are processed in that order, each path is processed in a forward order (will never relax edges out of a vertex until all edges into a vertex have been relaxed => 1 pass. Runtime in this case is O(V+E).
75
DIJKSTRA’S ALGORITHM Shortest Paths algorithm that works for non- negative weight edge graphs. Works like Breadth- First search. Data structure used is a priority queue which is keyed by d[v] while BFS uses a FIFO queue.
76
Dijkstra(G,s) for each v in V do d[v] = infinity d[s] = 0 S = empty set Q = V {put all vertices of V in queue} while Q != empty do u = ExtractMin(Q) S = S union {u} for each v in ADJ[u] do if d[v] > d[u] + w[u,v] then d[v] := d[u] + w[u,v]
78
Show a simple example and trace algorithm.
Notice that the relaxation step updates the Q by decreasing the key of d[v]. Show a simple example and trace algorithm. Runtime analysis is based on different Q implementations. ExtractMin is executed |V| times. DecreaseKey is executed |E| times. Q ExtractMin DecreaseKey Total array O(V) O(1) O(EV) heap O(lgV) O(lgV) O(ElgV)
79
Correctness proof (following) shows that when we remove u from Q (and add to S), it has already converged. Theorem 24.10: Correctness Proof Prove that whenever u is added to S that d[u] = z(s,u). Show diagram, next slide Note: initially all d[v] >= z(s,v); proved previously
81
Proof by contradiction
Let u be first vertex picked st there exists a shorter path than d[u], i.e. d[u] > z(s,u): when we ExtractMin(Q) and receive u then d[u] > z(s,u). Let y be the first vertex in V-S on the actual shortest path from s to u => d[y] = z(s,y). This must be true because d[x] must be set correctly as y’s predecessor (x in S) on the shortest path (because u was the first choice where this was not true) and when x was placed in S, the edge x was relaxed giving d[y] the correct value.
82
d[u] > z(s,u) by Triangle Inequality z(s,u) = z(s,y) + z(y,u) z(s,u) = d[y] + z(y,u) then d[u] > d[y] but d[u] > d[y] => algorithm would have chosen y to process next not u (since we ExtractMin from Q). This is a contradiction to original assumption (different from contradiction derived in text).
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.