John Levine, Computer and Information Sciences, Strathclyde University 1 Algorithms and Complexity 2: Complexity Notation John Levine
John Levine, Computer and Information Sciences, Strathclyde University 2 Last time What’s an algorithm then? –Intro and brief history –Sample use of algorithms –Sample algorithm - room count Course overview –Lectures, practical, labs/tuts, the exam
John Levine, Computer and Information Sciences, Strathclyde University 3 The travelling seller Mary, a computer seller, needs to visit 3 shops in a day (starting and finishing at the office): what’s the shortest route? What if there’s 12 shops? 5km 3km 2km 8km 12km
John Levine, Computer and Information Sciences, Strathclyde University 4 Today Size matters Complexity notation Calculating complexities 2
John Levine, Computer and Information Sciences, Strathclyde University 5 Download time from internet Say 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 is n / –a linear function –dominant element as data size grows is n, so using big-oh notation = O(n)
John Levine, Computer and Information Sciences, Strathclyde University 6 String Search Problem: –Find a string t in a longer string s Simple approach i=0; found = false; while (!found) & (i<s.length) j = 0; oksofar = true; while (oksofar) & (j<t.length()) if (s.charAt(i+j)!=t.charAt(j)) oksofar = false; j++ found = oksofar; i++;
John Levine, Computer and Information Sciences, Strathclyde University 7 Rules for big-oh notation Only record the highest, dominant, factor Ignore constants and multipliers Kind of like rounding 1,000,001 is roughly 1,000,000 so you say O(n 2 ) and not O(12n 2 ) 4n 3 + 3n n = O(n 3 ) 3n = O(n 3 )
John Levine, Computer and Information Sciences, Strathclyde University 8 Common classes of Big-Oh In increasing complexity roughly: - logarithmic O(log n) - linear O(n) - lin-log O(n log n) - quadratic O(n 2 ) - polynomial O(n k ), k > 1 - exponential O(a n ), n > 1 - factorial O(n!), n > 1
John Levine, Computer and Information Sciences, Strathclyde University 9 Common classes of Big-Oh
John Levine, Computer and Information Sciences, Strathclyde University 10 Tyranny of complexity 1000 items per sec plus 10 secs startup
John Levine, Computer and Information Sciences, Strathclyde University 11 = Big-Oh says “as the numbers grow the dominant factor will be no worse (<=) than given” –e.g. an O(n log n) function grows no faster than another t = n log n Big-Oh is used for the worst case - we will mostly talk worst, sometimes expected but never best nor average Also Ω(n) and θ(n) for >= and =
John Levine, Computer and Information Sciences, Strathclyde University 12 Simple String Search Peter Piper picked a peck of pickled pepper; A peck of pickled pepper Peter Piper picked. If Peter Piper picked a peck of pickled pepper, Where’s the peck of pickled pepper Peter Piper picked? Find pepper - lots of false starts Can you do better? Simple complexity is O(nm)
John Levine, Computer and Information Sciences, Strathclyde University 13 Better string search Start search at end and keep a table peter_piper picked a peck of pickled pepper pepperpiper_picked a peck of pickled pepper peter pepperpicked a peck of pickled pepper peter piper pepper a peck of pickled pepper peter piper pickedpepperk of pickled pepper peter piper picked a pecpepperickled pepper peter piper picked a peck pepperkled pepper peter piper picked a peck of picpepperepper peter piper picked a peck of picklpepperper peter piper picked a peck of pickledpepperr pepper peter piper picked a peck of pickled pepper 10 steps instead of 50 (I think) what about longer text? what about fewer letters - e.g. DNA coding? AGCCCGAACATTTACGCCGCTGGCGACTGCACCG
John Levine, Computer and Information Sciences, Strathclyde University 14 String search Basic algorithm is O(nm) Best algorithm is O(n) –BUT is much more complex Have to think of when it matters –is data big enough for more complex soln –watch constants and multipliers
John Levine, Computer and Information Sciences, Strathclyde University 15 Summary Size matters Complexity notation Calculating complexities Next time: start searching & sorting Ask the class quiz