1 / 16

Welcome to MM250

Welcome to MM250. To resize your pods: Place your mouse here. Left mouse click and hold. Drag to the right to enlarge the pod. Unit 9 Seminar: Algorithms. To maximize chat, minimize roster by clicking here. Algorithm: a step-by-step method for completing a task.

zeroun
Download Presentation

Welcome to MM250

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Welcome to MM250 To resize your pods: Place your mouse here. Left mouse click and hold. Drag to the right to enlarge the pod. Unit 9 Seminar: Algorithms To maximize chat, minimize roster by clicking here

  2. Algorithm: a step-by-step method for completing a task

  3. Algorithm Characteristics Input: not always, but typically Output: you get a result Precision: steps are precisely stated Determinism: results of each step are determined by results of the preceding steps Finiteness: stops Correctness: correctly solves the problem Generality: applies to a set of inputs

  4. Example 1: Program to find the lastoccurrence of an element in a list PROGRAM FINDIND Input: s, n, x s is a list, n is the length of the list, x is the element Output: index of last occurrence of x in s 1. findind(s,n,x) { 2. ind=0 // initialize index to 0 3. for i=1 to n // process the list one element at a time 4. if (si = x) then ind=i 5. return ind • } Note: // means comment. These are not part of the algorithm execution.

  5. Insertion Sort (Algorithm 8.2.3)

  6. Example 2: Trace of Algorithm 8.2.3 for input s = 7, 5, 4, 9 start 7 5 4 9 compare 5 to 7, it's smaller so 7 moves to the right ____ 7 insert 5 5 7 compare 4 to 7, it's smaller so 7 moves right 5 ____ 7 compare 4 to 5, it's smaller so 5 moves right ____ 5 7 insert 4 4 5 7 compare 9 to 7, it's bigger, so insert it 4 5 7 9 Example 3: Trace of Algorithm 8.2.3 for input s = 9, 7, 3, 2 start 9 7 3 2 compare 7 to 9, it's smaller so 9 moves right ____ 9 insert 7 7 9 compare 3 to 9, it's smaller so 9 moves right 7 ____ 9 compare 3 to 7, it's smaller so 7 moves right ____ 7 9 insert 3 3 7 9 compare 2 to 9, it's smaller so 9 moves right 3 7 ____ 9 compare 2 to 7, it's smaller so 7 moves right 3 ____ 7 9 compare 2 to 3, it's smaller so 3 moves right ____ 3 7 9 insert 2 2 3 7 9

  7. Analysis of Algorithms How long will it take an algorithm to run?

  8. Θ Notation Estimated time is given by the Greek letter Θ Suppose an algorithm is known to take 5n^2 +1 units of time for inputs of size n. Then we say that the Θ notation for the algorithm is Θ(n2). The Θ notation for this algorithm ignores the 5 and the 1 since when n is large it is the n2 that is dominating.

  9. Common Growth Functions Θ(1) Constant algorithm always takes the same amount of time regardless of the input Θ(lg lg n) Log Log Θ(lg n) Log Θ(n) Linear time it takes depends linearly on input of size n Θ(n lg n) n Log n Θ(n2 ) Quadratic Θ(n3 ) Cubic Θ(nk ) Polynomial Θ(cn ) Exponential Θ(n! ) Factorial

  10. Suppose an algorithm takes 4n3 + 2n2 +6 units of time for inputs of size n. What is its theta notation?

  11. Suppose an algorithm takes n8 + 5n units of time for inputs of size n. What is its theta notation?

  12. fast increasing theta function means a slow algorithm for large data size. If the time increases quickly with the size of the input, large inputs will take a lot of time to run. So a Θ(n!) algorithm is very slow for a big number n.

  13. Example 4: Theta Notation function THETA 3n3 - 2n2 + 6 n3n3 grows faster than n2 according to table 4.3.3 (it's lower down in the table) n lg n + 3n 3ncn grows faster than n lg n n5 + 5n 5n5n grows faster than n5

  14. Recursive Functions Algorithms that call themselves

  15. Factorial is a recursive function 5! = 5*4*3*2*1 = 5 * 4! Define ! recursively as follows: 0! = 1 n! = n*(n-1)!

  16. Robot Walking (Algorithm 8.4.6) walk(n) { if (n == 1 v n == 2) return n return walk(n-1) + walk(n-2) }

More Related