Graph Algorithms. Vertex set = { a,b,c,d } Edge set = { {a,c}, {b,c}, {c,d}, {b,d}} Figure 1: An example for a simple graph

Similar documents
Mid-Term Examination - Spring 2014 Mathematical Programming with Applications to Economics Total Score: 45; Time: 3 hours

CIT 596 Theory of Computation 1. Graphs and Digraphs

Counting Paths Between Vertices. Isomorphism of Graphs. Isomorphism of Graphs. Isomorphism of Graphs. Isomorphism of Graphs. Isomorphism of Graphs

Solutions for HW9. Bipartite: put the red vertices in V 1 and the black in V 2. Not bipartite!

Graph Theory. Simple Graph G = (V, E). V={a,b,c,d,e,f,g,h,k} E={(a,b),(a,g),( a,h),(a,k),(b,c),(b,k),...,(h,k)}

18.06 Problem Set 4 Due Wednesday, Oct. 11, 2006 at 4:00 p.m. in 2-106

CS 491G Combinatorial Optimization Lecture Notes

Connectivity in Graphs. CS311H: Discrete Mathematics. Graph Theory II. Example. Paths. Connectedness. Example

Lecture 6: Coding theory

Logic, Set Theory and Computability [M. Coppenbarger]

CSE 332. Sorting. Data Abstractions. CSE 332: Data Abstractions. QuickSort Cutoff 1. Where We Are 2. Bounding The MAXIMUM Problem 4

Global alignment. Genome Rearrangements Finding preserved genes. Lecture 18

22: Union Find. CS 473u - Algorithms - Spring April 14, We want to maintain a collection of sets, under the operations of:

The vertex leafage of chordal graphs

Lecture 3. XML Into RDBMS. XML and Databases. Memory Representations. Memory Representations. Traversals and Pre/Post-Encoding. Memory Representations

On a Class of Planar Graphs with Straight-Line Grid Drawings on Linear Area

I 3 2 = I I 4 = 2A

1 PYTHAGORAS THEOREM 1. Given a right angled triangle, the square of the hypotenuse is equal to the sum of the squares of the other two sides.

Lecture 2: Cayley Graphs

Algorithms & Data Structures Homework 8 HS 18 Exercise Class (Room & TA): Submitted by: Peer Feedback by: Points:

arxiv: v2 [math.co] 31 Oct 2016

Necessary and sucient conditions for some two. Abstract. Further we show that the necessary conditions for the existence of an OD(44 s 1 s 2 )

V={A,B,C,D,E} E={ (A,D),(A,E),(B,D), (B,E),(C,D),(C,E)}

Solutions to Problem Set #1

CS 2204 DIGITAL LOGIC & STATE MACHINE DESIGN SPRING 2014

CS 573 Automata Theory and Formal Languages

XML and Databases. Exam Preperation Discuss Answers to last year s exam. Sebastian Maneth NICTA and UNSW

Lecture 4: Graph Theory and the Four-Color Theorem

Chapter 4 State-Space Planning

Part 4. Integration (with Proofs)

Maximum size of a minimum watching system and the graphs achieving the bound

V={A,B,C,D,E} E={ (A,D),(A,E),(B,D), (B,E),(C,D),(C,E)}

Common intervals of genomes. Mathieu Raffinot CNRS LIAFA

Surds and Indices. Surds and Indices. Curriculum Ready ACMNA: 233,

CS311 Computational Structures Regular Languages and Regular Grammars. Lecture 6

Implication Graphs and Logic Testing

POSITIVE IMPLICATIVE AND ASSOCIATIVE FILTERS OF LATTICE IMPLICATION ALGEBRAS

Vidyalankar S.E. Sem. III [CMPN] Discrete Structures Prelim Question Paper Solution

Lecture 8: Abstract Algebra

The master ring problem

Finite State Automata and Determinisation

Laboratory for Foundations of Computer Science. An Unfolding Approach. University of Edinburgh. Model Checking. Javier Esparza

Monochromatic Plane Matchings in Bicolored Point Set

Welcome. Balanced search trees. Balanced Search Trees. Inge Li Gørtz

CS261: A Second Course in Algorithms Lecture #5: Minimum-Cost Bipartite Matching

Total score: /100 points

