Slide 1

Slide 1 text

A Qualitative Sense of Algorithms Thursday, July 26, 12

Slide 2

Slide 2 text

An algorithm is a repeatable process for determining a solution to a problem. Thursday, July 26, 12

Slide 3

Slide 3 text

For example: 1.Let sum be equal to 0 2.Let count be equal to the size of the list 3.For each element in the list: 1.Add its value to count 4.Divide count by sum Find the mean of a list of numbers: Thursday, July 26, 12

Slide 4

Slide 4 text

In contrast, an heuristic is a loose rule or set of rules that assists in discovering a solution via trial and error. Thursday, July 26, 12

Slide 5

Slide 5 text

Algorithmic analysis is the determination of the characteristic behavior of an algorithm with respect to time and/or space. Thursday, July 26, 12

Slide 6

Slide 6 text

How long does it take to run? How much memory will it consume? Thursday, July 26, 12

Slide 7

Slide 7 text

How long does it take to run? How much memory will it consume? Thursday, July 26, 12

Slide 8

Slide 8 text

Find an element: Locate the value 22 in a list of 8 randomized elements. How long will it take? Thursday, July 26, 12

Slide 9

Slide 9 text

Find an element: Locate the value 22 in a list of 8 randomized elements. How long will it take? Thursday, July 26, 12

Slide 10

Slide 10 text

Find an element: Locate the value 22. 17 0 14 8 22 4 3 23 0 steps Thursday, July 26, 12

Slide 11

Slide 11 text

Find an element: Locate the value 22. 17 0 14 8 22 4 3 23 1 step Thursday, July 26, 12

Slide 12

Slide 12 text

Find an element: Locate the value 22. 17 0 14 8 22 4 3 23 2 steps Thursday, July 26, 12

Slide 13

Slide 13 text

Find an element: Locate the value 22. 17 0 14 8 22 4 3 23 3 steps Thursday, July 26, 12

Slide 14

Slide 14 text

Find an element: Locate the value 22. 17 0 14 8 22 4 3 23 4 steps Thursday, July 26, 12

Slide 15

Slide 15 text

Find an element: Locate the value 22. 17 0 14 8 22 4 3 23 5 steps Thursday, July 26, 12

Slide 16

Slide 16 text

Find an element: Locate the value 17. 17 0 14 8 22 4 3 23 0 steps Thursday, July 26, 12

Slide 17

Slide 17 text

Find an element: Locate the value 17. 17 0 14 8 22 4 3 23 1 step Thursday, July 26, 12

Slide 18

Slide 18 text

Find an element: Locate the value 23. 17 0 14 8 22 4 3 23 0 steps Thursday, July 26, 12

Slide 19

Slide 19 text

Find an element: Locate the value 23. 17 0 14 8 22 4 3 23 1 step Thursday, July 26, 12

Slide 20

Slide 20 text

Find an element: Locate the value 23. 17 0 14 8 22 4 3 23 2 steps Thursday, July 26, 12

Slide 21

Slide 21 text

Find an element: Locate the value 23. 17 0 14 8 22 4 3 23 3 steps Thursday, July 26, 12

Slide 22

Slide 22 text

Find an element: Locate the value 23. 17 0 14 8 22 4 3 23 4 steps Thursday, July 26, 12

Slide 23

Slide 23 text

Find an element: Locate the value 23. 17 0 14 8 22 4 3 23 5 steps Thursday, July 26, 12

Slide 24

Slide 24 text

Find an element: Locate the value 23. 17 0 14 8 22 4 3 23 6 steps Thursday, July 26, 12

Slide 25

Slide 25 text

Find an element: Locate the value 23. 17 0 14 8 22 4 3 23 7 steps Thursday, July 26, 12

Slide 26

Slide 26 text

Find an element: Locate the value 23. 17 0 14 8 22 4 3 23 8 steps Thursday, July 26, 12

Slide 27

Slide 27 text

Find an element: 17 0 14 8 22 4 3 23 How many steps, on average, will be needed? Thursday, July 26, 12

Slide 28

Slide 28 text

Find an element: 17 0 14 8 22 4 3 23 Assuming randomness, approximately 8 / 2 steps. Thursday, July 26, 12

Slide 29

Slide 29 text

Find an element: 17 0 14 8 22 4 3 23 Generalize to n elements, about n / 2. Thursday, July 26, 12

Slide 30

Slide 30 text

Find an element: 17 0 14 8 22 4 3 23 So, the time performance is a linear function of n. Thursday, July 26, 12

Slide 31

Slide 31 text

Find an element: 17 0 14 8 22 4 3 23 So, the time performance is a linear function of n. (times some constant) Thursday, July 26, 12

Slide 32

Slide 32 text

Find an element: 17 0 14 8 22 4 3 23 0 1 2 3 4 5 6 7 Thursday, July 26, 12

Slide 33

Slide 33 text

Find an element: What is the value at position 3? 17 0 14 8 22 4 3 23 0 1 2 3 4 5 6 7 Thursday, July 26, 12

Slide 34

Slide 34 text

Find an element: 17 0 14 8 22 4 3 23 What is the value at position 3? 0 1 2 3 4 5 6 7 The value is 8. Thursday, July 26, 12

Slide 35

Slide 35 text

Find an element: What is the value at position 5? 17 0 14 8 22 4 3 23 0 1 2 3 4 5 6 7 Thursday, July 26, 12

