Lec2-Algorithms&Complexity

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

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

1 Algorithms and Complexity

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

View Full Document
2 Today 1. What is an algorithm? 1. How to describe and analyze  algorithms? 1. Big-Oh and relatives
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.

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

View Full Document
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.
5 A Well-defined Procedure

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

View Full Document
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
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

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

View Full Document
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; }
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?

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

View Full Document
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
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

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

View Full Document
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
13 What About Best case? Average case?

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

View Full Document
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
05/12/09 07:24 Practical Complexities 10 instructions/second n n nlogn n 2 n 3 1000 1mic 10mic 1milli 1sec 10000 10mic 130mic 100milli 17min 10 6 1milli 20milli 17min 32years

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

View Full Document
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 ??? ???
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern