1 / 32

Algorithms

Algorithms. CS/APMA 202 Rosen section 2.1 Aaron Bloomfield. What is an algorithm?. An algorithm is “a finite set of precise instructions for performing a computation or for solving a problem” A program is one type of algorithm All programs are algorithms Not all algorithms are programs!

opalp
Download Presentation

Algorithms

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. Algorithms CS/APMA 202 Rosen section 2.1 Aaron Bloomfield

  2. What is an algorithm? • An algorithm is “a finite set of precise instructions for performing a computation or for solving a problem” • A program is one type of algorithm • All programs are algorithms • Not all algorithms are programs! • Directions to somebody’s house is an algorithm • A recipe for cooking a cake is an algorithm • The steps to compute the cosine of 90° is an algorithm

  3. Some algorithms are harder than others • Some algorithms are easy • Finding the largest (or smallest) value in a list • Finding a specific value in a list • Some algorithms are a bit harder • Sorting a list • Some algorithms are very hard • Finding the shortest path between Miami and Seattle • Some algorithms are essentially impossible • Factoring large composite numbers • In section 2.2, we’ll see how to rate how “hard” algorithms are

  4. Algorithm 1: Maximum element • Given a list, how do we find the maximum element in the list? • To express the algorithm, we’ll use pseudocode • Pseudocode is kinda like a programming language, but not really

  5. Algorithm 1: Maximum element • Algorithm for finding the maximum element in a list: procedure max (a1, a2, …, an: integers) max := a1 for i := 2 to n if max < aithenmax := ai {max is the largest element}

  6. Algorithm 1: Maximum element max := a1 fori := 2 to n if max < aithenmax := ai procedure max (a1, a2, …, an: integers) max := a1 fori := 2 to n if max < aithenmax := ai 4 7 9 2 3 4 5 6 7 8 9 10

  7. Maximum element running time • How long does this take? • If the list has n elements, worst case scenario is that it takes n “steps” • Here, a step is considered a single step through the list

  8. Properties of algorithms • Algorithms generally share a set of properties: • Input: what the algorithm takes in as input • Output: what the algorithm produces as output • Definiteness: the steps are defined precisely • Correctness: should produce the correct output • Finiteness: the steps required should be finite • Effectiveness: each step must be able to be performed in a finite amount of time • Generality: the algorithm should be applicable to all problems of a similar form

  9. Searching algorithms • Given a list, find a specific element in the list • We will see two types • Linear search • a.k.a. sequential search • Binary search

  10. Algorithm 2: Linear search • Given a list, find a specific element in the list • List does NOT have to be sorted! procedure linear_search (x: integer; a1, a2, …, an: integers) i := 1 while ( i≤ n and x ≠ ai ) i := i + 1 ifi≤nthenlocation := i elselocation := 0 {location is the subscript of the term that equals x, or it is 0 if x is not found}

  11. location x 8 3 Algorithm 2: Linear search, take 1 procedure linear_search (x: integer; a1, a2, …, an: integers) i := 1 while ( i≤ n and x ≠ ai ) i := i + 1 ifi≤nthenlocation := i elselocation := 0 i := 1 while ( i≤ n and x ≠ ai ) i := i + 1 ifi≤nthenlocation := i elselocation := 0 2 3 4 5 6 7 8 1

  12. location x 0 11 Algorithm 2: Linear search, take 2 procedure linear_search (x: integer; a1, a2, …, an: integers) i := 1 while ( i≤ n and x ≠ ai ) i := i + 1 ifi≤nthenlocation := i elselocation := 0 i := 1 while ( i≤ n and x ≠ ai ) i := i + 1 ifi≤nthenlocation := i elselocation := 0 2 3 4 5 10 7 9 8 1 6 11

  13. Linear search running time • How long does this take? • If the list has n elements, worst case scenario is that it takes n “steps” • Here, a step is considered a single step through the list

  14. Algorithm 3: Binary search • Given a list, find a specific element in the list • List MUST be sorted! • Each time it iterates through, it cuts the list in half procedure binary_search (x: integer; a1, a2, …, an: increasing integers) i := 1 { i is left endpoint of search interval } j := n { j is right endpoint of search interval } whilei < j begin m := (i+j)/2 { m is the point in the middle } if x > amtheni := m+1 elsej := m end ifx = aithenlocation := i elselocation := 0 {location is the subscript of the term that equals x, or it is 0 if x is not found}

  15. procedure binary_search (x: integer; a1, a2, …, an: increasing integers) i := 1 j := n whilei < j begin m := (i+j)/2 if x > amtheni := m+1 elsej := m end ifx = aithenlocation := i elselocation := 0 location x 7 14 j m i Algorithm 3: Binary search, take 1 i := 1 j := n whilei < j begin m := (i+j)/2 if x > amtheni := m+1 elsej := m end ifx = aithenlocation := i 6 7 1 8 7 6 5 10 8 7

  16. procedure binary_search (x: integer; a1, a2, …, an: increasing integers) i := 1 j := n whilei < j begin m := (i+j)/2 if x > amtheni := m+1 elsej := m end ifx = aithenlocation := i elselocation := 0 location x 0 15 i j m Algorithm 3: Binary search, take 2 i := 1 j := n whilei < j begin m := (i+j)/2 if x > amtheni := m+1 elsej := m end ifx = aithenlocation := I elselocation := 0 6 1 8 8 7 5 8 10

  17. Algorithm 3: Binary search • A somewhat alternative view of what a binary search does…

  18. Quick survey • I felt I understood the search algorithms… • Very well • With some review, I’ll be good • Not really • Not at all

  19. Today’s demotivators

  20. Binary search running time • How long does this take (worst case)? • If the list has 8 elements • It takes 3 steps • If the list has 16 elements • It takes 4 steps • If the list has 64 elements • It takes 6 steps • If the list has n elements • It takes log2n steps

  21. Sorting algorithms • Given a list, put it into some order • Numerical, lexicographic, etc. • We will see two types • Bubble sort • Insertion sort

  22. Algorithm 4: Bubble sort • One of the most simple sorting algorithms • Also one of the least efficient • It takes successive elements and “bubbles” them up the list procedure bubble_sort (a1, a2, …, an) for i := 1 to n-1 forj := 1 ton-i ifaj > aj+1 then interchange aj and aj+1 { a1, …, an are in increasing order }

  23. Algorithm 4: Bubble sort • An example using physical objects…

  24. Bubble sort running time • Bubble sort algorithm: for i := 1 to n-1 forj := 1 ton-i ifaj > aj+1 then interchange aj and aj+1 • Outer for loop does n-1 iterations • Inner for loop does • n-1 iterations the first time • n-2 iterations the second time • … • 1 iteration the last time • Total: (n-1) + (n-2) + (n-3) + … + 2 + 1 = (n2-n)/2 • We can say that’s “about” n2 time

  25. Algorithm 5: Insertion sort • Another simple (and inefficient) algorithm • It starts with a list with one element, and inserts new elements into their proper place in the sorted part of the list procedure insertion_sort (a1, a2, …, an) forj := 2 ton begin i := 1 whileaj > ai i := i +1 m := aj fork := 0 toj-i-1 aj-k := aj-k-1 ai := m end { a1, a2, …, an are sorted } take successive elements in the list find where that element should be in the sorted portion of the list move all elements in the sorted portion of the list that are greater than the current element up by one put the current element into it’s proper place in the sorted portion of the list

  26. Insertion sort running time forj := 2 ton begin i := 1 whileaj > ai i := i +1 m := aj fork := 0 toj-i-1 aj-k := aj-k-1 ai := m end { a1, a2, …, an are sorted } • Outer for loop runs n-1 times • In the inner for loop: • Worst case is when the while keeps i at 1, and the for loop runs lots of times • If i is 1, the inner for loop runs 1 time (k goes from 0 to 0) on the first iteration, 1 time on the second, up to n-2 times on the last iteration • Total is 1 + 2 + … + n-2 = (n-1)(n-2)/2 • We can say that’s “about” n2 time

  27. Quick survey • I felt I understood the sort algorithms… • Very well • With some review, I’ll be good • Not really • Not at all

  28. Comparison of running times • Searches • Linear: n steps • Binary: log2n steps • Binary search is about as fast as you can get • Sorts • Bubble: n2 steps • Insertion: n2 steps • There are other, more efficient, sorting techniques • In principle, the fastest are heap sort, quick sort, and merge sort • These each take take n * log2n steps • In practice, quick sort is the fastest, followed by merge sort

  29. Greedy algorithms • Ignore this part…

  30. Quick survey • I felt I understood the material in this slide set… • Very well • With some review, I’ll be good • Not really • Not at all

  31. Quick survey • The pace of the lecture for this slide set was… • Fast • About right • A little slow • Too slow

  32. Quick survey • How interesting was the material in this slide set? Be honest! • Wow! That was SOOOOOO cool! • Somewhat interesting • Rather borting • Zzzzzzzzzzz

More Related