Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Complexity and Efficiency Computer Science 3 Gerb Objective: Understand complexity and efficiency of algorithms."— Presentation transcript:

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

2 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

3 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

4 Algorithm 2 position = 0, equal=true IF different lengths THEN equal=false ELSE WHILE Position < string length AND characters at position match Add one to position equal = position >= string length RETURN equal

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

6 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 name IF it’s your name STOP ELSE IF it’s earlier Binary search the 2nd half ELSE Binary search the 1st half

7 Example: Search for “Jacob” in a list of names Abraham Deborah Evelyn Gabriel Gay Henry Jacob Pamela Paul Peter Winston

8 Sequential Search: Start at the first name Abraham Deborah Evelyn Gabriel Gay Henry Jacob Pamela Paul Peter Winston

9 Sequential Search: Progress to the next name until you find Jacob Abraham Deborah Evelyn Gabriel Gay Henry Jacob Pamela Paul Peter Winston

10 Sequential Search: Progress to the next name until you find Jacob Abraham Deborah Evelyn Gabriel Gay Henry Jacob Pamela Paul Peter Winston

11 Sequential Search: Progress to the next name until you find Jacob Abraham Deborah Evelyn Gabriel Gay Henry Jacob Pamela Paul Peter Winston

12 Sequential Search: Progress to the next name until you find Jacob Abraham Deborah Evelyn Gabriel Gay Henry Jacob Pamela Paul Peter Winston

13 Sequential Search: Progress to the next name until you find Jacob Abraham Deborah Evelyn Gabriel Gay Henry Jacob Pamela Paul Peter Winston

14 Found! Abraham Deborah Evelyn Gabriel Gay Henry Jacob Pamela Paul Peter Winston

15 Binary Search: Look at the middle of the list Abraham Deborah Evelyn Gabriel Gay Henry Jacob Pamela Paul Peter Winston

16 The one we want is later in the alphabet, so binary search the 2 nd half Abraham Deborah Evelyn Gabriel Gay Henry Jacob Pamela Paul Peter Winston {

17 Look at the middle of the new list Abraham Deborah Evelyn Gabriel Gay Henry Jacob Pamela Paul Peter Winston {

18 Jacob is earlier in alphabetical order, so search the first half of the new list Abraham Deborah Evelyn Gabriel Gay Henry Jacob Pamela Paul Peter Winston {

19 Look at the middle of the new list. We round down in this case Abraham Deborah Evelyn Gabriel Gay Henry Jacob Pamela Paul Peter Winston {

20 Jacob is middle of the new list. Found! Abraham Deborah Evelyn Gabriel Gay Henry Jacob Pamela Paul Peter Winston {

21 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 Log 2 N names. Algorithm 1 is more complex. Algorithm 2 is more efficient. If N isNeed to look at 11 Name 2-32 Names 4-73 Names 8-154 Names 16-315 Names … 2048-409512 Names … 1-2 Million21 Names

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


Download ppt "Complexity and Efficiency Computer Science 3 Gerb Objective: Understand complexity and efficiency of algorithms."

Similar presentations


Ads by Google