




















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Main points of this lecture are: Inductive Proof, Inductive Hypothesis, Complexity, Linear Search, Binary Search, Hash Search, Space Complexity, Time Complexity, Trading Space and Time, Value of Preparation, Measuring Time, Concepts of Time and Space
Typology: Lecture notes
1 / 28
This page cannot be seen from the preview
Don't miss anything!





















Pasted from
Monday, October 04, 2010 12:52 PM Docsity.com
Saturday, October 02, 2010 5:40 PM Docsity.com
or die "can't read the dictionary: $!"; open(DICT, ") { chomp; push(@words, $_); } print "Enter words to look up: "; while () { chomp; my @line = split(/\s+/,$_); foreach my $word (@line) { my $found = &FALSE; for (my $i=0; $i<@words; $i++) { if ($words[$i] eq $word) { $found = &TRUE; last; } } if ($found) { print "'$word' is in the dictionary\n"; } else { print "'$word' is not in the dictionary\n"; } } print "Enter words to look up: "; } Pasted from
Saturday, October 02, 2010 5:48 PM Docsity.com
or die "can't read the dictionary: $!"; open(DICT, ") { chomp; push(@words, $_); } @words = sort (@words); print "Enter words to look up: "; while () { chomp; my @line = split(/\s+/,$_); foreach my $word (@line) { my $found = &FALSE; my $low = 0; my $high = @words-1; while ($high-$low>1) { my $mid = ($high + $low)/2; if ($word gt $words[$mid]) { $low=$mid; # rule out #$words[$low]-$words[$mid-1] } elsif ($word lt $words[$mid]) { $high=$mid; # rule out #words[$mid+1]-$words[$high] } else { # found it: end $found=&TRUE; last; } } if ($found) { print "'$word' is in the dictionary\n"; } else { print "'$word' is not in the dictionary\n"; } } print "Enter words to look up: "; } Pasted from
Saturday, October 02, 2010 6:28 PM Docsity.com
Saturday, October 02, 2010 6:35 PM Docsity.com
Saturday, October 02, 2010 6:37 PM Docsity.com
sub readmemory { open(PROC, ") { chomp; my ($key, $value)=split(/[\s:]+/); close PROC; if ($key eq 'VmSize') { return $value } } return undef; } $mem1 = &readmemory; for ($i=0; $i<100000; $i++) { $a[$i]=$i; } $mem2 = &readmemory; $diff = $mem2 - $mem1; print "mem1=$mem1 mem2=$mem2 diff=$diff kbytes\n"; See and run http://www.cs.tufts.edu/comp/14/examples/Complexity/space.perl Caution : not subtracting $mem1 would count the size of perl itself!
Sunday, October 03, 2010 2:49 PM Docsity.com
Sunday, October 03, 2010 2:58 PM Docsity.com
Sunday, October 03, 2010 3:06 PM Docsity.com
We define some concept of a "program step". Then we "count the steps". How long does a program take? If we define a step as a program statement, then for ($i=0; $i<100; $i++) { $sum+=$i; } takes 302 steps. Example: for ($i=0; $i<100; $i++) { $sum += $i; } Reasoning: is equivalent with $i=0; # 1 step while ($i<100) { # 1 step, TRUE 100 times, then FALSE $sum+=$i; # 1 step $i++; # 1 step } So the total is 1+(1+1+1)*100 +1 =302 total steps.
Saturday, October 02, 2010 7:05 PM Docsity.com
Saturday, October 02, 2010 7:11 PM Docsity.com
Saturday, October 02, 2010 7:12 PM Docsity.com
Let n be some measure of input size. C is a multiplicative constant that represents a change in units N is a boundary condition that determines how large n has to be for the change in units to work. We say that a program takes O(n) time (or space) if there are constants C and N such that if n>N, the time (or space) required < Cn.
Saturday, October 02, 2010 7:14 PM Docsity.com
Sunday, October 03, 2010 4:14 PM Docsity.com