Presentation is loading. Please wait.

Presentation is loading. Please wait.

Analysis of Algorithms (complexity)

Similar presentations


Presentation on theme: "Analysis of Algorithms (complexity)"— Presentation transcript:

1 Analysis of Algorithms (complexity)

2 Search Example: Finding a Name in a List
Consider a list of N names ordered randomly. How many comparisons must be made in order to find if a given name is in the list? What if the list is in alphabetic order? John Sarah Bob Frank Sally Anita Barbara Sam Fred Wilma Alan Harold Carol Mike Tom Beth Alan Anita Barbara Beth Bob Carol Frank Fred Harold John Mike Sally Sam Sarah Tom Wilma

3 Algorithm SequentialSearch (of an array) found=“not”
for i = 1 to number of items in the array if array(i) = desired element found=“” i = number of items in array + 1 end if next i print “Element was ”; found; “ found” John Sarah Bob Frank Sally Anita Barbara Sam Fred Wilma Alan Harold Carol Mike Tom Beth

4 Algorithm binarySearch (a, first, last, desiredItem) mid = (first + last)/2 // approximate midpoint of array if (first > last) return false else if (desiredItem equals a[mid]) return true else if (desiredItem < a[mid]) return binarySearch (a, first, mid-1, desiredItem) else // desiredItem > a[mid] return binarySearch (a, mid+1, last, desiredItem) Alan Anita Barbara Beth Bob Carol Frank Fred Harold John Mike Sally Sam Sarah Tom Wilma

5 The Traveling Salesman Problem
Search Example: The Traveling Salesman Problem What is the shortest route a salesman can take to visit all of the cities in his territory and return home?

6 The Traveling Salesman Problem
Search Example: The Traveling Salesman Problem What is the shortest route a salesman can take to visit all of the cities in his territory and return home?

7 The (Real) Traveling Salesman “Problem”
As the number of cities increases, the time it takes to find an exact solution increases exponentially. Example: Number of cities # paths Time to solve (on a fast PC) almost instantaneously , second million 20 seconds … … … ,800,000,000,000,000 ? x ? Intractable!

8 Chess

9 Carrano (2006), Data Structures and Abstractions with Java

10 How Can We put a List in Alphabetic order?
John Sarah Bob Frank Sally Anita Barbara Sam Fred Wilma Alan Harold Carol Mike Tom Beth How much effort is required?

11

12 Eight Puzzle

13 Eight Puzzle 30 shuffle moves Compare:
best-first with depth factor: 26 moves to solution (7725 in CLOSED; 7498 left in OPEN) best-first without depth factor: 48 moves to solution (272 in CLOSED; 332 in OPEN)

14 Shown below is a screen shot of a breath-first search in process for the 15-puzzle. The optimal solution is known (by another method) to be located at depth 26. How long will it take the breadth-first algorithm to discover this?

15 Shown below is a screen shot of a breath-first search in process for the 15-puzzle. The optimal solution is known (by another method) to be located at depth 26. How long will it take the breadth-first algorithm to discover this? Time ti spent at level i ≈4*ti-1 (e.g., 53278/13034=4.09, 13034/3171=4.11, 3171/782=4.05). The search needs to go = 10 levels deeper. So, it will take approximately 53278*49 to 53278*410 seconds to find the optimal solution (depending upon whether it finds it at the beginning of the search of level 26 or at the end). Taking the lower limit, we get 53278*49 seconds/(3600sec/hr)/(24hrs/day)/ (365days/yr) = 443 years. A similar calculation for the upper limit (or just multiply by 4) gives 1772 years. So, between 443 and 1772 years!

16 Complexity Comparison
Carrano (2006), Data Structures and Abstractions with Java

17 Let f and g be functions mapping nonnegative reals into nonnegative reals. Then f is BIG OH of g, written f = O(g), if there exist positive constants n and c such that for x ≥ n, f(x) ≤ cg(x). In this case, an algorithm's time requirement f(x) is of order at most g(x).

18 Data Structures Lists Stacks Queues Trees Graphs A B C D D C B A A B C


Download ppt "Analysis of Algorithms (complexity)"

Similar presentations


Ads by Google