1 / 26

Lecture 6

Lecture 6. Efficiency of Algorithms (2) (S&G, ch.3). Read S&G chapter 4. Warning: There are printing errors in ch. 4 in formulas! We will send out corrections by email. Sorting. One of the most common activities of a computer is sorting data.

jmeis
Download Presentation

Lecture 6

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. Lecture 6 Efficiency of Algorithms (2) (S&G, ch.3) CS 100 - Lecture 6

  2. Read S&G chapter 4 Warning: There are printing errors in ch. 4 in formulas! We will send out corrections by email CS 100 - Lecture 6

  3. Sorting • One of the most common activities of a computer is sorting data. • Arranging data into numerical or alphabetical order for purposes of • Reports by category • Summarizing data • Searching data CS 100 - Lecture 6 slide courtesy of S. Levy

  4. Sorting (2) • Given: n, N1, N2, …, Nn • Want: Rearrangement M1, M2, …, Mnwhere M1… Mn according to the appropriate understanding of . • There are many well-known algorithms for doing this. Preference may be due to • list size • degree of order already present • ease of programming • program available CS 100 - Lecture 6 slide courtesy of S. Levy

  5. Sorting - Selection Sort • Strategy: • find the largest element in list • swap it with last element in list • find next largest • swap it with next to last element in list • etc • Variables: • U - Marker for unsorted section • L - Position of largest so far • C - Current position CS 100 - Lecture 6 slide courtesy of S. Levy

  6. Selection Sort - The Algorithm:High Level Set U to n Repeat until U = 1 Search For Largest of N [1] to N [U] and put its location in L Exchange N [L] and N [U] Set U to U – 1 Stop CS 100 - Lecture 6

  7. Search For Largest ofN [1], …, N [U] andput location in L Selection Sort - The Algorithm Set U to n Repeat until U = 1 Set L to 1 Set C to 2 Repeat until C > U If N [C] > N [L] then Set L to C Set C to C + 1 Exchange N [L] and N [U] Set U to U – 1 Stop CS 100 - Lecture 6 slide courtesy of S. Levy

  8. L U 5 19 12 L U 9 6 9 6 9 6 9 6 9 6 9 6 9 6 L U C 5 19 12 5 19 C C U L 5 19 12 L U C U 5 19 12 L 5 12 19 C C L U 12 5 19 12 C Selection Sort - Example (Pass 1) CS 100 - Lecture 6 slide courtesy of S. Levy

  9. L U L U 5 12 12 5 19 9 6 9 6 9 6 6 9 6 6 9 19 C C L U L U 5 12 19 5 19 12 C C L U 9 5 12 19 C L U 12 19 5 C Selection Sort - Example (Pass 2) CS 100 - Lecture 6 slide courtesy of S. Levy

  10. L U 12 6 6 6 6 6 C L U 5 12 19 5 9 19 C 9 LU 19 5 9 12 19 C L L U U 5 5 9 9 12 12 19 19 C C Selection Sort - Example (Pass 3) CS 100 - Lecture 6 slide courtesy of S. Levy

  11. L U U 5 9 12 19 6 9 12 19 6 5 5 6 C L U 5 12 C LU 9 12 19 6 C 9 19 Selection Sort - Example (Pass 4) CS 100 - Lecture 6 slide courtesy of S. Levy

  12. Efficiency Analysisof Selection Sort • Makes n – 1 passes through the list • Each pass uses Search For Largest, on the unsorted part of the list, to find the largest element • If the unsorted part has length U, Search For Largest takes U – 1 comparisons CS 100 - Lecture 6

  13. Efficiency of Selection Sort (2) • To sort n element list: n – 1 comparisons to find largest n – 2 comparisons to find second largest … 2 comparisons to find largest of last three 1 comparisons to find largest of last two • Total number of comparisons: CS 100 - Lecture 6

  14. A Useful Formula CS 100 - Lecture 6

  15. A Useful Formula (2) CS 100 - Lecture 6

  16. Best, Worst, & Average CS 100 - Lecture 6

  17. Motivation forAsymptotic Complexity • We are interested in comparing the efficiency of various algorithms, independent of computer running them • Therefore, we choose to ignore: • the absolute time taken by the instructions • constant initialization overhead • time taken by the less frequently executed instructions CS 100 - Lecture 6

  18. Motivation forAsymptotic Complexity (2) • Not concerned about efficiency for small problems (they’re all quick enough) • Usually interested in using on large problems • We investigate how resource usage varies on progressively larger problems • Which is asymptotically better, i.e., for sufficiently large problems CS 100 - Lecture 6

  19. Quadratic vs. Linear Growth • On big enough problem, quadratic algorithm takes more time than linear algorithm CS 100 - Lecture 6

  20. Quadratic vs. Linear Growth (2) CS 100 - Lecture 6

  21. Equivalent Complexity • For our purposes:An algorithm taking time 10n sec. is as good as one taking 2n sec. • because absolute time for individual instructions doesn’t matter • An algorithm taking 2n + 10 sec. is as good as one taking 2n sec. • because fixed overhead doesn’t matter • An algorithm taking n2 + 2n sec. is as good as one taking n2 sec. • less frequently executed instructions don’t matter CS 100 - Lecture 6

  22. Complexity Classes • No growth: (1) • Linear growth: (n) • Quadratic growth: (n2) • Cubic growth: (n3) • Polynomial growth: (nk) for any k • Exponential growth: (kn) for any k •  (theta) is called the “exact order” of a function CS 100 - Lecture 6

  23. Comparison of Growth Orders CS 100 - Lecture 6

  24. Combinatorial Explosion • Consider a game with k possible moves on each play • Therefore: • k possible first moves • k2 possible replies • k3 possible replies to the replies, etc. • In general, must consider kn possibilities to look n moves ahead (exponential algorithm) • “Brute force” solutions to many problems lead to “combinatorial explosion” CS 100 - Lecture 6

  25. Intractable Problems • For some problems, all known algorithms are exponential-time • No known polynomial-time algorithms — not even (n100)! • Sometimes there are efficient approximation algorithms • give good, but not perfect answer • may be good enough for practical purposes CS 100 - Lecture 6

  26. Limitations ofAsymptotic Complexity • Note that 1,000,000 n is (n),but 0.000001 n2 is (n2) • Paradox: 1,000,000 n hrs >> 0.000001 n2 hrs (for n << 1012),but(n) is “more efficient” than (n2) • Not useful if concerned with absolute real-time constraints (e.g., must respond in 2 sec.) • Not useful if concerned with fixed size or bounded problems (e.g., n < 1,000,000) • Sometimes, “the constants matter” CS 100 - Lecture 6

More Related