Compression of Palindromes and Regularity.

CSC2542 State-Space Planning

Section 2.3. Matrix Inverses

Bisimulation, Games & Hennessy Milner logic

Discrete Structures, Test 2 Monday, March 28, 2016 SOLUTIONS, VERSION α

F / x everywhere in some domain containing R. Then, + ). (10.4.1)

Factorising FACTORISING.

XML and Databases. Outline. 1. Top-Down Evaluation of Simple Paths. 1. Top-Down Evaluation of Simple Paths. 1. Top-Down Evaluation of Simple Paths

Data Structures LECTURE 10. Huffman coding. Example. Coding: problem definition

The DOACROSS statement

On the Spectra of Bipartite Directed Subgraphs of K 4

CS 360 Exam 2 Fall 2014 Name

arxiv: v1 [cs.cg] 28 Apr 2009

Separable discrete functions: recognition and sufficient conditions

(a) A partition P of [a, b] is a finite subset of [a, b] containing a and b. If Q is another partition and P Q, then Q is a refinement of P.

Lecture 11 Binary Decision Diagrams (BDDs)

NON-DETERMINISTIC FSA

Linear choosability of graphs

Technische Universität München Winter term 2009/10 I7 Prof. J. Esparza / J. Křetínský / M. Luttenberger 11. Februar Solution

Eigenvectors and Eigenvalues

Lesson 2.1 Inductive Reasoning

Slope Lengths for 2-Bridge Parent Manifolds. Martin D. Bobb

Algorithm Design and Analysis

Sections 5.3: Antiderivatives and the Fundamental Theorem of Calculus Theory:

Algebra 2 Semester 1 Practice Final

First Midterm Examination

Outline Data Structures and Algorithms. Data compression. Data compression. Lossy vs. Lossless. Data Compression

Particle Physics. Michaelmas Term 2011 Prof Mark Thomson. Handout 3 : Interaction by Particle Exchange and QED. Recap

INTEGRATION. 1 Integrals of Complex Valued functions of a REAL variable

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

Lecture 1 - Introduction and Basic Facts about PDEs

6. Suppose lim = constant> 0. Which of the following does not hold?

= state, a = reading and q j

Exercise sheet 6: Solutions

for all x in [a,b], then the area of the region bounded by the graphs of f and g and the vertical lines x = a and x = b is b [ ( ) ( )] A= f x g x dx

Logic Synthesis and Verification

COMPUTING THE QUARTET DISTANCE BETWEEN EVOLUTIONARY TREES OF BOUNDED DEGREE

Computing all-terminal reliability of stochastic networks with Binary Decision Diagrams

Introduction to Graphical Models

Intermediate Math Circles Wednesday 17 October 2012 Geometry II: Side Lengths

On the existence of a cherry-picking sequence

Introduction to Olympiad Inequalities

Algorithm Design and Analysis

SOME COPLANAR POINTS IN TETRAHEDRON

McCreight s Suffix Tree Construction Algorithm. Milko Izamski B.Sc. Informatics Instructor: Barbara König

, g. Exercise 1. Generator polynomials of a convolutional code, given in binary form, are g. Solution 1.

Math 225B: Differential Geometry, Homework 6

QUADRATIC EQUATION. Contents

Green s Theorem. (2x e y ) da. (2x e y ) dx dy. x 2 xe y. (1 e y ) dy. y=1. = y e y. y=0. = 2 e

Graph States EPIT Mehdi Mhalla (Calgary, Canada) Simon Perdrix (Grenoble, France)

Graph width-parameters and algorithms

Lesson 2.1 Inductive Reasoning

] dx (3) = [15x] 2 0

A Primer on Continuous-time Economic Dynamics

Transcription:

