Computational Processes

Slides:



Advertisements
Similar presentations
Tutorial 5 of CSCI2110 Eulerian Path & Hamiltonian Cycle Tutor: Zhou Hong ( 周宏 )
Advertisements

Chapter Three: Closure Properties for Regular Languages
Mathematical Induction II Lecture 14: Nov
Tirgul 8 Graph algorithms: Strongly connected components.
1 Discrete Structures & Algorithms Graphs and Trees: IV EECE 320.
CSE115/ENGR160 Discrete Mathematics 04/05/11 Ming-Hsuan Yang UC Merced 1.
3 Gallon Jug5 Gallon Jug Greatest Common Divisor Lecture 8: Sep 30.
1 Bipartite Matching Polytope, Stable Matching Polytope x1 x2 x3 Lecture 10: Feb 15.
Proofs, Recursion, and Analysis of Algorithms Mathematical Structures for Computer Science Chapter 2 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesProofs,
Great Theoretical Ideas in Computer Science.
Induction and recursion
Great Theoretical Ideas in Computer Science.
Reading and Writing Mathematical Proofs
Chapter 3 (Part 3): Mathematical Reasoning, Induction & Recursion  Recursive Algorithms (3.5)  Program Correctness (3.6)
Greatest Common Divisor
Hopefully this lesson will give you an inception of what recursion is.
The Mathematics Of 1950’s Dating: Who wins the battle of the sexes? Adapted from a presentation by Stephen Rudich.
The Mathematics Of 1950’s Dating: Who wins the battle of the sexes? Presentation by Shuchi Chawla with some modifications.
The Integers. The Division Algorithms A high-school question: Compute 58/17. We can write 58 as 58 = 3 (17) + 7 This forms illustrates the answer: “3.
Great Theoretical Ideas in Computer Science.
Stable Matching Lecture 7: Oct 3. Matching A B C DE Boys Girls Today’s goal: to “match” the boys and the girls in a “good” way.
Stable Marriages (Lecture modified from Carnegie Mellon University course Great Theoretical Ideas in Computer Science)
MA/CSSE 473 Day 06 Euclid's Algorithm. MA/CSSE 473 Day 06 Student Questions Odd Pie Fight Euclid's algorithm (if there is time) extended Euclid's algorithm.
Foundations of Discrete Mathematics Chapter 4 By Dr. Dalia M. Gil, Ph.D.
4/3/2003CSE More Math CSE Algorithms Euclidean Algorithm Divide and Conquer.
Great Theoretical Ideas in Computer Science for Some.
Matching Lecture 19: Nov 23.
Application: Algorithms Lecture 20 Section 3.8 Wed, Feb 21, 2007.
Properties of the gcd Theorem: For any two integers a,b there exist integers x,y such that xa + yb = gcd(a,b). A proof will not be given at this point.
AF2. Turn off your phones Primes, gcd, some examples, reading.
CompSci 102 Discrete Math for Computer Science March 13, 2012 Prof. Rodger Slides modified from Rosen.
18.1 CompSci 102 Today’s topics 1950s TV Dating1950s TV Dating.
Application: Algorithms Lecture 19 Section 3.8 Tue, Feb 20, 2007.
AF2. Turn off your phones Primes, gcd, some examples, reading.
Algorithms used by CDNs Stable Marriage Algorithm Consistent Hashing.
Dating Advice from Mathematics
CSE 20: Discrete Mathematics for Computer Science Prof. Shachar Lovett.
If more pigeons than pigeonholes, Pigeonhole Principle.
Verifiable Programming Reason about imperative sequential programs such as Java Imperative program –defines state space defined by collection of typed.
מתמטיקה בדידה Discrete Math Lecture 12 1 TexPoint fonts used in EMF.
Chapter 4 (Part 3): Mathematical Reasoning, Induction & Recursion
Matching Boys Girls A B C D E
MA/CSSE 473 Day 06 Euclid's Algorithm.
MA/CSSE 473 Day 06 Euclid's Algorithm.
The Mathematics Of 1950’s Dating: Who wins the battle of the sexes?
Mathematical Induction II
Greatest Common Divisor
GCD and Optimization Problem
Number Theory and Modular Arithmetic
CSE 20: Discrete Mathematics for Computer Science Prof. Shachar Lovett
The Mathematics Of 1950’s Dating: Who wins The Battle of The Sexes?
Computational Processes
Foundations of Discrete Mathematics
The Mathematics Of 1950’s Dating: Who wins the battle of the sexes?
Asymptotics & Stirling’s Approximation
Computational Processes II
and Structural Induction II
and Structural Induction
and Structural Induction II
and Structural Induction
and Structural Induction
Computational Processes II
Asymptotics & Stirling’s Approximation
Mathematics for Computer Science MIT 6.042J/18.062J
Asymptotics & Stirling’s Approximation
Application: Algorithms
Application: Algorithms
Derived Variables A derived variable v is simply a function
and Structural Induction
Useful GCD Fact If a and b are positive integers, then gcd(a,b) = gcd(b, a mod b) Proof: By definition of mod, a = qb+ (a mod b) for.
Presentation transcript:

