{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

CS1132_Fall_2011_Lecture3_BB

CS1132_Fall_2011_Lecture3_BB - Lecture 3 Complexity(Chapter...

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

View Full Document Right Arrow Icon
Lecture 3 Complexity (Chapter 6) Analysis of sequential search More Linked List Passing a list by reference Recursion (Chapter 3) CS 103 1
Image of page 1

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

View Full Document Right Arrow Icon
Midterm1 on 10.13.10
Image of page 2
3 Complexity (Chapter 6) How many resources will it take to solve a problem of a given size? time space Resources are expressed as a function of problem size number of elements to be handled size of data to be operated on CS 103
Image of page 3

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

View Full Document Right Arrow Icon
4 Complexity or Asymptotic Analysis Analysis of the running time (aka computational complexity) of an algorithm in a theoretical model. Using a theoretical model allows us to ignore the effects of Which computer are we using? How good is our compiler at optimization We define the running time of an algorithm with input size n as T( n ) and examine the rate of growth of T( n ) as n grows larger and larger and larger. CS 103
Image of page 4
5 A graph of Growth Functions CS 103
Image of page 5

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

View Full Document Right Arrow Icon
6 Asymptotic Analysis What happens as problem size grows really, really large? (in the limit when n gets large) constants don’t matter lower order terms don’t matter CS 103
Image of page 6
Mileage Example Mileage example: How much gas does it take to go 20 miles? Worst case: all uphill Best case: all downhill, just coast Average case: “average terrain” 7 CS 103
Image of page 7

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

View Full Document Right Arrow Icon
8 Cases Example Consider a sequential search on an unsorted array of length n. What is the time complexity? Best case: Worst case: Average case: CS 103
Image of page 8
Analysis of Sequential Searching #define n 100 typedef int key; // the type can be any type (int, char etc.) typedef key SearchArray[n]; int SequuentialSearch ( Key k, SearchArray A ) { int i; for ( i = 0; i < n ; ++i ) { // b overhead for setting up the loop if ( k == A[i] ) { // a amount of work for every iteration return i ; } } return ( - 1); } CS 103 9
Image of page 9

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

View Full Document Right Arrow Icon
Analysis of Sequential Searching Total = (a*1 + b ) + …. + (a*n + b) = = a(1 + 2+ …. + n ) + b * n = = a n(n+1)/2 + bn The Average = Total/n= (a n(n+1)/2 + bn)/n = a(n+1)/2 + b = (a/2)n + (a/2 + b) Where a is the amount of work for every iteration b is the overhead for setting up the loop CS 103 10
Image of page 10
Definition of O-Notation F(n) is O[g(n)] if there exist two positive constants K and n 0 such that | f(n) | =< K | g(n) | for all n >= n 0 CS 103 11
Image of page 11

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

View Full Document Right Arrow Icon
Analysis of Sequential Searching (Contd.) Total = (a*1 + b ) + …. + (a*n + b) = = a(1 + 2+ …. + n ) + b * n =a n(n+1)/2 + bn f(n)=The Average = Total/n= (a n(n+1)/2 + bn)/n = a(n+1)/2 + b = (a/2)n + (a/2 + b) f(n) = An + B ( A=a/2; B=a/2+b) f(n)=An+B=<An + Bn=(A+B)n=Cn =Cg(n); (C=A+B) & g(n)=n |f(n)| =< K |g(n)| for all n >= n 0 ;where K=C, & n 0 =1 Therefore f(n) = O(n) CS 103 12
Image of page 12
Best, Worst, & Average Performance Typically we define behavior for three cases: o Best Case item searched found first in the list O(1) o Worst Case item searched found last in the list O(n) o Average Case item searched found in the middle of the list Generally we use the Average case behavior O(n) CS 103 13
Image of page 13

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

View Full Document Right Arrow Icon
Analysis of Sequential Search Best case O (1) Worst case O (n) Average case O (n) CS 103 14
Image of page 14
Definition of O-Notation F(n) is O[g(n)] if there exist two positive constants K and n 0 such that | f(n) | =< K | g(n) | for all n >= n 0 CS 103 15
Image of page 15

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

View Full Document Right Arrow Icon
Another Example : Formal Proof of O-Notation Suppose Algorithm A sorts a sequence of n numbers in an array into ascending order where f(n)
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