Study of Decision Trees & Recursive Algorithms: Binary Search & Merge Sort, Lecture notes of Computer Science

An in-depth exploration of decision trees, recursive algorithms, and their applications in computer science. The concepts of structural and non-structural recursion, recursively defined structures, and the importance of dividing and conquering in problem-solving. Examples of binary search and merge sort, explaining their workings and the benefits of using recursion in these algorithms.

Typology: Lecture notes

2012/2013

Uploaded on 04/23/2013

ashwini
ashwini 🇮🇳

4.5

(18)

167 documents

1 / 31

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
if f(x) is O(x) then f(x) is O(x2).
Homework problem:
Frid ay, October 15, 201 0
6:26 PM
Dividing Page 1
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f

Partial preview of the text

Download Study of Decision Trees & Recursive Algorithms: Binary Search & Merge Sort and more Lecture notes Computer Science in PDF only on Docsity!

if f(x) is O(x) then f(x) is O(x

2

Homework problem:

Friday, October 15, 2010 6:26 PM

Docsity.com

Docsity.com

Basis: a question and yes and no branches.

Inductive step: a new question has yes and no branches that are decision trees. Application: the classic game of "animals".

A decision tree

Decision trees Friday, October 15, 2010 5:53 PM

Docsity.com

Goal: traverse a decision tree to guess an animal.

A decision is a question with two consequences,

corresponding to "yes" and "no" answers.

a guess as to an animal.

another decision to make.

A consequence can be either

Form of the tree:

Animals

Friday, October 15, 2010 5:54 PM

Docsity.com

a question.

a yes consequence.

a no consequence.

Each decision has three parts:

Representing a decision tree:

Friday, October 15, 2010 5:58 PM

Docsity.com

["Is it a mammal?", "horse", "fish"]; Step 1: Distinguish between a horse and a fish Step 2: Distinguish between a horse and a pig. ["Is it rideable", "horse", "pig"] Step 3: Combine the above into one "tree": ["Is it a mammal?", ["Is it rideable", "horse", "pig"], "fish"] This is a recursively defined array structure. Step 4: distinguish between a fish and a lizard: ["Does it live in the desert?", "lizard", "fish"] Step 5: combine steps 3 and 4: ["Is it rideable?", "horse", "pig"] ["Does it live in the desert?", "lizard", "fish"] [ "Is it a mammal?", ] We can continue this as long as we want, refining lower levels. Building a decision tree Friday, October 15, 2010 5:59 PM

Docsity.com

A program can learn new decisions to correct its

mistakes!

Key is to modify the structure of the array to codify new

knowledge.

Just as we did in building the original tree.

Can learn from example: "supervised learning".

Point of decision trees

Friday, October 15, 2010 6:39 PM

Docsity.com

Many common algorithms "divide" a problem into

smaller ones and then "conquer" the problem at a

small size.

Dividing and conquering

Binary search

Merge sort

Examples:

Dividing and conquering

Friday, October 15, 2010 2:28 PM

Docsity.com

searching for an item among n sorted things => searching n/2 things. Binary search: Serial version: 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; } elsif ($word lt $words[$mid]) { $high=$mid; } else { $found=&TRUE; last; } } if ($found) { print "'$word' is in the dictionary\n"; } else { print "'$word' is not in the dictionary\n"; } Pasted from Example: binary search Friday, October 15, 2010 2:33 PM

Docsity.com

Start with a big array.

Cut it in half, repeatedly.

Until it has one element.

Recursive version:

# return 1 if a word is in a sorted list.

sub search {

my $words = shift; # a sorted list

my $word = shift; # word to look up

my $elts = @$words;

if ( $elts==1 ) { // inductive basis

if ($words->[0] eq $word) {

return 1;

} else {

return undef;

} else { // recursive step

my $mid = int($elts/2);

if ($word gt $words->[$mid]) {

return &search([@$words[$mid+1..$elts-1]],

$word);

} elsif ($word lt $words->[$mid]) {

return &search([@$words[0..$mid-1]],

$word);

} else {

return 1; # found it!

Pasted from

Recursive version of binary search

Friday, October 15, 2010 5:48 PM

Docsity.com

sub sorter { my $thing = shift; my $length = @$thing; if ($length==0) { return []; } elsif ($length==1) { return [$thing->[0]]; } else { my $size = @$thing; my $mid = int($size/2); my $bot = [@$thing[0..$mid-1]]; my $top = [@$thing[$mid..$size-1]]; my $sbot = &sorter($bot); my $stop = &sorter($top); return &merge($sbot,$stop) ; } } my $out = &sorter([1,6,3,2,9,1,203,40,20]); Pasted from Example: merge sort Friday, October 08, 2010 1:49 PM

Docsity.com

if ($a->[0] <= $b->[0]) { # choose lowest

push(@$c, shift @$a);

} else {

push(@$c, shift @$b);

if (@$a && @$b) { # if both have elements

} elsif (@$a) { # a is all that's left

push(@$c,@$a); $a=[];

} else { # b is all that's left

push(@$c,@$b); $b=[];

return $c;

sub merge {

my $a = shift; $a = [@$a]; # make a copy

my $b = shift; $b = [@$b]; # make a copy

my $c = [];

while (@$a || @$b) { # while one has an element

Merging two (sorted) arrays Sunday, October 10, 2010 4:42 PM

Docsity.com

if ($a->[0] <= $b->[0]) { # choose lowest

push(@$c, shift @$a);

} else {

push(@$c, shift @$b);

Monday, October 18, 2010 3:26 PM

Docsity.com

Docsity.com