Algorihmic Dicree Mahemaic. Exercie Shee Deparmen of Mahemaic SS 0 PD Dr. Ulf Lorenz 7. and 8. Juni 0 Dipl.-Mah. David Meffer Verion of June, 0 Groupwork Exercie G (Heap-Sor) Ue Heap-Sor wih a min-heap o or he array (,,,,, 9, ). Fir ep i o conruc he heap. Thi can be done by uing buildheap or ju inering all number and ue heapify afer each inerion. So we ge a heap where he minimal elemen i he roo. We exrac hi roo, pu i in a new array and pu he elemen on he righ boom of he heap o he roo. Now he heap propery will in general be violaed. Therefore we ue heapify o regain ha propery. The big roo elemen ink down ino he heap. Now he malle elemen of he remainig elemen i he roo of he heap and he procedure ar from he beginning. We do hi ill our heap i empy. Then our array i ored. For hi pecial li he iniial heap i of he form (,,,,, 9, ). (Fir: roo, econd: lef child of roo, hird: righ child roo, fourh: lef child of lef child of roo,... ) We exrac he elemen and become he new roo, o we ge he remaining heap (,,,,, 9). We wap and and and by uing heapify and ge o (,,,,, 9). The re of he oring proce work he ame way a before. Exercie G (Maximal flow) Le D = (V, E) be a direced graph wih ource and ink. The capaciie on he edge hould be naural number and he maximal flow x from o i given. Now we wan o modify he capaciy of one edge in he graph by (a) increaing i by, (b) decreaing i by. Give an algorihm ha calculae he maximal flow from o in ha changed graph in O( V + E ). (a) If we increae he capaciy of one edge by hen we ju need o verify if here i an augmening pah in he changed graph. Since we only increaed one edge be he maximal flow can only increae by. Checking if here i a augmening pah can be done by uing DFS which i O( V + E ). (b) If we decreae he capaciy of one edge by here can be wo differen cae..cae: The flow i ill valid and doe no exceed any capaciy on a edge in he graph. Then he maximal flow ju ay he ame..cae: The flow exceed he capaciy on he edge we decreaed he capciy of. We denoe hi edge by (u, v ). Now le P be pah from o u and P be a pah from v o. Thi pah hould be dijoin and only conain edge wih a poiive acual flow. Such pah do exi, ince oherwie here would be no flow on he edge (u, v ) and can be found by uing DFS again. So he runime i O( V + E ) for finding boh of hee pah. Now reduce he flow on he whole pah P (u, v )P by, o he general flow for he graph i valid again, wih a maximal flow reduced by. I could be he cae now ha hi i no he maximal flow, bu we know ha he maximal flow can only be one flow uni bigger han he acual flow. So we ju look for anoher augmening pah a in par (a) and ge he maximal flow for he changed graph. Thi can be done in O( V + E ). Since we did algorihm ep wih O( V + E ) he whole procedure i in O( V + E ). Exercie G (Menger Theorem) (a) A direced graph D = (V, E) i called rongly k-conneced, if for every pair of verice (u, v ) and every e of edge B E wih B k, here exi a direced pah from u o v in D = (V, E \ B). Prove he edge verion of Menger
Theorem. Menger Theorem for edge: A direced graph D = (V, E) i rongly k-conneced, iff for every pair of verice (, ) here exi a lea k direced pah from o which hare no edge. (b) A direced graph D = (V, E) i called k-verex-conneced, if for every pair of verice (u, v ) and every e of verice W V wih W k here exi a direced pah from u o v in D W. Prove he verex verion of Menger Theorem. Menger Theorem for verice: A direced graph D = (V, E) i k-verex-conneced, iff for every pair of verice (, ) here exi a lea k direced pah from o haring no verice. (c) Do Menger Theorem alo hold for undireced graph? (a) : We how ha D i rongly k-conneced implie he exiance of he k direced pah having dijoin edge fir. We do i by howing he following equivalen aemen: There exi verice, uch ha here are a mo k direced pah from o having dijoin edge D i no rongly k-conneced. So le (, ) be uch a pair of verice a decribed above wih a mo k direced pah. We e he capaciy of all edge o and look for flow from o. The maximal flow we ge by hi procedure can be inepreed a he number of direced pah from o having dijoin edge. Now we ue he Max-Flow-Min-Cu Theorem o ge a cu (S, S). The value of ha cu i a mo k, ince he maximal flow can be a mo k. Now we e B a he e of all edge in D connecing S and S. By he previou argumenaion B k. Now we delee all edge in he e B from D. By he cu propery of (S, S) here can be any more direced pah from o in (V, E \ B). Hence D i no rongly k-conneced. : We how ha he exiance of k direced pah from o for arbiary verice, yield ha D i rongly k-conneced. So le D be uch a graph and, V. For any ube B E wih B k we can only delee edge of a mo k direced pah having dijoin edge. So here remain a lea one more direced pah from (, ) which did no ue any of he edge in B. So he graph D i rongly k-conneced. (b) : We wan o how ha for every k-verex-conneced graph D and verice, V here exi a lea k direced pah from o haring no verice. Again we how he following equivalen aemen: If here exi verice, V where we don have k direced pah from o haring no verice, hen D i no k-verex-conneced. So le, V be uch verice, where we have a mo k direced pah from o haring no verice. A in par (a) we wan o ue he Max-Flow-Min-Cu Theorem. Therefore we double each verex i V and name he verice i and i. All edge which where direced o i now are direced o i and all edge which ared in i now ar in i. Addiionally we add edge from i o i wih capaciy. All oher edge ge capaciy.we call hi graph D. Now we deermine he maximal flow f from o in D. Thi flow equal he number of direced pah from o in he orginal graph, which hare no verice. Therefore we inroduced he doubling of all he verice. By he Min-Cu-Max-Flow Theorem we find a cu (S, S) in D wih value f. Now le U be he e of all edge in he minimal cu (S, S). Thi e U conain an edge (i, i ) for every pah from o and all hee edge correpond o a verex i in he orginal graph D. So deleing all edge of B in D i like deleing he correponding verice in D. We call hi verex e W. Since by aumpion B k we ge W k, o we may delee hi verex e in order o look for k-verex-conneciviy. By conrucion here i no more direced pah from o in D \ B. By ranlaing hi o he verex eing we conclude ha here exi no more direced pah from o in our graph D \ W. Thi mean our graph D i no k-verex-conneced. : Le D be a direced graph where for each pair of verice, V here exi a lea k direced pah from o, haring no verice. So we delee any verex e W V from D wih W k. In he wor cae we deleed a verex fromk differen pah from o. Since all hoe pah do no hare verice here mu be a lea one more pah where we did no delee any verex from. So here i a lea one direced pah in D W. Thi mean D i k-verex-conneced. (c) We did no ue ha our graph we worked wih i direced, o he proof work he ame way for undireced graph. We ju have o ubiue direced wih undireced a every place here. Homework Exercie H Look a he following varian of he quickor algorihm. Algorihm QuickSor(a,l,r) (0 poin)
Inpu: An array a of lengh n wih a[i], lower and upper bound l, r wih l r n. Oupu: The array a wih a[l] a[l + ]... a[r]. () Se i = l and j = r. () While i < j Do () Do i = i + While a[i] a[r]. () Do j = j - While (a[ j] a[r] and j i). () If j > i Then Swap a[ j] and a[i]. () End While (7) Swap a[i] and a[r]. (8) If l < i Then QuickSor (a, l, i ). (9) If i + < r Then QuickSor (a, i +, r). (0) reurn a. The ieraion ar by uing QuickSor(a,,lengh(a)). (a) Ue Quickor o or he number array (,, 8,,,, 9,,,, 7). (b) How doe a number array have o look like, o ha Quickor ha a runime of O(n )/O(n log n). Decribe i in general and give an example for boh cae by uing he fir 0 naural number. (a) We give a decripion wha happen while uing Quickor. In he fir run we wap and, 8 and, and and finally 9 and 7. Then 7 i already a he righ place in he array. A ha poin in he algorihm he array i given by (,,,,,, 7,, 8,, 9). Now we have o or he remaining lef and ride ide of he number 7. We do i for he lef ide. The righ ide work he ame way. The array (,,,,, ) become (,,,,, ) Now i a he righ place. The nex ep we wap and. So (,,,, ) become (,,,, ). The array () wih one elemen i already ored and he oher array (,, ) ill need hree Quickor ep where alway he elemen on he righ ide of he array i deleed ill he elemen i he la one lef. In hoe la ep no more wap are involved. Afer doing he ame hing wih he righ par (, 8,, 9) we ge he li (,,,,,, 7, 8, 9,, ). (b) The wor cae for Quickor, uch ha i ha runime O(n ) i a already ored li. Thi way he elemen on he righ hand ide alway ay here and Quickor i ued wih a li wih only one le elemen han before. So we need n Quickor ieraion. An example would be (,,,,,, 7, 8, 9, 0). If we wan a runime of O(n log n) for Quickor we have o pli he li of number in half every ime. Thi way we only have o do half of comparion of he ep before. An example i (,,,, 8,, 7, 0, 9, ).
Exercie H7 (Finding maximal flow) Find he maximal flow from o in he following direced graph and prove i maximaliy. (0 poin) The maximal flow i given by. One can proof ha by he minimal cu which coni of S = {,,,, } and S = {, }. Anoher way o do i i o ue he Ford-Fulkeron algorihm and find augmening pah ill here are no more lef. One poible oluion i: / / 0/ / / / / / / / / / / / Exercie H8 (Righ or Wrong?) (0 poin) Le D = (V, E) be a direced graph wih ineger capaciie C e for he edge e E. Le be he ource in hi graph and he ink. Which of he following aemen are righ? Which are wrong? Give a proof or a counerexample. (a) If all capaciie are even, hen here exi a maximal flow f from o, uch ha f (e) i even for all edge e E. (b) If all capaciie are odd, hen here exi a maximal flow f from o, uch ha f (e) i odd for all edge e E. (c) If f i a maximal flow from o, hen eiher f (e) = 0 or f (e) = c e hold for all edge e E. (d) There exi a maximal flow from o, uch ha eiher f (e) = 0 or f (e) = c e hold for all edge e E. (e) If all capaciie on he edge are differen, hen he minimal cu i unique. (f) If we muliply each capaciy wih he poiive real number λ +, hen every minimal cu in here original graph i a minimal cu in he modified graph. (g) If we add he poiive number λ + o each capaciy, hen every minimal cu in he orginal graph i a minimal cu in he modified graph.
/0 / 80 /0 / 00 7 Figure : graph Figure : graph (a) Thi aemen i rue. A he beginning he flow f = 0 i even. Every ime we find an augmening pah i add an even flow o all edge in ha pah ince all capaciie are even. The reuling graph afer ha augmening proce ill ha even capaciie ince we ubraced only even number from even number. So by inducion here i a maximal flow uch ha all flow on he edge are even. (b) Thi aemen i wrong. Look a he graph D wih V = {,,, u, }, E = {(, ), (, ), (, u), (, u), (u, )}. The capaciie for hoe edge hould be excep for he edge (u, ) wih capaciy. So all capaciie are odd, bu he maximal i wih f (u,) =. (c) Thi aemen i wrong, ee graph for a counerexample. (d) Thi aemen i wrong again, ee Graph for counerexample, ince here only exi one poible maximal flow graph which doe no fulfil he needed condiion. (e) Thi aemen i wrong, ee graph for a counerexample. A minimal cu ha value 0 bu here are wo cu namely ({}, {,,, }) and ({,,, }, {}) having ha value. (f) Thi aemen i righ. We denoe he value of a cu W by c(δ + (W )). Thi mean we um up he value of all edge going from verice in W o W = V \ W. Le S be a minimal cu, o we have c(δ + (S)) c(δ + (W )) for all cu W. In he changend graph (D = (V, E), ĉ) wih λ + we ge ĉ(δ + (S)) = λ c(δ + (S)) λ c(δ + (W )) = ĉ(δ + (W )) for all cu W. So S i ill a minimal cu in he graph (D = (V, E), ĉ). (g) Thi aemen i wrong, a we can ee be looking a graph again. A we have een here a wo cu ({}, {,,, }) and ({,,, }, {}) wih value 0. If we add for example o every edge hen ({}, {,,, }) would ill be minimal wih value. The cu ({,,, }, {}) would have value and herefore would no be minimal anymore.