Presentation is loading. Please wait.

Presentation is loading. Please wait.

Analysis of Algorithms (complexity). Search Example: Finding a Name in a List Consider a list of N names ordered randomly. 1.How many comparisons must.

Similar presentations


Presentation on theme: "Analysis of Algorithms (complexity). Search Example: Finding a Name in a List Consider a list of N names ordered randomly. 1.How many comparisons must."— Presentation transcript:

1 Analysis of Algorithms (complexity)

2 Search Example: Finding a Name in a List Consider a list of N names ordered randomly. 1.How many comparisons must be made in order to find if a given name is in the list? 2.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, mid+1, last, desiredItem) Alan Anita Barbara Beth Bob Carol Frank Fred Harold John Mike Sally Sam Sarah Tom Wilma

5 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 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# pathsTime to solve (on a fast PC) 8 2520almost instantaneously 8 2520almost instantaneously 10181,4401 second 10181,4401 second 1220 million 20 seconds 1220 million 20 seconds ……… ……… 2060,800,000,000,000,000? 2060,800,000,000,000,000? 1004.67 x 10 157 ? 1004.67 x 10 157 ?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 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 Time t i spent at level i ≈4*t i-1 (e.g., 53278/13034=4.09, 13034/3171=4.11, 3171/782=4.05). The search needs to go 26-16 = 10 levels deeper. So, it will take approximately 53278*4 9 to 53278*4 10 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*4 9 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 x 0 and c such that for x ≥ x 0, f(x) ≤ cg(x). In this case, an algorithm's time requirement f(x) is of order at most g(x). http://upload.wikimedia.org/wikipedia/commons/8/89/Big-O-notation.png

18 Consider a pattern matching algorithm whose purpose is to find all occurrences of string A in string B where A < B. What is the complexity of the task?

19 Identify the factors that determine the complexity…

20 Consider a pattern matching algorithm whose purpose is to find all occurrences of string A in string B where A < B. What is the complexity of the task? Identify the factors that determine the complexity… Length of each string Algorithm used to perform the match

21 Consider a pattern matching algorithm whose purpose is to find all occurrences of string A in string B where A < B. What is the complexity of the task? Identify the factors that determine the complexity… Length of each string Algorithm used to perform the match Suggest some techniques and evaluate each…

22 Data Structures ListsStacksQueuesTreesGraphs ABCD D C B A ABCD


Download ppt "Analysis of Algorithms (complexity). Search Example: Finding a Name in a List Consider a list of N names ordered randomly. 1.How many comparisons must."

Similar presentations


Ads by Google