27
Algorithms Friday 7th Week

Algorithms Friday 7th Week. Algorithms What is an Algorithm? –A series of precise steps, known to stop eventually, to solve a problem –NOT necessarily

Embed Size (px)

Citation preview

Page 1: Algorithms Friday 7th Week. Algorithms What is an Algorithm? –A series of precise steps, known to stop eventually, to solve a problem –NOT necessarily

Algorithms

Friday

7th Week

Page 2: Algorithms Friday 7th Week. Algorithms What is an Algorithm? –A series of precise steps, known to stop eventually, to solve a problem –NOT necessarily

Algorithms

• What is an Algorithm?– A series of precise steps, known to

stop eventually, to solve a problem– NOT necessarily tied to computers– There can be many algorithms to

solve the same problem

Page 3: Algorithms Friday 7th Week. Algorithms What is an Algorithm? –A series of precise steps, known to stop eventually, to solve a problem –NOT necessarily

Characteristics of an Algorithm

1. Precise steps

2. Effective steps

3. Has an output

4. Terminates eventually

Page 4: Algorithms Friday 7th Week. Algorithms What is an Algorithm? –A series of precise steps, known to stop eventually, to solve a problem –NOT necessarily

Trivial Algorithm

• Computing an average:1. Add up all of the values

2. Divide by the number of values

Page 5: Algorithms Friday 7th Week. Algorithms What is an Algorithm? –A series of precise steps, known to stop eventually, to solve a problem –NOT necessarily

Greatest Common Divisor

• Computing the Greatest Common Divisor (gcd) of two numbers

• Examples:– gcd(12, 2) = 2– gcd(100, 95) = 5– gcd(100, 75) = 25– gcd(39, 26) = 13– gcd(17, 8) = 1

Page 6: Algorithms Friday 7th Week. Algorithms What is an Algorithm? –A series of precise steps, known to stop eventually, to solve a problem –NOT necessarily

Possible Algorithm #1

• Assumption: A > B >= 0– If A is a multiple of B, then gcd(A, B) = B.– Otherwise, return an error.

• Works for gcd(12,2) = 2

• But what about gcd(100, 95)???

Page 7: Algorithms Friday 7th Week. Algorithms What is an Algorithm? –A series of precise steps, known to stop eventually, to solve a problem –NOT necessarily

Possible Algorithm #2

– Start with 1 and go up to B.– If a number if a common divisor of both A

and B, remember it.– When we get to B, stop. The last number

we remembered is the gcd.

• Works, but is there a better way?

• Think about gcd(100, 95)

Page 8: Algorithms Friday 7th Week. Algorithms What is an Algorithm? –A series of precise steps, known to stop eventually, to solve a problem –NOT necessarily

Euclid’s Algorithm

• Make use of the fact that:

• gcd(A, B) = gcd(B, A rem B)– Note: A rem B refers to the remainder left

when A is divided by B.– Examples:

• 12 rem 2 = 0• 100 rem 95 = 5

Page 9: Algorithms Friday 7th Week. Algorithms What is an Algorithm? –A series of precise steps, known to stop eventually, to solve a problem –NOT necessarily

Euclid’s Algorithm

– If B = 0, then gcd(A, B) = A– Otherwise, gcd(A, B) = gcd (B, A rem B)

• Note – this algorithm is recursive.

• Examples:– gcd(12, 2) = gcd(2, 0) = 2– gcd(100, 95) = gcd(95, 5) = gcd(5, 0) = 5

Page 10: Algorithms Friday 7th Week. Algorithms What is an Algorithm? –A series of precise steps, known to stop eventually, to solve a problem –NOT necessarily

Program vs. Algorithm

• Program: “A set of computer instructions written in a programming language”

• We write Programs (in JavaScript) that implement various Algorithms

Page 11: Algorithms Friday 7th Week. Algorithms What is an Algorithm? –A series of precise steps, known to stop eventually, to solve a problem –NOT necessarily

Program vs. Algorithm

• Problem: Sort a list of numbers• Algorithms:

– Quicksort– Insertion Sort – Merge Sort

• Programs: – Using JavaScript to implement Insertion Sort– Using Pascal to implement Quicksort

Page 12: Algorithms Friday 7th Week. Algorithms What is an Algorithm? –A series of precise steps, known to stop eventually, to solve a problem –NOT necessarily

Algorithms to Solve Problems

• Problems can be categorized into two categories– Computable problems can be solved using

an algorithm.– Non-computable problems have no

algorithm to solve them.

• We don’t necessariliy know which category a problem is in. (this is non-computable)

Page 13: Algorithms Friday 7th Week. Algorithms What is an Algorithm? –A series of precise steps, known to stop eventually, to solve a problem –NOT necessarily

Computable Problems

• Tractable– There is an efficient algorithm to solve the

problem.