Inin Institute of Informtion Tehnology Design n Mnufturing, Knheepurm, Chenni 00, Ini An Autonomous Institute uner MHRD, Govt of Ini http://www.iiitm..in COM 0T Design n Anlysis of Algorithms -Leture Notes Instrutor N.Sgopn Sribe: Dhnlkshmi S Grph Algorithms Objetive: In this moule, we shll introue grphs whih re powerful moel for moelling ombintoril problems in omputing. We lso isuss grph trversls, nmely, Breth First Serh (BFS) n Depth First Serh (DFS) n their pplitions. Preliminries Definition (Grph) A grph G is pir G = (V (G), E(G)) onsisting of finite set V (G) n set E(G) of two-element subsets of V (G). The elements of V (G) re lle verties of G. An element e = {, b} of E(G) is lle n ege of G with en verties n b. Vertex set = {,b,, } Ege set = { {,}, {b,}, {,}, {b,}} b Figure : An exmple for simple grph Note tht, grph is non-liner t struture. A grph is si to be simple if it hs no multiple eges between ny two verties n no self loops (n ege from vertex to itself). Throughout this leture, we will look t only finite n simple grphs. Definition (Subgrph) Let G be finite simple grph. A grph H is si to be subgrph of G if V (H) V (G) n E(H) E(G). The grph H is si to be n inue subgrph of G if V (H) V (G) n for every pir of verties u n v, {u, v} E(H) if n only if {u, v} E(G). e e e f f f b Grph G Subgrph of G Inue Subgrph of G Figure : A grph G, sub grph of G on the vertex set {,,, e, f} n n inue grph on the vertex set {,,, e, f}

x y z b Figure : An exmple for onnete grph Definition (Connete Grph) Two verties u n v of grph G re si to be onnete if there exists pth from u to v in G. A grph G is si to be onnete if every pir of its verties re onnete. Definition (Disonnete Grph) A grph G whih is not onnete is si to be isonnete. b Figure : An exmple for isonnete grph. The grph hs two onnete omponents, one is grph inue on the vertex set {, b, } n the other is {} Definition (Neighborhoo of vertex) The Neighborhoo of vertex v in grph G is the set N(u) onsisting of ll verties u whih re jent to v. For exmple, the neighborhoo of the vertex in Fig.. is N() = {, b}. Definition (Ayli Grph) A grph tht ontins no yles is lle yli grph. Definition (Tree) A onnete yli grph is lle tree. It is spnning tree of grph G if it spns G (tht is, it inlues every vertex of G) n is sub-grph of G (every ege in the tree belongs to G). In tree, we n fin only one pth for every pir of its verties. Tree is non-liner t struture. Definition (Biprtite Grph) A grph G is lle bigrph or biprtite grph if V n be prtitione into two isjoint subsets V n V suh tht every line of G joins point of V to point of V. (V, V ) is lle biprtition of G.. Breth First Serh(BFS) Algorithm

b e b e g g f f Grph G Tree T Figure : An exmple for grph n its orresponing spnning tree Algorithm BFS Spnning tree lgorithm(g) Input: A Grph G = (V, E) Output: Spnning Tree T of grph G Step : Let i = 0. Step : Strt with ny vertex v in G. T initilly ontins v t level 0; i = i +. Mrk v s visite. Step : Fin the neighbors of v n them in the next level (level ) in T. Mrk them s visite. Step : Fin the unvisite neighbors for eh vertex in level i n it in level i +. Mrk the explore verties. Step : Inrement i. Repet step until there re no neighbors to visit. Grph G BFS Tree T 0 0 Figure : An exmple for the onstrution of BFS tree Time omplexity: O(n) effort is spent in initilizing the boolen rry to keep trk of whih noe is visite/unvisite. As prt of BFS proeure, eh ege is visite t most one, therefore, the totl time is O(n + m), where n verties n m eges of grph G. E(G) enotes the eges in grph G, E(T ) enotes the eges in the BFS tree T of G. The set E n = E(G)\E(T ) enotes the set of non-tree eges i.e., the eges whih re in the grph G but not in the tree T. Definition A non-tree (missing) ege, {u, v} E(G)\E(T ) is si to be :