Slide 36

Slide 36 text

Find an element: What is the value at position 5? 17 0 14 8 22 4 3 23 0 1 2 3 4 5 6 7 The value is 4. Thursday, July 26, 12

Slide 37

Slide 37 text

Find an element: 17 0 14 8 22 4 3 23 How many steps, on average, will be needed? Thursday, July 26, 12

Slide 38

Slide 38 text

Find an element: 17 0 14 8 22 4 3 23 Only 1 step! Thursday, July 26, 12

Slide 39

Slide 39 text

Find an element: 17 0 14 8 22 4 3 23 So, the time performance is simply the cost of an access. Thursday, July 26, 12

Slide 40

Slide 40 text

Find an element: 17 0 14 8 22 4 3 23 So, the time performance is simply the cost of an access. (meaning, a constant) Thursday, July 26, 12

Slide 41

Slide 41 text

In general, the time performance of an algorithm can be described by an algebraic function times some constant. Thursday, July 26, 12

Slide 42

Slide 42 text

This is described by something referred to as Omicron notation, or simply O- notation. Thursday, July 26, 12

Slide 43

Slide 43 text

Linear search is O(n). Array access is O(C). Thursday, July 26, 12

Slide 44

Slide 44 text

Thursday, July 26, 12

Slide 45

Slide 45 text

Linear search is O(n). Array access is O(C). Thursday, July 26, 12

Slide 46

Slide 46 text

Linear search is O(n). Array access is O(C). Sorting is generally O(n log(n)) to O(n2). Thursday, July 26, 12

Slide 47

Slide 47 text

Thursday, July 26, 12

Slide 48

Slide 48 text

Sorting There is much more to algorithms than sorting, but sorting is foundational to many algorithms. In general, you should defer to the standard library sorting implementations for your language. But understanding and thinking about sort is valuable. Thursday, July 26, 12

Slide 49

Slide 49 text

Sorting Code examples of Mergesort and Quicksort... Thursday, July 26, 12

Slide 50

Slide 50 text

Bubble sort procedure bubbleSort( A : list of sortable items ) repeat swapped = false for i = 1 to length(A) - 1 inclusive do: /* if this pair is out of order */ if A[i-1] > A[i] then /* swap them and remember something changed */ swap( A[i-1], A[i] ) swapped = true end if end for until not swapped end procedure http://en.wikipedia.org/wiki/Bubble_sort Thursday, July 26, 12

Slide 51

Slide 51 text

Bubble sort procedure bubbleSort( A : list of sortable items ) repeat swapped = false for i = 1 to length(A) - 1 inclusive do: /* if this pair is out of order */ if A[i-1] > A[i] then /* swap them and remember something changed */ swap( A[i-1], A[i] ) swapped = true end if end for until not swapped end procedure http://en.wikipedia.org/wiki/Bubble_sort Bubble sort is O(?) Thursday, July 26, 12

Slide 52

Slide 52 text

Bubble sort procedure bubbleSort( A : list of sortable items ) repeat swapped = false for i = 1 to length(A) - 1 inclusive do: /* if this pair is out of order */ if A[i-1] > A[i] then /* swap them and remember something changed */ swap( A[i-1], A[i] ) swapped = true end if end for until not swapped end procedure http://en.wikipedia.org/wiki/Bubble_sort Bubble sort is O(n2) Thursday, July 26, 12

Slide 53

Slide 53 text

http://www.cs.columbia.edu/~hgs/teaching/isp/hw/qsort.c Real-world quick sort: Thursday, July 26, 12

Slide 54

Slide 54 text

Find an element via Binary Search: Locate the value 22 in a sorted list. 0 3 4 8 14 17 22 23 0 steps Thursday, July 26, 12

Slide 55

Slide 55 text

Find an element via Binary Search: Locate the value 22 in a sorted list. 0 3 4 8 14 17 22 23 0 steps Thursday, July 26, 12

Slide 56

Slide 56 text

Find an element via Binary Search: Locate the value 22 in a sorted list. 0 3 4 8 14 17 22 23 1 steps Thursday, July 26, 12

Slide 57

Slide 57 text

Find an element via Binary Search: Locate the value 22 in a sorted list. 0 3 4 8 14 17 22 23 1 steps Thursday, July 26, 12

Slide 58

Slide 58 text

Find an element via Binary Search: Locate the value 22 in a sorted list. 0 3 4 8 14 17 22 23 2 steps Thursday, July 26, 12

Slide 59

Slide 59 text

Find an element via Binary Search: Locate the value 22 in a sorted list. 0 3 4 8 14 17 22 23 2 steps Thursday, July 26, 12

Slide 60

Slide 60 text

Find an element via Binary Search: Locate the value 22 in a sorted list. 0 3 4 8 14 17 22 23 3 steps Thursday, July 26, 12

Slide 61

Slide 61 text

Find an element via Binary Search: Locate the value 22 in a sorted list. 0 3 4 8 14 17 22 23 3 steps = log2(8) Thursday, July 26, 12

Slide 62

Slide 62 text

Find an element via Binary Search: Locate the value 22 in a sorted list. 0 3 4 8 14 17 22 23 Binary Search is O(log2(n)) Thursday, July 26, 12

Slide 63

Slide 63 text

Thursday, July 26, 12

Slide 64

Slide 64 text

fin Thursday, July 26, 12