• Intractable– There is an algorithm to solve the problem

but the best known algorithm is not efficient.

Page 14: Algorithms Friday 7th Week. Algorithms What is an Algorithm? –A series of precise steps, known to stop eventually, to solve a problem –NOT necessarily

Examples

• Sorting: tractable

• Chess Playing: intractable

• Halting Problem: non-computable

Page 15: Algorithms Friday 7th Week. Algorithms What is an Algorithm? –A series of precise steps, known to stop eventually, to solve a problem –NOT necessarily

How do we measure efficiency?

• To do this, we count the number of steps an algorithm takes.

• For example, consider the following algorithm which adds up the numbers in an array:

Page 16: Algorithms Friday 7th Week. Algorithms What is an Algorithm? –A series of precise steps, known to stop eventually, to solve a problem –NOT necessarily

Add up the numbers in an arraysum = 0;for (var i=0; i<numArray.length; i++){

sum += numArray[i];

}• The statement inside the for loop gets executed

numArray.length times – it depends on what the length is.

• If the length is n, we say this algorithm is “on the order of n”, or, this algorithm is O(n).

Page 17: Algorithms Friday 7th Week. Algorithms What is an Algorithm? –A series of precise steps, known to stop eventually, to solve a problem –NOT necessarily

Definition of O(g(n))• f(n)=O(g(n)) means that for all n larger than

some threshold, f(n) <= C*g(n) for some constant C,

Or, the ratio f(n)/g(n) <= C.• This means that f grows no faster than g, or f

is proportional to g.• CONSTANTS DON’T MATTER• f could represent time an algorithm takes, or

the number of steps executed.

Page 18: Algorithms Friday 7th Week. Algorithms What is an Algorithm? –A series of precise steps, known to stop eventually, to solve a problem –NOT necessarily

Big O Analysis

• What is the worst case running time on input of size N, discounting constants

Page 19: Algorithms Friday 7th Week. Algorithms What is an Algorithm? –A series of precise steps, known to stop eventually, to solve a problem –NOT necessarily

Comparison of Functions

• Look at graphs of f(x)=x, g(x)=x2, h(x)=x3, j(x)=logx k(x) = 2x

• How do they grow? How do they compare with each other?

Page 20: Algorithms Friday 7th Week. Algorithms What is an Algorithm? –A series of precise steps, known to stop eventually, to solve a problem –NOT necessarily

Tractable vs. Intractable

• Polynomial time algorithms are considered tractable.

• Exponential time algorithms are considered intractable.

Page 21: Algorithms Friday 7th Week. Algorithms What is an Algorithm? –A series of precise steps, known to stop eventually, to solve a problem –NOT necessarily

Sorting (Insertion Sort)

function insertionSort (numArray) { for ( var i=1; i < numArray.length; i++) { var temp = numArray[i]; for (var j = i; (j > 0) && ( numArray[j-1] > temp); j--) { numArray[j] = numArray[j-1]; } numArray[j] = temp; }}

Page 22: Algorithms Friday 7th Week. Algorithms What is an Algorithm? –A series of precise steps, known to stop eventually, to solve a problem –NOT necessarily

Sorting (Merge Sort)

• Given a list, split it into 2 equal piles.

• Then split each of these piles in half. Continue to do this until you are left with 1 element in each pile.

• Now merge piles back together in order.

Page 23: Algorithms Friday 7th Week. Algorithms What is an Algorithm? –A series of precise steps, known to stop eventually, to solve a problem –NOT necessarily

(Merge Sort, cont)

• An example of how the merge works:Suppose the first half and second half of an array are sorted:5 9 10 12 17 1 8 11 20 32

• Merge these by taking a new element from either the first or second subarray, choosing the smallest of the remaining elements.

Page 24: Algorithms Friday 7th Week. Algorithms What is an Algorithm? –A series of precise steps, known to stop eventually, to solve a problem –NOT necessarily

Big O Can Be Deceiving

• Big O analysis is concerned with worst case behavior

• Sometimes we know that we are not dealing with the worst case

Page 25: Algorithms Friday 7th Week. Algorithms What is an Algorithm? –A series of precise steps, known to stop eventually, to solve a problem –NOT necessarily

Search the Array

for (var i=0; i<numArray.length; i++)

{if (numArray[i] == “what we want”)

stop and jump for joy;

}

Page 26: Algorithms Friday 7th Week. Algorithms What is an Algorithm? –A series of precise steps, known to stop eventually, to solve a problem –NOT necessarily

Binary Search

• If we are searching in a sorted list, we choose the half that the value would be in.

• We continue to cut the area we are searching in half until we find the value we want or there are no more values to check.

Page 27: Algorithms Friday 7th Week. Algorithms What is an Algorithm? –A series of precise steps, known to stop eventually, to solve a problem –NOT necessarily

Algorithms Exercise