analysis of algorithms (complexity). search example: finding a name in a list consider a list of n...
TRANSCRIPT
Analysis of AlgorithmsAnalysis of Algorithms(complexity)(complexity)
Search Example:Search Example:
Finding a Name in a ListFinding a Name in a ListConsider a list of N names ordered randomly.Consider a list of N names ordered randomly.
1.1.How many comparisons must be made in order to find if a given How many comparisons must be made in order to find if a given name is in the list?name is in the list?
2.2.What if the list is in alphabetic order?What if the list is in alphabetic order?
JohnSarahBobFrankSallyAnitaBarbaraSamFredWilmaAlanHaroldCarolMikeTomBeth
AlanAnitaBarbaraBethBobCarolFrankFredHaroldJohnMikeSallySamSarahTomWilma
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”
JohnSarahBobFrankSallyAnitaBarbaraSamFredWilmaAlanHaroldCarolMikeTomBeth
Algorithm binarySearch (a, first, last, desiredItem)mid = (first + last)/2 // approximate midpoint of arrayif (first > last)
return falseelse if (desiredItem equals a[mid])
return trueelse if (desiredItem < a[mid])
return binarySearch (a, first, mid-1, desiredItem)else // desiredItem > a[mid]
return binarySearch (a, mid+1, last, desiredItem)
AlanAnitaBarbaraBethBobCarolFrankFredHaroldJohnMikeSallySamSarahTomWilma
Search Example:Search Example:
The Traveling Salesman ProblemThe Traveling Salesman Problem
What is the shortest route a salesman can take What is the shortest route a salesman can take to visit all of the cities in his territory and return to visit all of the cities in his territory and return home?home?
Search Example:Search Example:
The Traveling Salesman ProblemThe Traveling Salesman Problem
What is the shortest route a salesman can What is the shortest route a salesman can take to visit all of the cities in his territory take to visit all of the cities in his territory and return home?and return home?
The (Real) Traveling Salesman “Problem”The (Real) Traveling Salesman “Problem”
As the number of cities increases, the time it takes to As the number of cities increases, the time it takes to find an exact solution increases exponentially.find an exact solution increases exponentially.
Example:Example:
Number of citiesNumber of cities # paths# paths Time to solve (on a fast PC)Time to solve (on a fast PC)
88 25202520 almost instantaneouslyalmost instantaneously
1010 181,440181,440 1 second1 second
1212 20 million 20 million 20 seconds20 seconds
… … …… ……
2020 60,800,000,000,000,00060,800,000,000,000,000 ??
100100 4.67 x 104.67 x 10157157 ??
Intractable!Intractable!
Chess
Carrano (2006), Data Structures and Abstractions with Java
How Can We put a List in Alphabetic order?How Can We put a List in Alphabetic order?
JohnSarahBobFrankSallyAnitaBarbaraSamFredWilmaAlanHaroldCarolMikeTomBeth
How much effort is required?How much effort is required?
Eight Puzzle
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?
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 26-16 = 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!
Complexity ComparisonComplexity 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 x0 and c such that for x ≥ x0, 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
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?
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 stringAlgorithm 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 stringAlgorithm used to perform the match
Suggest some techniques and evaluate each…
Data StructuresData StructuresListsLists
StacksStacks
QueuesQueues
TreesTrees
GraphsGraphs
AA BB CC DD
DD
CC
BB
AA
AA BB CC DD