Lec2-Algorithms&Complexity

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

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

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

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

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

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

View Full Document Right 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.
Image of page 4
5 A Well-defined Procedure
Image of page 5

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

View Full Document Right 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
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
Image of page 7

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

View Full Document Right 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; }
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?
Image of page 9

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

View Full Document Right 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 
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
Image of page 11

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

View Full Document Right 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
Image of page 12
13 What About Best case? Average case?
Image of page 13

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

View Full Document Right 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
Image of page 14
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
Image of page 15

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

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern