Lec2-Algorithms&Complexity

Lec2-Algorithms&Complexity - Algorithms and Complexity...

Info iconThis preview shows pages 1–17. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Algorithms and Complexity
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Today 1. What is an algorithm? 1. How to describe and analyze  algorithms? 1. Big-Oh and relatives
Background image of page 2
3 What is an Algorithm? Algorithm Input Output An  algorithm  is a step-by-step procedure for solving a problem in a finite amount of time.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 What is an Algorithm? Algorithm : a procedure for solving a well- defined problem in a finite number of steps.  The statement of the problem specifies in  general terms an input/output relationship. The algorithm describes a well-defined  procedure for achieving the input/output  relationship.
Background image of page 4
5 A Well-defined Procedure
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 Sorting Input: An array A of n integers. Output: rearranged array A with  elements in non-decreasing order: A[0] <= A[1] <= … <= A[n-1] Example: 8, 6, 9, 4, 3 => 3, 4, 6, 8, 9
Background image of page 6
7 Insert an Element 3, 6, 9, 14       insert 5 Compare new element (5) and last one  (14) Shift 14 right to get 3, 6, 9, , 14 Shift 9 right to get 3, 6, , 9, 14 Shift 6 right to get 3, , 6, 9, 14 Insert 5 to get 3, 5, 6, 9, 14
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
8 Insertion Sort for (int i = 1; i < a.length; i++) {// insert a[i] into a[0:i-1]     int t = a[i];    int j;    for (j = i - 1; j >= 0 && t < a[j]; j--)        a[j + 1] = a[j];    a[j + 1] = t; }
Background image of page 8
05/12/09 07:24 Comparison Count for (j = i - 1; j >= 0 &&  t < a[j] ; j--)        a[j + 1] = a[j]; How many comparisons are made?
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
05/12/09 07:24 Comparison Count for (j = i - 1; j >= 0 &&  t < a[j] ; j--)        a[j + 1] = a[j]; number of compares depends on  a[]s and t as well as on i 
Background image of page 10
05/12/09 07:24 Worst-Case Comparison  Count for (j = i - 1; j >= 0 &&  t < a[j] ; j--)        a[j + 1] = a[j]; a = [1, 2, 3, 4] and t = 0 => 4 compares a = [1,2,3,…,i] and t = 0 => i compares
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
05/12/09 07:24 Worst-Case Comparison  Count for (int i = 1; i < n; i++)    for (j = i - 1; j >= 0 &&  t < a[j] ; j--)        a[j + 1] = a[j]; total compares = 1 + 2 + 3 + … + (n-1) = (n-1)n/2
Background image of page 12
13 What About Best case? Average case?
Background image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
14 Analysing Algorithms Show algorithm is correct Most algorithms transform input objects  into output objects, and their running  time grows with the input size. Identify basic operations and problem  size. Count number of basic operations as  function of problem size. Worst case: MAXIMUM number of  basic operations over ALL inputs of size  n. Best case: MINIMUM number of basic  operations over ALL inputs of size n. Average case: AVERAGE … 0 20 40 60 80 100 120 Running Time 1000 2000 3000 4000 I nput Size best case average case worst case
Background image of page 14
05/12/09 07:24 Practical Complexities 10 instructions/second n n nlogn 2 n 3 1000 1mic 10mic 1milli 1sec 10000 10mic 130mic 100milli 17min 10 6 1milli 20milli 17min 32years
Background image of page 15

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
05/12/09 07:24 Impractical Complexities 10 9  instructions/second n n 4 n 10 2 n 1000 17min 3.2 x 10 13 years 3.2 x 10 283 years 10000 116 days ??? ???
Background image of page 16
Image of page 17
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/20/2008 for the course CSC 530 taught by Professor Steffenheber during the Spring '08 term at N.C. State.

Page1 / 43

Lec2-Algorithms&Complexity - Algorithms and Complexity...

This preview shows document pages 1 - 17. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online