Cross ege if u L i n v L i (i.e., both the verties re in sme level). Let E enotes the set of ll ross eges in T. Slnting ege if u L i n v L j, j = i + or j = i (i.e., both the verties re in jent levels). Let E s enotes the set of ll ross eges in T. Remrk: Note tht, j nnot be greter thn i + or less thn i... Applitions of BFS Test for Conneteness: Problem: Given grph G, fin whether the given grph is onnete or not? Solution using BFS: Cll BFS lgorithm one, if V (G) = V (T ), then G is onnete n if V (G) V (T ), then G is isonnete, where T is the BFS tree onstrute in the first ll to BFS lgorithm. i.e., if number of lls to BFS is greter thn one, then G is isonnete n the number of lls to BFS gives the number of isonnete omponents. Test for yliity: Problem : Given onnete grph G, fin whether G ontins yle or not? Solution using BFS: Run BFS(G). If E n =, then G is yli. Otherwise G ontins t lest one yle. Problem : Given grph G, fin whether G ontins yle or not? Solution using BFS: Run BFS for eh onnete omponent of G n hek if E n = for ll suh omponents, if so, then G is yli. Otherwise G ontins t lest one yle. Problem : Given grph G, fin whether G is tree or not? Solution using BFS: Do test for onneteness n test for yliity. If G is onnete n yli, then G is tree. Test for existene of C : Problem: Given grph G, fin whether G ontins C or not? Solution using BFS: Run BFS(G) n ollet ll non-tree eges, E n. For ll, e = {u, v} E n, hek whether the shortest pth between u n v in G = G\e is two (i.e., hek for ommon neighbor in the sme level of u or in the jent levels of u), if so, G ontins C. This hek n be one in O(m) O(n + m) time, s for every non-tree ege (O(m)), onstrution of BFS for shortest pth omputtion tkes O(n + m) time. Approh : For every ege {u, v} E(G), hek whether N G (u) N G (v) or not. If it is empty, then G oes not ontin C. Otherwise, G hs t lest one C. Time omplexity: In jeny mtrix of G, o Boolen AND for the two rows orresponing to the en verties of ross ege n sn for the existene of in tht, this tkes O(n) time n we o this for ll eges in G, O(m). Thus, this lgorithm tkes O(nm) time. Test for existene of o yles: Problem: Given grph G, fin whether G ontins n o yle, yle of length k +, k, or not? Solution using BFS: The existene of ross eges in T (E ) implies the existene of o yles in G. Let e = {u, v} be n ross ege in T n let x be the ommon prent of u n v. It is ler tht, the length of P xu (Pth from x to u in T ) is equl to the length of P xv. Thus, P xu n P xv forms n o yle together with e. The onverse of this sttement: The existene of o yles in G implies the existene of ross eges in T (E ) is lso true.

Test for existene of C : Problem: Given grph G, fin whether G ontins C or not? Solution using BFS: Run BFS(G) n let E n enote the set of non-tree eges. For eh e = {u, v} E n, onstrut two sets A = N G (u)\n G (v) n B = N G (v)\n G (u), where G = G\e. Now, for every element x in A, hek whether it hs neighbor y in B or not. If it hs neighbor then the set {u, x, y, v} form C. Time omplexity: Number of non-tree eges is O(m), number of elements in A is O(n) n in B is O(n). Thus, this pproh tkes O(mn ) time. Existene of even yles through slnting eges: Problem: Given grph G, fin whether G ontins n even yle, yle of length k +, k, or not? Solution using BFS: The existene of slnting eges in T (E s ) implies the existene of even yles in G. Let e = {u, v} be slnting ege in T n let x be the ommon prent of u n v. It is ler tht, the length of P xu (Pth from x to u in T ) is equl to the length of P xv + or the length of P xv (Pth from x to u in T ) is equl to the length of P xu +. Thus, P xu n P xv forms n even yle together with e. The onverse of this sttement if flse. i.e., onsier omplete grph on four verties, onsier the BFS tree with respet to vertex, in the tree there is n even yle using four ross eges. Test for Biprtiteness: Problem: Given grph G, fin whether G is biprtite grph or not? Trivil Algorithm: Prtition the vertex set V of G into two sets with ifferent ombintions n hek for the biprtiteness for eh ombintion. Time omplexity of this lgorithm = n+nc +...+nc n/ = O( n ). Algorithm using BFS: Mrk the non-tree (missing) eges using otte lines in the BFS tree T. Deompose E n into E n E s. We know tht, grph is biprtite if n only if it is o yle free. By using the ft in test for o yles: we n onlue tht, if E =, then G is biprtite. Test for -olorbility: Problem: Given grph G, hek whether n we olor the verties of grph G using two olors suh tht no two jent verties hve the sme olor. Solution using BFS: Testing whether grph is -olorble or not is equivlent to testing whether grph is biprtite or not. Shortest pth omputtion: Problem: Given grph G n two verties u n v, fin the shortest pth between u n v. Solution using BFS: Run BFS(G) by hving strting vertex s u. Sine, T=BFS(G) is tree, there exist only one pth from u to v n tht pth is the shortest pth from u to v. This tkes O(n + m) time. All pirs Shortest pth problem: Problem: Given grph G, fin the shortest pth between ll pirs of verties in G. Solution using BFS: For every vertex v V (G), Run BFS(G) by hving strting vertex s v. For eh BFS tree T, print the pth from v to x for ll x in T. This tkes O(n(n + m)) time. Remrks: If E = then G is biprtite.