Computational Processes Mathematics for Computer Science MIT 6.042J/18.062J Computational Processes Copyright © Albert Meyer, 2002. Prof. Albert Meyer & Dr. Radhika Nagpal

Die Hard Picture source: http://movieweb.com/movie/diehard3/

Die Hard Supplies: 3 Gallon Jug 5 Gallon Jug Water

Die Hard Transferring water: 3 Gallon Jug 5 Gallon Jug

Die Hard Transferring water: 3 Gallon Jug 5 Gallon Jug

Psychopath’s challenge: Disarm bomb by putting 4 gallons Die Hard Psychopath’s challenge: Disarm bomb by putting 4 gallons of water on top, or it will blow up. Question: How to do it?

Die Hard Work it out now!

How to do it Start with empty jugs: (0,0) 3 Gallon Jug 5 Gallon Jug

How to do it Fill the big jug: (0,5) 3 Gallon Jug 5 Gallon Jug

Pour from big to little: How to do it Pour from big to little: (3,2) 3 Gallon Jug 5 Gallon Jug

How to do it Empty the little: (0,2) 3 Gallon Jug 5 Gallon Jug

Pour from big to little: How to do it Pour from big to little: (2,0) 3 Gallon Jug 5 Gallon Jug

How to do it Fill the big jug: (2,5) 3 Gallon Jug 5 Gallon Jug

Done!! Pour from big to little: How to do it (3,4) 3 Gallon Jug

Die Hard once and for all What if you have a 9 gallon jug instead? 3 Gallon Jug 5 Gallon Jug 9 Gallon Jug

Die Hard once and for all What if you have a 9 gallon jug instead? 3 Gallon Jug 9 Gallon Jug Can you do it? Can you prove it?

Die Hard Work it out now!

State machine: Step by step procedure, possibly responding to input.

The state graph of a 99-bounded counter: State machines The state graph of a 99-bounded counter: start state overflow 1 2 99

States: {0,1,…,99, overflow} State machines The state graph of a 99-bounded counter: start state 1 2 99 overflow States: {0,1,…,99, overflow}

States: {0,1,…,99, overflow} Transitions: State machines The state graph of a 99-bounded counter: start state 1 2 99 overflow States: {0,1,…,99, overflow} Transitions: 0  i < 99 i i+1 99 overflow

States: {0,1,…,99, overflow} Transitions: State machines The state graph of a 99-bounded counter: start state 1 2 99 overflow States: {0,1,…,99, overflow} Transitions: overflow overflow

Die hard state machine State machines State = amount of water in the jug: (b,l) where 0  b  3 and 0  l  5. Start state = (0,0)

State machines Die Hard Transitions: Fill the little jug: Fill the big jug: Empty the little jug: Empty the big jug:

