MCA 202: Discrete Structures Instructor Neelima Gupta

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Graph Algorithms
Advertisements

Graph Algorithms Algorithm Design and Analysis Victor AdamchikCS Spring 2014 Lecture 11Feb 07, 2014Carnegie Mellon University.
Comp 122, Spring 2004 Graph Algorithms – 2. graphs Lin / Devi Comp 122, Fall 2004 Identification of Edges Edge type for edge (u, v) can be identified.
Instructor Neelima Gupta Table of Contents Graph Algorithms Thanks to: Sunaina Kalucha (29) (MCS '11)
Introduction to Algorithms Second Edition by Cormen, Leiserson, Rivest & Stein Chapter 22.
MCA 520: Graph Theory Instructor Neelima Gupta
Theory of Computing Lecture 6 MAS 714 Hartmut Klauck.
Lecture 16: DFS, DAG, and Strongly Connected Components Shang-Hua Teng.
More Graphs COL 106 Slides from Naveen. Some Terminology for Graph Search A vertex is white if it is undiscovered A vertex is gray if it has been discovered.
Graphs – Depth First Search ORD DFW SFO LAX
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?
Graph Searching (Graph Traversal) Algorithm Design and Analysis Week 8 Bibliography: [CLRS] – chap 22.2 –
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?
Graph traversals / cutler1 Graph traversals Breadth first search Depth first search.
Tirgul 8 Graph algorithms: Strongly connected components.
Applications of graph traversals
Graphs-Part II Depth First Search (DFS). We Already Covered Breadth First Search(BFS) Traverses the graph one level at a time – Visit all outgoing edges.
16a-Graphs-More (More) Graphs Fonts: MTExtra:  (comment) Symbol:  Wingdings: Fonts: MTExtra:  (comment) Symbol:  Wingdings:
CS 473Lecture 151 CS473-Algorithms I Lecture 15 Graph Searching: Depth-First Search and Topological Sort.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 14 Strongly connected components Definition and motivation Algorithm Chapter 22.5.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Spring, 2001 Makeup Lecture Chapter 23: Graph Algorithms Depth-First SearchBreadth-First.
Graph Traversals Reading Material: Chapter 9. Graph Traversals Some applications require visiting every vertex in the graph exactly once. The application.
1 Data Structures DFS, Topological Sort Dana Shapira.
CSE 780 Algorithms Advanced Algorithms Graph Alg. DFS Topological sort.
Tirgul 11 BFS,DFS review Properties Use. Breadth-First-Search(BFS) The BFS algorithm executes a breadth search over the graph. The search starts at a.
CS344: Lecture 16 S. Muthu Muthukrishnan. Graph Navigation BFS: DFS: DFS numbering by start time or finish time. –tree, back, forward and cross edges.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 14 Strongly connected components Definition and motivation Algorithm Chapter 22.5.
Elementary graph algorithms Chapter 22
Tirgul 7 Review of graphs Graph algorithms: – BFS (next tirgul) – DFS – Properties of DFS – Topological sort.
Depth-First Search Idea: Keep going forward as long as there are unseen nodes to be visited. Backtrack when stuck. v G G G G is completely traversed.
David Luebke 1 10/1/2015 CS 332: Algorithms Topological Sort Minimum Spanning Tree.
Spring 2015 Lecture 10: Elementary Graph Algorithms
Discussion #32 1/13 Discussion #32 Properties and Applications of Depth-First Search Trees.
Graphs.
MCA 202: Discrete Structures Instructor Neelima Gupta
Lecture 11 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Jan Topological Order and SCC Edge classification Topological order Recognition of strongly connected components.
Topological Sort (an application of DFS) CSC263 Tutorial 9.
1 Chapter 22 Elementary Graph Algorithms. 2 Introduction G=(V, E) –V = vertex set –E = edge set Graph representation –Adjacency list –Adjacency matrix.
Elementary Graph Algorithms Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
 2004 SDU Lectrue4-Properties of DFS Properties of DFS Classification of edges Topological sort.
1 Chapter 22: Elementary Graph Algorithms II. 2 About this lecture Depth First Search DFS Tree and DFS Forest Properties of DFS Parenthesis theorem (very.
Chapter 22: Elementary Graph Algorithms
Trees Thm 2.1. (Cayley 1889) There are nn-2 different labeled trees
Properties and Applications of Depth-First Search Trees and Forests
CSE 421 Algorithms Richard Anderson Winter 2009 Lecture 5.
Strongly Connected Components for Directed Graphs Kelley Louie Credits: graphs by /demo/graphwin/graphwin.
 2004 SDU 1 Lecture5-Strongly Connected Components.
Graphs + Shortest Paths David Kauchak cs302 Spring 2013.
CSE 421 Algorithms Richard Anderson Autumn 2015 Lecture 5.
November 22, Algorithms and Data Structures Lecture XII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
MCA 520: Graph Theory Instructor Neelima Gupta
CSC317 1 At the same time: Breadth-first search tree: If node v is discovered after u then edge uv is added to the tree. We say that u is a predecessor.
Chapter 22: Elementary Graph Algorithms Overview: Definition of a graph Representation of graphs adjacency list matrix Elementary search algorithms breadth-first.
Graph Algorithms – 2. graphs Parenthesis Theorem Theorem 22.7 For all u, v, exactly one of the following holds: 1. d[u] < f [u] < d[v] < f [v] or.
Introduction to Algorithms
Breadth-First Search (BFS)
Chapter 22 Elementary Graph Algorithms
Graphs-Part II Depth First Search (DFS)
Main algorithm with recursion: We’ll have a function DFS that initializes, and then calls DFS-Visit, which is a recursive function and does the depth first.
Lecture 12 Graph Algorithms
Greedy Algorithms / Minimum Spanning Tree Yin Tat Lee
CSE 421: Introduction to Algorithms
Many slides here are based on E. Demaine , D. Luebke slides
Lecture 10 Algorithm Analysis
Advanced Algorithms Analysis and Design
Lectures on Graph Algorithms: searching, testing and sorting
CSE 421: Introduction to Algorithms
Richard Anderson Autumn 2016 Lecture 5
Graph Traversals Some applications require visiting every vertex in the graph exactly once. The application may require that vertices be visited in some.
Presentation transcript:

MCA 202: Discrete Structures Instructor Neelima Gupta

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

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

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)

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)

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.

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)

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})

Implementation of BFS

Application of BFS Shortest Path Bipartite Graphs

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)

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)

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)

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

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)

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)

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)

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 )

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

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)

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)

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 )

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)

: Topological Sort: An Application of DFS

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)

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

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)

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)

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

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)

EXAMPLE :-

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)

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)

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

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:-

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:-

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)

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

Implementation

Topological Sort from Saumya, Saurabh