Presentation is loading. Please wait.

Presentation is loading. Please wait.

Breadth First Search. Two standard ways to represent a graph –Adjacency lists, –Adjacency Matrix Applicable to directed and undirected graphs. Adjacency.

Similar presentations


Presentation on theme: "Breadth First Search. Two standard ways to represent a graph –Adjacency lists, –Adjacency Matrix Applicable to directed and undirected graphs. Adjacency."— Presentation transcript:

1 Breadth First Search

2 Two standard ways to represent a graph –Adjacency lists, –Adjacency Matrix Applicable to directed and undirected graphs. Adjacency lists Graph G(V, E) is represented by array Adj of |V| lists For each u  V, the adjacency list Adj[u] consists of all the vertices adjacent to u in G The amount of memory required is: (V + E) Representations of Graphs

3 A graph G(V, E) assuming the vertices are numbered 1, 2, 3, …, |V| in some arbitrary manner, then representation of G consists of: |V| × |V| matrix A = (a ij ) such that 1 if (i, j)  E 0 otherwise Preferred when graph is dense –|E| is close to |V| 2 a ij = { Adjacency Matrix

4 Adjacency matrix of undirected graph 1 45 2 3 1 45 2 4 2

5 The amount of memory required is T(V 2 ) For undirected graph to cut down needed memory only entries on and above diagonal are saved –In an undirected graph, (u, v) and (v, u) represents the same edge, adjacency matrix A of an undirected graph is its own transpose A = A T It can be adapted to represent weighted graphs. Adjacency Matrix

6 Breadth First Search

7 One of simplest algorithm searching graphs A vertex is discovered first time, encountered Let G (V, E) be a graph with source vertex s, BFS –discovers every vertex reachable from s. –gives distance from s to each reachable vertex –produces BF tree root with s to reachable vertices To keep track of progress, it colors each vertex –vertices start white, may later gray, then black –Adjacent to black vertices have been discovered –Gray vertices may have some adjacent white vertices Breadth First Search

8 It is assumed that input graph G (V, E) is represented using adjacency list. Additional structures maintained with each vertex v  V are –color[u] – stores color of each vertex –π[u] – stores predecessor of u –d[u] – stores distance from source s to vertex u Breadth First Search

9 BFS(G, s) 1 for each vertex u  V [G] – {s} 2 do color [u] ← WHITE 3 d [u] ← ∞ 4 π[u] ← NIL 5 color[s] ← GRAY 6 d [s] ← 0 7 π[s] ← NIL 8 Q ← Ø /* Q always contains the set of GRAY vertices */ 9 ENQUEUE (Q, s) 10 while Q ≠ Ø 11 do u ← DEQUEUE (Q) 12 for each v  Adj [u] 13 do if color [v] = WHITE /* For undiscovered vertex. */ 14 then color [v] ← GRAY 15 d [v] ← d [u] + 1 16 π[v] ← u 17 ENQUEUE(Q, v) 18 color [u] ← BLACK Breadth First Search

10       rstu vwxy  Q Except root node, s For each vertex u  V(G) color [u]  WHITE d[u]  ∞ π [s]  NIL

11   0      rstu vwxy s Q Breadth First Search Considering s as root node color[s]  GRAY d[s]  0 π [s]  NIL ENQUEUE (Q, s)

12 1  0 1     rstu vwxy Breadth First Search DEQUEUE s from Q Adj[s] = w, r color [w] = WHITE color [w] ← GRAY d [w] ← d [s] + 1 = 0 + 1 = 1 π[w] ← s ENQUEUE (Q, w) color [r] = WHITE color [r] ← GRAY d [r] ← d [s] + 1 = 0 + 1 = 1 π[r] ← s ENQUEUE (Q, r) color [s] ← BLACK w Q r

13 1  0 1 2 2   rstu vwxy Breadth First Search DEQUEUE w from Q Adj[w] = s, t, x color [s] ≠ WHITE color [t] = WHITE color [t] ← GRAY d [t] ← d [w] + 1 = 1 + 1 = 2 π[t] ← w ENQUEUE (Q, t) color [x] = WHITE color [x] ← GRAY d [x] ← d [w] + 1 = 1 + 1 = 2 π[x] ← w ENQUEUE (Q, x) color [w] ← BLACK r Q tx

14 1 2 0 1 2 2   rstu vwxy Breadth First Search DEQUEUE r from Q Adj[r] = s, v color [s] ≠ WHITE color [v] = WHITE color [v] ← GRAY d [v] ← d [r] + 1 = 1 + 1 = 2 π[v] ← r ENQUEUE (Q, v) color [r] ← BLACK Q txv

15 Breadth First Search DEQUEUE t from Q Adj[t] = u, w, x color [u] = WHITE color [u] ← GRAY d [u] ← d [t] + 1 = 2 + 1 = 3 π[u] ← t ENQUEUE (Q, u) color [w] ≠ WHITE color [x] ≠ WHITE color [t] ← BLACK 1 2 0 1 2 2 3  rstu vwxy Q xvu

16 Breadth First Search DEQUEUE x from Q Adj[x] = t, u, w, y color [t] ≠ WHITE color [u] ≠ WHITE color [w] ≠ WHITE color [y] = WHITE color [y] ← GRAY d [y] ← d [x] + 1 = 2 + 1 = 3 π[y] ← x ENQUEUE (Q, y) color [x] ← BLACK 1 2 0 1 2 2 3 3 rstu vwxy Q vuy

17 Breadth First Search DEQUEUE v from Q Adj[v] = r color [r] ≠ WHITE color [v] ← BLACK 1 2 0 1 2 2 3 3 rstu vwxy Q uy

18 Breadth First Search DEQUEUE u from Q Adj[u] = t, x, y color [t] ≠ WHITE color [x] ≠ WHITE color [y] ≠ WHITE color [u] ← BLACK 1 2 0 1 2 2 3 3 rstu vwxy Q y

19 Breadth First Search DEQUEUE y from Q Adj[y] = u, x color [u] ≠ WHITE color [x] ≠ WHITE color [y] ← BLACK 1 2 0 1 2 2 3 3 rstu vwxy Q 

20 Each vertex is enqueued and dequeued atmost once –Total time devoted to queue operation is O(V) The sum of lengths of all adjacency lists is  (E) –Total time spent in scanning adjacency lists is O(E) The overhead for initialization O(V) Total Running Time of BFS = O(V+E) Breadth First Search

21 The shortest-path-distance δ (s, v) from s to v as the minimum number of edges in any path from vertex s to vertex v. –if there is no path from s to v, then δ (s, v) = ∞ A path of length δ (s, v) from s to v is said to be a shortest path from s to v. Breadth First search finds the distance to each reachable vertex in the graph G (V, E) from a given source vertex s  V. The field d, for distance, of each vertex is used. Shortest Paths

22 BFS-Shortest-Paths (G, s) 1  v  V 2 d [v] ← ∞ 3 d [s] ← 0 4 ENQUEUE (Q, s) 5 while Q ≠ φ 6 do v ← DEQUEUE(Q) 7 for each w in Adj[v] 8 do if d [w] = ∞ 9 then d [w] ← d [v] +1 10 ENQUEUE (Q, w) Shortest Paths

23 Print Path PRINT-PATH (G, s, v) 1 if v = s 2 then print s 3 else if π[v] = NIL 4 then print “no path from s to v exists 5 else PRINT-PATH (G, s, π[v]) 6 print v


Download ppt "Breadth First Search. Two standard ways to represent a graph –Adjacency lists, –Adjacency Matrix Applicable to directed and undirected graphs. Adjacency."

Similar presentations


Ads by Google