5. Pour from little jug into big jug: If no overflow, then State machines 5. Pour from little jug into big jug: If no overflow, then

5. Pour from little jug into big jug: If no overflow, then b + l  5 State machines 5. Pour from little jug into big jug: If no overflow, then b + l  5

(b,l)  5. Pour from little jug into big jug: (for l >0) State machines 5. Pour from little jug into big jug: (for l >0) If no overflow, then …, (b,l)  otherwise.

5. Pour from little jug into big jug: (for l >0) State machines 5. Pour from little jug into big jug: (for l >0) 6. Pour from big jug into little jug. Likewise.

Invariant: State Invariants Die hard once and for all P(state) = 3 divides number of gallons in each jug.

Invariant: State Invariants Die hard once and for all P(state) = 3 divides number of gallons in each jug.

State Invariants Proof method (just like induction): Show base case: P(start). Invariant case: if P(q) and , then P(r). Conclusion: P holds for all reachable states, including final state (if any). q r

The Robot The robot is on a grid. y 2 1 x 0 1 2 3

The robot can move diagonally. y 2 1 x 0 1 2 3

Can it reach from (0,0) to (1,0)? y The Robot Can it reach from (0,0) to (1,0)? y ? 2 1 GOAL x 0 1 2 3

Robot Invariant NO! P((x,y)) ::= x+y is even

Robot Invariant NO! P((x,y)) ::= x+y is even P((0,0)) is true.

NO! P((x,y)) ::= x+y is even P((0,0)) is true. Transition adds ±1 to Robot Invariant NO! P((x,y)) ::= x+y is even P((0,0)) is true. Transition adds ±1 to both x and y

So all positions (x,y) reachable by robot Robot Invariant So all positions (x,y) reachable by robot have x + y even, but 1 + 0 = 1 is odd. Therefore (1,0) is not reachable.

GCD correctness Computing GCD(a,b) using the Euclidean Algorithm: Let x=a, y=b. If y=0, return x & terminate. Else set (x:=y, y:=x mod y) simultaneously; Repeat process.

GCD correctness Example: GCD(414,662)

GCD correctness Example: GCD(414,662) = GCD(662,414) since 414 (mod 662) = 414

GCD correctness Example: GCD(414,662) = GCD(662,414) since 414 mod 662 = 414 = GCD(414,248) since 662 mod 414 = 248

GCD correctness Example: GCD(414,662) = GCD(662,414) since 414 mod 662 = 414 = GCD(414,248) since 662 mod 414 = 248 = GCD(248,166) since 414 mod 248 = 166

GCD correctness Example: GCD(414,662) = GCD(662,414) since 414 mod 662 = 414 = GCD(414,248) since 662 mod 414 = 248 = GCD(248,166) since 414 mod 248 = 166 = GCD(166,82) since 248 mod 166 = 82

GCD correctness Example: GCD(414,662) = GCD(662,414) since 414 mod 662 = 414 = GCD(414,248) since 662 mod 414 = 248 = GCD(248,166) since 414 mod 248 = 166 = GCD(166,82) since 248 mod 166 = 82 = GCD(82,2) since 166 mod 82 = 2

GCD correctness Example: GCD(414,662) = GCD(662,414) since 414 mod 662 = 414 = GCD(414,248) since 662 mod 414 = 248 = GCD(248,166) since 414 mod 248 = 166 = GCD(166,82) since 248 mod 166 = 82 = GCD(82,2) since 166 mod 82 = 2 = GCD(2,0) since 82 mod 2 = 0

GCD correctness Example: GCD(414,662) = GCD(662,414) since 414 mod 662 = 414 = GCD(414,248) since 662 mod 414 = 248 = GCD(248,166) since 414 mod 248 = 166 = GCD(166,82) since 248 mod 166 = 82 = GCD(82,2) since 166 mod 82 = 2 = GCD(2,0) since 82 mod 2 = 0 Return 2.

P((x,y)) ::= [gcd(a,b) = gcd(x,y)]. GCD correctness The Invariant is P((x,y)) ::= [gcd(a,b) = gcd(x,y)].