If E then G is not biprtite. If E = n E s = then the given grph G is tree (biprtite). The exmple given below hs n even yle, however, E s =.. Depth First Serh(DFS) Algorithm Algorithm DFS Spnning tree lgorithm(g) Input: A Grph G = (V, E) Output: Spnning Tree T of grph G Step : Let i = 0. Step : Strt with ny vertex v in G. A v in level i of T ; i = i +. Mrk v s visite. Step : Fin neighbor of v n it in the next level of the tree T. Step : For vertex v in level i, fin unvisite neighbor w n w in level i +. Mrk w s visite. Step : When there is no w to visit, bktrk to v n explore the other neighbors reursively.

Grph G DFS Tree T 0 0 Figure : An exmple for the onstrution of DFS Time omplexity: Sine eh ege is visite t most twie: one uring DFS ll n the other visit is uring bk trking, the effort is O(n + m), where n represents the number of verties n m represents the number of eges of grph G. Note: Here the non-tree eges re lle s bk eges... Applitions of DFS Test for Conneteness: Problem: Given grph G, fin whether the given grph is onnete or not? Solution using DFS: Cll DFS lgorithm one, if V (G) = V (T ), then G is onnete n if V (G) V (T ), then G is isonnete, where T is the DFS tree onstrute in the first ll for DFS lgorithm. i.e., if number of lls to DFS is greter thn one, then G is isonnete. Test for yliity: Problem : Given onnete grph G, fin whether G ontins yle or not? Solution using DFS: Run DFS(G). If there is no bk ege, then G is yli. Otherwise G ontins t lest one yle. Problem : Given grph G, fin whether G ontins yle or not? Solution using DFS: Run DFS for eh onnete omponent of G n hek if the number of bk eges is equl to zero for ll suh omponents, if so, then G is yli. Otherwise G ontins t lest one yle. Problem : Given grph G, fin whether G is tree or not? Solution using DFS: Do test for onneteness n test for yliity. If G is onnete n yli, then G is tree.

Determine the number of onnete omponents: Problem: Given grph G, fin the number of onnete omponents in G. Solution using DFS: Run DFS until V (G) = V (T ). The number of lls to DFS etermines the number of onnete omponents in G. Definition 0 (Artiultion Point/Critil noe) Let G be onnete grph. A vertex v V (G) is si to be n rtiultion point if the removl of the vertex v from G results in isonnete grph. Definition (Brige/Critil link) Let G be onnete grph. brige if removl of the ege e from G results in isonnete grph. A ege e E(G) is si to be 0 Figure : Artiultion Points: vertex, vertex, vertex, vertex n vertex ; Briges: {,}, {,}, {,0}, {,} n {,}. Note: - If network oes not ontin brige n n rtiultion point then it is onsiere goo network s it n tolerte single noe or single ege filure. - If G is -onnete then it n tolerte single noe filures but not -noe filures - It is not neessry tht existene of rtiultion point implies the existene of briges. - In ny brige, tlest one en-point is n rtiultion point. - The upper boun for the number of rtiultion points in onnete grph G with n verties is n. - The upper boun for the number of briges in onnete grph G with n verties is n. Test for existene of n rtiultion point: Problem: Given grph G, fin the rtiultion points in G. Approh : For every vertex v V (G), run DF S(G = G\{v}), if the number of onnete omponents is greter thn one, then the vertex v is n rtiultion point. This pproh tkes O(n(n + m)) = O(nm) time.

