Design and Analysis of Algorithms Notes

Size: px
Start display at page:

Download "Design and Analysis of Algorithms Notes"

Transcription

1 Desig ad Aalysis of Algorithms Notes Notes by Wist Course taught by Dr. K Amer Course started: Jauary 4, 013 Course eded: December 13, 01 Curret geeratio: December 18, 013 Listigs 1 Array sum pseudocode Sequetial search pseudocode Sequetial search pseudocode Biary Search Pseudocode Selctio sort pseudocode Isertio sort pseudocode Select pseudocude Tur biary tree stored as array ito heap Pseudocode for adjust method for heapify Breadth first search Depth first search Prim s Algorithm Uio(i,j) Dijkstra s algorithm NextValue(k) for Hamiltoia Hamiltoia(k) No text is used. Refereces iclude Computer Algorithms - Sara Basse, Alle Va Gelder, Fudametals of Algorithms - Gilles Brassard, Paul Bratley, Computer Algorithms - Ellis Horowitz, Sartaj Sahi, Saguthevar Rajasekara 1

2 Part I Algorithm A solutio of a problem by a fiite sequece of istructios, each of which ca be performed by a machie. 1 Mathematical Backgroud Defiitio. T () = O(f()) - T of is of order f() or big oh of f() if there exists positive costats c ad 0 such that T () cf() for all 0 Example. 3 + = O() = 4 ( ) So c = 4 ad 0 = Example = O( ) ( ) = 15 So c = 15 ad 0 = Example. 6 + = O( ) ( 4) = 7 So c = 7 ad 0 = 4 Hw: Prove each of = O() = O( ) = O( ) Solutio: = 4, 3 c = 4, 0 = 3. c = 110, 0 = , , 1 = 110

