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.