Presentation is loading. Please wait.

Presentation is loading. Please wait.

Algorithms A well-defined computational procedure that takes some value as input and produces some value as output. (Also, a sequence of computational.

Similar presentations


Presentation on theme: "Algorithms A well-defined computational procedure that takes some value as input and produces some value as output. (Also, a sequence of computational."— Presentation transcript:

1 Algorithms A well-defined computational procedure that takes some value as input and produces some value as output. (Also, a sequence of computational steps that transforms the input into the output.)

2 Shampooing Lather Rinse Repeat

3 Baking Preheat Oven to 350 F Grease Pan Mix Ingredients Pour into Pan Back until Done

4 Algorithms The Notion of a Finite or Effective Procedure Like a Recipe 1. Input (x,y) Coordinates 2. For each (x,y) compute c 3. If c is even, plot black dot 4. Repeat

5 Analysis of Algorithms How do we describe an algorithm and the cost of an algorithm? SORTING: Input is a sequence of numbers Output is a permutation of that sequence so that they are in increasing order How many steps does it take?

6 Pseudocode Liberal Use of English Use of indentation for block structure Clear and concise expressive methods Not concerned with software engineering or implementation issues. Language Independent

7 Insertion Sort: Main Idea A[i..j-1] - currently sorted part A[j+1..n]- currently unsorted part pick element A[j] move elements A[j-1]..A[1] to right until proper position for A[j] is found Insert key (A[j]) at that position

8 Insertion Sort Sorts A[1..n] in place 1. For j <--- 2 to length[A] 2. Do key <--- A[j] 3. |> Insert A[j] into the sorted sequence A[1..j-1] 4. i <--- j-1 5. While i>0 and A[i] > key 6. Do A[i+1} <--- A[i] 7. i <--- i-1 8. A[i+1] <--- key

9 Pseudocode Conventions 1. Indentation indicates block structure. “for” loop, “while” loop 2. |> indicates comment. 3. “i <--- j” assign value of j to i. 4. Array elements A[j].

10 Running Time For a given input, what is the algorithms running time. What is meant by “time?” CPU? Wall-Clock? Doesn’t it depend on implementation?

11 Running Time Typically count primitive operations Indexing array Moving an item Loop accounting Characterization independent of machine What is upper bound?

12 Running Time Independent of Input Size Express as a function of input size, rather that of a particular input More that one answer

13 Kinds of Analysis Experss as function of input size n T(n) Usually, worst case: T(n)=max time on any input of size n Average Case: T(n)=average time over all input size n Best Case: Bad if not uniform

14 Asymptotic Analysis Look at growth of T(n) as n--->infinity  Notation –Drop low-order terns –Ignore Leading constants

15 Insertion Sort Sorts A[1..n] in place : while loop time 1. For j <--- 2 to length[A] : n 2. Do key <--- A[j] : n-1 3. |> Insert A[j] into the sorted sequence A[1..j-1] 4. i <--- j-1 5. While i>0 and A[i] > key 6. Do A[i+1} <--- A[i] 7. i <--- i-1 8. A[i+1] <--- key

16 Divide and Conquer Recursive: To solve a problem, algorithm calls itself. Divide and conquer: break into smaller problems Merge Sort: Divide n-element sequence into two sequences of n/2 elements Sort two subsequences and merge MERGE-SORT(A,p,r) sort A[p…r]

17 MERGE Key is MERGE operation, sort two lists, xm and yn into new sorted list zm+n Takes order n=r-p+1 steps 1. Initialize: i <---1, j<---1, k<---1 2. If xi < yi, go to 3, else go to 5 3. Set zk <--- xi, k<---k+1, i<---i+1. If i<=m, go to 2. 4. Set (zk…zm+n) <--- (yj…yn) terminate 5. Set zk <---yj, k<---k+1, j<---j+1. If j<=n, got to 2. 6. Set (zk…zm+n) <---(xi…xm) terminate

18 MERGE Some More Two piles of sorted cards face up on table Smallest on top Choose smaller of two and place face down on table Repeat until one pile empty Place remaining pile face down on output pile

19 Merge Sort MERGE-SORT(A,p,r) 1. If p < r 2. Then q <--- |_(p+r)/2_| 3. MERGE-SORT(A,p,q) 4. MERGE-SORT(A,q+1,r) 5. MERGE(A,p,q,r) If p>=r, then array has one element and is already sorted.

20 Analyzing Recurrence Write as a recurrence equation D(n) time to divide into subproblems C(n) time to combine solutions of subproblems T(n) running time: a subproblems, each 1/b the size of the original.

21 Big O Notatation How long will it take? How much memory will it use? Order of Magnitude Notation instance size n f(n) = N than, f(n) = O(g(n)) O(n) < O(n log n)<O(n 1/2 )<O(n 2 )<O(2 n )


Download ppt "Algorithms A well-defined computational procedure that takes some value as input and produces some value as output. (Also, a sequence of computational."

Similar presentations


Ads by Google