Download presentation

Presentation is loading. Please wait.

Published byAnnis Owens Modified about 1 year ago

1
MCA 202: Discrete Structures Instructor Neelima Gupta

2
Table of Contents Graph Traversal Breadth First Search, DFS and their Applications Thanks: Meghna 22 and Mrityunjaya 23 (MCA 202)

3
Breadth First Search Tree (Undirected Graph) u1 u3 u2 u6 u4 u5 Thanks Nicky Bagaria (29), Prerna Mishra (30) MCA(2012) i Note: There are no forward and backward edges in an undirected BFS Tree. Red Edges: Tree Edge Green Edges: Cross Edge

4
Rooted Tree A tree is said to be a rooted tree if:- One of the vertices can be designated as root say r. For every (u,v) edge in the tree u=parent(v),v=child(u),parent(r)=r. A Breadth First Search Tree is also a rooted tree. Thanks Nicky Bagaria (29), Prerna Mishra (30) MCA(2012)

5
Tree Edge and Cross Edge Ancestor(u): Ancestor of a vertex ‘u’ is any vertex that lies above ‘u’ on the path between ‘u’ and the root(including the root). Descendant(u): Descendant of a vertex ‘u’ is any vertex that lies below ‘u’ on a path from ‘u’ down to a leaf. Tree Edge: (u,v) is a tree edge if ‘v’ is visited first time while scanning u’s neighbours. We set pr(v) = u and add v to child(u). Cross Edge: (u,v) is a cross edge if neither ‘v’ is a descendant of ‘u’ nor ‘u’ is a descendant of ‘v’. Thanks Nicky Bagaria (29), Prerna Mishra (30) MCA(2012)

6
Breadth First Search Tree (Directed Graph) u1 u3 u2 u6 u4 u5 Thanks Nicky Bagaria (29), Prerna Mishra (30) MCA(2012) i Red Edges are all Tree edges here. Green Edges are all Cross Edges. Black Edges are all Back Edges. Note: We can never have a forward edge in a BFS tree.

7
Back Edge and Forward Edge Back Edge: (u,v) is a Back Edge if ‘v’ is an ancestor of ‘u’. Forward Edge: (u,v) is a forward edge if ‘v’ is a descendant of ‘u’, but ‘v’ is not in child(u). Thanks Nicky Bagaria (29), Prerna Mishra (30) MCA(2012)

8
Complexity For undirected graph, very node is scanned twice. complexity = 2|E| = O(E) where, E – number of edges For directed graph, complexity = |E| If graph is disconnected, complexity = no. of vertices = V Therefore, Overall Complexity = O(max{V,E})

9
Implementation of BFS

10
Application of BFS Shortest Path Bipartite Graphs

11
Shortest Path Claim: Let T be a BFS tree with u as root. Let v be a node r hops away from u (i..e the length of the shortest path between u and v is r), then the BFS path between ‘u’ & ‘v’ is of length r. Proof: Let r = 1, then by the very definition of BFS, it will visit every node in its neighbourhood so path would be of distance 1. Suppose the claim holds for all nodes r hops away from u. Let ‘v’ be node (r+1) hops away from ‘u’, then Э a path (u=) u 0 – u 1 – u u r - u r+1 = v in G. Then Э a path (u=) u 0 – u’ 1 – u’ u’ r = u r of length r in T ……..by IH When u’ r = u r is visited in the BFS, we should visit v, if it has not already been visited and hence we get a path of length r+1 between u and v in T. If v was already visited, then let Let that path be (u=) u 0 – u” 1 – u” u” k-1 - u” k = v be the BFS path. Then k < r+1, since r+1 is the length of the shortest path between u and v in G. This implies that shortest path between u and u” k-1 is < r. Thus by IH, the BFS path namely (u=) u 0 – u” 1 – u” u” k-1 between u and u” k-1 is < r i.e. k – 1 < r i.e k < r + 1 Hence proved. Read from Cormen then Э a path (u=) u 0 – u 1 – u u r - u r+1 = v Thanks: Rakesh 31, Riya 32, Sanju 33, Saroj 34 (MCA 2012)

12
Bipartite Graphs A graph is said to be bipartite if its vertex set can be partitioned into two sets u, v and edge set E={(u, v):u U and v V }. Thanks: Rakesh 31, Riya 32, Sanju 33, Saroj 34 (MCA 2012)

13
Exercise Give a linear time algorithm to determine whether a graph is bipartite or not. Thanks: Rakesh 31, Riya 32, Sanju 33, Saroj 34 (MCA 2012)

14
Depth first search on an undirected graph Thanks: Rakesh 31, Riya 32, Sanju 33, Saroj 34 (MCA 2012)

15
Depth First Search Depth first search means to search deeper in the graph as much as possible. In the graph numbers representing start and finish times are from 1 to 2n, where n is the number of vertices. There are tree edges and back edges in depth first tree. There are no cross edges in depth first tree. Thanks: Rakesh 31, Riya 32, Sanju 33, Saroj 34 (MCA 2012)

16
u1u1 u2u2 u3u3 u4u4 u 10 u5u5 u7u7 u6u6 u 11 u 12 u8u8 u 13 u9u9 Thanks: Rakesh 31, Riya 32, Sanju 33, Saroj 34 (MCA 2012)

17
u1u1 u2u2 u3u3 u4u4 u 10 u5u5 u7u7 u6u6 u 11 u 12 u8u8 u 13 u9u9 (3, (4,(16, (8, (6,(22,(10, (20, (5, (9, (21, Thanks: Rakesh 31, Riya 32, Sanju 33, Saroj 34 (MCA 2012) (1, (2, 7) 11) 12) 13) 14) 15) 18) 17) 19) 23) 24) 25) 26)

