Advced Algorithmic Prolem Solvig Le Mth d Serch Fredrik Heitz Dept of Computer d Iformtio Sciece Liköpig Uiversity
Outlie Arithmetic (l. d.) Solvig lier equtio systems (l. d.) Chiese remider theorem (l.5 d.) Prime umers d fctoriztio (l.7 d.8) Heuristic Serch (eercise )
Arithmetic Rge of defult iteger dt types (C++) usiged it = usiged log: (9 digits) usiged log log: (9 digits) How to represet 777! Opertios o Big Iteger Bsic: dd, sutrct, multiply, divide, etc Use high school method
Arithmetic Gretest Commo Divisor (Euclide Algorithm) GCD(, ) = GCD(, ) = GCD(, mod ) it gcd(it, it ) { retur ( ==? : gcd(, % )); } Lest Commo Multiplier LCM(, ) = * / GCD(, ) it lcm(it, it ) { retur ( / gcd(, ) * ); } // Q: why we write the lcm code this wy? GCD/LCM of more th umers: GCD(,, c) = GCD(, GCD(, c)) Fid d,, y such tht d = + y d d = GCD(,) (Eteded Euclide Algorithm) EGCD(,) = (,,) EGCD(,) (d,,y ) = EGCD(, mod ) (d,,y) = (d,y, /*y )
Arithmetic 5 Represetig rtiol umers. Pirs of itegers, where GCD(,)=. Represetig rtiol umers modulo m. The oly difficult opertio is iverse, = (mod m), where iverse eists if d oly if d m re co prime (gcd(,m)=). C e foud usig the Eteded Euclide Algorithm = (mod m) => = qm => qm= (d,, y) = EGCD(,m) => is the solutio iff d =.
Systems of Lier Equtios Mtri form Stdrd form 7 5.5 7 5.5 i differet forms lier equtios c e preseted A system of
Solutios of Lier Equtios 7 is solutio to the followig equtios : 5 7
Solutios of Lier Equtios 8 A set of equtios is icosistet if there eists o solutio to the system of equtios: 5 These equtios re icosistet 8
9 Solutios of Lier Equtios CISE_Topic 9 Some systems of equtios my hve ifiite umer of solutios solutio for ll is ).5( solutios hve ifiite umer of
Grphicl Solutio of Systems of Lier Equtios 5 Solutio =, = CISE_Topic
Crmer s Rule is Not Prcticl Crmer' s Rule c e used to solve the system 5 5, Crmer' s To solve N y N system requires (N )(N -)N! multiplictios. To solve y system,.8 It c e used if Rule is ot prcticl for lrge systems. the determits re computed i efficiet wy CISE_Topic 5 multiplictios re eeded.
CISE_Topic Nive Gussi Elimitio The method cosists of two steps: Forwrd Elimitio: the system is reduced to upper trigulr form. A sequece of elemetry opertios is used. Bckwrd Sustitutio: Solve the system strtig from the lst vrile. ' ' ' ' '
Elemetry Row Opertios Addig multiple of oe row to other Multiply y row y o zero costt CISE_Topic
CISE_Topic Emple: Forwrd Elimitio 8 7 8,, from equtios Elimite Step: Forwrd Elimitio : Prt 9 8 9 8
5 CISE_Topic 5 Emple: Forwrd Elimitio 9 5 fromequtio Elimite Step: 9 5, from equtios Elimite Step :
CISE_Topic Emple: Forwrd Elimitio 9 5 9 8 9 8 Elimitio : the Forwrd Summry of
7 CISE_Topic 7 Emple: Bckwrd Sustitutio () ) ( (), () ) ( 5 9,,...solve for for solve the, for Solve 9 5
8 CISE_Topic 8 Forwrd Elimitio i j i j i i i j i ij ij i i i j i ij ij ) ( To elimite ) ( To elimite
9 CISE_Topic 9 Forwrd Elimitio is elimited. Cotiue util ) ( To elimite k kk ik i i kj kk ik ij ij k i k j k
CISE_Topic Bckwrd Sustitutio i i i j j j i i i,,,,,,,,
Nive Gussi Elimitio o The method cosists of two steps o Forwrd Elimitio: the system is reduced to upper trigulr form. A sequece of elemetry opertios is used. o Bckwrd Sustitutio: Solve the system strtig from the lst vrile. Solve for,,. CISE_Topic ' ' ' ' '
CISE_Topic Emple 7 7 5 8 7 ) ( 8, from equtios Elimite Elimitio Step: Forwrd : Prt Gussi Elimitio : Nive usig Solve eq eq eq eq eq eq equtio pivot uchged eq
CISE_Topic Emple 8 5 7 7 5 ) ( 8 from equtio Elimite Step : Forwrd Elimitio : Prt eq eq eq equtio pivot uchged eq uchged eq
CISE_Topic Emple : Bckwrd Sustitutio The solutio is 8,,,,,,,
Determit 5 The elemetry opertios do ot ffect the determit Emple: Elemetry opertios A A' det(a) det(a') CISE_Topic 5
How My Solutios Does System of Equtios AX=B Hve? Uique det(a) reduced mtri hs o zero rows No solutio det(a) reduced mtri hs oe or more zero rows correspodig B elemets Ifiite det(a) reduced mtri hs oe or more zero rows correspodig B elemets CISE_Topic
7 CISE_Topic 7 Emples.5!.5 # : solutios ifite # of No solutio Uique X impossile X solutios Ifiite No solutio solutio X X X X X X
Pseudo-Code: Forwrd Elimitio 8 Do k = to Do i = k+ to fctor = i,k / k,k Do j = k+ to i,j = i,j fctor * k,j Ed Do i = i fctor * k Ed Do Ed Do CISE_Topic 8
Pseudo-Code: Bck Sustitutio 9 = /, Do i = dowto sum = i Do j = i+ to sum = sum i,j * j Ed Do i = sum / i,i Ed Do CISE_Topic 9
Prolems with Nive Gussi Elimitio o The Nive Gussi Elimitio my fil for very simple cses. (The pivotig elemet is zero). o Very smll pivotig elemet my result i serious computtio errors CISE_Topic
How Do We Kow If Solutio is Good or Not Give AX=B X is solutio if AX B= Compute the residul vector R= AX B Due to roudig error, R my ot e zero The solutio is cceptle if m i r i CISE_Topic
CISE_Topic How Good is the Solutio?....5 Residues :.75.98.9.87 5 8 5 R solutio
Chiese Remider Theorem First foud i ciet Chiese puzzle: There re certi thigs whose umer is ukow. Repetedly divided y, the remider is ; y 5 the remider is ; d y 7 the remider is. Wht will e the umer? I moder ottio = (mod ) = (mod 5) = (mod 7)
Chiese Remider Theorem
Chiese Remider Theorem 5 Why is the solutio correct? = 7 + + 5 (mod 5) otice tht 7 = (mod ) = (mod 5) = (mod 7) = (mod ) = (mod 5) = (mod 7) 5 = (mod ) = (mod 5) = (mod 7)
7
Chiese Remider Theorem 8 For emple, cosider the prolem of fidig iteger such tht A rute force pproch coverts these cogrueces ito sets d writes the elemets out to the product of 5 = (the solutios modulo for ech cogruece): {, 5, 8,,, 7,,,, 9,, 5, 8,,, 7, 5, 5, 5, 59, } {, 7,, 5, 9,, 7,, 5, 9,, 7, 5, 55, 59, } {,,,,,,,,,, 5, 5, } To fid tht stisfies ll three cogrueces, itersect the three sets to get: {, } Which c e epressed s
Chiese Remider Theorem 9 Aother wy to fid solutio is with sic lger, modulr rithmetic, d stepwise sustitutio. We strt y trsltig these cogrueces ito equtios for some t, s, d u: Eq : = + t Eq : = + s Eq : = + 5u Sustitute from equtio ito cogruece : +t = (mod ) = > t = + s Sustitute t ito equtio : = +s Sustitute this ito cogruece : +s = (mod 5) => s = + 5u Filly, = +s = + (5u) = + u
Primes First prime d the oly eve prime: First primes: {,, 5, 7,,, 7, 9,, 9} Primes i rge: to : 5 primes to, : 8 primes to 7,99 :, primes to, :,9 primes Lrgest prime i siged it it =,7,8,7
Prime Testig Algorithms for testig if N is prime: isprime(n) First try: check if N is divisile y i ϵ [.. N ]? O(N) Improved : Is N divisile y i ϵ [.. sqrt(n)]? O(sqrt(N)) Improved : Is N divisile y i ϵ [, 5,.. sqrt(n)]? Oe test for i=, o eed to test other eve umers! O(sqrt(N)/) = O(sqrt(N)) Improved : Is N divisile y i ϵ primes sqrt(n) O(π(sqrt(N))) = O(sqrt(N)/log(sqrt(N))) π(m) = um of primes up to M For this, we eed smller primes eforehd
Prime Geertio Geerte primes etwee [ N]: Use itset of size N, set ll true ecept ide & Strt from i= util k*i > N If itset t ide iis o, cross ll multiple of i (i.e. tur off it t ide i) strtig from i*i Filly, whtever ot crossed re primes Emple:
Prime Testig d Geertio
Fctoriztio A iteger N c e epressed s: N = PF * N, where PF = prime fctor N' = other umer which is N / PF If N' =, stop; otherwise, repet N is reduced every time we fid divisor
Serch 5 Whe prolem is smll or (lmost) ll possiilities hve to e tried complete serch is cdidte pproch. To determie the fesiility of complete serch estimte the umer of clcultios tht hve to e mde i the worst cse. Itertive complete serch uses ested loops to geerte every possile complete solutio d filter out the vlid oes. Itertig over ll permuttios usig et_permuttio Itertig over ll susets usig it set techique Recursive complete serch eteds prtil solutio with oe elemet util complete d vlid solutio is foud. This pproch is ofte clled recursive cktrckig. Pruig is used to sigifictly improve the efficiecy y removig prtil solutios tht c ot led to solutio s soo s possile. I the est cse oly vlid solutios re geerted.
Summry Arithmetic (l. d.) Solvig lier equtio systems (l. d.) Chiese remider theorem (l.5 d.) Prime umers d fctoriztio (l.7 d.8) Heuristic Serch (eercise )