Approh : The vertex w in DFS tree T is si to be n rtiultion point if there is no bk ege from the esennt verties of w to the nestor verties of w. The root noe of T is n rtiultion point if egree of the root noe in DFS tree is greter thn or equl to two. This n be one using the following lgorithm: Algorithm To ompute Artiultion point Input: DFS tree T of Grph G = (V, E) n vertex u. Output: Whether the vertex u is n rtiultion point or not. Step : Visit the verties of T in post-orer trversl n ompute L(u) = min{fn(u), min{l(w) w is hil of u}, min{fn(w) (u, w) is bk ege}} Step : If u is root in T with egree then u is n rtiultion point. Step :If u is not root in T then u is n rtiultion point iff u hs hil w suh tht L(w) fn(u) Remrk: Wht oes for vertex v, for ll hil w i, L(w i ) fn(v) men? It mens tht, there oes not exist bk ege from ny esennt of v to ny nestor of v. Moreover, to hek whether vertex v is n rtiultion point or not, it is enough to hek whether there exist hil for v whose esennts o not hve bk ege to ny nestor of v. Time Complexity to list ll APs: O(n + m) [ O(n + m) for DFS Tree + O(n) for post orer trversl + O(n) for heking whether it is A.P ] Exmple : Grph G DFS Tree Vertex fn 0 0 0 0 L - Vlues for the tree T: L() = min{,, } = L() = min{0,, } = 0 L() = min{, 0, } = L(0) = min{,, } =

L() = min{,, } = L() = min{, min{, }, } = L() = min{,, } = L() = min{,, } = L() = min{,, } = L() = min{,, } = L() = min{,, } = L() = min{, min{,, }, } = By Algorithm, for the exmple given, rtiultion points re,,. Query : v is n rtiultion point if there exist t lest one hil of v, sy w, suh tht L(v) L(w)? Counter exmple: In the exmple, given bove, for the vertex 0, there exist hil suh tht L() = L(0) =, but 0 is not n rtiultion point. Query : v is n rtiultion point if there exist t lest one hil of v, sy w, suh tht fn(v) L(w)? Counter exmple: In the exmple, given bove, for vertex, there exist hil 0 suh tht fn() = > L(0) =, but is not n rtiultion point. Test for the existene of brige: Problem: Given grph G, fin the briges in G. Approh : For every ege e E(G), run DF S(G = G\e), if the number of onnete omponents is greter thn one, then the ege e is brige. This pproh tkes O(m(n+m)) = O(m ) time. Approh : The ege {u, v} in DFS tree is si to be brige if there is no bk ege from the esennt verties of v to u or to the nestor verties of u. This n be one using the following lgorithm: By Algorithm, for the exmple given, the briges re {,}, {,}, {,}, {,}. Algorithm To ompute Briges Input: DFS tree T of Grph G=(V,E) n n ege (u, v). Output: Whether the ege (u, v) is brige or not. Step : Visit T in post-orer trversl n ompute, L(u) = min{fn(u), min{l(w) w, is hil ofu}, min{fn(w) (u, w)is bk ege}} Step : (u, v) is brige if fn(u) < fn(v) n L(v) > fn(u). Query : the ege {u, v} is brige if (i) {u, v} is tree ege (ii) f n(u) < f n(v) n (iii) L(v) = fn(v)? 0

Sine the bove onition respets the efinition of brige, n in prtiulr, onition (iii) ensures there is no bk ege from ny esennt of v to u or the nestor of u, the bove hek inee works. Query : the ege {u, v} is brige if there exist hil w for v suh tht L(w) L(v)? Counter exmple: Consier the Exmple : Tke n ege {0, }, there exist hil for suh tht L() = 0 > L() =, but {0, } is not brige. Definition (Bionnete Components) A mximl onnete omponents of grph G without ny rtiultion point. Grph G Bionnete omponents of G Determine the bionnete omponents: Problem: Given grph G, fin ll the bionnete omponents of the grph G. Algorithm Bionnete Components Input: Grph G = (V, E) Output: Bionnete Components. Step : Ientify ny one rtiultion point. Step : Remove tht point from the grph. We will get olletion of onnete grphs G Step : A bk the rtiultion point to ll the onnete omponents. Step : The onnete omponents whih hs no rtiultion point re bionnete. For the omponents whih hs rtiultion point, repet the bove proess.. Strongly Connete Components in irete grph G A strongly onnete omponent C in irete grph G hs the property tht for ll u, v in C, there exists u v irete pth n v u irete pth. Problem: Given irete grph G, fin ll of its strongly onnete omponents.