3 3. so c = 13, 0 = = 13, 1 So, if T () is the executio time of a algorithm, whe is the size of the iput, the T () = O(1) meas that the executio time is costat sice T () c 1. This is excellet but does t happe very ofte. Also T () = O(log ) is also very good [e.g. if = 1000, the T () log 1000 c log 104 = c log 10 = 10c log = 10c] T () = O() is also good i this case. We say that our algorithm is liear. If T () = O( ) the algorithm is quadratic, which is ot good [e.g. = 1000, T (1000) c 1000 ] Fially if T () = O( ), the the algorithm is horrible (T (1000) = 1000 ). I this case the algorithm is expoetial. Remark. O(f()) = T () is meaigless Theorem If T () = a 0 + a a m m, the T () = O( m ). The max rule: If T () = T 1 () + T (), the T () = O(max(T 1 (), T ()) The maximum rule ca be geeralized to ay fiite umber of T i s Example. T () = 1 3 log 5 + log + 36 = O( 3 log ) Solutio. T () = O(1 3 log, 5, log, 36) = O( 3 log ). compoet 5 is egative. This is ot right sice the 1 log ( ) log 5 3 log 5 0 T () = 1 3 log 5 + log + 36 = 7 3 log log 5 + log + 36 = O(7 3 log, 5 3 log 5, log, 36) = O( 3 log ) Example. Write a algorithm to compute the sum of the elemets of a array whe size is. 1 sum (a, ){ s =0; 3 for i = 1 to 4 s = s + a(i) 5 Next i 6 Retur s 7 } Listig 1: Array sum pseudocode 3

4 Aalysis s is assiged 0, i is assiged 1, ad the value is retured oly oce. Assume that the time eeded to so is c 1. i is icremeted times ad the s icremetatio is doe times. Assume that the time eeded to do those tests is c. Also, i is compared to, + 1 times. Assume it takes c 3 to do it oce. So T () = c 1 + c + c 3 ( + 1) = (c 1 + c 3 ) + (c + c 3 ) = O() 1.1 Omega Defiitio. T () = Ω(f()) T of is omega of f() If there exists two positive costats c ad 0 such that T () c f() for all 0 Example. 3 + = Ω() ( 1) so c = 3, 0 = 1 Example = Ω( ) ( 1) so c = 10, 0 = 1 Example. 6 + = Ω( ) ( 1) so c = 6, 0 = 1 Hw: Prove each of the followig = Ω() = Ω( ) = Ω( ) Solutios: = Ω() , 1 So c = 3, 0 = c = 100, 0 = ,

5 = Ω( ) c = 1, 0 = 5 Theorem 1 is true for Ω, so for sum(a, ), T () = Ω() O(f()) is a upper boud Ω(f()) is a lower boud. So, if T () = O(f()), we say that the algorithm takes at most f() to execute, ad if T () = Ω(f()), we say that the algorithm takes at least f() to execute. 1. Theta Defiitio. T () = Θ(f()) T () is theta of f() If there exists positive costats c 1, c ad 0 such that T () c 1 f() ad T () c f(), for all 0 Example. 3 + = Θ() Solutio. so c 1 = 4, c = 3, 0 = Example = Θ( ) Solutio. so c 1 = 15, c = 10, 0 = Example..5 3 = Θ( ) Solutio. c 1 =.5, c =.5, 0 = = 4 ( ) ad ( 1) = 15 ( ) ad ( 1) ( 1) ad 3 ( 5)

6 HW: Prove each = Θ() = Θ( ) = Θ( ) Solutios: = Θ() , , 1 c 1 = 4, c = 3, 0 = = Θ( ) = ( 1) c 1 =, c = 1, 0 = = Θ( ) 5 4 5, ( 1) 4 4, ( 4) c 1 = 5, c = 1, 0 = 4 Theorem 1 remais to be true for Θ so i sum(a, ) T () = Θ() 6

7 Sequetial search Defiitio. Let D be the set of all iputs of size for the problem uder cosideratio ad let I be ay elemet of D. Let T (I) be the time of executio of the algorithm o iput I. The worst case complexity of the algorithm is w() = max{t (I) I D } so the worst case is the case that takes the logest to execute. Example. The problem is to search a array of size 3 for a umber x. The possible cases are x is the first elemet, the secod elemet, the third elemet, or x is ot i the array. 1 sequetial_search (a,, x){ i = 1 3 Do while (x a(c)) ad (i < ) 4 i = i Loop 6 If x = a(i) The 7 Retur i 8 Else 9 Retur 0 10 EdIf 11 } Listig : Sequetial search pseudocode.1 Worst-case aalysis The worst case occurs whe x is the last elemet i the array or x is ot i the array. I this case lies ad 6-10 are doe oce. Let the time eeded to execute them be c 1. Lie 4 is executed ( 1) times. Let the time eeded to execute it oce be c. Fially, the test of the whole loop is doe times. Assume that the time eeded to do it oce is c 3. So w() = c 1 + ( 1)c + c 3 = Θ() 1 sequetial_search1 (a,, x){ a( +1) = x 3 i = 1 4 Do while (x a(i)) 5 i = i loop 7 If i < + 1 The 8 Retur i 9 Else 10 Retur 0 11 EdIf 1 } Listig 3: Sequetial search pseudocode 7

8 . Worst-case aalysis The worst case occurs whe x is ot i the array. Lies, 3, ad 7-11 are executed oly oce. Let the time eeded to do this be d 1. Also, i is icremeted times ad the test of the while loop is doe + 1 times. Let the time eeded for the executio of the icremetig oce be d ad the time eeded for testig oce be d 3. So, w() = d 1 + d + ( + 1)d 3 = Θ() Both algorithms are Θ(), but clearly the secod is much more efficiet, as the first has 3 times umber of the logic operatios as the secod. So, the costats matter. w() 1 1 w() Average-case aalysis of sequetial search (Searchig for x i a array whose size is ): We will assume that the probability for x to be i the array is q. So, the probability that x is ot i the array is 1 q. Defiitio. Let P r(i) be the probability that iput I occurs. The the average behavior of the algorithm is defied by A() = I D P r(i)t (I) Where T (I) is the time eeded to execute the algorithm o iput I. We will also assume that if x is i the array, the it is equally likely to be ay positio of the array. So the probability that x is i positio i is q So we have q A() = T (i) + (1 q)w() i=1 = q T (i) + (1 q)w() = q = q i=1 (c 1 + c i + c 3 (i 1)) + (1 q)(c 1 + c + c 3 ( 1)) i=1 ((c 1 c 3 ) + (c + c 3 )i) + (1 q)((c 1 c 3 ) + (c + c 3 )) i=1 8

9 Let c 1 c 3 = e 1, c + c 3 = e = q (e 1 + e i) + (1 q)(e 1 + e ) i=1 ( ) = q e 1 + e i + (1 q)(e 1 + e ) i=1 i=1 ( ) = q e 1 + e i + (1 q)(e 1 + e ) i=1 = q ( ) ( + 1) e 1 + e + (1 q)(e 1 + e ) = qe 1 + q( + 1)e = qe 1 + qe = qe + qe + e 1 + e ( = e 1 + qe ) ( + e qe = Θ() + (1 q)(e 1 + e ) + qe + e 1 + e qe 1 qe ) 3 Biary Search Biary search is a example of a divide ad coquer algorithm. To use biary search, the array must be sorted. We will use the variable left to keep track of the subscript of the leftmost elemet of the portio of the array to be sorted, ad the variable right to keep track of the subscript of the rightmost elemet of the portio of the array to be sorted. 1 BiarySearch (a,, x){ left = 1; right = 3 Do while ( left <= right ) 4 mid = (lef t + right)/ 5 If x > a( mid ) The 6 left = mid Else If x < a( mid ) The 8 right = mid Else 10 Retur mid 11 loop 1 Retur 0 13 } Listig 4: Biary Search Pseudocode 3.1 Worst-case aalysis This occurs whe x is ot i the array. The first thig we eed to do is to cout the umber of times the whole loop will be executed. After the first time through the loop the portio of the array yet to be searched will have 1 = 1 But if 1 is ot a iteger, tha the 9

10 portio yet to be searched is will have either or 1. So the highest umber of elemets i the portio yet to be searched is So after the secod time through the loop the highest umber of elemets i the portio we are still searchig is. So after the kth time through the loop the highest possible umber of elemets i the portio we are still searchig is k. We keep loopig as log as 1. So we k stop loopig whe < 1. So we loop at most k times, where k is the smallest iteger such k that k < 1 < k log < log k log < k log log < k So k = log + 1 log + 1 Let c 1 be the time eeded to execute all the statemets out of the while loop oce ad the test of the while loop oce. (We cout the test because it s always executed oe more time tha the body). Also assume that maximum time eeded to execute the while loop icludig the test oce is c. So, W () c 1 + (log + 1)c = (c 1 + c ) + c log So W () = O(log ) We will also show that W () = Ω(log ). First recall that k + = Now, the lowest possible umber of elemets i the portio we are still searchig after the first time through the loop is 1. So after the secod time through the loop the lowest possible umber of elemets i the portio we are still searchig is 1 1 = 1 1 = (1 + 1 ). So after the third time through the loop the lowest possible umber of elemets i the portio we are still searchig is ( ) 1 = 3 ( ) 1 = 3 ( ) So after the kth time through the loop the lowest possible umber of elemets i the portio we are still searchig is ( k ) k 1 Let k = log k ( ) k 1 k 1 = log log = log = 1 4 = 4 = 10

11 So we have to loop more tha log. Let c 1 be as before ad let c be the miimum time eeded to execute the while loop icludig the test oce. So w() c 1 + c (log ) So w() = Ω(log ). So w() = O(log ) ad w() = Ω(log ). Hece W () = Θ(log ). 4 Selectio Sort We fid the subscript of the smallest elemet i the array ad swap the elemet with the first elemet. The we fid the subscript of the secod smallest elemet ad swap it with the secod elemet, ad so o. Listig 5: Selctio sort pseudocode 1 SelectioSort (a, ){ For i = 1 To - 1 // i is idex to be swappe 3 j = i // j is secod idex of swap 4 For k = i + 1 To // fid smallest elem 5 If a(k) < a(j) The 6 j = k 7 EdIf 8 Next k 9 Temp = a(i) // swap the elemets 10 a(i) = a(j) 11 a(j) = Temp 1 Next i 13 } Aalysis i is iitialized to 1 oce. Let the time eeded to do this be c 1. i is assiged to j, k is iitialized to i + 1, i is icremeted, ad the three statemets for swappig are doe 1 times. Let the time eeded to do these tasks oce be c. Also, i is compared to 1, times. Let the time eeded to do this oce be c 3. Moreover the If statemet ad the icremetig of k are doe i times for every i. Let c 4 be the maximum time eeded to do this. Fially, for every i, k is compared to, i + 1 times. Let the time eeded to do it oce be c 5. So 1 1 T () c 1 + c ( 1) + c 3 + c 4 ( i) + c 5 ( i + 1) i=1 = c 1 + c ( 1) + c 3 + c 4 ( 1) i=1 + c 5 ( ( + 1) ) 1 why does tha ter? because of the followig derivatios: 1 1 c 4 ( i) = c 4 ( i) i=1 i=1 = c 4 (( 1) + ( ) + + ( ( 1))) = c 4 (( 1) + ( ) + + 1) = c 4 ( 1) 11

12 1 c 5 ( i + 1) = c 5 ( i + 1) 1 i=1 i=1 = c 5 ( + ( 1) + + ( ( 1) + 1) = c 5 ( + ( 1) + + ) = c 5 ( + ( 1) ) ( ) ( + 1) = c 5 1 compariso So T () = O( ). Now, we let c 4 be the miimum time eeded to execute the If statemet ad comparig k to. So we have 1 1 T () c 1 + c ( 1) + c 3 + c 4( i) + c 5 ( i + 1) i=1 = c 1 + c ( 1) + c 3 + c 4 So T () = Ω( ) So T () = Θ( ), regardless of the iput. 5 Isertio Sort ( 1) i=1 + c 5 ( ( + 1) ) 1 I the first ru, we isert a() i the right place so that a(1), a() is sorted. I the secod ru, we isert a(3) i the right place so that a(1), a(), a(3) is sorted. Ad so o. There is o swappig i isertio sort. Table 1: Isertio Sort behavior a(1) a() a(3) a(4) a(5) IsertioSort (a, ){ Listig 6: Isertio sort pseudocode Table : Isertio Sort behavior at edge a(1) a() a(3)

13 For i = To // i is elem to be placed 3 x = a( i); j = i - 1 // x is temp var. j is traversig var 4 while (j > 0) ad (a(j) > x) 5 a(j + 1) = a(j) 6 j = j Loop 8 a( j + 1) = x // placig elem 9 Next i 10 } If the laguage does ot short circuit, we eed to place ay value i a(0) so we do ot get a bad rage error. I some cases, we ca use this to make the algorithm more efficiet. For example if we are sortig strigs, we ca place the empty strig i a(0). This allows us to delete the coditio j > 0 sice a(j) > x will always be before. 5.1 Worst-Case Aalysis This occurs whe the origial array is sorted i decreasig order. I this case, for every i, i, the test of the while loop, which is doe more tha ay other task, will be executed i times (j will ru from i 1 to 0), for a total of i = i= i 1 = i=1 ( + 1) 1 Hece w() = Θ( ) HW: Perform the best-case aalysis 5. Best-Case Aalysis The best case occurs whe the arrays are already sorted. Each time through the For loop, the test of the while loop fails the first test, ad the body of the while loop is ever executed. The task doe more tha ay other is comparig i to, which is doe times. So B() = Θ() 5.3 Average Case Aalysis: For every i 1, i, a(i) is iserted i ay of the positios 1,,... i,. We will assume that all these outcomes are equally the probability that a(i) is iserted i ay oe of these positios is 1 i. The test of the while loop is doe oce if a(i) is iserted i positio i, twice if i i 1, ad i times for a(1). So the umber of times the test of the while-loop is executed is 1 i i i i = 1 ( i) i = 1 i i(i + 1) = i + 1 So the total umber of times, o average, the test of the while loop is doe over the life of 13

14 the algorithm is i= i + 1 = 1 i= i + 1 = 1 ( ) = 1 ( ( + 1) 3) = 1 ( ) ( + 1)( + ) 3 = Sice the test of the while loop is doe more tha ay other task, A() = Θ( ) We should otice, however, that the test of the while loop is doe o average half as much as if it is doe i the worst case 14

15 Part II 6 Quick Sort We use a elemet of the array as a pivot. The array will be partitioed so that the elemets to the right of the pivot are greater tha the pivot, ad the elemets to the left are smaller or equal. We sort every part separately usig recursive calls of Quick Sort. Whe this is doe the etire array is sorted. We start by makig the call Quicksort(a, l, h) from the mai program 1 QuickSort (a, low, high ){ If low < high The 3 m = partitio (a, low, high ) 4 QuickSort ( a, low, m - 1) 5 QuickSort (a, m + 1, high ) 6 EdIf 7 } partitio returs the ew positio of the pivot. We use the leftmost elemet of the portio we are partitioig as the pivot. 1 partitio (a, low, high ){ p = a( low ); r = low ; l = high repeat r = r + 1 util (a(r) > p or (r = high )) 4 repeat l = l 1 util (a(l) P or (l = low )) 5 while r < l 6 swap a(r) ad a(l) 7 repeat r = r + 1 util a( r) > P 8 repeat l = l - 1 util a(l) p 9 loop 10 swap (a( low ), a(l)) 11 retur l 1 } Hw 1. Complete the forcig of the above example.. Why do we eed the coditio r = high i lie? 3. Why is o similar coditio eeded i lie 3 4. Why the coditio r = high is ot eeded i lie 6? 5. Chage the code to save oe additio ad oe subtractio. 15

16 Solutios. Without the coditio r = high, r will go past the rightmost elemet of the portio of the array we are partitioig if the pivot is the highest elemet i the array. 3. No similar coditio is eeded i lie 3 cause the coditio a(l) p will be two whe l reaches low if ot before so l will ever go past the leftmost elemet of the partitio we are partitioig. 4. No similar coditio is eeded i lie 6 sice i lie 5 we place a value greater tha the pivot to the right of the curret positio of r. So the coditio a(r) > p will become true whe r reaches this value if ot before. So there is o chace for r to go past the rightmost elemet of the portio we are partitioig. 5. We ca chage l = high + 1 to l = high thus savig oe additio ad chage lie 3 to while(a((l) > p) Do l = l Worst-case Aalysis Assume that the array is sorted ad all the elemets differet. I the first call to partitio, the total umber of comparisos to the pivot is + 1 sice l starts at ad stops at 1 ad r starts ad stops at. I this case, the pivot is swapped with itself ad the recursive calls to quicksort are made with 0 ad 1 elemets, respectively. I the first call, partitio is ot called. I the secod, the umber of comparisos to the pivot doe i lies ad 3 is, ad so o. So the total umber of comparisos doe i lies ad 3 to the pivots over the life of the algorithm is ( + 1) = ( + 1) = ( + 1)( + ) We stop at 3 sice i this case, we have elemets, which meas that the recursive calls to quicksort are doe with 0 ad 1 elemets. causig o further calls to partitio. So W () = Ω( ) Now, we will show that W () = O( ). At ay level of the recursio the total umber of comparisos to the pivot or pivots is + 1 (look at the top levels of the above example). But the umber of elemets ivolved i the recursio at ay level is at least oe less tha the umber of elemets ivolved i the recursio i the level above it. So the maximum umber of levels of recursio is 1 level of recursio maximum umbers of elemets ivolved No more levels sice with elemets the recursive calls are made with 0 ad 1 elemets causig o further calls to partitio. So the total umber of comparisos to the pivot or pivots doe i lie ad 3 over the life of the algorithms is ( + 1)( 1) So w() = O( ) Hece w() = Θ( ) 3 16

17 6. Average-case Aalysis We assume that it is equally likely that partitioig will place the pivot i ay positio. So the probability that i the first call to partitio the pivot will be placed i positio k is 1. We let C A () be the umber of comparisos to the pivots ad the life of the algorithm. I the first call to partitio, the umber of comparisos to the pivot + 1 (We have see examples where the umber of comparisos is + 1); ad if we cosider the example , we see that the umber of comparisos is ). So we have ( ) C A () ( + 1) + 1 C A (k 1) + C A ( k) So C A () ( + 1) + k=1 C A (k 1) + k=1 k=1 C A ( k) C A () ( + 1) + C A (0) + C A (1) + + C A ( 1) + C A ( 1) + + C A (0) Now, for the case of 1 C A () ( + 1) + C A (0) + C A (1) + + C A ( 1) (1) C A ( 1) ( 1) ( 1 k=1 k=1 ) 1 C A (k 1) + C A ( k 1) 1 1 ( 1)C A ( 1) ( 1) + C A (k 1) + C A ( k 1) k=1 ( 1)C A ( 1) ( 1) + C A (0) + C A (1) + + C A ( ) + C A ( ) + + C A (0) ( 1)C A ( 1) ( 1) C A (0) C A ( ) () subtractig from 1 we get k=1 k=1 C A () ( 1)C A ( 1) ( + 10 ( 1) + C A ( 1) = C A( 1) 3 + C A ( 1) So C A () ( 1)C A ( 1) 3 + C A ( 1) C A () 3 + ( 1)C A ( 1) + C A ( 1) = 3 + ( 1 + )C A ( 1) = 3 + ( + 1)C A ( 1) 1 Now, multiply both sides by ( + 1) C A () C A( 1) Sice 3 is true for ay value of, we may substitute 1 for to get C A ( 1) 3 + C A( ) 1 (3) 17

18 Hece, C A () C A( ) 1 Now substitutig for i 3, we get C A ( ) C A( 3) Hece C A () C A( 3) We cotiue this process util we substitute for i 3 to get C A () C A(1) Notice that C A (1) = 0 sice whe we have oe elemet, o calls to partitio are made. So we have ( C A () ) 3 = 3 1 k But So +1 k= k 1 x dx = l x +1 = l( + 1) l = 3 3 k=3 C A () 3(l( + 1) l ) + 1 C A () 3( + 1)(l( + 1) l ) So C A() + 1 = O( log ) 7 Fidig the Media The media of a set of scores is the middle value whe the scores are arraged i icreasig (or decreasig) order. So if we have a sorted array whose size is odd, the the media is the elemet i positio +1. If is eve, the media is the average of the two elemets i positios ad + 1. Example. Fid the media of the scores Sorted: So the media is 4. Example. Fid the media to the scees 5, 5, 5, 3, 1, 5, 1, 4, 3, 5, 1, 1,, 3, 3, 4, 5, 5, 5, 5, 5 5, 5, 5, 3, 1, 5, 1, 4, 3, 5 Sorted: So the media is 4.5 1, 1, 3, 3, 4, 5, 5, 5, 5, 5 18

19 But sortig is time cosumig. Ca we fid the media without sortig the array? Yes, but before we do this, we have to discuss the selectio problem. The selectio problem: I this problem, we wat to fid the kth smallest elemet of a array of size. We will use the partitio algorithm of QuickSort to solve the problem. Our algorithm the would the pace the kth smallest elemet i its positio. The remaiig elemets will be igored so that a(m) a(k), for 1 m < R ad a(m) a(k) for R < m. But we eed to chage the coditio r = high i lie 3 of partitio to r high. 1 select (a,, k){ left = 1, right = 3 Repeat 4 j = partitio ( a, left, right ) 5 if( j = k) the retur 6 else if ( j > k) the right = j else left = j util ( false ) 9 } Example. Fid the 4th smallest elemet of Listig 7: Select pseudocude 3, 1, 4, 1, 5, 9,, 6, 5, 3, 5, 8, 9 As show before, after the first call to partitio, we have, 1, 3, 1 3 9, 5, 6, 5, 4, 5, 8, 9, ad j = 5. So the secod call to partitio will be called with partitio(a, 1, 4). So we get 1, 1 3 So j = 3. So the third call to partitio is made with partitio(a, 4, 4). So the partitio of the array we are partitioig is just a(4). r is iitialized to 4 ad icremeted to 5 (this is the reaso we had to chage r = high to r >= high). Also, l is iitialized to 5, the decremeted i lie 3 to 4. The while loop is ot etered, ad 4 will be retured. This eds the executio of the the algorithm. 1 Media (a, ){ If mod = 1 the 3 select (a,, + 1 ) 4 retur a( + 1 ) 5 else 6 select (a,, ) // lower midval 7 i = a( + 1) 8 for j = i + 1 to // fids higher midval 9 if a(j) < i 10 i = j 11 retur (a( ) + a(i))/ 1 } 19

20 8 Heap A complete biary tree is a biary tree such that if it cotais a ode x at level k, the it must cotai all possible odes to the left of at level k ad all possible odes at each level i such that i < k. Lemma. 1. The maximum possible umber of odes at level i is i 1. The maximum possible of odes i a complete biary tree of depth (the umber of levels) d is d 1. Proof. 1. Whe i = 1, i 1 = 0 = 1. So the statemet is true whe i = 1. Assume that the statemet is true whe i = k, that is, the maximum umber of odes at level k is k 1. Each oe of these odes ca have up to childre, so the maximum umber of odes at k + 1 is k 1 = k = (k+1) 1. Whe d = 1, d 1 = 1 1 = 1. So that statemet is true whe d = 1. Assume that the statemet is true whe d = k, that is, the maximum possible umber of odes i the tree whose depth is k is k 1. But by part (1), the maximum umber of odes i level k 1 is k, so if d = k +1, the the maximum umber of odes i the tree is k + k 1 = k+1 1 Lemma 3. I a complete biary tree, the childre of a(i) are a(i) ad a(i + 1). Corollary. The paret of a(i) is a( i ) Proof. If i is eve, the i = i. Hece by lemma 3, the left child of a( i ) is a( i ) = a(i). So a( i ) is the paret of a(i). O the other had, if i is odd, the i = i 1. Hece by lemma 3, the rightmost child of a( i ) = a(( i 1 ) + 1) = a(i). So a( i ) is the paret of a(i) Defiitio. A max heap is a complete biary tree such that the value at each ode is greater tha or equal to the value at its childre, (if they exist). How ca we tur a complete biary tree ito a heap? We start at the paret of the last leaf, ad do the required chages so that the subtree rooted at the paret is a heap. Movig left ad up, we do the same thig. Whe we are doe with the root, we have a heap. 1 heapify (a, ){ for i = to 1 step adjust (a, i, ) 4 ext i 5 } Listig 8: Tur biary tree stored as array ito heap Adjust will make the required chages so that the subtree rooted at i is a heap provided that the subtree rooted at the childre of a(i) are heaps. 0

21 Listig 9: Pseudocode for adjust method for heapify 1 adjust (a, i, ){ j = i; temp = a(i) //j is the subscript of the left child of a(i) 3 while j <= do{ 4 if ( j < ) ad a( j) < a( j + 1) the j = j + 1 // j gets idex of larger child 5 if ( temp < a( j)) the // if larger child is larger tha paret 6 a( j ) = a(j) // set paret ode with child s value 7 j = j 8 else 9 break ; loop // ed of while 1 } 13 a( j ) = temp 14 } 8.1 Worst-Case aalysis of Heapify Assume that the depth is k for every level i such that i < k, the umber of odes is i 1 by lemma. A value at level i ca be moved dow to level k, which meas that it ca move dow k i levels. This meas that the umber of times a while loop of Adjust will be executed is k i for a give value. So the total amout of times the while loop will be executed is k 1 0 (k 1) + 1 (k ) + (k 3) + + k 3 + k = j k j 1 = j=1 k 1 j=1 j k 1 Now otice that levels 1 through k 1 are full. So we have by lemma. But we have at least 1 ode at level k. So the umber of odes k 1. So So heapify is O(). 1 HeapSort (a, ){ Heapify (a, ) 3 For i = To step -1 4 swap (a (1),a(i)) 5 adjust (a, 1, i - 1) 6 loop 7 } k 1 j=1 j k 1 k 1 j j j j=1 k 1 j = j j=1 sice k 1 j=1 j j The ext exam will have traceig as was doe i class. Kow how to do. 1 j

22 8. Worst-case aalysis of HeapSort We kow that heapify is O(). So if we assume that the size is, we ca show as before that k 1, where k is the depth of the complete biary tree. Hece log() log( k 1 ) = k 1 log() = k 1 But the value at a(1) i each call to adjust will ed up at level k i the worst case which is log. So i each call to adjust, the while loop will be executed log times. But we call adjust times. So the total umber of times the while loop of adjust will be executed is log. So W () = O( log ) 9 Biary Tree Traversal Preorder For every ode we visit, the we traverse the left subtree i preorder, the traverse the right subtree i preorder. Iorder Traverse the left subtree, visit, the traverse the right subtree. Postorder Traverse the left subtree, right subtee, the visit. Example. A B C D E F G H I Preorder: ABDFGHIEC Iorder: FDHGIBEAC HW List the odes of the followig trees i preorder, iorder, postorder. A 1. B C D EF G H Note: Node E is overlappig Node F. I do t kow how to correct this tikz drawig. This tree is a exact reflectio of the previous.

23 Solutio 1. Preorder ABDGECFH Iorder DGBEAFHC Postorder GDEBHFCA. Preorder ACFHBEDG Iorder CHFAEBGD Postorder HFCEGDBA 9.1 Code for Biary Tree Traversal 1 Node { data : datatype 3 leftchild, rightchild : ptr to ode 4 } 5 Tree : ptr to ode A tree is a poiter to its root. 1 Iorder ( T){ // T poits at the root of the tree If T ull The 3 Iorder ( T - > leftchild ) 4 visit (T) 5 Iorder ( T - > rightchild ) 6 EdIf 7 } 1 preorder (T){ If(T ull ) The 3 visit (T) 4 preorder ( T - > leftchild ) 5 preorder ( T - > rightchild ) 6 EdIf 7 } HW Write pseudocode for postorder. Solutio 1 postorder (T){ If(T ull ) The 3 postorder (T-> leftchild ) 4 postorder (T-> rightchild ) 5 visit (T) 6 edif 7 } Example. Trace Iorder o We will assume that whe we visit a ode we will just prit the data. Also, for clarity, each time we will make a recursive call we will push T data o the rutime stack istead of just T. See hadwritte otes. 3

24 A B C D E F G H I HW Trace preorder ad postorder usig the example tree. Extra credit (4/18): Write iorder orecursively. Just preset ad uderstad (10-15 pts) 9. Aalysis of Iorder, Preorder, Postorder We will assume that visit(t) is doe i costat time. Let T () be the time it takes to execute ay of the three algorithms. The T () = Θ(), where is the umber of odes. The algorithms is called recursively times. The coditio of the If statemet is executed + 1 times. Also, a total of N visits are made. Hece T () = c 1 + ( + 1)c + c 3 = Θ() 4

25 Part III 10 Graphs A graph G cosists of two sets V, E. The set V is a fiite oempty set of vertices. The set E is a fiite set of pairs of vertices. The members of E are called edges. If e E, such that e = {u, v}, we say that u ad v are adjacet ad e is icidet with u ad v. A graph is simple if it does ot have a vertex which is adjacet to itself ad does ot have two edges icidet with the same two vertices. From ow o, wheever we use the word graph we will mea a simple graph Graph Represetatio If V = {v 1, v,... v }, the G is represeted as a -elemet array of headers of liked lists. Each cell of the liked list has two fields. Oe has the data, the vertex, ad the other poits at the ext cell. The ith liked list cotais all the vertices adjacet to v i [Graph Represetatios Figure 1] A path from a vertex u to a vertex v i a graph is a sequece of vertices u = v 1, v,... v = v such that {v i, v i+1 } is a edge i G, 1 i 1. All the edges ad all the vertices must be distict except possibly u ad v. I this case, the path is called a cycle. I a graph, two vertices are said to be coected if there is a path betwee them. G is said to be coected if every pair of vertices of G are coected. 10. The Reachability problem Let G = (V, E) ad let i V. Fid all vertice that are coected to i. Breadth first search. We will have a array visited() which will be iitialized with zeros. At the ed of the search, visited(j) = 1 iff j is coected to i. We start by markig i as havig bee visited ad create a queue cotaiig all the vertices that are adjacet to i ad mark them as visited. We delete the first vertex i the queue ad add the vertices which are adjacet to it which have t bee visited to the queue ad mark them as visited. We keep doig this util the queue is empty. We will make G ad visited global variables. Also (# vertices) is global. 11 BFS The graph has headers to the first cell of liked lists. [Example of tracig BFS i otes] [Example of tracig BFS i otes] Defiitio. The degree of a vertex V is the umber of edges that are icidet o the vertex. 5

26 Listig 10: Breadth first search 1 BFS (i){ create ( q); // creates a empty queue 3 for (l = 1 to ) 4 visited (l) =0 5 ext l 6 visited (i) = 1 7 Repeat 8 p = G(i) 9 while P ull do 10 j = p -> ode #j = p. ext 11 if( visited ( j) = 0) The # whe a uvisited ode is foud 1 visited ( j) = 1 # set the ode as visited 13 equeue (q, j) 14 edif 15 p = p. ext 16 loop 17 If q is empty the retur 18 i = dequeue (q) 19 util ( false ) 0 } Theorem 4. If v 1,..., v are the vertices of a graph G, the deg(v i ) = E(G) i=1 The sum of the degrees of all vertices equals m, where m is the umber of edges i the graph 11.1 Worst Case Aalysis of BFS We will assume that G has vertices, m edges, ad it is coected. The body of the for loop is doe times ad its test is doe + 1 times. We go through the Repeat loop times, oce for every vertex. This meas that lies 8 ad 17 are doe times. Lie 18 is doe oly 1 times sice it is ot executed durig the last loop because we retur. Now, for every vertex, the while loop is executed as may times as deg(j). So, for every j, lies 10, 15, ad the test of the If statemet are doe deg(j) times. Statemets 1-13 are doe 1 times for the etire algorithm. By the above theorem lies 10, 15, ad the test of the If statemets are doe m times. Fially, i every i the test of the while is doe oe more time tha the body. So the total umber of times the test of the while loop is doe is m +. So W (, m) = Θ(max(m, )). 1 Depth First Search [Diagram] We visit the vertices of G 1, the G, the G 3 Each time a recursive call is made for elemets of visited are equal to 1. So the size of the problem is gettig smaller. [Trace] 6

27 Listig 11: Depth first search 1 for l = 1 to // set all values to 0 visited (l) = 0 3 ext l 4 5 DFS (i){ 6 visited (i) = 1 7 p = G(i) 8 while p ull do 9 j = p - > ext // use j to do recursive call 10 If( visited (j) = 0) the DFS (j) 11 p = p - > ext // update p 1 loop 13 } HW Trace DFS(1) for the graph give o writte otes. 1.1 Aalysis We will assume that we have vertices, m edges, ad the graph G is coected. The body of the For loop is doe times, ad it s test is doe + 1 times. Lies 6 ad 7 are doe times. Also, 1 recursive calls will be made. Everythig else i the body of the while loop will be doe deg(j) for each vertex j for a total of m. Moreover, for each vertex, a test of the while loop is doe oe more time tha the body for a total of m + Hece DFS(i) is Θ(max(m, )) 13 The Greedy Method 13.1 Miimum Cost Spaig Trees A tree is a coected graph which does ot cotai ay cycles. H is a subgraph of G if all the vertices of H are vertices of G ad all the edges of H are edges of G. A subgraph H of G is a spaig tree of G if H is a tree which cotais all the vertices of G. I applicatio problems edges are assiged weights (cost of costructio, legth of a lik, etc). Weights are positive umbers. A miimum cost spaig tree is a tree where the sum of the weights are miimum Prim s Algorithm: We build the mcst as follows: we start by puttig a miimum cost edge i T. We cosider all the edges with oe vertex i T ad oe vertex yet to be added to T. We pick the edge whose cost is miimum ad add it to T. We repeat util we have 1 edges i T. After addig the 1st edge T has vertices. Each subsequet edge itroduces oe ew vertex. So T has all the vertices. After the first edge, we do ot have a cycle. Also, each ew edge will ot cause a cycle to be formed sice for this to happe, the edge has to coect two ew vertices already i T, which is ot the case. 7

28 It ca be show that T is coected. It ca also be show that the sum of the weights is miimum. So T is a mcst. [see otes for example trace of algorithm] HW Fid a mcst for [graph i otes]. L A TEX d graph i progress a Assume that the vertices are {1,,... } Cost [1 to, 1 to ] is the cost adjacecy matrix. cost(i, j) is either the weight of the edge icidet o i ad j or if if there is o edge icidet o i ad j. The mcst will be stored i t (1 to 1, 1 to ). So if t(i, 1) = k ad t(i, ) = l, the {k, l} is a edge i the mcst. We will also eed a array ear (1 to ). ear(i) = 0 idicates that i has bee added to the tree. O the other had if ear(i) = j, the this idicates that the weight betwee i ad j is the smallest amog all the weight of edges joiig i to a vertex which has already bee added to the tree. Listig 1: Prim s Algorithm 1 prim (cost,, t){ let {k, l} be a edge of miimum cost. 3 t(1, 1) = k, t(1, )=l 4 for i = 1 to // populates ear ( t) 5 If cost (i, l) < cost (i, k), the ear (i) = l else ear (i) = k 6 ext i 7 ear ( k) = 0; ear (l) = 0; 8 9 For i = To Let j have a vertex such that ear ( j) 0 ad cost ( j, ear ( j)) is miimum 11 t(i,1) = j, t(i, ) = ear (j) 1 ear (j) = 0 13 For k = 1 To // update ear () for all odes 14 If ear (k) 0 ad cost (k, j) < cost (k, ear (k)) The! if the cost of the edge betwee k ( which is ot i the tree ) ad j ( the vertex that has just bee added to the tree ) is < the cost of the edge betwee k ad the curretly earest vertex of the tree to k 15 ear ( k) = j! the j is the ew earest vertex to k 16 EdIf 17 Next k 18 Next i I lie 3 of prims, we fid the miimum of ( 1) + ( ) umbers. So we fid the miimum of ( 1) umbers. So the executio time of lie is Θ( ). 8

29 The executio time of the for loop of Prim s alg is Θ() Also lie 10 takes Θ() time to execute ad the For loop of lie 13 takes Θ() time to execute. so the For loop of lie lie 9 takes Θ( ) time to execute Kruskal s Algorithm We start with T (the mcst) cotaiig all the vertices ad o edges. We add a miimum cost edge to T. We cosider all the remaiig edges ad pick a edge e whose cost is miimum. We add e to T if doig so wo t form a cycle. Otherwise e is discarded. Whe T has 1 edges it will be a mcst. check lie u bers. they co wrog HW Fid a miimum cost spaig tree usig Kruskal s algorithm for the graph of the above HW problem. l Let S = {1,,... }. The oempty subsets S 1, S,..., S l form a partitio of S if S i = S ad S i S j = for all i, j such that i j, j l ad i j. We represet a partitio with a forest but with poiters from the childre to the paret. The forest is implemeted as a array p(1to). For every i, p(i) = j meas that j is the paret of i. A elemet i belogs to the same subset as its paret. If i does ot have a paret, tha p(i) = 1. We will [] to the subsets by the roots of the trees represetig them. Example. S = {1,,... 10}, S 1 = {1, 7, 8, 9}, S = {, 5, 10}, S 3 = {3, 4, 6}0 [see figures i partitios 4/11/13 p] Uio(i, j) fids the uio of the subset rooted at i ad the subset rooted at j i=1 1 Uio (i,j) { p(i) = j 3 } Listig 13: Uio(i,j) so if we compute uio(1, 5) we get [see figures i partitios 4/11/13 p] We also eed the fuctio fid(i), which fids the root of the tree 1 fid (i){ while p(i) > 0 Do{ 3 i = p(i) 4 } 5 retur i 6 } so fid(9) returs 5 sice the subset represeted by the tree rooted at 5. The set of all edges will be represeted as a array E of structures. Each structure cotais 3 fields. The first two fields have the two vertices the edges are icidet o ad the third field has the cost of the edge. We will use the third field to trasform E ito a miheap. This meas we ca fid the edge with miimum cost efficietly. t(1 to 1, 1 to ) will be the same as before.the set of vertices will be partitioed so that at ay give poit two vertices belog to the same subset iff they are coected by edges that have bee added to the tree. 1 Kruskal (E,, m, t){! m= umber of edges, t= tree beig built Heapify (E, m) 3 For i = 1 to 4 p( i) = -1! iitially o vertices are coected what does th mea.. 9

30 5 Next i 6 i = 0 7 while i < - 1 Do! while sice some edges might be discarded 8 [ remove miimum cost edge { u, v} from heap, use adjust to reheapify ] 9 j = Fid (u), k = Fid (v) 10 If j k The! if j = k, the u ad v are already coected ad addig the edge will form a cycle 11 i = i + 1! oe edge closer to fiishig 1 t(i,1) = u; t(i,) = v 13 uio ( j, k)! all vertices that are coected to u ad all vertices that are coected to v will become coected. They should belog to the same subset. 14 EdIf 15 } See otes 4/18/13 p1 HW: Trace Kruskal s algorithm usig the graph of the above hw problem. The efficiecy of the algorithm depeds o the time spet o the fid operatios. Assume that the edges {1, }, {, 3},..., {, 1} are the first edges to be added to the tree. This causes the forest represetig the partitio to be (liked list like). Now assume we have to perform the operatios Fid(1), Fid(),... Fid(-1) (this ca happe whe the vertices are packed ad discarded). The total umber of times that the of the while loop of fid will be executed as a subset of their calls is ( 1) + ( ) = ( 1) = Θ( ) To help solve this problem,we make j the paret of i whe we perform uio(i, j) if the subset rooted at j has more elemets tha the subset rooted at i. Otherwise we make i the paret of j. I the case of the above example we will get... To do this efficietly, p(i) will cotai -1 times the umber of elemets i the subset represeted by the tree rooted at i (iitially every vertex is i a subset by itself, so every tree represetig the subset will have 1 ode. So p() is iitialized as before). 1 weighteduio (i,j){ temp = p(i) + p(j) 3 If p(i) > p(j) the 4 p(i) = j; p(j) = temp 5 else 6 p(j) = i; p(i) = temp 7 EdIf 8 } We ca also collapse the tree represetig a subset as we perform fid. For example, if we 30

31 perform fid(x) o 8 to collapsigfid (i){ r = i 3 while (p(r) > 0) Do r = p(r) 4 while ( i!= r) Do 5 s = p(i); p(i) = r; 6 i = s 7 loop 8 retur r; 9 } Assume that the graph has vertices ad m edges. As we have see before, lie is O(m). Also, the For loop from lie 3 to lie 5 is O(). Also sice some edges might get discarded, the while loop might be executed m times. This makes the total time for lie 8 O(m log m). Moreover, util the chages we made to uio ad Fid, the total time eeded for lie 9 is almost liear. The coditio of the If statemet from lie 10 to lie 14 is O(m) ad the statemet after The is O(). (it is doe oce for every edge). So the alg is O(m log m). 14 The Travelig Salesperso Problem A complete graph is a graph i which every pair of distict vertices is coected by a edge. For the travelig salesperso problem (TSP), we are give a complete weighted graph ad we wat to fid a tour (a cycle through all vertices) of miimum weight. The TSP is a example of a NP Problem ad cosequetly there is o kow polyomial time algorithm for solvig it Approximatio algorithms for the TSP The earest eighbor algorithm We start at a arbitrary vertex, v 1. We select a miimum weight edge amog the edges icidet over v 1. Let this edge be {v 1, v }. The we select a miimum cost edge amog the edges icidet o v but ot v 1, ad so o. So at step i if the partial cycle cotais the edges {v 1, v }, {v, v 3 },..., {v i 1, v i }, we pick a miimum weight edge icidet o v i but ot o ay of v 1, v,... v i 1. Evetually we will have a path cotaiig {v 1, v }, {v, v 3 },..., {v 1, v } so we add the edge {v, v 1 } to complete the cycle. We have 4 choices for the first vertex, 3 choices for the secod vertex, ad so o. So we have 4! = 4 possible tours. (i geeral,! tours). But as we are dealig with cycles, we ca say there are 3! uique cycles (i geeral ( 1)!). This does ot cotradict the fact that 31

32 [see hadwritte otes 4/18/13 p] Figure 1: small example with 4 vertices, ad cost for startig at each ode ad applyig the earest eighbor algorithm the outcome of the earest eighbor algorithm depeds o the 1st vertex sice i this case we cosider all possible tours. Figure : Graph of all possible tours of a K 4 graph So the optimal tour is = Shortest Lik Algorithm We pick a edge with miimum cost ad add it to C, where C is the tour beig built. Out of the remaiig edges, we pick a edge e with miimum cost. e is added to C if it will ot cause a cycle to be formed ad it will ot cause a vertex to have a degree of 3 (otice that i the cycle, every vertex has to have a degree of ). Otherwise e is discarded. We keep doig this till we have a ope path cotaiig all the vertices. To complete the tour, we iclude the edge betwee the two ed vertices of the ope path. Hw: [K4 graph trace of shortest-lik algorithm - see 4/5/13 p1] 1. Give a example where both the earest eighbor algorithm (regardless of the startig poit) ad the shortest lik algorithm produce optimal tours. The edges should have differet weights.. Give a example where the earest eighbor algorithm startig at a give vertex produces a tour whose cost is less tha the cost of the tour produced by the shortest lik algorithm (the edges should have differet weights) 15 Sigle-Source shortest path Here we have a directed graph, which has edges that are represeted by ordered pairs. If (u, v) is a edge, the we have a edge betwee u to v, but ot a edge betwee v ad u. We ca 3

33 still have a edge betwee v ad u, but for this case, (v, u) must be a edge of the graph. Now, we have a directed graph with oe of the vertices desigated as the source. The problem is to fid the legth of a shortest path from the source to each of the other odes. A path is a shortest path if the sum of the weights of the path is miimum Dijkstra s Algorithm The cost adjacecy matrix will be the same as before except for the fact that for each i, the (i, i) etry is 0. We will have a set S, which will be iitialized to the source, v. We will also have a array dist(1 to ). dist(i) will equal the curret distace betwee v ad i. dist(i) will be iitialized to cost(v, i). At each step we will pick a vertex i / S, so that dist(i) is miimum amog the distaces betwee v ad the vertices which are ot i S. i is added to S. This meas we foud the shortest path to i. The we update dist() as follows. For every i / S, we compare dist(i) ad dist(j) + cost(j, i), where j is the vertex which has just bee added to S. If dist(j) + cost(j, i) is less tha dist(i), the dist(i) is set to be dist(j) + cost(j, i). S is implemeted as a array S(1 to ). S(i) = 0 meas i / S ad S(i) = 1 meas i S. Listig 14: Dijkstra s algorithm 1 Dijkstra (v, cost, dist, ){ For i = 1 to 3 s(i) = 0, dist (i) = cost (v,i) 4 Next i 5 S(v) = 1! iclude v 6 For um = 1 to -! First time through the loop, shortest path to vertices will be foud. After d time, shortest path to 3 vertices foud. Aft - times, shortest paths to -1 vertices foud. But we update dist ( i), where i is remaiig vertex, so oly - iteratios eeded to fid shortest paths of all vertices. 7 choose u amog the vertices i S s. t. dist (u) is miimum. 8 S(u) = 1 9 For k = 1 to 10 If S(k) = 0 ad dist (k) > dist (u) + cost (u,k) The 11 dist (k) = dist (u) + cost (u,k) 1 Next k 13 Next um 14 } [Directed graph trace of Dijkstra s] The cost adjacecy matrix is:

34 Aalysis The time eeded for the For loop of lie is Θ(). Also the time eeded to execute lie 7 oce is Θ() ad the time eeded to execute the For loop of lie 9 oce is Θ(). So the etire time eeded for the For loop of lie 6 is Θ( ). Hece Dijkstra s algorithm is Θ( ). What if we wat the paths as well as the distaces. We will itroduce a array P(1 to ) whose elemets will be iitialized to the source. Each time we update dist(k), we update P(k) to u, whose u is the vertex which just has bee added to S. To fid the path from v to i, we start at i ad follow P(i) till we get v. The path will be listed backward. HW: Trace with [Graph i otes 4/3/13 P] 16 Bous Assigmet Backtrackig Hamiltoia Cycles A coected graph is said to be Hamiltoia if it cotais a cycle that visits every vertex exactly oce, except for the start vertex, which is visited at the begiig ad the ed. Such a cycle is said to be a Hamiltoia cycle A algorithm Hamiltoia(k) will retur all the Hamiltoia cycles startig at 1. x(1) = 1 We set x() to the lowest umbered vertex which is adjacet to 1. x(3) will be the lowest umbered vertex which is adjacet to x() ad differet from x(1), ad so o. So, x(i) will be the lowest umbered vertex which is adjacet to x(i 1) ad differet from all previous vertices. If this process cotiues, till we assiged x(), where is the umber of vertices, a vertex ad x() is adjacet to x(1), the we will have foud a hamiltoia cycle, ad we output the cycle. Whether we fid a cycle or ot, we backtrackig x(1),..., x() as before ad assigig the secod lowest umbered vertex that satisfies the above coditio to x(). [there is oly oe choice for x() i this istace.] Whe we are doe with all possible choices for x(), we will backtrack further by keepig x(1),..., x( ) as before ad choose x( 1) to be the secod lowest umbered vertex that is adjacet to x( ) ad differet from x(1),..., x( 3), ad try all possible choices for x(). Evetually, we will backtrack all the way to x(). We pick a ew vertex for x() if possible ad try all possible values for x(3),..., x(). The alg is ot completed till we try all possible choices for x(), which all possible lists for x(3),..., x(). x(1) will be iitialized to 1, ad x(i), i 1 will be iitialized to 0. This will be doe i mai. Also, A() will be iitialized i mai. The alg will get started by Hamiltoia() from mai. First, we write NextValue(k) which assigs a vertex to x(k) assumig that x(1),..., x(k 1) have bee assiged values. Listig 15: NextValue(k) for Hamiltoia 1 NextValue (k){ Repeat 3 x(k) = (x(k) + 1) mod ( + 1)! So x(k) does t get a higher value tha 4 If x( k) = 0 The retur! This will happe whe there is o vertex that meets the right coditios. 5 If A(x(k - 1), x(k)) 0 The!x(k -1) ad x(k) are adjacet 6 For j = 1 to k - 1! To check that x( k) is differet from all the previous vertices 34

35 7 If x(j) = x(k) the break 8 Next j 9 If(j = k) The 10 If ((k < ) or ((k==) ad a(x(), x (1) ) 0)) The! x ( ) has to be adjacet to x (1) 11 Retur 1 EdIf 13 util false 14 } Listig 16: Hamiltoia(k) 1 Hamiltoia (k){ Repeat 3 Nextvalue (k) 4 If x( k) = 0 the retur! o ext value assiged to x( k) so this path does ot lead to a hamiltoia cycle. Retur to backtrack. 5 If k = write ( x())! hamiltoia cycle has bee foud. prit. 6 Else 7 Hamiltoia ( k + 1) 8 Util false 9 } [see Hamiltoia trace at 5//13 P1] 35

Graphs. Minimum Spanning Trees. Slides by Rose Hoberman (CMU)

Graphs. Minimum Spanning Trees. Slides by Rose Hoberman (CMU) Graphs Miimum Spaig Trees Slides by Rose Hoberma (CMU) Problem: Layig Telephoe Wire Cetral office 2 Wirig: Naïve Approach Cetral office Expesive! 3 Wirig: Better Approach Cetral office Miimize the total

More information

CIS 121 Data Structures and Algorithms with Java Spring Stacks and Queues Monday, February 12 / Tuesday, February 13

CIS 121 Data Structures and Algorithms with Java Spring Stacks and Queues Monday, February 12 / Tuesday, February 13 CIS Data Structures ad Algorithms with Java Sprig 08 Stacks ad Queues Moday, February / Tuesday, February Learig Goals Durig this lab, you will: Review stacks ad queues. Lear amortized ruig time aalysis

More information

Lecture 5. Counting Sort / Radix Sort

Lecture 5. Counting Sort / Radix Sort Lecture 5. Coutig Sort / Radix Sort T. H. Corme, C. E. Leiserso ad R. L. Rivest Itroductio to Algorithms, 3rd Editio, MIT Press, 2009 Sugkyukwa Uiversity Hyuseug Choo choo@skku.edu Copyright 2000-2018

More information

Minimum Spanning Trees

Minimum Spanning Trees Miimum Spaig Trees Miimum Spaig Trees Spaig subgraph Subgraph of a graph G cotaiig all the vertices of G Spaig tree Spaig subgraph that is itself a (free) tree Miimum spaig tree (MST) Spaig tree of a weighted

More information

CIS 121 Data Structures and Algorithms with Java Spring Stacks, Queues, and Heaps Monday, February 18 / Tuesday, February 19

CIS 121 Data Structures and Algorithms with Java Spring Stacks, Queues, and Heaps Monday, February 18 / Tuesday, February 19 CIS Data Structures ad Algorithms with Java Sprig 09 Stacks, Queues, ad Heaps Moday, February 8 / Tuesday, February 9 Stacks ad Queues Recall the stack ad queue ADTs (abstract data types from lecture.

More information

CSE 417: Algorithms and Computational Complexity

CSE 417: Algorithms and Computational Complexity Time CSE 47: Algorithms ad Computatioal Readig assigmet Read Chapter of The ALGORITHM Desig Maual Aalysis & Sortig Autum 00 Paul Beame aalysis Problem size Worst-case complexity: max # steps algorithm

More information

CIS 121. Introduction to Trees

CIS 121. Introduction to Trees CIS 121 Itroductio to Trees 1 Tree ADT Tree defiitio q A tree is a set of odes which may be empty q If ot empty, the there is a distiguished ode r, called root ad zero or more o-empty subtrees T 1, T 2,

More information

Chapter 24. Sorting. Objectives. 1. To study and analyze time efficiency of various sorting algorithms

Chapter 24. Sorting. Objectives. 1. To study and analyze time efficiency of various sorting algorithms Chapter 4 Sortig 1 Objectives 1. o study ad aalyze time efficiecy of various sortig algorithms 4. 4.7.. o desig, implemet, ad aalyze bubble sort 4.. 3. o desig, implemet, ad aalyze merge sort 4.3. 4. o

More information

Lecturers: Sanjam Garg and Prasad Raghavendra Feb 21, Midterm 1 Solutions

Lecturers: Sanjam Garg and Prasad Raghavendra Feb 21, Midterm 1 Solutions U.C. Berkeley CS170 : Algorithms Midterm 1 Solutios Lecturers: Sajam Garg ad Prasad Raghavedra Feb 1, 017 Midterm 1 Solutios 1. (4 poits) For the directed graph below, fid all the strogly coected compoets

More information

Heaps. Presentation for use with the textbook Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015

Heaps. Presentation for use with the textbook Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015 Presetatio for use with the textbook Algorithm Desig ad Applicatios, by M. T. Goodrich ad R. Tamassia, Wiley, 201 Heaps 201 Goodrich ad Tamassia xkcd. http://xkcd.com/83/. Tree. Used with permissio uder

More information

CIS 121 Data Structures and Algorithms with Java Fall Big-Oh Notation Tuesday, September 5 (Make-up Friday, September 8)

CIS 121 Data Structures and Algorithms with Java Fall Big-Oh Notation Tuesday, September 5 (Make-up Friday, September 8) CIS 11 Data Structures ad Algorithms with Java Fall 017 Big-Oh Notatio Tuesday, September 5 (Make-up Friday, September 8) Learig Goals Review Big-Oh ad lear big/small omega/theta otatios Practice solvig

More information

15-859E: Advanced Algorithms CMU, Spring 2015 Lecture #2: Randomized MST and MST Verification January 14, 2015

15-859E: Advanced Algorithms CMU, Spring 2015 Lecture #2: Randomized MST and MST Verification January 14, 2015 15-859E: Advaced Algorithms CMU, Sprig 2015 Lecture #2: Radomized MST ad MST Verificatio Jauary 14, 2015 Lecturer: Aupam Gupta Scribe: Yu Zhao 1 Prelimiaries I this lecture we are talkig about two cotets:

More information

How do we evaluate algorithms?

How do we evaluate algorithms? F2 Readig referece: chapter 2 + slides Algorithm complexity Big O ad big Ω To calculate ruig time Aalysis of recursive Algorithms Next time: Litterature: slides mostly The first Algorithm desig methods:

More information

Pseudocode ( 1.1) Analysis of Algorithms. Primitive Operations. Pseudocode Details. Running Time ( 1.1) Estimating performance

Pseudocode ( 1.1) Analysis of Algorithms. Primitive Operations. Pseudocode Details. Running Time ( 1.1) Estimating performance Aalysis of Algorithms Iput Algorithm Output A algorithm is a step-by-step procedure for solvig a problem i a fiite amout of time. Pseudocode ( 1.1) High-level descriptio of a algorithm More structured

More information

why study sorting? Sorting is a classic subject in computer science. There are three reasons for studying sorting algorithms.

why study sorting? Sorting is a classic subject in computer science. There are three reasons for studying sorting algorithms. Chapter 5 Sortig IST311 - CIS65/506 Clevelad State Uiversity Prof. Victor Matos Adapted from: Itroductio to Java Programmig: Comprehesive Versio, Eighth Editio by Y. Daiel Liag why study sortig? Sortig

More information

Homework 1 Solutions MA 522 Fall 2017

Homework 1 Solutions MA 522 Fall 2017 Homework 1 Solutios MA 5 Fall 017 1. Cosider the searchig problem: Iput A sequece of umbers A = [a 1,..., a ] ad a value v. Output A idex i such that v = A[i] or the special value NIL if v does ot appear

More information

Minimum Spanning Trees

Minimum Spanning Trees Presetatio for use with the textbook, lgorithm esig ad pplicatios, by M. T. Goodrich ad R. Tamassia, Wiley, 0 Miimum Spaig Trees 0 Goodrich ad Tamassia Miimum Spaig Trees pplicatio: oectig a Network Suppose

More information

Sorting in Linear Time. Data Structures and Algorithms Andrei Bulatov

Sorting in Linear Time. Data Structures and Algorithms Andrei Bulatov Sortig i Liear Time Data Structures ad Algorithms Adrei Bulatov Algorithms Sortig i Liear Time 7-2 Compariso Sorts The oly test that all the algorithms we have cosidered so far is compariso The oly iformatio

More information

Data Structures Week #9. Sorting

Data Structures Week #9. Sorting Data Structures Week #9 Sortig Outlie Motivatio Types of Sortig Elemetary (O( 2 )) Sortig Techiques Other (O(*log())) Sortig Techiques 21.Aralık.2010 Boraha Tümer, Ph.D. 2 Sortig 21.Aralık.2010 Boraha

More information

A graphical view of big-o notation. c*g(n) f(n) f(n) = O(g(n))

A graphical view of big-o notation. c*g(n) f(n) f(n) = O(g(n)) ca see that time required to search/sort grows with size of We How do space/time eeds of program grow with iput size? iput. time: cout umber of operatios as fuctio of iput Executio size operatio Assigmet:

More information

Minimum Spanning Trees. Application: Connecting a Network

Minimum Spanning Trees. Application: Connecting a Network Miimum Spaig Tree // : Presetatio for use with the textbook, lgorithm esig ad pplicatios, by M. T. oodrich ad R. Tamassia, Wiley, Miimum Spaig Trees oodrich ad Tamassia Miimum Spaig Trees pplicatio: oectig

More information

Lecture Notes 6 Introduction to algorithm analysis CSS 501 Data Structures and Object-Oriented Programming

Lecture Notes 6 Introduction to algorithm analysis CSS 501 Data Structures and Object-Oriented Programming Lecture Notes 6 Itroductio to algorithm aalysis CSS 501 Data Structures ad Object-Orieted Programmig Readig for this lecture: Carrao, Chapter 10 To be covered i this lecture: Itroductio to algorithm aalysis

More information

Sorting 9/15/2009. Sorting Problem. Insertion Sort: Soundness. Insertion Sort. Insertion Sort: Running Time. Insertion Sort: Soundness

Sorting 9/15/2009. Sorting Problem. Insertion Sort: Soundness. Insertion Sort. Insertion Sort: Running Time. Insertion Sort: Soundness 9/5/009 Algorithms Sortig 3- Sortig Sortig Problem The Sortig Problem Istace: A sequece of umbers Objective: A permutatio (reorderig) such that a ' K a' a, K,a a ', K, a' of the iput sequece The umbers

More information

Algorithm Design Techniques. Divide and conquer Problem

Algorithm Design Techniques. Divide and conquer Problem Algorithm Desig Techiques Divide ad coquer Problem Divide ad Coquer Algorithms Divide ad Coquer algorithm desig works o the priciple of dividig the give problem ito smaller sub problems which are similar

More information

Ones Assignment Method for Solving Traveling Salesman Problem

Ones Assignment Method for Solving Traveling Salesman Problem Joural of mathematics ad computer sciece 0 (0), 58-65 Oes Assigmet Method for Solvig Travelig Salesma Problem Hadi Basirzadeh Departmet of Mathematics, Shahid Chamra Uiversity, Ahvaz, Ira Article history:

More information

Computational Geometry

Computational Geometry Computatioal Geometry Chapter 4 Liear programmig Duality Smallest eclosig disk O the Ageda Liear Programmig Slides courtesy of Craig Gotsma 4. 4. Liear Programmig - Example Defie: (amout amout cosumed

More information

condition w i B i S maximum u i

condition w i B i S maximum u i ecture 10 Dyamic Programmig 10.1 Kapsack Problem November 1, 2004 ecturer: Kamal Jai Notes: Tobias Holgers We are give a set of items U = {a 1, a 2,..., a }. Each item has a weight w i Z + ad a utility

More information

Data Structures and Algorithms Part 1.4

Data Structures and Algorithms Part 1.4 1 Data Structures ad Algorithms Part 1.4 Werer Nutt 2 DSA, Part 1: Itroductio, syllabus, orgaisatio Algorithms Recursio (priciple, trace, factorial, Fiboacci) Sortig (bubble, isertio, selectio) 3 Sortig

More information

Running Time ( 3.1) Analysis of Algorithms. Experimental Studies. Limitations of Experiments

Running Time ( 3.1) Analysis of Algorithms. Experimental Studies. Limitations of Experiments Ruig Time ( 3.1) Aalysis of Algorithms Iput Algorithm Output A algorithm is a step- by- step procedure for solvig a problem i a fiite amout of time. Most algorithms trasform iput objects ito output objects.

More information

Analysis of Algorithms

Analysis of Algorithms Aalysis of Algorithms Iput Algorithm Output A algorithm is a step-by-step procedure for solvig a problem i a fiite amout of time. Ruig Time Most algorithms trasform iput objects ito output objects. The

More information

Lecture 1: Introduction and Strassen s Algorithm

Lecture 1: Introduction and Strassen s Algorithm 5-750: Graduate Algorithms Jauary 7, 08 Lecture : Itroductio ad Strasse s Algorithm Lecturer: Gary Miller Scribe: Robert Parker Itroductio Machie models I this class, we will primarily use the Radom Access

More information

CSE 2320 Notes 8: Sorting. (Last updated 10/3/18 7:16 PM) Idea: Take an unsorted (sub)array and partition into two subarrays such that.

CSE 2320 Notes 8: Sorting. (Last updated 10/3/18 7:16 PM) Idea: Take an unsorted (sub)array and partition into two subarrays such that. CSE Notes 8: Sortig (Last updated //8 7:6 PM) CLRS 7.-7., 9., 8.-8. 8.A. QUICKSORT Cocepts Idea: Take a usorted (sub)array ad partitio ito two subarrays such that p q r x y z x y y z Pivot Customarily,

More information

. Written in factored form it is easy to see that the roots are 2, 2, i,

. Written in factored form it is easy to see that the roots are 2, 2, i, CMPS A Itroductio to Programmig Programmig Assigmet 4 I this assigmet you will write a java program that determies the real roots of a polyomial that lie withi a specified rage. Recall that the roots (or

More information

Analysis of Algorithms

Analysis of Algorithms Aalysis of Algorithms Ruig Time of a algorithm Ruig Time Upper Bouds Lower Bouds Examples Mathematical facts Iput Algorithm Output A algorithm is a step-by-step procedure for solvig a problem i a fiite

More information

Running Time. Analysis of Algorithms. Experimental Studies. Limitations of Experiments

Running Time. Analysis of Algorithms. Experimental Studies. Limitations of Experiments Ruig Time Aalysis of Algorithms Iput Algorithm Output A algorithm is a step-by-step procedure for solvig a problem i a fiite amout of time. Most algorithms trasform iput objects ito output objects. The

More information

Analysis Metrics. Intro to Algorithm Analysis. Slides. 12. Alg Analysis. 12. Alg Analysis

Analysis Metrics. Intro to Algorithm Analysis. Slides. 12. Alg Analysis. 12. Alg Analysis Itro to Algorithm Aalysis Aalysis Metrics Slides. Table of Cotets. Aalysis Metrics 3. Exact Aalysis Rules 4. Simple Summatio 5. Summatio Formulas 6. Order of Magitude 7. Big-O otatio 8. Big-O Theorems

More information

CHAPTER IV: GRAPH THEORY. Section 1: Introduction to Graphs

CHAPTER IV: GRAPH THEORY. Section 1: Introduction to Graphs CHAPTER IV: GRAPH THEORY Sectio : Itroductio to Graphs Sice this class is called Number-Theoretic ad Discrete Structures, it would be a crime to oly focus o umber theory regardless how woderful those topics

More information

Big-O Analysis. Asymptotics

Big-O Analysis. Asymptotics Big-O Aalysis 1 Defiitio: Suppose that f() ad g() are oegative fuctios of. The we say that f() is O(g()) provided that there are costats C > 0 ad N > 0 such that for all > N, f() Cg(). Big-O expresses

More information

6.854J / J Advanced Algorithms Fall 2008

6.854J / J Advanced Algorithms Fall 2008 MIT OpeCourseWare http://ocw.mit.edu 6.854J / 18.415J Advaced Algorithms Fall 2008 For iformatio about citig these materials or our Terms of Use, visit: http://ocw.mit.edu/terms. 18.415/6.854 Advaced Algorithms

More information

Data Structures and Algorithms. Analysis of Algorithms

Data Structures and Algorithms. Analysis of Algorithms Data Structures ad Algorithms Aalysis of Algorithms Outlie Ruig time Pseudo-code Big-oh otatio Big-theta otatio Big-omega otatio Asymptotic algorithm aalysis Aalysis of Algorithms Iput Algorithm Output

More information

Combination Labelings Of Graphs

Combination Labelings Of Graphs Applied Mathematics E-Notes, (0), - c ISSN 0-0 Available free at mirror sites of http://wwwmaththuedutw/ame/ Combiatio Labeligs Of Graphs Pak Chig Li y Received February 0 Abstract Suppose G = (V; E) is

More information

The isoperimetric problem on the hypercube

The isoperimetric problem on the hypercube The isoperimetric problem o the hypercube Prepared by: Steve Butler November 2, 2005 1 The isoperimetric problem We will cosider the -dimesioal hypercube Q Recall that the hypercube Q is a graph whose

More information

n n B. How many subsets of C are there of cardinality n. We are selecting elements for such a

n n B. How many subsets of C are there of cardinality n. We are selecting elements for such a 4. [10] Usig a combiatorial argumet, prove that for 1: = 0 = Let A ad B be disjoit sets of cardiality each ad C = A B. How may subsets of C are there of cardiality. We are selectig elemets for such a subset

More information

CS 683: Advanced Design and Analysis of Algorithms

CS 683: Advanced Design and Analysis of Algorithms CS 683: Advaced Desig ad Aalysis of Algorithms Lecture 6, February 1, 2008 Lecturer: Joh Hopcroft Scribes: Shaomei Wu, Etha Feldma February 7, 2008 1 Threshold for k CNF Satisfiability I the previous lecture,

More information

Lecture 6. Lecturer: Ronitt Rubinfeld Scribes: Chen Ziv, Eliav Buchnik, Ophir Arie, Jonathan Gradstein

Lecture 6. Lecturer: Ronitt Rubinfeld Scribes: Chen Ziv, Eliav Buchnik, Ophir Arie, Jonathan Gradstein 068.670 Subliear Time Algorithms November, 0 Lecture 6 Lecturer: Roitt Rubifeld Scribes: Che Ziv, Eliav Buchik, Ophir Arie, Joatha Gradstei Lesso overview. Usig the oracle reductio framework for approximatig

More information

Hash Tables. Presentation for use with the textbook Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015.

Hash Tables. Presentation for use with the textbook Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015. Presetatio for use with the textbook Algorithm Desig ad Applicatios, by M. T. Goodrich ad R. Tamassia, Wiley, 2015 Hash Tables xkcd. http://xkcd.com/221/. Radom Number. Used with permissio uder Creative

More information

Big-O Analysis. Asymptotics

Big-O Analysis. Asymptotics Big-O Aalysis 1 Defiitio: Suppose that f() ad g() are oegative fuctios of. The we say that f() is O(g()) provided that there are costats C > 0 ad N > 0 such that for all > N, f() Cg(). Big-O expresses

More information

Outline and Reading. Analysis of Algorithms. Running Time. Experimental Studies. Limitations of Experiments. Theoretical Analysis

Outline and Reading. Analysis of Algorithms. Running Time. Experimental Studies. Limitations of Experiments. Theoretical Analysis Outlie ad Readig Aalysis of Algorithms Iput Algorithm Output Ruig time ( 3.) Pseudo-code ( 3.2) Coutig primitive operatios ( 3.3-3.) Asymptotic otatio ( 3.6) Asymptotic aalysis ( 3.7) Case study Aalysis

More information

Data Structures Week #5. Trees (Ağaçlar)

Data Structures Week #5. Trees (Ağaçlar) Data Structures Week #5 Trees Ağaçlar) Trees Ağaçlar) Toros Gökarı Avrupa Gökarı October 28, 2014 Boraha Tümer, Ph.D. 2 Trees Ağaçlar) October 28, 2014 Boraha Tümer, Ph.D. 3 Outlie Trees Deiitios Implemetatio

More information

Algorithm. Counting Sort Analysis of Algorithms

Algorithm. Counting Sort Analysis of Algorithms Algorithm Coutig Sort Aalysis of Algorithms Assumptios: records Coutig sort Each record cotais keys ad data All keys are i the rage of 1 to k Space The usorted list is stored i A, the sorted list will

More information

Counting the Number of Minimum Roman Dominating Functions of a Graph

Counting the Number of Minimum Roman Dominating Functions of a Graph Coutig the Number of Miimum Roma Domiatig Fuctios of a Graph SHI ZHENG ad KOH KHEE MENG, Natioal Uiversity of Sigapore We provide two algorithms coutig the umber of miimum Roma domiatig fuctios of a graph

More information

Solution printed. Do not start the test until instructed to do so! CS 2604 Data Structures Midterm Spring, Instructions:

Solution printed. Do not start the test until instructed to do so! CS 2604 Data Structures Midterm Spring, Instructions: CS 604 Data Structures Midterm Sprig, 00 VIRG INIA POLYTECHNIC INSTITUTE AND STATE U T PROSI M UNI VERSI TY Istructios: Prit your ame i the space provided below. This examiatio is closed book ad closed

More information

Basic allocator mechanisms The course that gives CMU its Zip! Memory Management II: Dynamic Storage Allocation Mar 6, 2000.

Basic allocator mechanisms The course that gives CMU its Zip! Memory Management II: Dynamic Storage Allocation Mar 6, 2000. 5-23 The course that gives CM its Zip Memory Maagemet II: Dyamic Storage Allocatio Mar 6, 2000 Topics Segregated lists Buddy system Garbage collectio Mark ad Sweep Copyig eferece coutig Basic allocator

More information

Major CSL Write your name and entry no on every sheet of the answer script. Time 2 Hrs Max Marks 70

Major CSL Write your name and entry no on every sheet of the answer script. Time 2 Hrs Max Marks 70 NOTE:. Attempt all seve questios. Major CSL 02 2. Write your ame ad etry o o every sheet of the aswer script. Time 2 Hrs Max Marks 70 Q No Q Q 2 Q 3 Q 4 Q 5 Q 6 Q 7 Total MM 6 2 4 0 8 4 6 70 Q. Write a

More information

Graphs ORD SFO LAX DFW

Graphs ORD SFO LAX DFW Graphs SFO 337 1843 802 ORD LAX 1233 DFW Graphs A graph is a pair (V, E), where V is a set of odes, called vertices E is a collectio of pairs of vertices, called edges Vertices ad edges are positios ad

More information

Greedy Algorithms. Interval Scheduling. Greedy Algorithms. Interval scheduling. Greedy Algorithms. Interval Scheduling

Greedy Algorithms. Interval Scheduling. Greedy Algorithms. Interval scheduling. Greedy Algorithms. Interval Scheduling Greedy Algorithms Greedy Algorithms Witer Paul Beame Hard to defie exactly but ca give geeral properties Solutio is built i small steps Decisios o how to build the solutio are made to maximize some criterio

More information

DATA STRUCTURES. amortized analysis binomial heaps Fibonacci heaps union-find. Data structures. Appetizer. Appetizer

DATA STRUCTURES. amortized analysis binomial heaps Fibonacci heaps union-find. Data structures. Appetizer. Appetizer Data structures DATA STRUCTURES Static problems. Give a iput, produce a output. Ex. Sortig, FFT, edit distace, shortest paths, MST, max-flow,... amortized aalysis biomial heaps Fiboacci heaps uio-fid Dyamic

More information

Examples and Applications of Binary Search

Examples and Applications of Binary Search Toy Gog ITEE Uiersity of Queeslad I the secod lecture last week we studied the biary search algorithm that soles the problem of determiig if a particular alue appears i a sorted list of iteger or ot. We

More information

CS211 Fall 2003 Prelim 2 Solutions and Grading Guide

CS211 Fall 2003 Prelim 2 Solutions and Grading Guide CS11 Fall 003 Prelim Solutios ad Gradig Guide Problem 1: (a) obj = obj1; ILLEGAL because type of referece must always be a supertype of type of object (b) obj3 = obj1; ILLEGAL because type of referece

More information

Priority Queues. Binary Heaps

Priority Queues. Binary Heaps Priority Queues Biary Heaps Priority Queues Priority: some property of a object that allows it to be prioritized with respect to other objects of the same type Mi Priority Queue: homogeeous collectio of

More information

2. ALGORITHM ANALYSIS

2. ALGORITHM ANALYSIS 2. ALGORITHM ANALYSIS computatioal tractability survey of commo ruig times 2. ALGORITHM ANALYSIS computatioal tractability survey of commo ruig times Lecture slides by Kevi Waye Copyright 2005 Pearso-Addiso

More information

The Magma Database file formats

The Magma Database file formats The Magma Database file formats Adrew Gaylard, Bret Pikey, ad Mart-Mari Breedt Johaesburg, South Africa 15th May 2006 1 Summary Magma is a ope-source object database created by Chris Muller, of Kasas City,

More information

Thompson s Group F (p + 1) is not Minimally Almost Convex

Thompson s Group F (p + 1) is not Minimally Almost Convex Thompso s Group F (p + ) is ot Miimally Almost Covex Claire Wladis Thompso s Group F (p + ). A Descriptio of F (p + ) Thompso s group F (p + ) ca be defied as the group of piecewiseliear orietatio-preservig

More information

quality/quantity peak time/ratio

quality/quantity peak time/ratio Semi-Heap ad Its Applicatios i Touramet Rakig Jie Wu Departmet of omputer Sciece ad Egieerig Florida Atlatic Uiversity oca Rato, FL 3343 jie@cse.fau.edu September, 00 . Itroductio ad Motivatio. relimiaries

More information

Python Programming: An Introduction to Computer Science

Python Programming: An Introduction to Computer Science Pytho Programmig: A Itroductio to Computer Sciece Chapter 6 Defiig Fuctios Pytho Programmig, 2/e 1 Objectives To uderstad why programmers divide programs up ito sets of cooperatig fuctios. To be able to

More information

University of Waterloo Department of Electrical and Computer Engineering ECE 250 Algorithms and Data Structures

University of Waterloo Department of Electrical and Computer Engineering ECE 250 Algorithms and Data Structures Uiversity of Waterloo Departmet of Electrical ad Computer Egieerig ECE 250 Algorithms ad Data Structures Midterm Examiatio ( pages) Istructor: Douglas Harder February 7, 2004 7:30-9:00 Name (last, first)

More information

Elementary Educational Computer

Elementary Educational Computer Chapter 5 Elemetary Educatioal Computer. Geeral structure of the Elemetary Educatioal Computer (EEC) The EEC coforms to the 5 uits structure defied by vo Neuma's model (.) All uits are preseted i a simplified

More information

Computer Science Foundation Exam. August 12, Computer Science. Section 1A. No Calculators! KEY. Solutions and Grading Criteria.

Computer Science Foundation Exam. August 12, Computer Science. Section 1A. No Calculators! KEY. Solutions and Grading Criteria. Computer Sciece Foudatio Exam August, 005 Computer Sciece Sectio A No Calculators! Name: SSN: KEY Solutios ad Gradig Criteria Score: 50 I this sectio of the exam, there are four (4) problems. You must

More information

1.2 Binomial Coefficients and Subsets

1.2 Binomial Coefficients and Subsets 1.2. BINOMIAL COEFFICIENTS AND SUBSETS 13 1.2 Biomial Coefficiets ad Subsets 1.2-1 The loop below is part of a program to determie the umber of triagles formed by poits i the plae. for i =1 to for j =

More information

CS 11 C track: lecture 1

CS 11 C track: lecture 1 CS 11 C track: lecture 1 Prelimiaries Need a CMS cluster accout http://acctreq.cms.caltech.edu/cgi-bi/request.cgi Need to kow UNIX IMSS tutorial liked from track home page Track home page: http://courses.cms.caltech.edu/courses/cs11/material

More information

CMPT 125 Assignment 2 Solutions

CMPT 125 Assignment 2 Solutions CMPT 25 Assigmet 2 Solutios Questio (20 marks total) a) Let s cosider a iteger array of size 0. (0 marks, each part is 2 marks) it a[0]; I. How would you assig a poiter, called pa, to store the address

More information

CSC165H1 Worksheet: Tutorial 8 Algorithm analysis (SOLUTIONS)

CSC165H1 Worksheet: Tutorial 8 Algorithm analysis (SOLUTIONS) CSC165H1, Witer 018 Learig Objectives By the ed of this worksheet, you will: Aalyse the ruig time of fuctios cotaiig ested loops. 1. Nested loop variatios. Each of the followig fuctios takes as iput a

More information

Chapter 11. Friends, Overloaded Operators, and Arrays in Classes. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Chapter 11. Friends, Overloaded Operators, and Arrays in Classes. Copyright 2014 Pearson Addison-Wesley. All rights reserved. Chapter 11 Frieds, Overloaded Operators, ad Arrays i Classes Copyright 2014 Pearso Addiso-Wesley. All rights reserved. Overview 11.1 Fried Fuctios 11.2 Overloadig Operators 11.3 Arrays ad Classes 11.4

More information

Random Graphs and Complex Networks T

Random Graphs and Complex Networks T Radom Graphs ad Complex Networks T-79.7003 Charalampos E. Tsourakakis Aalto Uiversity Lecture 3 7 September 013 Aoucemet Homework 1 is out, due i two weeks from ow. Exercises: Probabilistic iequalities

More information

A New Morphological 3D Shape Decomposition: Grayscale Interframe Interpolation Method

A New Morphological 3D Shape Decomposition: Grayscale Interframe Interpolation Method A ew Morphological 3D Shape Decompositio: Grayscale Iterframe Iterpolatio Method D.. Vizireau Politehica Uiversity Bucharest, Romaia ae@comm.pub.ro R. M. Udrea Politehica Uiversity Bucharest, Romaia mihea@comm.pub.ro

More information

ECE4050 Data Structures and Algorithms. Lecture 6: Searching

ECE4050 Data Structures and Algorithms. Lecture 6: Searching ECE4050 Data Structures ad Algorithms Lecture 6: Searchig 1 Search Give: Distict keys k 1, k 2,, k ad collectio L of records of the form (k 1, I 1 ), (k 2, I 2 ),, (k, I ) where I j is the iformatio associated

More information

COSC 1P03. Ch 7 Recursion. Introduction to Data Structures 8.1

COSC 1P03. Ch 7 Recursion. Introduction to Data Structures 8.1 COSC 1P03 Ch 7 Recursio Itroductio to Data Structures 8.1 COSC 1P03 Recursio Recursio I Mathematics factorial Fiboacci umbers defie ifiite set with fiite defiitio I Computer Sciece sytax rules fiite defiitio,

More information

Computers and Scientific Thinking

Computers and Scientific Thinking Computers ad Scietific Thikig David Reed, Creighto Uiversity Chapter 15 JavaScript Strigs 1 Strigs as Objects so far, your iteractive Web pages have maipulated strigs i simple ways use text box to iput

More information

Exact Minimum Lower Bound Algorithm for Traveling Salesman Problem

Exact Minimum Lower Bound Algorithm for Traveling Salesman Problem Exact Miimum Lower Boud Algorithm for Travelig Salesma Problem Mohamed Eleiche GeoTiba Systems mohamed.eleiche@gmail.com Abstract The miimum-travel-cost algorithm is a dyamic programmig algorithm to compute

More information

CS200: Hash Tables. Prichard Ch CS200 - Hash Tables 1

CS200: Hash Tables. Prichard Ch CS200 - Hash Tables 1 CS200: Hash Tables Prichard Ch. 13.2 CS200 - Hash Tables 1 Table Implemetatios: average cases Search Add Remove Sorted array-based Usorted array-based Balaced Search Trees O(log ) O() O() O() O(1) O()

More information

Module 8-7: Pascal s Triangle and the Binomial Theorem

Module 8-7: Pascal s Triangle and the Binomial Theorem Module 8-7: Pascal s Triagle ad the Biomial Theorem Gregory V. Bard April 5, 017 A Note about Notatio Just to recall, all of the followig mea the same thig: ( 7 7C 4 C4 7 7C4 5 4 ad they are (all proouced

More information

Improved Random Graph Isomorphism

Improved Random Graph Isomorphism Improved Radom Graph Isomorphism Tomek Czajka Gopal Paduraga Abstract Caoical labelig of a graph cosists of assigig a uique label to each vertex such that the labels are ivariat uder isomorphism. Such

More information

BST Sequence of Operations

BST Sequence of Operations Splay Trees Problems with BSTs Because the shape of a BST is determied by the order that data is iserted, we ru the risk of trees that are essetially lists 12 21 20 32 24 37 15 40 55 56 77 2 BST Sequece

More information

Recursion. Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Review: Method Frames

Recursion. Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Review: Method Frames Uit 4, Part 3 Recursio Computer Sciece S-111 Harvard Uiversity David G. Sulliva, Ph.D. Review: Method Frames Whe you make a method call, the Java rutime sets aside a block of memory kow as the frame of

More information

Pattern Recognition Systems Lab 1 Least Mean Squares

Pattern Recognition Systems Lab 1 Least Mean Squares Patter Recogitio Systems Lab 1 Least Mea Squares 1. Objectives This laboratory work itroduces the OpeCV-based framework used throughout the course. I this assigmet a lie is fitted to a set of poits usig

More information

Fundamental Algorithms

Fundamental Algorithms Techische Uiversität Müche Fakultät für Iformatik Lehrstuhl für Effiziete Algorithme Dmytro Chibisov Sadeep Sadaada Witer Semester 2007/08 Solutio Sheet 6 November 30, 2007 Fudametal Algorithms Problem

More information

Graphs. Shortest Path and Topological Sort

Graphs. Shortest Path and Topological Sort Graphs Shortest Path ad Topological Sort Example Relatioal Networks School Friedship Network (from Moody 2001) Yeast Metabolic Network (from https://www.d.edu/~etworks/cell/) Terrorist Network (by Valdis

More information

Lower Bounds for Sorting

Lower Bounds for Sorting Liear Sortig Topics Covered: Lower Bouds for Sortig Coutig Sort Radix Sort Bucket Sort Lower Bouds for Sortig Compariso vs. o-compariso sortig Decisio tree model Worst case lower boud Compariso Sortig

More information

1 Graph Sparsfication

1 Graph Sparsfication CME 305: Discrete Mathematics ad Algorithms 1 Graph Sparsficatio I this sectio we discuss the approximatio of a graph G(V, E) by a sparse graph H(V, F ) o the same vertex set. I particular, we cosider

More information

On (K t e)-saturated Graphs

On (K t e)-saturated Graphs Noame mauscript No. (will be iserted by the editor O (K t e-saturated Graphs Jessica Fuller Roald J. Gould the date of receipt ad acceptace should be iserted later Abstract Give a graph H, we say a graph

More information

Chapter 1. Introduction to Computers and C++ Programming. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

Chapter 1. Introduction to Computers and C++ Programming. Copyright 2015 Pearson Education, Ltd.. All rights reserved. Chapter 1 Itroductio to Computers ad C++ Programmig Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Overview 1.1 Computer Systems 1.2 Programmig ad Problem Solvig 1.3 Itroductio to C++ 1.4 Testig

More information

Chapter 8. Strings and Vectors. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Chapter 8. Strings and Vectors. Copyright 2014 Pearson Addison-Wesley. All rights reserved. Chapter 8 Strigs ad Vectors Overview 8.1 A Array Type for Strigs 8.2 The Stadard strig Class 8.3 Vectors Slide 8-3 8.1 A Array Type for Strigs A Array Type for Strigs C-strigs ca be used to represet strigs

More information

Chapter 3 Classification of FFT Processor Algorithms

Chapter 3 Classification of FFT Processor Algorithms Chapter Classificatio of FFT Processor Algorithms The computatioal complexity of the Discrete Fourier trasform (DFT) is very high. It requires () 2 complex multiplicatios ad () complex additios [5]. As

More information

EE123 Digital Signal Processing

EE123 Digital Signal Processing Last Time EE Digital Sigal Processig Lecture 7 Block Covolutio, Overlap ad Add, FFT Discrete Fourier Trasform Properties of the Liear covolutio through circular Today Liear covolutio with Overlap ad add

More information

Analysis of Algorithms

Analysis of Algorithms Presetatio for use with the textbook, Algorithm Desig ad Applicatios, by M. T. Goodrich ad R. Tamassia, Wiley, 2015 Aalysis of Algorithms Iput 2015 Goodrich ad Tamassia Algorithm Aalysis of Algorithms

More information

Massachusetts Institute of Technology Lecture : Theory of Parallel Systems Feb. 25, Lecture 6: List contraction, tree contraction, and

Massachusetts Institute of Technology Lecture : Theory of Parallel Systems Feb. 25, Lecture 6: List contraction, tree contraction, and Massachusetts Istitute of Techology Lecture.89: Theory of Parallel Systems Feb. 5, 997 Professor Charles E. Leiserso Scribe: Guag-Ie Cheg Lecture : List cotractio, tree cotractio, ad symmetry breakig Work-eciet

More information

Lecture 18. Optimization in n dimensions

Lecture 18. Optimization in n dimensions Lecture 8 Optimizatio i dimesios Itroductio We ow cosider the problem of miimizig a sigle scalar fuctio of variables, f x, where x=[ x, x,, x ]T. The D case ca be visualized as fidig the lowest poit of

More information

Name of the Student: Unit I (Logic and Proofs) 1) Truth Table: Conjunction Disjunction Conditional Biconditional

Name of the Student: Unit I (Logic and Proofs) 1) Truth Table: Conjunction Disjunction Conditional Biconditional SUBJECT NAME : Discrete Mathematics SUBJECT CODE : MA 2265 MATERIAL NAME : Formula Material MATERIAL CODE : JM08ADM009 (Sca the above QR code for the direct dowload of this material) Name of the Studet:

More information

Abstract. Chapter 4 Computation. Overview 8/13/18. Bjarne Stroustrup Note:

Abstract. Chapter 4 Computation. Overview 8/13/18. Bjarne Stroustrup   Note: Chapter 4 Computatio Bjare Stroustrup www.stroustrup.com/programmig Abstract Today, I ll preset the basics of computatio. I particular, we ll discuss expressios, how to iterate over a series of values

More information

Chapter 8. Strings and Vectors. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

Chapter 8. Strings and Vectors. Copyright 2015 Pearson Education, Ltd.. All rights reserved. Chapter 8 Strigs ad Vectors Copyright 2015 Pearso Educatio, Ltd.. All rights reserved. Overview 8.1 A Array Type for Strigs 8.2 The Stadard strig Class 8.3 Vectors Copyright 2015 Pearso Educatio, Ltd..

More information