complexity and efficiency computer science 3 gerb objective: understand complexity and efficiency of...
DESCRIPTION
Algorithm 1 position = 0, equal=true IF different lengths THEN equal=false ELSE WHILE Position < string length IF characters at position do not match THEN equal=false add one to position RETURN equalTRANSCRIPT
![Page 1: Complexity and Efficiency Computer Science 3 Gerb Objective: Understand complexity and efficiency of algorithms](https://reader035.vdocument.in/reader035/viewer/2022062504/5a4d1b3a7f8b9ab05999e2fc/html5/thumbnails/1.jpg)
Complexity and Efficiency
Computer Science 3Gerb
Objective: Understand complexity and efficiency of algorithms
![Page 2: Complexity and Efficiency Computer Science 3 Gerb Objective: Understand complexity and efficiency of algorithms](https://reader035.vdocument.in/reader035/viewer/2022062504/5a4d1b3a7f8b9ab05999e2fc/html5/thumbnails/2.jpg)
Complexity and Efficiency
• Algorithms that run more quickly are said to be more efficient.
• Algorthims that run less quickly are said to be more complex.
• For example, consider two algorithms for determining if two strings are equal
![Page 3: Complexity and Efficiency Computer Science 3 Gerb Objective: Understand complexity and efficiency of algorithms](https://reader035.vdocument.in/reader035/viewer/2022062504/5a4d1b3a7f8b9ab05999e2fc/html5/thumbnails/3.jpg)
Algorithm 1position = 0, equal=trueIF different lengths THEN equal=falseELSE WHILE Position < string length IF characters at position do not match THEN equal=false add one to positionRETURN equal
![Page 4: Complexity and Efficiency Computer Science 3 Gerb Objective: Understand complexity and efficiency of algorithms](https://reader035.vdocument.in/reader035/viewer/2022062504/5a4d1b3a7f8b9ab05999e2fc/html5/thumbnails/4.jpg)
Algorithm 2position = 0, equal=trueIF different lengths THEN equal=falseELSE WHILE Position < string length AND characters at position match Add one to position equal = position >= string lengthRETURN equal
![Page 5: Complexity and Efficiency Computer Science 3 Gerb Objective: Understand complexity and efficiency of algorithms](https://reader035.vdocument.in/reader035/viewer/2022062504/5a4d1b3a7f8b9ab05999e2fc/html5/thumbnails/5.jpg)
Algorithm 2 is more efficient
• Algorithm 1 keeps searching to the end of the string even if the first characters don’t match.– PERFORMANCE– yyyyyynnnnn– PERFORATION
• Algorithm 2 stops searching when it finds a mismatch.– PERFORMANCE– yyyyyyn– PERFORATION
• Algorithm 2 is more efficient. Algorithm 1 is more complex.
![Page 6: Complexity and Efficiency Computer Science 3 Gerb Objective: Understand complexity and efficiency of algorithms](https://reader035.vdocument.in/reader035/viewer/2022062504/5a4d1b3a7f8b9ab05999e2fc/html5/thumbnails/6.jpg)
Another example• Example: Find your name in an alphabetically ordered
list of N names.• Algorithm 1: Start at the first. Keep looking at the next
until you find it. Called sequential search.• Algorithm 2: Called binary search:Look at the exact middle nameIF it’s your name STOPELSE IF it’s earlier Binary search the 2nd halfELSE Binary search the 1st half
![Page 7: Complexity and Efficiency Computer Science 3 Gerb Objective: Understand complexity and efficiency of algorithms](https://reader035.vdocument.in/reader035/viewer/2022062504/5a4d1b3a7f8b9ab05999e2fc/html5/thumbnails/7.jpg)
Example: Search for “Jacob” in a list of names
AbrahamDeborahEvelynGabrielGayHenryJacobPamelaPaulPeterWinston
![Page 8: Complexity and Efficiency Computer Science 3 Gerb Objective: Understand complexity and efficiency of algorithms](https://reader035.vdocument.in/reader035/viewer/2022062504/5a4d1b3a7f8b9ab05999e2fc/html5/thumbnails/8.jpg)
Sequential Search: Start at the first name
AbrahamDeborahEvelynGabrielGayHenryJacobPamelaPaulPeterWinston
![Page 9: Complexity and Efficiency Computer Science 3 Gerb Objective: Understand complexity and efficiency of algorithms](https://reader035.vdocument.in/reader035/viewer/2022062504/5a4d1b3a7f8b9ab05999e2fc/html5/thumbnails/9.jpg)
Sequential Search: Progress to the next name until you find Jacob
AbrahamDeborahEvelynGabrielGayHenryJacobPamelaPaulPeterWinston
![Page 10: Complexity and Efficiency Computer Science 3 Gerb Objective: Understand complexity and efficiency of algorithms](https://reader035.vdocument.in/reader035/viewer/2022062504/5a4d1b3a7f8b9ab05999e2fc/html5/thumbnails/10.jpg)
Sequential Search: Progress to the next name until you find Jacob
AbrahamDeborahEvelynGabrielGayHenryJacobPamelaPaulPeterWinston
![Page 11: Complexity and Efficiency Computer Science 3 Gerb Objective: Understand complexity and efficiency of algorithms](https://reader035.vdocument.in/reader035/viewer/2022062504/5a4d1b3a7f8b9ab05999e2fc/html5/thumbnails/11.jpg)
Sequential Search: Progress to the next name until you find Jacob
AbrahamDeborahEvelynGabrielGayHenryJacobPamelaPaulPeterWinston
![Page 12: Complexity and Efficiency Computer Science 3 Gerb Objective: Understand complexity and efficiency of algorithms](https://reader035.vdocument.in/reader035/viewer/2022062504/5a4d1b3a7f8b9ab05999e2fc/html5/thumbnails/12.jpg)
Sequential Search: Progress to the next name until you find Jacob
AbrahamDeborahEvelynGabrielGayHenryJacobPamelaPaulPeterWinston
![Page 13: Complexity and Efficiency Computer Science 3 Gerb Objective: Understand complexity and efficiency of algorithms](https://reader035.vdocument.in/reader035/viewer/2022062504/5a4d1b3a7f8b9ab05999e2fc/html5/thumbnails/13.jpg)
Sequential Search: Progress to the next name until you find Jacob
AbrahamDeborahEvelynGabrielGayHenryJacobPamelaPaulPeterWinston
![Page 14: Complexity and Efficiency Computer Science 3 Gerb Objective: Understand complexity and efficiency of algorithms](https://reader035.vdocument.in/reader035/viewer/2022062504/5a4d1b3a7f8b9ab05999e2fc/html5/thumbnails/14.jpg)
Found!AbrahamDeborahEvelynGabrielGayHenryJacobPamelaPaulPeterWinston
![Page 15: Complexity and Efficiency Computer Science 3 Gerb Objective: Understand complexity and efficiency of algorithms](https://reader035.vdocument.in/reader035/viewer/2022062504/5a4d1b3a7f8b9ab05999e2fc/html5/thumbnails/15.jpg)
Binary Search: Look at the middle of the list
AbrahamDeborahEvelynGabrielGayHenryJacobPamelaPaulPeterWinston
![Page 16: Complexity and Efficiency Computer Science 3 Gerb Objective: Understand complexity and efficiency of algorithms](https://reader035.vdocument.in/reader035/viewer/2022062504/5a4d1b3a7f8b9ab05999e2fc/html5/thumbnails/16.jpg)
The one we want is later in the alphabet, so binary search the 2nd half
AbrahamDeborahEvelynGabrielGayHenryJacobPamelaPaulPeterWinston
{
![Page 17: Complexity and Efficiency Computer Science 3 Gerb Objective: Understand complexity and efficiency of algorithms](https://reader035.vdocument.in/reader035/viewer/2022062504/5a4d1b3a7f8b9ab05999e2fc/html5/thumbnails/17.jpg)
Look at the middle of the new listAbrahamDeborahEvelynGabrielGayHenryJacobPamelaPaulPeterWinston
{
![Page 18: Complexity and Efficiency Computer Science 3 Gerb Objective: Understand complexity and efficiency of algorithms](https://reader035.vdocument.in/reader035/viewer/2022062504/5a4d1b3a7f8b9ab05999e2fc/html5/thumbnails/18.jpg)
Jacob is earlier in alphabetical order, so search the first half of the new list
AbrahamDeborahEvelynGabrielGayHenryJacobPamelaPaulPeterWinston
{
![Page 19: Complexity and Efficiency Computer Science 3 Gerb Objective: Understand complexity and efficiency of algorithms](https://reader035.vdocument.in/reader035/viewer/2022062504/5a4d1b3a7f8b9ab05999e2fc/html5/thumbnails/19.jpg)
Look at the middle of the new list. We round down in this case
AbrahamDeborahEvelynGabrielGayHenryJacobPamelaPaulPeterWinston
{
![Page 20: Complexity and Efficiency Computer Science 3 Gerb Objective: Understand complexity and efficiency of algorithms](https://reader035.vdocument.in/reader035/viewer/2022062504/5a4d1b3a7f8b9ab05999e2fc/html5/thumbnails/20.jpg)
Jacob is middle of the new list. Found!
AbrahamDeborahEvelynGabrielGayHenryJacobPamelaPaulPeterWinston
{
![Page 21: Complexity and Efficiency Computer Science 3 Gerb Objective: Understand complexity and efficiency of algorithms](https://reader035.vdocument.in/reader035/viewer/2022062504/5a4d1b3a7f8b9ab05999e2fc/html5/thumbnails/21.jpg)
Compare Binary and Sequential Search
• Algorithm 1 requires looking at an average of N/2 names.
• Algorithm 2, can double the size of the list, still only need to look at one more name
• I.e. requires you to look at Log2N names.
• Algorithm 1 is more complex. Algorithm 2 is more efficient.
If N is Need to look at1 1 Name2-3 2 Names4-7 3 Names8-15 4 Names16-31 5 Names…2048-4095 12 Names…1-2 Million 21 Names
![Page 22: Complexity and Efficiency Computer Science 3 Gerb Objective: Understand complexity and efficiency of algorithms](https://reader035.vdocument.in/reader035/viewer/2022062504/5a4d1b3a7f8b9ab05999e2fc/html5/thumbnails/22.jpg)
Summary
• Complex algorithms take longer than efficient algorithms
• Sequential search (looking through a list until you find an item) is more complex than binary search (eliminate half the list each time).