0% found this document useful (0 votes)
24 views11 pages

Alogorithm Discussion

The document discusses various topics related to data structures and algorithms: 1) Floating point numbers - It discusses how floating point numbers are stored and compared in different ways such as using cout vs printf. It also discusses limitations of floating point representation. 2) Hashing - It discusses hashing techniques like separate chaining and open addressing for resolving collisions. It provides examples of questions related to hashing like finding the most frequent integer. 3) Recurrence relations - It explains what recurrence relations are and how to solve them. It provides examples like the Fibonacci sequence. 4) Binary search trees - It provides a basic definition of a binary search tree where every node has a key, value, and

Uploaded by

murad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views11 pages

Alogorithm Discussion

The document discusses various topics related to data structures and algorithms: 1) Floating point numbers - It discusses how floating point numbers are stored and compared in different ways such as using cout vs printf. It also discusses limitations of floating point representation. 2) Hashing - It discusses hashing techniques like separate chaining and open addressing for resolving collisions. It provides examples of questions related to hashing like finding the most frequent integer. 3) Recurrence relations - It explains what recurrence relations are and how to solve them. It provides examples like the Fibonacci sequence. 4) Binary search trees - It provides a basic definition of a binary search tree where every node has a key, value, and

Uploaded by

murad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Agenda'

Data Structures
and Algorithms

