Analysis of Algorithms (complexity). Search Example: Finding a Name in a List Consider a list of N...
-
Upload
beryl-barton -
Category
Documents
-
view
222 -
download
3
Transcript of Analysis of Algorithms (complexity). Search Example: Finding a Name in a List Consider a list of N...
![Page 1: 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.](https://reader036.fdocuments.us/reader036/viewer/2022062313/56649f505503460f94c72cda/html5/thumbnails/1.jpg)
Analysis of AlgorithmsAnalysis of Algorithms(complexity)(complexity)
![Page 2: 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.](https://reader036.fdocuments.us/reader036/viewer/2022062313/56649f505503460f94c72cda/html5/thumbnails/2.jpg)
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
![Page 3: 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.](https://reader036.fdocuments.us/reader036/viewer/2022062313/56649f505503460f94c72cda/html5/thumbnails/3.jpg)
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
![Page 4: 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.](https://reader036.fdocuments.us/reader036/viewer/2022062313/56649f505503460f94c72cda/html5/thumbnails/4.jpg)
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
![Page 5: 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.](https://reader036.fdocuments.us/reader036/viewer/2022062313/56649f505503460f94c72cda/html5/thumbnails/5.jpg)
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?
![Page 6: 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.](https://reader036.fdocuments.us/reader036/viewer/2022062313/56649f505503460f94c72cda/html5/thumbnails/6.jpg)
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?
![Page 7: 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.](https://reader036.fdocuments.us/reader036/viewer/2022062313/56649f505503460f94c72cda/html5/thumbnails/7.jpg)
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!
![Page 8: 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.](https://reader036.fdocuments.us/reader036/viewer/2022062313/56649f505503460f94c72cda/html5/thumbnails/8.jpg)
Chess
![Page 9: 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.](https://reader036.fdocuments.us/reader036/viewer/2022062313/56649f505503460f94c72cda/html5/thumbnails/9.jpg)
Carrano (2006), Data Structures and Abstractions with Java
![Page 10: 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.](https://reader036.fdocuments.us/reader036/viewer/2022062313/56649f505503460f94c72cda/html5/thumbnails/10.jpg)
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?
![Page 11: 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.](https://reader036.fdocuments.us/reader036/viewer/2022062313/56649f505503460f94c72cda/html5/thumbnails/11.jpg)
![Page 12: 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.](https://reader036.fdocuments.us/reader036/viewer/2022062313/56649f505503460f94c72cda/html5/thumbnails/12.jpg)
Eight Puzzle
![Page 13: 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.](https://reader036.fdocuments.us/reader036/viewer/2022062313/56649f505503460f94c72cda/html5/thumbnails/13.jpg)
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)
![Page 14: 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.](https://reader036.fdocuments.us/reader036/viewer/2022062313/56649f505503460f94c72cda/html5/thumbnails/14.jpg)
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?
![Page 15: 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.](https://reader036.fdocuments.us/reader036/viewer/2022062313/56649f505503460f94c72cda/html5/thumbnails/15.jpg)
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!
![Page 16: 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.](https://reader036.fdocuments.us/reader036/viewer/2022062313/56649f505503460f94c72cda/html5/thumbnails/16.jpg)
Complexity ComparisonComplexity Comparison
Carrano (2006), Data Structures and Abstractions with Java
![Page 17: 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.](https://reader036.fdocuments.us/reader036/viewer/2022062313/56649f505503460f94c72cda/html5/thumbnails/17.jpg)
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
![Page 18: 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.](https://reader036.fdocuments.us/reader036/viewer/2022062313/56649f505503460f94c72cda/html5/thumbnails/18.jpg)
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?
![Page 19: 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.](https://reader036.fdocuments.us/reader036/viewer/2022062313/56649f505503460f94c72cda/html5/thumbnails/19.jpg)
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…
![Page 20: 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.](https://reader036.fdocuments.us/reader036/viewer/2022062313/56649f505503460f94c72cda/html5/thumbnails/20.jpg)
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
![Page 21: 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.](https://reader036.fdocuments.us/reader036/viewer/2022062313/56649f505503460f94c72cda/html5/thumbnails/21.jpg)
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…
![Page 22: 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.](https://reader036.fdocuments.us/reader036/viewer/2022062313/56649f505503460f94c72cda/html5/thumbnails/22.jpg)
Data StructuresData StructuresListsLists
StacksStacks
QueuesQueues
TreesTrees
GraphsGraphs
AA BB CC DD
DD
CC
BB
AA
AA BB CC DD