introduction to algorithmic complexity: searching

41
Introduction to Algorithmic Complexity: Searching Instructor: Nihshanka Debroy (Slides borrowed from Tammy Bailey and Dr. Forbes)

Upload: others

Post on 14-Jun-2022

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Algorithmic Complexity: Searching

Introduction to Algorithmic Complexity: Searching

Instructor: Nihshanka Debroy(Slides borrowed from Tammy Bailey and Dr. Forbes)

Page 2: Introduction to Algorithmic Complexity: Searching

Algorithm Analysis● Determine the amount of resources an algorithm

needs to run (computation time, space in memory)● Running time: number of basic operations

performed - additions, multiplications, comparisons - usually grows with size of input - faster to add 2 numbers than to add 2,000,000!

Page 3: Introduction to Algorithmic Complexity: Searching
Page 4: Introduction to Algorithmic Complexity: Searching
Page 5: Introduction to Algorithmic Complexity: Searching
Page 6: Introduction to Algorithmic Complexity: Searching
Page 7: Introduction to Algorithmic Complexity: Searching
Page 8: Introduction to Algorithmic Complexity: Searching
Page 9: Introduction to Algorithmic Complexity: Searching
Page 10: Introduction to Algorithmic Complexity: Searching
Page 11: Introduction to Algorithmic Complexity: Searching
Page 12: Introduction to Algorithmic Complexity: Searching
Page 13: Introduction to Algorithmic Complexity: Searching
Page 14: Introduction to Algorithmic Complexity: Searching
Page 15: Introduction to Algorithmic Complexity: Searching

Can you color this map with 4 colors?

Page 16: Introduction to Algorithmic Complexity: Searching

Can you color this map with 3 colors?

Page 17: Introduction to Algorithmic Complexity: Searching

Can you color this map with 3 colors?

Page 18: Introduction to Algorithmic Complexity: Searching

Can you color this graph with 3 colors?

Page 19: Introduction to Algorithmic Complexity: Searching
Page 20: Introduction to Algorithmic Complexity: Searching
Page 21: Introduction to Algorithmic Complexity: Searching
Page 22: Introduction to Algorithmic Complexity: Searching
Page 23: Introduction to Algorithmic Complexity: Searching
Page 24: Introduction to Algorithmic Complexity: Searching
Page 25: Introduction to Algorithmic Complexity: Searching
Page 26: Introduction to Algorithmic Complexity: Searching
Page 27: Introduction to Algorithmic Complexity: Searching
Page 28: Introduction to Algorithmic Complexity: Searching
Page 29: Introduction to Algorithmic Complexity: Searching
Page 30: Introduction to Algorithmic Complexity: Searching
Page 31: Introduction to Algorithmic Complexity: Searching
Page 32: Introduction to Algorithmic Complexity: Searching
Page 33: Introduction to Algorithmic Complexity: Searching
Page 34: Introduction to Algorithmic Complexity: Searching
Page 35: Introduction to Algorithmic Complexity: Searching

Linear Search: Runtime Analysispublic class LinearSearch

{ public static void main(String[] args) {

int[] input = {4, 5, 23, 36, 44, 101, 12, 19, 156, 77, 81, 2, 81};

int goal = 0;

System.out.print("\n\nEnter a number to search for: ");

goal = Keyboard.readInt();

boolean inArray = linearSearch(input, goal);

System.out.println("\nWas the entry found: "+inArray);

}

public static boolean linearSearch(int[] input, int goal)

{ boolean found = false;

for(int i=0; i<input.length; i++)

{ if(input[i] == goal)

found = true;

}

return found; } }

Page 36: Introduction to Algorithmic Complexity: Searching
Page 37: Introduction to Algorithmic Complexity: Searching
Page 38: Introduction to Algorithmic Complexity: Searching
Page 39: Introduction to Algorithmic Complexity: Searching
Page 40: Introduction to Algorithmic Complexity: Searching
Page 41: Introduction to Algorithmic Complexity: Searching

Running Time● Linear Search: O(n) time● Binary Search: O(lg n) time