Floa%ng(Point(Numbers(
Hashing(
Recurrence(Rela%ons(
Binary(Search(Tree(

Discussion 3: Week of Sep 21, 2011

Floa+ng'Point'Numbers'

Floa+ng'Point'Numbers'

!double d1 = 1.1234;"
!double d2 = 2.1234;"
!cout << d2 - d1 << endl;"
!// 1 or 1.0000 ?(

!double d1 = 1.1234;"
!double d2 = 2.1234;"
!cout << d2 - d1 << endl;"
!// outputs 1"
"
!printf(%.4f\n,d2 - d1);"
!// outputs 1.0000(

Floa+ng'Point'Numbers'
#include <iostream>!
#include <iomanip>!
using namespace std;!
int main()!
{!
!double d1 = 1.1234;!
!double d2 = 2.1234;!
!cout << setprecision
!cout << d1 << endl;!
!cout << d2 << endl;!
!cout << setprecision
!cout << fixed;!
!cout << setprecision
!cout << setprecision
!return 0;!
}!

Floa+ng'Point'Numbers'
!if (1 == 1.0000) {"
! !cout << "Equal" << endl;"
!}"
!else {"
! !cout << "Not equal" << endl;"
!}"

(3);!
(9) << d1 << endl;!
(3) << d1 << endl;!
(9) << d1 << endl;!

Floa+ng'Point'Numbers'

Float'Limita+on'

!if (1 == 1.0000000000000001) {"


! !cout << "Equal" << endl;"
!}"
!else {"
! !cout << "Not equal" << endl;"
!}"

Float'value'

Hex'

Decimal'

1.99999976((

0x3FFFFFFE(

1073741822(

1.99999988(

0x3FFFFFFF(

1073741823(

2.00000000(

0x4000000(

1073741824(

2.00000024(

0x4000001(

1073741825(

2.00000048(

0x4000002(

1073741826(

To(store(values(between(1.99999988(and(2,(
you(need(to(either(use(a(double(or(parse(the(
input(as(characters(and(use(a(type(that(has(
enough(bits(to(t(

Hashing'

Separate Chaining

Associa%ve(container(
No(concept(of(previous/next(
Insert/delete/lookup(in(O(1)(%me(

Hash(a(key(into(index,(store(the(value(into(
hash_map[index](
Collision(resolu%on(
Separate(chaining((
Probing/open(addressing(
linear(
quadra%c(

10

Quiz'for'Hashing'

Open addressing

Given(n(integers,(nd(the(one(with(highest(frequency.(Return(
the(smallest(one(if(theres(a(%e.((

11

12

Quiz'for'Hashing'

Quiz'for'Hashing'

Given(n(integers,(nd(the(one(with(highest(frequency.(Return(
the(smallest(one(if(theres(a(%e.((
1.(Count(the(frequency(for(each(integer(using(hash(table(
2.(Key(is(the(number,(value(is(the(frequency((
3.(Scan(the(hash(table(to(return(the(smallest(with(highest(freq(

Given(n(integers,(nd(the(one(with(highest(frequency.(Return(
the(smallest(one(if(theres(a(%e.((
1.(Count(the(frequency(for(each(integer(using(hash(table(
2.(Key(is(the(number,(value(is(the(frequency((
3.(Scan(the(hash(table(to(return(the(smallest(with(highest(freq(
Complexity?(Size(of(hash(table(=(N(

13

14

Quiz'for'Hashing'

Quiz'for'Hashing'

Given(n(integers,(nd(the(one(with(highest(frequency.(Return(
the(smallest(one(if(theres(a(%e.((
1.(Count(the(frequency(for(each(integer(using(hash(table(
2.(Key(is(the(number,(value(is(the(frequency((
3.(Scan(the(hash(table(to(return(the(smallest(with(highest(freq(
Complexity(=(O(n(+(N)((Size(of(hash(table(=(N(

Given(n(integers,(nd(the(one(with(highest(frequency.(Return(
the(smallest(one(if(theres(a(%e.((
1.(Count(the(frequency(for(each(integer(using(hash(table(
2.(Key(is(the(number,(value(is(the(frequency((
3.(Scan(the(hash(table(to(return(the(smallest(with(highest(freq(
Complexity(=(O(n(+(N)((Size(of(hash(table(=(N(
Whats(our(hash(func%on?(

15

16

Quiz'for'Hashing'

Quiz'for'Hashing'

Given(n(integers,(nd(the(one(with(highest(frequency.(Return(
the(smallest(one(if(theres(a(%e.((
1.(Count(the(frequency(for(each(integer(using(hash(table(
2.(Key(is(the(number,(value(is(the(frequency((
3.(Scan(the(hash(table(to(return(the(smallest(with(highest(freq(
Complexity(=(O(n(+(N)((Size(of(hash(table(=(N(
Whats(our(hash(func%on?(

Given(n(integers,(nd(the(one(with(highest(frequency.(Return(
the(smallest(one(if(theres(a(%e.((
1.(Count(the(frequency(for(each(integer(using(hash(table(
2.(Key(is(the(number,(value(is(the(frequency((
3.(Scan(the(hash(table(to(return(the(smallest(with(highest(freq(
Complexity(=(O(n(+(N)((Size(of(hash(table(=(N(
Whats(our(hash(func%on?(

int(hash(int(value)({(return(value;(}(

int(hash(int(value)({(return(value;(}(
Does(not(work(for(hash(i1)(

17

18

Quiz'for'Hashing'

Quiz'for'Hashing'

Given(n(integers,(nd(the(one(with(highest(frequency.(Return(
the(smallest(one(if(theres(a(%e.((
1.(Count(the(frequency(for(each(integer(using(hash(table(
2.(Key(is(the(number,(value(is(the(frequency((
3.(Scan(the(hash(table(to(return(the(smallest(with(highest(freq(
Complexity(=(O(n(+(N)((Size(of(hash(table(=(N(
Whats(our(hash(func%on?(

Given(n(integers,(nd(the(one(with(highest(frequency.(Return(
the(smallest(one(if(theres(a(%e.((
1.(Count(the(frequency(for(each(integer(using(hash(table(
2.(Key(is(the(number,(value(is(the(frequency((
3.(Scan(the(hash(table(to(return(the(smallest(with(highest(freq(
Complexity(=(O(n(+(N)((Size(of(hash(table(=(N(
Whats(our(hash(func%on?(

int(hash(int(value)({(return(value;(}(
Does(not(work(for(hash(i1)(
How(about({(return(abs(value);(}(?(

int(hash(int(value)({(return(value;(}(
Does(not(work(for(hash(i1)(
How(about({(return(abs(value);(}(?(

19

20

Quiz'for'Hashing'

Quiz'for'Hashing'

Given(n(integers,(nd(the(one(with(highest(frequency.(Return(
the(smallest(one(if(theres(a(%e.((
1.(Count(the(frequency(for(each(integer(using(hash(table(
2.(Key(is(the(number,(value(is(the(frequency((
3.(Scan(the(hash(table(to(return(the(smallest(with(highest(freq(
Complexity(=(O(n(+(N)((Size(of(hash(table(=(N(
Whats(our(hash(func%on?(

Given(n(integers,(nd(the(one(with(highest(frequency.(Return(
the(smallest(one(if(theres(a(%e.((
1.(Count(the(frequency(for(each(integer(using(hash(table(
2.(Key(is(the(number,(value(is(the(frequency((
3.(Scan(the(hash(table(to(return(the(smallest(with(highest(freq(
Complexity(=(O(n(+(N)((Size(of(hash(table(=(N(
Whats(our(hash(func%on?(

int(hash(int(value)({(return(value;(}(
Does(not(work(for(hash(i1)(
How(about({(return(abs(value);(}(?(
Does(not(work(for((i2^31),(cant(store(2^31(((>(INT_MAX)(

int(hash(int(value)({(return(value;(}(
Does(not(work(for(hash(i1)(
How(about({(return(abs(value);(}(?(
Does(not(work(for((i2^31),(cant(store(2^31(((>(INT_MAX)(
More(importantly,(input(distribu%on(decides(output(distribu%on(

21

22

Quiz'for'Hashing'

Recurrence'Rela+ons'

Given(n(integers,(nd(the(one(with(highest(frequency.(Return(
the(smallest(one(if(theres(a(%e.((
1.(Count(the(frequency(for(each(integer(using(hash(table(
2.(Key(is(the(number,(value(is(the(frequency((
3.(Scan(the(hash(table(to(return(the(smallest(with(highest(freq(
Complexity(=(O(n(+(N)((Size(of(hash(table(=(N(
Whats(our(hash(func%on?(

Usually(used(to(analyze(algorithm(run%mes.((

int(hash(int(value)({(return(value;(}(
Does(not(work(for(hash(i1)(
How(about({(return(abs(value);(}(?(
Does(not(work(for((i2^31),(cant(store(2^31(((>(INT_MAX)(
More(importantly,(input(distribu%on(decides(output(distribu%on(
{(return(abs(value(%(LARGE_PRIME);(}(

23

Many(algorithms(loop(on(a(problem(set,(do(something(each(
%me,(and(create(smaller(subiproblems(to(solve.(This(is(the(
idea(behind(a(recurrence(rela%on.((
To(solve(them((think!((
How(is(this(problem(set(changing(each(%me?(Decreasing(
exponen%ally?(Variably?(Constantly?((
How(many(new(subiproblems(are(being(created(each(%me?((

24

Recurrence'Rela+ons'

Recurrence'Rela+ons'

Recurrence(rela%ons(are(those(that(are(dened(in(terms(of(
themselves.(

Recurrence(rela%ons(are(those(that(are(dened(in(terms(of(
themselves.(

S(0) = 0
S(n) = n + S(n - 1)

S(0) = 0
S(n) = n + S(n - 1)

Whats(the(closed(form(of(S(n)?(

Whats(the(closed(form(of(S(n)?(
S(n) = n + (n-1) + + 1 + 0
S(n) = n * (n - 1)/2(

25

26

Master'Theorem'''

Recurrence'Rela+ons'

Suppose(
(T(n)(=(a(*(T(n/c)(+(f(n)((
where((a>=1,(c>1(and(n/c(means(either(!n/c"(or(#n/c$(
Then(
1)(If(f(n)=O(n^(logcai))(for(some(>0,(then(
T(n)=(n^logca)(
2)(If(f(n)=(n^logca),(then(T(n)=((n^logca)logn)(
3)(If(f(n)=(n^(logca+))(for(some(>0(and(if(
a*f(n/c)kf(n)(for(some(constant(k<1(and(all(
suciently(large(n,(then(T(n)= (f(n))(
(
27

How would we express the Fibonacci sequence as


a recurrence relation?

What about the tribonacci sequence?

28

Recurrence'Rela+ons'

Recursive'Func+ons'

How would we express the Fibonacci sequence as


a recurrence relation?

We can use the following method to define a function


with the natural numbers as its domain:
1. Specify the value of the function at zero.
2. Give a rule for finding its value at any integer
from its values at smaller integers.

F(0)=0,(F(1)=1,(F(n)=F(ni1)(+(F(ni2)(
What about the tribonacci sequence?
(((((((T(0)=1,(T(1)=1,(T(2)=2,(T(n)=T(ni1)+T(ni2)+T(ni3)

Such a definition is called recursive.

29

30

Recursive'Func+ons'

Recursive'Func+ons'

Example:

How can we recursively define the factorial function


f(n) = n! ?

f(0) = 3
f(n + 1) = 2f(n) + 3

f(0) = 1
f(n + 1) = (n + 1)f(n)

f(0) = 3
f(1) = 2f(0) + 3 = 23 + 3 = 9
f(2) = 2f(1) + 3 = 29 + 3 = 21
f(3) = 2f(2) + 3 = 221 + 3 = 45
f(4) = 2f(3) + 3 = 245 + 3 = 93

31

f(0) = 1
f(1) = 1f(0) = 11 = 1
f(2) = 2f(1) = 21 = 2
f(3) = 3f(2) = 32 = 6
f(4) = 4f(3) = 46 = 24

32

Recursive Function
Iterative version of factorial function

Function does NOT


calls itself

34

33

BST'

BST'

Binary(Tree(

(Node({(

Every(node(has(0,(1,(2(children(

(Key;(
(Value;(
(Node*(lew;(
(Node*(right;(

Full(Binary(Tree(
Every(node(other(than(leaves(has(2(children(
All(leave(nodes(have(same(path(length(
Also(called(proper(binary(tree,(strictly(binary(tree(

}(
(

Complete(Binary(Tree(
Every(level(above(the(last(level(is(completely(lled(
Nodes(in(the(last(level(are(as(far(lew(as(possible(

all(keys(in(lew(subtree(<(current(key(<(all(keys(in(right(subtree((

Binary(Search(Tree((
Ordered(binary(tree(

35

36

BST'

BST'
Average'Case'

Worst'Case'

Search(

Average'Case'

Worst'Case'
Search(

O(log(n)(

O(n)(

Insert(

Insert(

O(log(n)(

O(n)(

Delete(

Delete(

O(log(n)(

O(n)(

37

Inser+ng'a'node'to'BST'

38

Inser+ng'a'node'to'BST'

void insert(node* &root, key, value)!


{!
!if (root == NULL) {!
! !root = new node(key,value);!
!}!
!else if (key < root->key) {!
! !insert(root->left,key,value);!
!}!
!else {!
! !insert(root->right,key,value);!
!}!
}"

void insert(node* &root, key, value)!


{!
!if (root == NULL) {!
! !root = new node(key,value);!
!}!
!else if (key < root->key) {!
! !insert(root->left,key,value);!
!}!
!else {!
! !insert(root->right,key,value);!
!}!
}"

"

"
!
!!

Can!you!write!it!as!a!non.recursion?!!
!!
39

40

Inser+ng'a'node'to'BST'
void insert(node* &root, key, value)!
{!
!if (root == NULL) {!
! !root = new node(key, value);!
! !return;!
!}!
!node* cur = root;!
!while (true) {!
! !if (key < cur->key) {!
! !
!if (cur->left == NULL) {!
! !
!
!cur->left = new node(key, value);!
! !
!
!break;!
! !
!}!
! !
!else!
! !
!
!cur = cur->left;!
! !}!
! !else {!
! !
!if (cur->right == NULL) {!
! !
!
!cur->right = new node(key, value);!
! !
!
!break;!
! !
!}!
! !
!else!
! !
!
!cur = cur->right;!
! !}!
!}!
}!

Delete'a'node'from'BST'
Dele+ng'a'leaf'(node'with'no'children):(Dele%ng(
a(leaf(is(easy,(as(we(can(simply(remove(it(from(
the(tree.(
Dele+ng'a'node'with'one'child:(Remove(the(
node(and(replace(it(with(its(child.(
Dele+ng'a'node'with'two'children:(Call(the(node(
to(be(deleted(N.(Do(not(delete(N.(Instead,(choose(
either(its(iniorder(successor(node(or(its(
predecessor(node,(R.(Replace(the(value(of(N(with(
the(value(of(R(and(then(delete(R.((Why(R(cannot(
have(more(than(2(children?)(
41

42

You might also like