820 likes | 840 Views
Explore how Breadth-First Search (BFS) algorithm can be applied to directed graphs. Trace the algorithm using a directed graph, showing distances and times of discovery.
E N D
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.
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;
Trace BFS on G using s as source. Visit adjacent vertices in alphabetical order.
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.
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.
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.
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.
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.
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.
Example with shortest paths from s, blue lines showing precedence relation
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.
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}