yeah - recursion to the rescue!yeah - recursion to the rescue! anton apostolatos menger sponge....

61
YEAH - Recursion to the Rescue! Anton Apostolatos Menger Sponge

Upload: others

Post on 10-Oct-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: YEAH - Recursion to the Rescue!YEAH - Recursion to the Rescue! Anton Apostolatos Menger Sponge. Doctors Without Orders A4: Recursion to the Rescue! Disaster Preparations DNA Detective

YEAH - Recursion to the Rescue!Anton Apostolatos

Menger Sponge

Page 2: YEAH - Recursion to the Rescue!YEAH - Recursion to the Rescue! Anton Apostolatos Menger Sponge. Doctors Without Orders A4: Recursion to the Rescue! Disaster Preparations DNA Detective
Page 3: YEAH - Recursion to the Rescue!YEAH - Recursion to the Rescue! Anton Apostolatos Menger Sponge. Doctors Without Orders A4: Recursion to the Rescue! Disaster Preparations DNA Detective
Page 4: YEAH - Recursion to the Rescue!YEAH - Recursion to the Rescue! Anton Apostolatos Menger Sponge. Doctors Without Orders A4: Recursion to the Rescue! Disaster Preparations DNA Detective

Doctors Without Orders

A4: Recursion to the Rescue!

Disaster Preparations DNA Detective Winning the Presidency

Page 5: YEAH - Recursion to the Rescue!YEAH - Recursion to the Rescue! Anton Apostolatos Menger Sponge. Doctors Without Orders A4: Recursion to the Rescue! Disaster Preparations DNA Detective

Recursive Backtracking

Page 6: YEAH - Recursion to the Rescue!YEAH - Recursion to the Rescue! Anton Apostolatos Menger Sponge. Doctors Without Orders A4: Recursion to the Rescue! Disaster Preparations DNA Detective

Outline before you write!

Page 7: YEAH - Recursion to the Rescue!YEAH - Recursion to the Rescue! Anton Apostolatos Menger Sponge. Doctors Without Orders A4: Recursion to the Rescue! Disaster Preparations DNA Detective

Doctors Without Orders

Page 8: YEAH - Recursion to the Rescue!YEAH - Recursion to the Rescue! Anton Apostolatos Menger Sponge. Doctors Without Orders A4: Recursion to the Rescue! Disaster Preparations DNA Detective

Doctors

Patients

Page 9: YEAH - Recursion to the Rescue!YEAH - Recursion to the Rescue! Anton Apostolatos Menger Sponge. Doctors Without Orders A4: Recursion to the Rescue! Disaster Preparations DNA Detective
Page 10: YEAH - Recursion to the Rescue!YEAH - Recursion to the Rescue! Anton Apostolatos Menger Sponge. Doctors Without Orders A4: Recursion to the Rescue! Disaster Preparations DNA Detective

struct Doctor { string name; int hoursFree;};

struct Patient { string name; int hoursNeeded;};

bool canAllPatientsBeSeen(Vector<Doctor> doctors,Vector<Patient> patients,Map<string, Set<string>>& schedule)

If so, populate this with schedule

Return whether it’s possible for all patients to be attended

Page 11: YEAH - Recursion to the Rescue!YEAH - Recursion to the Rescue! Anton Apostolatos Menger Sponge. Doctors Without Orders A4: Recursion to the Rescue! Disaster Preparations DNA Detective

Tips and Tricks- Think about what decisions you have at every step (what you’re exploring)

and what the base case could be

- Before writing any code, go through simple toy examples by hand to make sure your proposed solution’s logic is sound

- If your function returns false, the final contents of schedule don’t matter

- You can assume no two doctors or patients have the same name

- Start by only worrying about getting the return value right; then work on populating schedule

Page 12: YEAH - Recursion to the Rescue!YEAH - Recursion to the Rescue! Anton Apostolatos Menger Sponge. Doctors Without Orders A4: Recursion to the Rescue! Disaster Preparations DNA Detective

Questions?

Page 13: YEAH - Recursion to the Rescue!YEAH - Recursion to the Rescue! Anton Apostolatos Menger Sponge. Doctors Without Orders A4: Recursion to the Rescue! Disaster Preparations DNA Detective

Disaster Preparations

Page 14: YEAH - Recursion to the Rescue!YEAH - Recursion to the Rescue! Anton Apostolatos Menger Sponge. Doctors Without Orders A4: Recursion to the Rescue! Disaster Preparations DNA Detective
Page 15: YEAH - Recursion to the Rescue!YEAH - Recursion to the Rescue! Anton Apostolatos Menger Sponge. Doctors Without Orders A4: Recursion to the Rescue! Disaster Preparations DNA Detective
Page 16: YEAH - Recursion to the Rescue!YEAH - Recursion to the Rescue! Anton Apostolatos Menger Sponge. Doctors Without Orders A4: Recursion to the Rescue! Disaster Preparations DNA Detective

