Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1 Algorithms Friday 7th Week

2 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

3 Characteristics of an Algorithm 1.Precise steps 2.Effective steps 3.Has an output 4.Terminates eventually

4 Trivial Algorithm Computing an average: 1.Add up all of the values 2.Divide by the number of values

5 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

6 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)???

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

8 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

9 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

10 Program vs. Algorithm Program: “A set of computer instructions written in a programming language” We write Programs (in JavaScript) that implement various Algorithms

11 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

12 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)

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

14 Examples Sorting: tractable Chess Playing: intractable Halting Problem: non-computable

15 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:

16 Add up the numbers in an array sum = 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).

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

18 Big O Analysis What is the worst case running time on input of size N, discounting constants

19 Comparison of Functions Look at graphs of f(x)=x, g(x)=x 2, h(x)=x 3, j(x)=logx k(x) = 2 x How do they grow? How do they compare with each other?

20 Tractable vs. Intractable Polynomial time algorithms are considered tractable. Exponential time algorithms are considered intractable.

21 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; }

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

23 (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.

24 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

25 Search the Array for (var i=0; i<numArray.length; i++) { if (numArray[i] == “what we want”) stop and jump for joy; }

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

27 Algorithms Exercise


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

Similar presentations


Ads by Google