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 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
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
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
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?
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?
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) almost instantaneously almost 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? x ? x ?Intractable!
Chess
Carrano (2006), Data Structures and Abstractions with Java
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?
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)
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 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 = 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!
Complexity Comparison Carrano (2006), Data Structures and Abstractions with Java
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).
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…
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
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…
Data Structures ListsStacksQueuesTreesGraphs ABCD D C B A ABCD