Algorithms and Complexity 2: Complexity Notation

  • Published on
    03-Jan-2016

  • View
    33

  • Download
    4

Embed Size (px)

DESCRIPTION

Algorithms and Complexity 2: Complexity Notation. John Levine John.Levine@cis.strath.ac.uk. Last time. Whats an algorithm then? Intro and brief history Sample use of algorithms Sample algorithm - room count Course overview Lectures, practical, labs/tuts, the exam. The travelling seller. - PowerPoint PPT Presentation

Transcript

  • Algorithms and Complexity2: Complexity NotationJohn LevineJohn.Levine@cis.strath.ac.uk

  • Last timeWhats an algorithm then?Intro and brief historySample use of algorithmsSample algorithm - room countCourse overviewLectures, practical, labs/tuts, the exam

  • The travelling sellerMary, a computer seller, needs to visit 3 shops in a day (starting and finishing at the office): whats the shortest route?

    What if theres 12 shops?5km3km2km8km12km

  • TodaySize mattersComplexity notationCalculating complexities2

  • Download time from internetSay it takes 2s to establish a link and then we download at 5K/s, then if n is size of file in K:time to download isn/5 + 2a linear functiondominant element as data size grows is n, so using big-oh notation = O(n)

  • String SearchProblem:Find a string t in a longer string sSimple approach i=0; found = false; while (!found) & (i
  • Rules for big-oh notationOnly record the highest, dominant, factorIgnore constants and multipliersKind of like rounding1,000,001 is roughly 1,000,000so you say O(n2) and not O(12n2) 4n3 + 3n2 + 1000n + 2000000 = O(n3)3n3 + 5 = O(n3)

  • Common classes of Big-OhIn increasing complexity roughly:- logarithmic O(log n)- linear O(n)- lin-log O(n log n)- quadratic O(n2)- polynomial O(nk), k > 1- exponential O(an), n > 1- factorial O(n!), n > 1

  • Common classes of Big-Oh

    Chart1

    010121

    122482

    1.584962500734.75488750229186

    248163224

    2.3219280949511.60964047442550120

    2.5849625007615.50977500433672720

    2.8073549221719.651484454449985040

    38246412840320

    3.1699250014928.52932501381162362880

    3.32192809491033.21928094891002003628800

    3.45943161861138.05374780512124239916800

    3.58496250071243.019550008714428812

    3.70043971811348.105716335816933813

    3.80735492211453.302968908819639214

    3.90689059561558.603358934122545015

    4166425651216

    4.08746284131769.486868301328957817

    4.16992500141875.05865002632464818

    4.24792751341980.710622755436172219

    4.32192809492086.438561897740080020

    4.39231742282192.238665878444188221

    4.45943161862298.1074956148496822

    4.523561956123104.0419249893529105823

    4.584962500724110.0391000173576115224

    4.643856189825116.0964047444625125025

    4.700439718126122.2114326717676135226

    4.754887502227128.3819625584729145827

    4.807354922128134.6059378176784156828

    4.857980995129140.8814488587841168229

    4.906890595630147.2067178683900180030

    4.954196310431153.580085622961192231

    5321601024204832

    5.044394119433166.46500593881089217833

    5.087462841334172.97373660251156231234

    5.129283016935179.52490559311225245035

    5.169925001436186.11730005191296259236

    5.209453365637192.74977452831369273837

    5.247927513438199.42124551091444288838

    5.285402218939206.13068653561521304239

    5.321928094940212.87712379551600320040

    5.357552004641219.65963218931681336241

    5.392317422842226.47733175671764352842

    5.426264754743233.32938445221849369843

    5.459431618644240.214991221936387244

    5.491853096345247.13338933482025405045

    5.523561956146254.08384997862116423246

    5.554588851747261.06567602882209441847

    5.584962500748268.07820003462304460848

    5.614709844149275.12078236162401480249

    5.643856189850282.19280948872500500050

    5.67242534251289.29369244052601520251

    5.700439718152296.42286534332704540852

    5.727920454653303.57978409182809561853

    5.754887502254310.76392511682916583254

    5.781359713555317.97478424393025605055

    5.807354922156325.21187563523136627256

    5.832890014257332.47473080743249649857

    5.857980995158339.76289771743364672858

    5.882643049459347.07593991233481696259

    5.906890595660354.41343573653600720060

    5.930737337661361.77497759133721744261

    5.954196310462369.1601712443844768862

    5.977279923563376.56863518053969793863

    6643844096819264

    6.02236781365391.45390784684225845065

    6.044394119466398.93001187774356871266

    6.066089190567406.42797576074489897867

    6.087462841368413.9474732054624924868

    6.108524456869421.48818751774761952269

    6.129283016970429.04981118614900980070

    6.149747119571436.632045484850411008271

    6.169925001472444.234600103851841036872

    6.189824558973451.857192798253291065873

    6.209453365674459.499549056554761095274

    6.228818690575467.161401787256251125075

    6.247927513476474.842491021757761155276

    6.266786540777482.542563633559291185877

    6.285402218978490.261373071360841216878

    6.303780748279497.99867910662411248279

    6.321928094980505.75424759164001280080

    6.339850002981513.527850233765611312281

    6.357552004682521.319264378767241344882

    6.375039431383529.128272801868891377883

    6.392317422884536.954663513470561411284

    6.409390936185544.798229571772251445085

    6.426264754786552.658768904473961479286

    6.442943495887560.536084138875691513887

    6.459431618688568.429982440177441548888

    6.47573343189576.34027535679211584289

    6.491853096390584.266778669781001620090

    6.507794640291592.209312258182811656291

    6.523561956192600.167699957284641692892

    6.539158811193608.14176943386491729893

    6.554588851794616.131352057788361767294

    6.569855608395624.136282791490251805095

    6.584962500796632.156400069292161843296

    6.599912842297640.191545692294091881897

    6.614709844198648.241564723396041920898

    6.629356620199656.306305387998011960299

    6.6438561898100664.38561897751000020000100

    log n

    n

    n log n

    n*n

    2*n*n

    n!

    number

    time

    Sheet2

    nlog nnn log nn*n2*n*nn!

    1010121

    2122482

    31.584962500734.75488750229186

    4248163224

    52.3219280949511.60964047442550120

    62.5849625007615.50977500433672720

    72.8073549221719.651484454449985040

    838246412840320

    93.1699250014928.52932501381162362880

    103.32192809491033.21928094891002003628800

    113.45943161861138.05374780512124239916800

    123.58496250071243.0195500087144288

    133.70043971811348.1057163358169338

    143.80735492211453.3029689088196392

    153.90689059561558.6033589341225450

    1641664256512

    174.08746284131769.4868683013289578

    184.16992500141875.058650026324648

    194.24792751341980.7106227554361722

    204.32192809492086.4385618977400800

    214.39231742282192.2386658784441882

    224.45943161862298.10749561484968

    234.523561956123104.04192498935291058

    244.584962500724110.03910001735761152

    254.643856189825116.09640474446251250

    264.700439718126122.21143267176761352

    274.754887502227128.38196255847291458

    284.807354922128134.60593781767841568

    294.857980995129140.88144885878411682

    304.906890595630147.20671786839001800

    314.954196310431153.5800856229611922

    3253216010242048

    335.044394119433166.465005938810892178

    345.087462841334172.973736602511562312

    355.129283016935179.524905593112252450

    365.169925001436186.117300051912962592

    375.209453365637192.749774528313692738

    385.247927513438199.421245510914442888

    395.285402218939206.130686535615213042

    405.321928094940212.877123795516003200

    415.357552004641219.659632189316813362

    425.392317422842226.477331756717643528

    435.426264754743233.329384452218493698

    445.459431618644240.2149912219363872

    455.491853096345247.133389334820254050

    465.523561956146254.083849978621164232

    475.554588851747261.065676028822094418

    485.584962500748268.078200034623044608

    495.614709844149275.120782361624014802

    505.643856189850282.192809488725005000

    515.67242534251289.293692440526015202

    525.700439718152296.422865343327045408

    535.727920454653303.579784091828095618

    545.754887502254310.763925116829165832

    555.781359713555317.974784243930256050

    565.807354922156325.211875635231366272

    575.832890014257332.474730807432496498

    585.857980995158339.762897717433646728

    595.882643049459347.075939912334816962

    605.906890595660354.413435736536007200

    615.930737337661361.774977591337217442

    625.954196310462369.16017124438447688

    635.977279923563376.568635180539697938

    6466438440968192

    656.02236781365391.453907846842258450

    666.044394119466398.930011877743568712

    676.066089190567406.427975760744898978

    686.087462841368413.94747320546249248

    696.108524456869421.488187517747619522

    706.129283016970429.049811186149009800

    716.149747119571436.6320454848504110082

    726.169925001472444.2346001038518410368

    736.189824558973451.8571927982532910658

    746.209453365674459.4995490565547610952

    756.228818690575467.1614017872562511250

    766.247927513476474.8424910217577611552

    776.266786540777482.5425636335592911858

    786.285402218978490.2613730713608412168

    796.303780748279497.998679106624112482

    806.321928094980505.754247591640012800

    816.339850002981513.5278502337656113122

    826.357552004682521.3192643787672413448

    836.375039431383529.1282728018688913778

    846.392317422884536.9546635134705614112

    856.409390936185544.7982295717722514450

    866.426264754786552.6587689044739614792

    876.442943495887560.5360841388756915138

    886.459431618688568.4299824401774415488

    896.47573343189576.340275356792115842

    906.491853096390584.2667786697810016200

    916.507794640291592.2093122581828116562

    926.523561956192600.1676999572846416928

    936.539158811193608.141769433864917298

    946.554588851794616.1313520577883617672

    956.569855608395624.1362827914902518050

    966.584962500796632.1564000692921618432

    976.599912842297640.1915456922940918818

    986.614709844198648.2415647233960419208

    996.629356620199656.3063053879980119602

    1006.6438561898100664.38561897751000020000

    Sheet3

  • Tyranny of complexity1000 items per sec plus 10 secs startup

  • =Big-Oh says as the numbers grow the dominant factor will be no worse (= and =

  • Simple String SearchPeter Piper picked a peck of pickled pepper; A peck of pickled pepper Peter Piper picked. If Peter Piper picked a peck of pickled pepper, Wheres the peck of pickled pepper Peter Piper picked?

    Find pepper - lots of false startsCan you do better?

    Simple complexity is O(nm)

  • Better string searchStart search at end and keep a tablepeter_piper picked a peck of pickled pepperpepperpiper_picked a peck of pickled pepperpeter pepperpicked a peck of pickled pepperpeter piper pepper a peck of pickled pepperpeter piper pickedpepperk of pickled pepperpeter piper picked a pecpepperickled pepperpeter piper picked a peck pepperkled pepperpeter piper picked a peck of picpepperepperpeter piper picked a peck of picklpepperperpeter piper picked a peck of pickledpepperrpeter piper picked a peck of pickled pepper10 steps instead of 50 (I think)what about longer text?what about fewer letters - e.g. DNA coding?AGCCCGAACATTTACGCCGCTGGCGACTGCACCG

  • String searchBasic algorithm is O(nm)Best algorithm is O(n)BUT is much more complex

    Have to think of when it mattersis data big enough for more complex solnwatch constants and multipliers

  • SummarySize mattersComplexity notationCalculating complexitiesNext time: start searching & sorting

    Ask the class quiz

Recommended

View more >