22
Complexity and Efficiency Computer Science 3 Gerb Objective: Understand complexity and efficiency of algorithms

Complexity and Efficiency Computer Science 3 Gerb Objective: Understand complexity and efficiency of algorithms

Embed Size (px)

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 equal

Citation preview

Page 1: Complexity and Efficiency Computer Science 3 Gerb Objective: Understand complexity and efficiency of algorithms

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

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

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

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

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

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

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

Sequential Search: Start at the first name

AbrahamDeborahEvelynGabrielGayHenryJacobPamelaPaulPeterWinston

Page 9: Complexity and Efficiency Computer Science 3 Gerb Objective: Understand complexity and efficiency of algorithms

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

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

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

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

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

Found!AbrahamDeborahEvelynGabrielGayHenryJacobPamelaPaulPeterWinston

Page 15: Complexity and Efficiency Computer Science 3 Gerb Objective: Understand complexity and efficiency of algorithms

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

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

Look at the middle of the new listAbrahamDeborahEvelynGabrielGayHenryJacobPamelaPaulPeterWinston

{

Page 18: Complexity and Efficiency Computer Science 3 Gerb Objective: Understand complexity and efficiency of algorithms

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

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

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

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

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).