Algorithms : Discrete structures CS 330
. Algorithm sequence of metrication that describes def : a , unambiguously , how to solve a problem in a finite amount of time English baneful ly ! ) . pseudocode questions related to algorithms :/ code ( language / paradigm ) ? - how do we spicily sow ability / decidability them ? - does one exist to solve a given problem ✓ ? - does it always give the correct answer ? - correctness ( proofs ! ) } - time I space completely - how long does it take ? - how much memory does it take ? - does it compute in a reasonable amount of time ? - tractability ( END - how do we design them ? - many algorithmic paradigms to help ! , divide + conquer ) . . brute-force , greedy ( e.g
the man of two numbers , finding x , y e. g. : max ( x , y ) def : if x > g : return x else : return y correct ? ( un proof by cases )
e. g. , finding the mad of a sequence of values . are a , , ay . . max - sef ( [ a , as , - , an ] ) def : - ME a ' for i ← E . n ] : . me max ( m , ai ) return m - at end of " loop invariant " correct ? based on each for loop , m holds max . ai , and i = u at end of ai , . . . .
e. g , Insertion sort - sort ( [ a. , a a , def irisation . , an ] ) : . . for i ← LI . n ] : L . * i a j , a j swap - I else : break also based on loop invariant , [ ai , . , ai ] is sorted correct ? . . at end of each inner loop , and i = u at end
run forever or determine if some program P will e. g. , on some input X eventually terminate ( halt ) H ( p , × ) = { True if PK ) halts . I implement the function i - e False , if PG ) runs forever exists H suppose - - we can implement the function G , like so : def G Cp ) if H Lp , P ) look forever C) ← i. e. , G does the opposite of : about P what H reports ← else : return - what is the result of H ( G , G) ? - Edin ! H cannot exist ( we cannot implement it ! )
an algorithm to complete how long does it take ? : 1) algorithm trivial cans has no variables . x t ) too ! = I 00 × 99 × 98 x ( e . g. , compute . can run once to get estimate ) - constant runtime ( 2) algorithm performs the same # of operations for all inputs , 200 oooo ) ) . , Mak ( I , 2) , Max ( 1000 , 2000 ) , Mak G ooo ooo ( e - g runtime ( can run on any set of cuprite - constant to get estimate )
an algorithm to complete how long does it take ? Variable runtime is dependent on : - number of operations exegeegffhfgmnaadtfftfaeeac.in - speed of execution environment ← hen estimating y - cost your instruction theoretical runtime determined by - size of input Cs ) -
an algorithm to complete how long does it take ? some algorithm with inputs i. e. , for come up . iz , i , in . . . . , . . , in ) that computes with the function T ( i. , in the the algorithm for the given . carried out by # of operations wipes . the computational / runtime complicity - T represents of the algorithm .
. find timing function for maxey : e.g . max - sef ( ( ai , as , - , an ] ) def # times : . I ← M = a for i' . n ] : ← n - I ← ( z . , ai ) ← Tmaic ( n - l ) e z ( n - t ) max ( m m = ← I return m T( n ) 3 ( n - 1) t2 - I 3h - = - [ length of uiput list .
e- g. , find timing function for insertion - soft , - sort ( [ a. , aa , def irisation . , an ] ) F times : . . I for i ← LI . n ] c- n - : . for j e fi . In - l ) . 2 ] : ← It zt . . . if a j , > aj " ← : , aj ← u ai e ¥ ap - I break-lworst-caeranahp.is is also useful , but often more difficult ) case analysis ( average
antiemetic series ? I t zt . t n = : . . = 15 I t 2+3+4+5 e. g. , - x # terms ) ( avg - value =3 x 5 = ( Stl ) -2 x 5 . tu =( i. e. , I t 2T . n . . Z = CCn - i ) . @ - th - I ) I t z t n - . . . Z = n(n 2
e. g. , find timing function for insertion - soft , - sort ( [ a. , aa , def irisation . , an ] ) F times : . . I nCu for i ← LI . n ] c- n - : . : ← lt2t...t(n-lT for j e fi . 2 ] . if an > aj " ← : , aj ← u ai ¥ 97 - I breaks - nLu = 3 ¥ - Z Tcu ) I t 3 - I = n -
we have : so Tmakseq ( n ) - I 3 n = Tension - sort (a) = 3 ¥ - uz - I on the behavior of algorithms as uiputs grow large we often focus " runtime complicity i. e. , asymptotic " . as inputs grow large , we can ignore - growing slower - terms and constants of our runtime function - formalised in " notation " big - oh
t.si#axn4fti.:oo9:iIn ) " of notation ! X " abuse MT daff ! :c , ( there are infinitely many ) ⇐ F C , Xo ER where txzxo ( Ifk ) / E C - GG ) ) when dealing up strictly ✓ positive functions , me Jfk can lose the ) . operator a abs gcx ) # i > X Xo
X ' t 2x H E O ( XZ ) show that where tf X Z Xo ( x 72kt I E CX ) - need to find witnesses C , Xo - simplification we know that txzl ( ( x EX 2) n ( I Ex - ) ) : Fx ? I ( x ' t 2x t l E X 2+2 × 27 × 2 ) ' - - FX Z l ( x 't 2x H E 4 × 2 ) t - our witnesses C - - I - 4 , Xo - - tx Z l ( X 't 2x H E 042 ) E 4 × 2 ) → xztzxt l any larger Cor Xo will also serve as witnesses ! -
for polynomial flex ) " - ' t " t an X . ta , X t Ao = an x . . X " ) an f- 0 , ft x ) E O ( where . , a o EIR and an , an - i , . . , the term w/ the highest exponent ( aka the degree ) of i. e. a polynomial dominates its growth , asymptotically ( for large x )
to the analysis of our algorithms returning : - l E O ( n ) Tmakseq ( n ) 3 n = Tension - sort (a) = 3 ¥ - l E O ( rt ) - E i. e. , the worst - can asymptotic runtime complexity of is bounded by a linear function mak - sq , and " polynomial ( of degree 2) function insertion . sort u n u
" ) for CH , no n ! C- Oln n ! EOC ? ) - I - -_ nxcn - 1) X n ! - x 2 × 1 - - Enx . Xu Xu u x . . Eun hegler ! ) Eolulogu ) forced ,no=I loglu ! ) EOC ? ) " ) loglutlelogcu Enugu
- o relationships : useful big a , b E Rt ) and ( a s b s l ) ✓ ( : Xa Cf O ( E O ( Xa ) , but b ) b x x " Ef O ( bx ) × E O ( ax ) , but a b , c E Rt ) and ( a > l ) t ( a , b : xb E O ( at ) , but ax ¢ O Gb ) b ) , but x of o ( ( he ga E 064 x ) b ( log a x )
combinations of functions : if f Cx ) E 0cg . Cx ) ) and folk ) E O Cgd xD then f Cx ) tf Cx ) E O ( CN ) ) maa ( g . LN , ga if Atx ) E o ( gem ) and flat E Olg Cx ) ) then fix ) t fix ) E O ( g Cx ) ) ) E Olgolx ) ) if f , CH E 0cg . Cx ) ) and fix . GIN ) . fix ) then fix ) O Cgi Cx ) E
TIF ? C- O( 3) ✓ xz - 3 × EO( ⇒ X 42 logxeofrx ) ✓ 4xtIo0EO(x)V xtxlogx EOCX )X 3 × 2 - 42xE0( look )X 6x2tlogxE0Kwgx))X5x'oEO(z × - tooo )V
- O notation only an upper bound ! describes : ti g NB e.g ; if two algorithms have runtime complexities E 062 ) , it is possible for them to have very different asymptotic be linear , and the other constant time ! ) behavior ( one may Kx ) J a fix ) E O Ch Cx ) ) guy # glx ) E O Ch Cx ) ) f Cx ) # -
AN Ekg Cx ) ) ) fix ) is Aga ) ) for { fix ) # gcx ) ) x ER ⇐ F C , Xo EIR where V-xzx.ffflxyzc.GG ) ) gcx ) ✓ " age , Sx × .
* six :# am for fits : 'm ) ' Ii and fix ) is Ngk ) ) ⇒ fix ) is Olgcx ) ) i. e. , if I c. , Ca , Xo EIR where txzxo ( C , guys If Cx ) / E Cz g Cx ) ) Czlgcx ) ) ^ " sandwich ✓ " fcx ) f- Cx ) ou ✓ Cx ) bread g a guy # >
if ft x ) E O Cgl x ) ) we say that fix ) is of order g Cx ) , or that fu ) and glx ) one of the same order . when possible , we prefer to compare the order of ddfineut over their tag - O functions . many algorithms texts / authors use O - notation when they mean fniply O - notation mistakenly . but remember , 0 doesn't tell us the whole story ! two algorithms that are both 062 ) can still have very different runtime in practice ! ( constants matter )
rank these functions mimireasiug ( slowest tofadeet ) order of growth : 8- FIX )=4z × 6- f. ( x ) - 8 × 712 × 2-13 - - 424 × 71 ) 2- fzlx )=logdogx ) 9- fzcx ) - - Clogx ) Efscx )=x4heg × P ' 3- FIX ) - 4- fqcx )= 32 × +1000 1- fact ) - 2400 # ( x ) - 1.5 " X ! b- fscx ) -
classes of problems & complexity of algorithms that solve them : : there is a polynomial time solution ( ) " " P - " tractable " class problem : there is no known polynomial time solution - " intractable " problem ( doesn't necessarily mean it doesn't exist ! ) " unsolvable : no solution can exist ( e . g. , halting problem ) " problem - : problems whose solutions can be checked in polynomial time , ' l H - class NP but for which no known polynomial time solutions exist - complete : all problems " " in this class can be " transformed - class " NP into each other , and also belong to class NP
? m ¥ :::%%m I whose solutions can be ienfied wi i. e. , for all problems solving it in polynomial time ? ) polynomial time , does there exist an algorithm for ¥ - E .si#::u::iD m÷÷÷;÷÷÷÷÷÷÷÷÷÷÷ which most folks if P f- NP ( if p = NP , many problems could be done relatively easily ) ' K !
Recommend
More recommend