Download presentation

Presentation is loading. Please wait.

Published byBeatrice Black Modified over 2 years ago

1
Graph Traversals Visit vertices of a graph G to determine some property: Is G connected? Is there a path from vertex a to vertex b? Does G have a cycle? Will removing a single edge disconnect G? If G is directed, what are the strongly connected components? If G is the WWW, follow links to locate information. Most graph algorithms need to examine or process each vertex and each edge.

2
Breadth-First Search Find the shortest path from a source node s to all other nodes. It returns the distance of each vertex from s. a tree of shortest paths called the breadth-first tree. Idea: Find nodes at distance 0, then at distance 1, then at distance 2, etc.

3
A BFS Example s d b g f e c a L = s 0

4
s d b g f e c a Visualized as many simultaneous explorations starting from s and spreading out independently. L = s 0 L = a, c 1 frontier of exploration

5
s d b g f e c a L = s 0 L = a, c 1 L = d, e, f 2 Dashed edges were explored but had previously been discovered.

6
s d b g f e c a L = s 0 L = a, c 1 L = d, e, f 2 L = b, g 3

7
s d b g f e c a L = s 0 L = a, c 1 L = d, e, f 2 L = b, g 3 The Finish

8
s d b g f e c a Breadth-First Tree 0 1 1 2 2 2 3 3 d(b): shortest distance from s to b The number of visited vertices is one more than the number of tree edges and hence at most one more than the number of explored edges.

9
The BFS Algorithm BFS(G, s) for each v V(G) – s do d(v) // shortest distance from s d(s) 0 // initialization L s T i 0 while L ≠ do // all nodes at distance i from s L for each u L do for each v Adj(u) do if d(v) = then // not yet visited d(v) d(u) + 1 insert v into L T T {(u, v)} i i + 1 0 i i+1 i

10
Correctness Lemma For each i, the set L includes all nodes at distance i from s. i Proof By induction on the distance k from s. s … uv k Basis: L = s includes the only node at distance 0 from s. 0 Inductive step: Suppose L consists of all nodes at distance k ≥ 0 from s. Every node v at distance k+1 from s must be adjacent to a node u at distance k. By hypothesis u L. Thus v L. k k k+1 Corollary At the finish of BFS, d(v) is the length of the shortest path from s to v.

11
Running Time O(|E|) if G is represented using adjacency lists. The total number of edge scans is ≤ |E| for a directed graph ≤ 2 |E| for an undirected graph u v e u v e O(|V| ) if represented as an adjacency matrix. 2 Every vertex is inserted at most once into some list L. i The number of inserted vertices is at most one more than the number of scanned edges; hence ≤ |E| + 1.

12
Depth-First Search Idea: Keep going forward as long as there are unseen nodes to be visited. Backtrack when stuck. v G G G 1 2 3 G is completely traversed before exploring G and G. 1 23 From Computer Algorithms by S. Baase and A. van Gelder

13
The DFS Algorithm DFS(G) time 0 // global variable for each v V(G) do disc(v) unseen for each v V(G) do if disc(v) = unseen then DFS-visit(v) time time + 1 disc(v) time for each u Adj(v) do if disc(u) = unseen then DFS-visit(u)

14
A DFS Example a l g f b ced j k ih time = 1

15
a l g f b ced j k ih 1 2

16
a l g f b c e d j k ih 1 2 3 4 5

17
a l g f b ced j k ih 1 2 3 4 5 6 7

18
a l g f b ced j k ih 1 2 3 4 5 6 7 8 9

19
a l g f b ced j k ih 1 2 3 4 5 6 7 8 9 10 11 12

Similar presentations

Presentation is loading. Please wait....

OK

Chapter 22: Elementary Graph Algorithms

Chapter 22: Elementary Graph Algorithms

© 2018 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Options call put ppt on loop Ppt on low level language examples Ppt on nepal earthquake Ppt on money and credit download Ppt on power grid failure 1965 Ppt on iis web server Ppt on column chromatography setup Ppt on intelligent manufacturing systems Download ppt on oxidation and reduction reactions Ppt on french revolution class 9