Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2014 Curt Hill Algorithm Analysis How Do We Determine the Complexity of Algorithms.

Similar presentations


Presentation on theme: "Copyright © 2014 Curt Hill Algorithm Analysis How Do We Determine the Complexity of Algorithms."— Presentation transcript:

1 Copyright © 2014 Curt Hill Algorithm Analysis How Do We Determine the Complexity of Algorithms

2 Two Flavors of Complexity Time and Space Time complexity deals with how many instructions need to be executed based on the number of inputs Space complexity deals with how much memory is required, also based on number of inputs –This is dependent on the data structures used –Book will not discuss this one Copyright © 2014 Curt Hill

3 Time Complexity We typically focus on one or several types of instructions These may be any of the following types: –Comparisons –Assignment statements Copyright © 2014 Curt Hill

4 Why These? There is considerable difference in the speeds of these instructions over expenses of various computers Moreover, there may be other support instructions that may be present or absent depending on the machine language used Yet these factors generally get rolled into the constants in Big O and similar notations Copyright © 2014 Curt Hill

5 Max of Sequence procedure max(a 1, a 2, …a n : integer) max := a 1 {Assignment on name} for i=2 to n if max < a i then max := a i return max {All done} There is an obvious and a subtle comparison Similarly there are obvious assignments and subtle Copyright © 2014 Curt Hill

6 Analysis For comparisons –N-1 comparisons in the if –The for uses N compares on whether i has reached its limit –Thus 2N – 1 comparisons Assignments is harder for it varies –Best case, first is max, just 1 –Worst case, list is in ascending order N –Average case, N/2 We would call this O(N) Copyright © 2014 Curt Hill

7 Bubble Copyright © 2014 Curt Hill procedure bubble(a 1,a 2 …a n :integer) swapped: Boolean n: integer; do swapped := false for j := 1 to n-1 if a j < a j+1 temp := a j a j := a j+1 a j+1 := temp swapped := true n := n – 1 until not swapped

8 Commentary This bubble sort is slightly better than that given in Rosen The largest element always sinks to the bottom, so why check it again? –This is the n:=n-1 It also stops sorting when there are no interchanges However, these do complicate the analysis Copyright © 2014 Curt Hill

9 Inner loop contents Inside the inner loop there will be two comparisons –One for loop, one for if Zero or four assignments 1 increment of control variable Copyright © 2014 Curt Hill

10 How many loops? Copyright © 2014 Curt Hill

11 Cases Copyright © 2014 Curt Hill

12 Commentary Typically sorts fall into three big O categories O(n 2 ) – bad sorts –Bubble, selection, insertion and others O(n log n) – good sorts –Quick, Heap, Merge Weird cases –Shell O(n ~1.25 ) Copyright © 2014 Curt Hill

13 Tractable and not Algorithms with explosively growing Big Os are called intractable Polynomial and slower growing are tractable –Algorithms with exponents larger than four are unusual Exponential, factorial and faster growing algorithms are generally intractable Copyright © 2014 Curt Hill

14 Tractable and Intractable Copyright © 2014 Curt Hill 10501003001000 O(n)10501003001000 O(n log n)332826652,4699,966 n2n2 1002,50010,00090,0001 x 10 7 n3n3 1000125,0001 x 10 7 2.7 x 10 8 1 x 10 10 2n2n 1024~10 16 ~10 161 ~10 623 Big n!3.6 x 10 7 ~10 65 ~10 201 ~10 744 Big

15 Worst and Average There are instances of problems where worst case estimates are intractable, but average cases are not We typically attempt to solve these, but if the program runs longer than desired we quit In problems that we know are intractable we may have to settle for an approximate solution Copyright © 2014 Curt Hill

16 P and NP Those problems with known solutions belong to a class labeled P There is also a class of problems labeled NP which are believed to have intractable solutions, but if a solution is known it can be checked in polynomial time There is also the NP complete set of problems –If any of them can be solved in polynomial time then all of them can be solved in polynomial time Copyright © 2014 Curt Hill

17 Traveling Salesman Problem Given a set of cities with distances between them Find the shortest route that visits each city exactly once and returns to the city of origin Variations are NP-Complete with others being even worse NP-Hard Copyright © 2014 Curt Hill

18 Knapsack Problem Given various articles of different weights and volumes Find the set which has a weight less than some maximum with the greatest volume Copyright © 2014 Curt Hill

19 Bin Packing Problem Given a set bins of a given volume Pack a set of items which are of differing volumes into a minimum number of bins If the number of bins is restricted to one this becomes the knapsack problem Copyright © 2014 Curt Hill

20 Worse than intractable? The halting problem show us that there are also unsolvable problems Clearly worse than intractable Copyright © 2014 Curt Hill

21 Exercises 3.3 –3, 7, 11, 17, 23 Copyright © 2014 Curt Hill


Download ppt "Copyright © 2014 Curt Hill Algorithm Analysis How Do We Determine the Complexity of Algorithms."

Similar presentations


Ads by Google