1 city?

Page 17: YEAH - Recursion to the Rescue!YEAH - Recursion to the Rescue! Anton Apostolatos Menger Sponge. Doctors Without Orders A4: Recursion to the Rescue! Disaster Preparations DNA Detective
Page 18: YEAH - Recursion to the Rescue!YEAH - Recursion to the Rescue! Anton Apostolatos Menger Sponge. Doctors Without Orders A4: Recursion to the Rescue! Disaster Preparations DNA Detective

3 cities?

Page 19: YEAH - Recursion to the Rescue!YEAH - Recursion to the Rescue! Anton Apostolatos Menger Sponge. Doctors Without Orders A4: Recursion to the Rescue! Disaster Preparations DNA Detective

Nope :(

Page 20: YEAH - Recursion to the Rescue!YEAH - Recursion to the Rescue! Anton Apostolatos Menger Sponge. Doctors Without Orders A4: Recursion to the Rescue! Disaster Preparations DNA Detective

4 cities?

Page 21: YEAH - Recursion to the Rescue!YEAH - Recursion to the Rescue! Anton Apostolatos Menger Sponge. Doctors Without Orders A4: Recursion to the Rescue! Disaster Preparations DNA Detective

Yes!

Page 22: YEAH - Recursion to the Rescue!YEAH - Recursion to the Rescue! Anton Apostolatos Menger Sponge. Doctors Without Orders A4: Recursion to the Rescue! Disaster Preparations DNA Detective

bool canBeMadeDisasterReady(Map<string, Set<string>> roadNetwork,int numCities,Set<string>& locations)

Maximum number of cities to stockpile

If possible, fill with all locations we want to stockpile

Page 23: YEAH - Recursion to the Rescue!YEAH - Recursion to the Rescue! Anton Apostolatos Menger Sponge. Doctors Without Orders A4: Recursion to the Rescue! Disaster Preparations DNA Detective

There are different ways of thinking of the problem

Page 24: YEAH - Recursion to the Rescue!YEAH - Recursion to the Rescue! Anton Apostolatos Menger Sponge. Doctors Without Orders A4: Recursion to the Rescue! Disaster Preparations DNA Detective

Option 1: Enumerate all possibilities

Page 25: YEAH - Recursion to the Rescue!YEAH - Recursion to the Rescue! Anton Apostolatos Menger Sponge. Doctors Without Orders A4: Recursion to the Rescue! Disaster Preparations DNA Detective
Page 26: YEAH - Recursion to the Rescue!YEAH - Recursion to the Rescue! Anton Apostolatos Menger Sponge. Doctors Without Orders A4: Recursion to the Rescue! Disaster Preparations DNA Detective
Page 27: YEAH - Recursion to the Rescue!YEAH - Recursion to the Rescue! Anton Apostolatos Menger Sponge. Doctors Without Orders A4: Recursion to the Rescue! Disaster Preparations DNA Detective

A B

C D

X

Option 2: Choose by city cover

Page 28: YEAH - Recursion to the Rescue!YEAH - Recursion to the Rescue! Anton Apostolatos Menger Sponge. Doctors Without Orders A4: Recursion to the Rescue! Disaster Preparations DNA Detective

A B

C D

X

Choose to cover X

Option 2: Choose by city cover

Page 29: YEAH - Recursion to the Rescue!YEAH - Recursion to the Rescue! Anton Apostolatos Menger Sponge. Doctors Without Orders A4: Recursion to the Rescue! Disaster Preparations DNA Detective

A B

C D

X

Option 2: Choose by city cover

Choose to cover XPick A?

Page 30: YEAH - Recursion to the Rescue!YEAH - Recursion to the Rescue! Anton Apostolatos Menger Sponge. Doctors Without Orders A4: Recursion to the Rescue! Disaster Preparations DNA Detective

B

C D

Option 2: Choose by city cover

Choose to cover XPick A?

Explore resulting graph

Page 31: YEAH - Recursion to the Rescue!YEAH - Recursion to the Rescue! Anton Apostolatos Menger Sponge. Doctors Without Orders A4: Recursion to the Rescue! Disaster Preparations DNA Detective

A B

C D

X

Option 2: Choose by city cover

Choose to cover XPick B?

Page 32: YEAH - Recursion to the Rescue!YEAH - Recursion to the Rescue! Anton Apostolatos Menger Sponge. Doctors Without Orders A4: Recursion to the Rescue! Disaster Preparations DNA Detective

A

C D

Option 2: Choose by city cover

Choose to cover XPick B?

Explore resulting graph

Page 33: YEAH - Recursion to the Rescue!YEAH - Recursion to the Rescue! Anton Apostolatos Menger Sponge. Doctors Without Orders A4: Recursion to the Rescue! Disaster Preparations DNA Detective

A B

C D

X

Option 2: Choose by city cover

Choose to cover XPick X?

Page 34: YEAH - Recursion to the Rescue!YEAH - Recursion to the Rescue! Anton Apostolatos Menger Sponge. Doctors Without Orders A4: Recursion to the Rescue! Disaster Preparations DNA Detective

Option 2: Choose by city cover

Choose to cover XPick X?

Explore resulting graph

Page 35: YEAH - Recursion to the Rescue!YEAH - Recursion to the Rescue! Anton Apostolatos Menger Sponge. Doctors Without Orders A4: Recursion to the Rescue! Disaster Preparations DNA Detective

Option 2: Choose by city cover

Choose to cover XPick X?

Explore resulting graph

Page 36: YEAH - Recursion to the Rescue!YEAH - Recursion to the Rescue! Anton Apostolatos Menger Sponge. Doctors Without Orders A4: Recursion to the Rescue! Disaster Preparations DNA Detective

Tips and Tricks- The road is bidirectional (if A → B then B → A)

- Every city appears as a key in the map

- It’s fine if you find a way to solve using fewer cities!

- Some of the test files have a lot of cities; your code may take up to two minutes to complete

Page 37: YEAH - Recursion to the Rescue!YEAH - Recursion to the Rescue! Anton Apostolatos Menger Sponge. Doctors Without Orders A4: Recursion to the Rescue! Disaster Preparations DNA Detective

Questions?

Page 38: YEAH - Recursion to the Rescue!YEAH - Recursion to the Rescue! Anton Apostolatos Menger Sponge. Doctors Without Orders A4: Recursion to the Rescue! Disaster Preparations DNA Detective

DNA Detective

Page 39: YEAH - Recursion to the Rescue!YEAH - Recursion to the Rescue! Anton Apostolatos Menger Sponge. Doctors Without Orders A4: Recursion to the Rescue! Disaster Preparations DNA Detective
Page 40: YEAH - Recursion to the Rescue!YEAH - Recursion to the Rescue! Anton Apostolatos Menger Sponge. Doctors Without Orders A4: Recursion to the Rescue! Disaster Preparations DNA Detective
Page 41: YEAH - Recursion to the Rescue!YEAH - Recursion to the Rescue! Anton Apostolatos Menger Sponge. Doctors Without Orders A4: Recursion to the Rescue! Disaster Preparations DNA Detective

Edit Distance- Minimum number of operations that need to be performed to one string into

another string.

- Operations:- Insert a character into one of the strings- Delete a character from one of the strings- Replace a character in one of the strings

Page 42: YEAH - Recursion to the Rescue!YEAH - Recursion to the Rescue! Anton Apostolatos Menger Sponge. Doctors Without Orders A4: Recursion to the Rescue! Disaster Preparations DNA Detective

bool approximatelyMatch(const string& one,const string& two,int maxDistance)

Second string

Maximum number of edits allowed

First string

Page 43: YEAH - Recursion to the Rescue!YEAH - Recursion to the Rescue! Anton Apostolatos Menger Sponge. Doctors Without Orders A4: Recursion to the Rescue! Disaster Preparations DNA Detective

editDistance(table, maple) → 2

editDistance(rate, pirate) → 2

editDistance(cat, dog) → 3

Page 44: YEAH - Recursion to the Rescue!YEAH - Recursion to the Rescue! Anton Apostolatos Menger Sponge. Doctors Without Orders A4: Recursion to the Rescue! Disaster Preparations DNA Detective

Hint: Look at first char!

Page 45: YEAH - Recursion to the Rescue!YEAH - Recursion to the Rescue! Anton Apostolatos Menger Sponge. Doctors Without Orders A4: Recursion to the Rescue! Disaster Preparations DNA Detective
Page 46: YEAH - Recursion to the Rescue!YEAH - Recursion to the Rescue! Anton Apostolatos Menger Sponge. Doctors Without Orders A4: Recursion to the Rescue! Disaster Preparations DNA Detective
Page 47: YEAH - Recursion to the Rescue!YEAH - Recursion to the Rescue! Anton Apostolatos Menger Sponge. Doctors Without Orders A4: Recursion to the Rescue! Disaster Preparations DNA Detective

Remove A from start of both strings

Page 48: YEAH - Recursion to the Rescue!YEAH - Recursion to the Rescue! Anton Apostolatos Menger Sponge. Doctors Without Orders A4: Recursion to the Rescue! Disaster Preparations DNA Detective
Page 49: YEAH - Recursion to the Rescue!YEAH - Recursion to the Rescue! Anton Apostolatos Menger Sponge. Doctors Without Orders A4: Recursion to the Rescue! Disaster Preparations DNA Detective
Page 50: YEAH - Recursion to the Rescue!YEAH - Recursion to the Rescue! Anton Apostolatos Menger Sponge. Doctors Without Orders A4: Recursion to the Rescue! Disaster Preparations DNA Detective

Option 1: Delete the T from first string

Option 2: Delete the G from second string

Option 3: ...

Page 51: YEAH - Recursion to the Rescue!YEAH - Recursion to the Rescue! Anton Apostolatos Menger Sponge. Doctors Without Orders A4: Recursion to the Rescue! Disaster Preparations DNA Detective

Questions?

Page 52: YEAH - Recursion to the Rescue!YEAH - Recursion to the Rescue! Anton Apostolatos Menger Sponge. Doctors Without Orders A4: Recursion to the Rescue! Disaster Preparations DNA Detective

Winning the Presidency

Page 53: YEAH - Recursion to the Rescue!YEAH - Recursion to the Rescue! Anton Apostolatos Menger Sponge. Doctors Without Orders A4: Recursion to the Rescue! Disaster Preparations DNA Detective

US Presidency- Every state has a certain number of electors and a certain population- If you win majority of popular votes in a state, you get all electors- You need a majority of electoral votes to become president!

Page 54: YEAH - Recursion to the Rescue!YEAH - Recursion to the Rescue! Anton Apostolatos Menger Sponge. Doctors Without Orders A4: Recursion to the Rescue! Disaster Preparations DNA Detective
Page 55: YEAH - Recursion to the Rescue!YEAH - Recursion to the Rescue! Anton Apostolatos Menger Sponge. Doctors Without Orders A4: Recursion to the Rescue! Disaster Preparations DNA Detective

struct State { string name; int electoralVotes; int popularVotes; };

State michigan = {“Michigan”, 16, 9,910,000};

If 4,955,001 Michiganders vote for you, you gain 16

electoral votes!

If 4,955,000 Michiganders vote for you, you gain 0

electoral votes...

Page 56: YEAH - Recursion to the Rescue!YEAH - Recursion to the Rescue! Anton Apostolatos Menger Sponge. Doctors Without Orders A4: Recursion to the Rescue! Disaster Preparations DNA Detective

struct MinInfo { int popularVotesNeeded; Vector<State> statesUsed;};

MinInfo minPopularVoteToWin(Vector<State> states)

Question: What’s the fewest number of votes needed to be elected president?

Page 57: YEAH - Recursion to the Rescue!YEAH - Recursion to the Rescue! Anton Apostolatos Menger Sponge. Doctors Without Orders A4: Recursion to the Rescue! Disaster Preparations DNA Detective

Demo!

Page 58: YEAH - Recursion to the Rescue!YEAH - Recursion to the Rescue! Anton Apostolatos Menger Sponge. Doctors Without Orders A4: Recursion to the Rescue! Disaster Preparations DNA Detective

There are different ways of thinking of the problem

Page 59: YEAH - Recursion to the Rescue!YEAH - Recursion to the Rescue! Anton Apostolatos Menger Sponge. Doctors Without Orders A4: Recursion to the Rescue! Disaster Preparations DNA Detective

Think about this problem instead:

What is the minimum number of popular votes needed to get at least V electoral votes, using only states from index i

and greater in the Vector of states?

If you can solve this problem, you solve the original problem!

What if it’s impossible? (e.g. one state left, and 75 electoral votes needed)

Use MAX_INT sentinel value!

Page 60: YEAH - Recursion to the Rescue!YEAH - Recursion to the Rescue! Anton Apostolatos Menger Sponge. Doctors Without Orders A4: Recursion to the Rescue! Disaster Preparations DNA Detective

Memoization is required! (or else your code will never finish…)

Page 61: YEAH - Recursion to the Rescue!YEAH - Recursion to the Rescue! Anton Apostolatos Menger Sponge. Doctors Without Orders A4: Recursion to the Rescue! Disaster Preparations DNA Detective

Questions?