Algorithm Strongly Connete Components (SCC) Input: Direte Grph G Output: Strongly Connete Components of G. Step : Run DFS(G) n ompute the finish time for ll verties. Step : Fin G T n sort the vertex set of G in eresing orer bse on its finish time. Step : Run DF S(G T ) from the vertex whih hs mximum finish time. Do this step repetely until ll the verties in G T re visite t lest one (this gives you the olletion of SCC). Step : Eh forest in DF S(G T ) is SCC. (,0) Grph G 0 (,) (,) (,) (,) (,0) (,) (,) DFS(G) (,) The seon o-orinte of the re olor orere pir enotes the finish time of the orresponing vertex n the first o-orinte enotes the fs number 0 (,) Grph G^T 0 Step : Run DFS from the vertex, whih hs the high finish time. No further vertex to visit. Thus, {} is strongly onnete omponent Step : Run DFS from the vertex, whih hs the next high finish time. The grph inue on the vertex set {,,} forms strongly onnete omponent Step : Run DFS from the vertex, whih hs the next high finish time. The grph inue on the vertex set {,,,0} forms strongly onnete omponent 0 Step : Run DFS from the vertex, whih hs the next high finish time. No further vertex to visit. Thus, {} is strongly onnete omponent Step : Run DFS from the vertex, whih hs the next high finish time. No further vertex to visit. Thus, {} is strongly onnete omponent

. Topologil Sorting Problem: How long oes it tke to omplete B.Teh progrmme? Desription: Every B.Teh stuent in IIITD&M hs to omplete the set of ourses in the urriulum to get their egree ertifite. If you re given hne to o s mny ourses s possible in semester with onstrint: some ourses hs pre-requisite ourse, whih hs to be omplete in the previous semesters, wht is the minimum number of semesters to omplete ll ourses? (The question mximum is invli beuse one n o one ourse in semester to reh the mximum number) Strtegy : Construt pre-requisite grph on ourses. i.e., onstrut grph with verties (eh vertex orrespons to ourse) n n irete ege (u, v) E(G) if the ourse u is the pre-requisite for the ourse v. Now, remove the verties of in-egree zero n the orresponing ourses in semester. Repet this proess until you hve visite ll the verties in the grph G, to get the minimum number of semesters. An exmple is illustrte below: Verties with in-egree zero = {} Semester = {} Verties with in-egree zero = {,} Semester = {,} A pre-requisite grph G for eight ourses Verties with in-egree zero = {,} Semester = {,} Verties with in-egree zero = {} Semester = {} Verties with in-egree zero = {} Semester = {} Verties with in-egree zero = {} Semester = {} The minimum number of require semesters is Strtegy : Run DFS n ompute the height for eh forest in the DFS. Pik the mximum height. This strtegy fils beuse of the following ounter exmple:

DFS(G) A pre-requisite grph G for eight ourses Figure : Course hs to be one before the ourse, whih is ontrition s ourse is pre-requisite ourse for ourse Aknowlegements: Leture ontents presente in this moule n subsequent moules re bse on the following text books n most importntly, uthor hs gretly lernt from letures by lgorithm exponents ffilite to IIT Mrs/IMS; Prof C. Pnu Rngn, Prof N.S.Nrynswmy, Prof Venktesh Rmn, n Prof Anurg Mittl. Author sinerely knowleges ll of them. Speil thnks to Tehing Assistnts Mr.Renjith.P n Ms.Dhnlkshmi.S for their sinere n eite effort n mking this sribe possible. Author hs benefite lot by tehing this ourse to senior unergrute stuents n junior unergrute stuents who hve lso ontribute to this sribe in mny wys. Author sinerely thnk ll of them. Referenes:. E.Horowitz, S.Shni, S.Rjsekrn, Funmentls of Computer Algorithms, Glgoti Publitions.. T.H. Cormen, C.E. Leiserson, R.L.Rivest, C.Stein, Introution to Algorithms, PHI.. Sr Bse, A.V.Geler, Computer Algorithms, Person.