This preview shows pages 1–15. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Lecture 3 Complexity (Chapter 6) Analysis of sequential search More Linked List Passing a list by reference Recursion (Chapter 3) CS 103 1 Midterm1 on 10.13.10 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 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 5 A graph of Growth Functions CS 103 6 Asymptotic Analysis What happens as problem size grows really, really large? (in the limit when n gets large) constants dont matter lower order terms dont matter CS 103 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 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 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 CS 103 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 10 CS 103 Definition of ONotation 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 CS 103 11 11 CS 103 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 ;where K=C, & n =1 Therefore f(n) = O(n) CS 103 12 12 CS 103 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 13 CS 103 Analysis of Sequential Search Best case O (1) Worst case O (n) Average case O (n) CS 103 14 14 CS 103 Definition of ONotation...
View
Full
Document
This note was uploaded on 11/14/2011 for the course CSCI 1132 taught by Professor Haya during the Fall '11 term at GWU.
 Fall '11
 Haya
 C Programming, Recursion

Click to edit the document details