Presentation is loading. Please wait.

Presentation is loading. Please wait.

Complexity (Running Time)

Similar presentations


Presentation on theme: "Complexity (Running Time)"— Presentation transcript:

1 Complexity (Running Time)
Introduction to Computing Science and Programming I

2 Complexity An important part of Computer Science that deals with how difficult (complex) different problems are and how long it will take to solve them. Complexity is the term generally used to describe this area. For the most part we’ll be talking about how to determine the running-time of different algorithms.

3 Complexity Essentially what we’ll be doing is examining and comparing algorithms that are used to solve the same problem. If one algorithm has a shorter running-time, it is generally the better algorithm. (There are exceptions, but we won’t get into those) An example of a problem that has a wide array of algorithms for solving is that of sorting data.

4 Complexity Why is this important?
If you’re trying to write a program that solves a complex problem and deals with a large amount of data, using a bad algorithm can cause a slowdown of many orders of magnitude (multiples of 10) When dealing with simple problems and small amounts of data as we have for the most part in this course, it doesn’t always really matter, no matter how badly you write your program, it probably won’t take long to finish.

5 Running Time We’re going to start by looking at two algorithms for a guessing game. The guessing game works as follows. The computer tells the user to guess a number between 1 and 100 The computer tries to guess the user’s number, repeating until it guesses correctly

6 Running Time Here’s a simple program that will work
print "Think of a number between 1 and 100." guess = 1 answer = "" while answer != "equal": answer = raw_input("Is your number equal to or not equal to " \ + str(guess) + "?") guess = guess + 1

7 Running Time How many guesses might the computer need to make before getting the correct answer? Now let’s look at another algorithm for the game. This algorithm is discussed in some detail in the guide.

8 Running Time print "Think of a number from 1 to 100." smallest = 1
largest = 100 answer = "“ while answer != "equal": guess = (smallest + largest) / 2 answer = raw_input( "Is your number ’more’, ’less’," \ " or ’equal’ to " + str(guess) + "? " ) if answer == "more": smallest = guess + 1 elif answer == "less": largest = guess - 1 print smallest, largest print "I got it!"

9 Running Time How many guesses might the computer have to make with this algorithm? The range of possibilities is printed out each time the loop repeats. Look at how the values change if the user’s guess is 2. possibilities 1-5 5 1-2 2 2-2 1 The number of possibilities is (approximately) halved each time. 1_________________________________________________100 1_____________________49 1___________24 1______11

10 Running Time With the first algorithm we could have as many as 100 guesses. With the second algorithm halving the number of possibilities after every guess, there’ll never be more than 7 guesses.

11 Running Time Let’s generalize these algorithms to a game where the user guesses a number between 1 and n. The first algorithm will need up to n guesses. The second algorithm will need up to log2 n steps.   represent the ceiling operator, 1.2 = 2 etc. log2 x is the inverse of 2x log2 8 = 3 log2 1 = 0 log2 16 = 4 log = 10 log = 20 This illustrates how much more powerful the second algorithm is

12 Running Time The running time for an algorithm tells you approximately how many steps the algorithm will take to complete. We will just look at worst case running times though examining average or best case running times can also be useful.

13 Running TIme Repeated Letters
Check a string for any repeated letters (not necessarily consecutive) write “Enter the word:” read word set counter to 0 for all letters letter a in the word, do this: for all letters letter b to the right of letter a, do this: if letter a is equal to letter b then set counter to counter+1 if counter > 0 then write “There are repetitions” else write “No repetitions”

14 Running Time The two loops compare every character with all the characters that come after it. If the string is n characters long this works out to n(n-1)/2 = n2/2 - n/2 steps We’ll simplify this to say that the algorithm has a running time of approximately n2 steps We’ll discuss why the n/2 and the ½ coefficient in front of the n2 aren’t too important later

15 Running Time Subset Sum
Given a set of numbers and a target, determine if some subset sums to the target value for every subset in the list: set sum to the sum of this subset if sum is equal to target : answer “yes” and quit answer “no”

16 Running Time How many steps will the algorithm take?
The loop repeats for every possible subset. Each element is either in, or not in, each subset so if there are n elements there are 2n subsets giving about 2n steps. We actually get about n2n total since finding the sum can take n steps, but the exponential part the important part. Exponential algorithms are terribly slow.

17 Running Time Assuming the loop is repeated 1000 times per second here are running times for the algorithm.

18 Running Time So far we’ve just talked about “steps” in general.
To be specific you could just count the number of lines of code that would be executed. However, we’re generally just concerned with the parts of the program that will repeat the highest number of times (loops) and just with how many times they repeat, not how many lines of code are within them.

19 Running Time The innermost loop will be repeated most. For nested loops multiply the number of times the outer loop repeats by the number of times the inner loop repeats. code … for i from 1 to n: for j from 1 to n: code The outer loop repeats n times, so does the inner which gives n * n = n2

20 Running Time Using algorithms with good running times becomes extremely important when a program may deal with large inputs. As to why we throw out smaller terms and leading coefficients. Any running time involving a more significant term (2n > n2 > n) will always become larger than a running time only involving smaller terms as n becomes large. This is true independent of coefficients for the terms

21 Running Time


Download ppt "Complexity (Running Time)"

Similar presentations


Ads by Google