18
u1u1 u2u2 u3u3 u4u4 u 10 u5u5 u7u7 u6u6 u 11 u 12 u8u8 u 13 u9u9 (1,26) (2,19) (3,18) (4,15)(16,17)(8,13)(6,7)(22,23)(10,11) (20,25) (5,14) (9,12) (21,24) Thanks: Rakesh 31, Riya 32, Sanju 33, Saroj 34 (MCA 2012) Back edge (u 4, u 1 )

19
Depth first search on a directed graph Thanks: Rakesh 31, Riya 32, Sanju 33, Saroj 34 (MCA 2012)

20
u1u1 u2u2 u3u3 u4u4 u 10 u5u5 u7u7 u6u6 u 11 u 12 u8u8 u 13 u9u9 Thanks: Rakesh 31, Riya 32, Sanju 33, Saroj 34 (MCA 2012)

21
u1u1 u9u9 u6u6 u5u5 u8u8 u 10 u 11 u 12 u 13 u2u2 u3u3 u4u4 u7u7 Thanks: Rakesh 31, Riya 32, Sanju 33, Saroj 34 (MCA 2012) (1, (2, (3, (4, (6, (9, (10,(12, (16, (17, (18, (21, (22, 5) 7) 8) 11) 13) 14) 15) 19) 20) 23) 24) 25) 26)

22
u1u1 u9u9 u6u6 u5u5 u8u8 u 10 u 11 u 12 u 13 u2u2 u3u3 u4u4 u7u7 Thanks: Rakesh 31, Riya 32, Sanju 33, Saroj 34 (MCA 2012) (1,26) (2,15) (3,8) (4,5) (6,7) (9,14) (10,11)(12,13) (16,25) (17,20) (18,19) (21,24) (22,23) Cross edges (u 6, u 4 ) and (u 12, u 7 ) Forward edge (u 1, u 4 ) Back edge (u 10, u 1 )

23
Time Complexity Time Complexity of DFS is <= 2E+V which is O(V+E) For a connected graph E >= V-1 Thus O(V+E)=O(E) Hence, Time Complexity=O(E) Thanks: Rakesh 31, Riya 32, Sanju 33, Saroj 34 (MCA 2012)

24
: Topological Sort: An Application of DFS

25
Considering a cyclic graph: Does there exist a labeling for this graph? u x w v THANKS Saumya Agarwal (Roll No 35) & Saurabh Garg(Roll No 36) (MCA 2012)

26
THANKS Saumya Agarwal (Roll No 35) & Saurabh Garg(Roll No 36) (MCA 2012)

27
Algorithm 1.Perform DFS on any vertex with in-degree 0 2.Label the vertices in decreasing order of finishing times THANKS Saumya Agarwal (Roll No 35) & Saurabh Garg(Roll No 36) (MCA 2012)

28
a fe gd cb Example (1,14) (2,13) (3,4) (5, (6, (7,8) 9) 10) (11, 12) THANKS Saumya Agarwal (Roll No 35) & Saurabh Garg(Roll No 36) (MCA 2012)

29
Applications Designing Parallel Algorithms Prioritizing tasks/commands x=a print x THANKS Saumya Agarwal (Roll No 35) & Saurabh Garg(Roll No 36) (MCA 2012)

30
Application of DFS STRONGLY CONNECTED COMPONENTS Steps to compute Strongly Connected Components of a graph :- 1.Perform DFS on the original graph. 2.Compute transpose of a graph G T i.e. reverse the direction of the edges. 3. Perform DFS on G T in decreasing order of finishing time f(u) of step 1. Thanks: Rakesh 31, Riya 32, Sanju 33, Saroj 34 (MCA 2012)

31
EXAMPLE :-

32
u2u2 u3u3 u4u4 u1u1 Thanks: Rakesh 31, Riya 32, Sanju 33, Saroj 34 (MCA 2012) (12, (1, (13, u5u5 u6u6 u7u7 u8u8 u9u9 u 10 (2, (3, (4, (5, (7,(6, (14, STEP 1:- 8)9) 10) 11) 15) 16) 17) 18) 19) 20)

33
u2u2 u3u3 u4u4 u1u1 Thanks: Rakesh 31, Riya 32, Sanju 33, Saroj 34 (MCA 2012) (12,17) (1,20) (13,16) u5u5 u6u6 u7u7 u8u8 u9u9 u 10 (2,19) (3,18) (4,11) (5,10) (7,8) (6,9) (14,15)

34
u2u2 u3u3 u4u4 u 1 Thanks: Rakesh 31, Riya 32, Sanju 33, Saroj 34 (MCA 2012) u5u5 u6u6 u7u7 u8u8 u9u9 u 10 STEP 2:- GTGT

35
u2u2 u3u3 u4u4 u 1 Thanks: Rakesh 31, Riya 32, Sanju 33, Saroj 34 (MCA 2012) u5u5 u6u6 u7u7 u8u8 u9u9 u 10 STEP 3:-

36
u2u2 u3u3 u4u4 u 1 Thanks: Rakesh 31, Riya 32, Sanju 33, Saroj 34 (MCA 2012) u5u5 u6u6 u7u7 u8u8 u9u9 u 10 STEP 3:-

37
The graph is divided into two strongly connected components :- { u 1, u 2,u 3, u 4, u 8,u 9,u 10 } and { u 5, u 6, u 7 } Thanks: Rakesh 31, Riya 32, Sanju 33, Saroj 34 (MCA 2012)

38
u2u2 u1u1 u3u3 u4u4 u5u5 u6u6 u7u7 u8u8 u9u9 u 10 Two strongly connected components

39
Implementation

40
Topological Sort from Saumya, Saurabh

Similar presentations

© 2016 SlidePlayer.com Inc.

All rights reserved.

Ads by Google