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
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
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
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.
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
Example: Search for “Jacob” in a list of names Abraham Deborah Evelyn Gabriel Gay Henry Jacob Pamela Paul Peter Winston
Sequential Search: Start at the first name Abraham Deborah Evelyn Gabriel Gay Henry Jacob Pamela Paul Peter Winston
Sequential Search: Progress to the next name until you find Jacob Abraham Deborah Evelyn Gabriel Gay Henry Jacob Pamela Paul Peter Winston
Sequential Search: Progress to the next name until you find Jacob Abraham Deborah Evelyn Gabriel Gay Henry Jacob Pamela Paul Peter Winston
Sequential Search: Progress to the next name until you find Jacob Abraham Deborah Evelyn Gabriel Gay Henry Jacob Pamela Paul Peter Winston
Sequential Search: Progress to the next name until you find Jacob Abraham Deborah Evelyn Gabriel Gay Henry Jacob Pamela Paul Peter Winston
Sequential Search: Progress to the next name until you find Jacob Abraham Deborah Evelyn Gabriel Gay Henry Jacob Pamela Paul Peter Winston
Found! Abraham Deborah Evelyn Gabriel Gay Henry Jacob Pamela Paul Peter Winston
Binary Search: Look at the middle of the list Abraham Deborah Evelyn Gabriel Gay Henry Jacob Pamela Paul Peter Winston
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 {
Look at the middle of the new list Abraham Deborah Evelyn Gabriel Gay Henry Jacob Pamela Paul Peter Winston {
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 {
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 {
Jacob is middle of the new list. Found! Abraham Deborah Evelyn Gabriel Gay Henry Jacob Pamela Paul Peter Winston {
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 Names Names … Names … 1-2 Million21 Names
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).