Presentation is loading. Please wait.

Presentation is loading. Please wait.

John Levine, Computer and Information Sciences, Strathclyde University 1 Algorithms and Complexity 2: Complexity Notation.

Similar presentations


Presentation on theme: "John Levine, Computer and Information Sciences, Strathclyde University 1 Algorithms and Complexity 2: Complexity Notation."— Presentation transcript:

1 John Levine, Computer and Information Sciences, Strathclyde University http://www.cis.strath.ac.uk/~johnl/ 1 Algorithms and Complexity 2: Complexity Notation John Levine John.Levine@cis.strath.ac.uk

2 John Levine, Computer and Information Sciences, Strathclyde University http://www.cis.strath.ac.uk/~johnl/ 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

3 John Levine, Computer and Information Sciences, Strathclyde University http://www.cis.strath.ac.uk/~johnl/ 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

4 John Levine, Computer and Information Sciences, Strathclyde University http://www.cis.strath.ac.uk/~johnl/ 4 Today Size matters Complexity notation Calculating complexities 2

5 John Levine, Computer and Information Sciences, Strathclyde University http://www.cis.strath.ac.uk/~johnl/ 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 / 5 + 2 –a linear function –dominant element as data size grows is n, so using big-oh notation = O(n)

6 John Levine, Computer and Information Sciences, Strathclyde University http://www.cis.strath.ac.uk/~johnl/ 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++;

7 John Levine, Computer and Information Sciences, Strathclyde University http://www.cis.strath.ac.uk/~johnl/ 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 2 + 1000n + 2000000 = O(n 3 ) 3n 3 + 5 = O(n 3 )

8 John Levine, Computer and Information Sciences, Strathclyde University http://www.cis.strath.ac.uk/~johnl/ 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

9 John Levine, Computer and Information Sciences, Strathclyde University http://www.cis.strath.ac.uk/~johnl/ 9 Common classes of Big-Oh

10 John Levine, Computer and Information Sciences, Strathclyde University http://www.cis.strath.ac.uk/~johnl/ 10 Tyranny of complexity 1000 items per sec plus 10 secs startup

11 John Levine, Computer and Information Sciences, Strathclyde University http://www.cis.strath.ac.uk/~johnl/ 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 =

12 John Levine, Computer and Information Sciences, Strathclyde University http://www.cis.strath.ac.uk/~johnl/ 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)

13 John Levine, Computer and Information Sciences, Strathclyde University http://www.cis.strath.ac.uk/~johnl/ 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

14 John Levine, Computer and Information Sciences, Strathclyde University http://www.cis.strath.ac.uk/~johnl/ 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

15 John Levine, Computer and Information Sciences, Strathclyde University http://www.cis.strath.ac.uk/~johnl/ 15 Summary Size matters Complexity notation Calculating complexities Next time: start searching & sorting Ask the class quiz


Download ppt "John Levine, Computer and Information Sciences, Strathclyde University 1 Algorithms and Complexity 2: Complexity Notation."

Similar presentations


Ads by Google