P((x,y)) ::= [gcd(a,b) = gcd(x,y)]. GCD correctness The Invariant is P((x,y)) ::= [gcd(a,b) = gcd(x,y)]. P(start start): at start x = a , y =b.

Transitions: (x,y)  (y, rem(x,y)) GCD correctness Transitions: (x,y)  (y, rem(x,y))

Transitions: (x,y)  (y, rem(x,y)) GCD correctness Transitions: (x,y)  (y, rem(x,y)) Invariant holds by Lemma: gcd(x,y) = gcd(y,rem(x,y)), for y  0.

Conclusion: on termination GCD correctness Conclusion: on termination x equals gcd(a,b).

Conclusion: on termination GCD correctness Conclusion: on termination x equals gcd(a,b). Because terminate when y = 0, so x = gcd(x,0) = gcd(x,y) = gcd(a,b).

Class Problems

Robert Floyd Picture source: http://www.stanford.edu/dept/news/report/news/november7/floydobit-117.html

A derived variable v is simply a function Derived variables A derived variable v is simply a function that associate value with state. Die Hard Example: total amount of water in the two jugs

For GCD, already have varables Derived variables For GCD, already have varables x, y.

Proof of GCD termination: Derived variables Proof of GCD termination: Show that variable y is strictly decreasing and natural number valued.

Termination follows by least number principle: Derived variables Termination follows by least number principle: y must take a least value, and thus the algorithm is stuck.

Strictly Decreasing Variable 16 12 8 4 State

Weakly Decreasing Variable 16 12 8 4 State

Weakly Decreasing Variable We used to call weakly decreasing variables “nonincreasing” variables. OK terminology but remember: nondecreasing is NOT EQUAL to “not decreasing.”

There are 5 boys and 5 girls Stable Marriage There are 5 boys and 5 girls 2 3 4 5 A B D E

Preferences: Boys Girls 1: CBEAD A: 35214 2 : ABECD B : 52143 Stable marriage Preferences: Boys Girls 1: CBEAD A: 35214 2 : ABECD B : 52143 3 : DCBAE C : 43512 4 : ACDBE D : 12345 5 : ABDEC E : 23415

Stable marriage Boys 1: CBEAD 2 : ABECD 3 : DCBAE 4 : ACDBE 5 : ABDEC

Boys 1: CBEAD 2 : ABECD 3 : DCBAE 4 : ACDBE 5 : ABDEC Marry 1 with C: Stable marriage Boys 1: CBEAD 2 : ABECD 3 : DCBAE 4 : ACDBE 5 : ABDEC Marry 1 with C: C 1

Only 4 boys and 4 girls left. Boys 2 : ABED 3 : DBAE 4 : ADBE 5 : ABDE Stable marriage Only 4 boys and 4 girls left. Boys 2 : ABED 3 : DBAE 4 : ADBE 5 : ABDE 2 3 4 5 A B D E

A problem: Rogue couples Stable marriage A problem: Rogue couples 1 C 4 B Girl C likes like boy 4 better Boy 4 like girl C better

The mating algorithm – day by day Stable marriage The mating algorithm – day by day

The mating algorithm – day by day Stable marriage The mating algorithm – day by day Morning: boys serenade favorite girls

The mating algorithm – day by day Stable marriage The mating algorithm – day by day Morning: boys serenade favorite girls Afternoon: girl rejects all but best Serenader

The mating algorithm – day by day Stable marriage The mating algorithm – day by day Morning: boys serenade favorite girls Afternoon: girl rejects all but best Serenader Evening: rejected boys cross off girls

Termination condition: stop when no rejection is possible. Stable marriage Termination condition: stop when no rejection is possible. The algorithm terminates. Everybody ends up married. The resulting marriages are stable.

Infinitely Wide Trees … …

Infinitely Wide Trees … …

Infinitely Wide Trees

Infinitely Wide Trees

Tutorial Problem 1