week 12 - wednesday. what did we talk about last time? matching stable marriage started euler...

43
CS221 Week 12 - Wednesday

Upload: jerome-griffin

Post on 17-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Week 12 - Wednesday.  What did we talk about last time?  Matching  Stable marriage  Started Euler paths

CS221Week 12 - Wednesday

Page 2: Week 12 - Wednesday.  What did we talk about last time?  Matching  Stable marriage  Started Euler paths

Last time

What did we talk about last time? Matching Stable marriage Started Euler paths

Page 3: Week 12 - Wednesday.  What did we talk about last time?  Matching  Stable marriage  Started Euler paths

Questions?

Page 4: Week 12 - Wednesday.  What did we talk about last time?  Matching  Stable marriage  Started Euler paths

Project 4

Page 5: Week 12 - Wednesday.  What did we talk about last time?  Matching  Stable marriage  Started Euler paths

Assignment 6

Page 6: Week 12 - Wednesday.  What did we talk about last time?  Matching  Stable marriage  Started Euler paths

Euler Paths and Tours

Page 7: Week 12 - Wednesday.  What did we talk about last time?  Matching  Stable marriage  Started Euler paths

Graph theoretical view

By simplifying the problem into a graph, the important features are clear

To arrive as many times as you leave, the degrees of each node must be even (except for the starting and ending points)

Center Island

North Shore

East Island

South Shore

Page 8: Week 12 - Wednesday.  What did we talk about last time?  Matching  Stable marriage  Started Euler paths

Eulerian Path Algorithm

There is actually an way to find such a path on a graph where one exists: Start with a node of odd degree if there

is one Every time we move across an edge,

delete it If you have a choice, always pick an

edge whose deletion will not disconnect the graph

At the end of the algorithm, you will either have an Eulerian path or an Eulerian cycle, depending on the graph

Page 9: Week 12 - Wednesday.  What did we talk about last time?  Matching  Stable marriage  Started Euler paths

The Two Princes and the Bishop We can label the nodes to make a more

interesting problem Now each piece of land is associated with the

Blue Prince, the Red Prince, the Bishop, or the Tavern

Page 10: Week 12 - Wednesday.  What did we talk about last time?  Matching  Stable marriage  Started Euler paths

The Blue Prince: 8th Bridge

The Blue Prince wants to build an 8th bridge so that he can walk starting at his castle, cross every bridge once, and end at the Tavern to brag

Page 11: Week 12 - Wednesday.  What did we talk about last time?  Matching  Stable marriage  Started Euler paths

The Blue Prince: Solution

Put the bridge from the Bishop’s land to the Red Prince’s land

Page 12: Week 12 - Wednesday.  What did we talk about last time?  Matching  Stable marriage  Started Euler paths

The Red Prince: 9th Bridge

Furious, the Red Prince wants to build his own bridge so that only he can start at his own castle, cross all the bridges once and then end at the Tavern

Page 13: Week 12 - Wednesday.  What did we talk about last time?  Matching  Stable marriage  Started Euler paths

The Red Prince: Solution

Put the bridge from the Red Prince’s land to the Blue Prince’s land

Page 14: Week 12 - Wednesday.  What did we talk about last time?  Matching  Stable marriage  Started Euler paths

The Bishop: 10th Bridge

Upset by this pettiness, the Bishop decides to build a 10th bridge which allows all citizens to cross all bridges and return to their starting point

Page 15: Week 12 - Wednesday.  What did we talk about last time?  Matching  Stable marriage  Started Euler paths

The Bishop: Solution

Put the bridge from the Center Island to the Red Prince’s land, making all pieces of land have even degree

Page 16: Week 12 - Wednesday.  What did we talk about last time?  Matching  Stable marriage  Started Euler paths

Euler Practice

Page 17: Week 12 - Wednesday.  What did we talk about last time?  Matching  Stable marriage  Started Euler paths

Is there an Euler tour?

87

9 5

6

0

3

4

2

1

Page 18: Week 12 - Wednesday.  What did we talk about last time?  Matching  Stable marriage  Started Euler paths

What about here?

1

5 2

4 3

Page 19: Week 12 - Wednesday.  What did we talk about last time?  Matching  Stable marriage  Started Euler paths

B-treesStudent Lecture

Page 20: Week 12 - Wednesday.  What did we talk about last time?  Matching  Stable marriage  Started Euler paths

B-trees

Page 21: Week 12 - Wednesday.  What did we talk about last time?  Matching  Stable marriage  Started Euler paths

Multiway trees

Binary trees are great However, only two splits means that

you have a height of log2 n when you want to store n things If n = 1,000,000, log2 n = 20

What if depth was expensive? Could we have say, 10 splits? If n = 1,000,000, log10 n = 6

Page 22: Week 12 - Wednesday.  What did we talk about last time?  Matching  Stable marriage  Started Euler paths

When would we need such a thing?

Answer: When the tree is in secondary storage

Each read of a block from disk storage is slow We want to get a whole node at once Each node will give us information about

lots of child nodes We don’t have to make many decisions

to get to the node we want

Page 23: Week 12 - Wednesday.  What did we talk about last time?  Matching  Stable marriage  Started Euler paths

B-tree definition

A B-tree of order m has the following properties:1. The root has at least two subtrees

unless it is a leaf2. Each nonroot and each nonleaf node

holds k keys and k + 1 pointers to subtrees where m/2 ≤ k ≤ m

3. Each leaf node holds k keys where m/2 ≤ k ≤ m

4. All leaves are on the same level

Page 24: Week 12 - Wednesday.  What did we talk about last time?  Matching  Stable marriage  Started Euler paths

B-tree of order 4

50

10

15

20

70

80

6 811

12

16

18

21

25

27

29

54

56

71

76

81

89

Page 25: Week 12 - Wednesday.  What did we talk about last time?  Matching  Stable marriage  Started Euler paths

Easy add

12

5 813

15

Insert 7 1

2

5 7 813

15

Page 26: Week 12 - Wednesday.  What did we talk about last time?  Matching  Stable marriage  Started Euler paths

Tougher add

12

2 5 7 813

15

Insert 6 6

12

2 513

15

7 8

Page 27: Week 12 - Wednesday.  What did we talk about last time?  Matching  Stable marriage  Started Euler paths

B-tree practice

Insert the following numbers: 86 69 81 15 100 94 8 27 56 68 92 89 38

53 88

Page 28: Week 12 - Wednesday.  What did we talk about last time?  Matching  Stable marriage  Started Euler paths

Deletions

When the list of keys drops below half m, we have to redistribute keys

In the worst case, we have to delete a level

Page 29: Week 12 - Wednesday.  What did we talk about last time?  Matching  Stable marriage  Started Euler paths

B*-tree

Instead of requiring every non-root node to be half full, every non-root node must be at least 2/3 full

Key redistribution becomes more complex

However, the tree is fuller

Page 30: Week 12 - Wednesday.  What did we talk about last time?  Matching  Stable marriage  Started Euler paths

B+-tree

Essentially, make a B-tree such that all the leaves are tied together in a linked list

It is also necessary that all keys in a B-tree appear as leaves

Some other variations are possible, but we’ll end the list here

612

2 512

13

15

6 7 8

Page 31: Week 12 - Wednesday.  What did we talk about last time?  Matching  Stable marriage  Started Euler paths

Are you ready to get Hamiltonian with it?

Hamiltonian Style

Page 32: Week 12 - Wednesday.  What did we talk about last time?  Matching  Stable marriage  Started Euler paths

Hamiltonian Cycle

The Eulerian Cycle problem asked if you could start at some node, cross every edge once, and return to the start

The Hamiltonian Cycle problem asks if you can start at some node, visit every node only once, and return to the start

In other words, find a tour Sounds easy, right?

Page 33: Week 12 - Wednesday.  What did we talk about last time?  Matching  Stable marriage  Started Euler paths

Find the Hamiltonian Cycle

On this graph:

E

A

C

B

D

E

A

C

B

D

Page 34: Week 12 - Wednesday.  What did we talk about last time?  Matching  Stable marriage  Started Euler paths

Find the Hamiltonian Cycle

Now, on this graph: There isn’t one!

E

A

C

B

D

F

Page 35: Week 12 - Wednesday.  What did we talk about last time?  Matching  Stable marriage  Started Euler paths

Find the Hamiltonian Cycle

Now, on this graph: Hmmm…

Page 36: Week 12 - Wednesday.  What did we talk about last time?  Matching  Stable marriage  Started Euler paths

TSP

Page 37: Week 12 - Wednesday.  What did we talk about last time?  Matching  Stable marriage  Started Euler paths

Traveling Salesman Problem:Hamiltonian Cycle Meets Shortest Path

Given a graph, find the shortest tour that visits every node and comes back to the starting point

Like a lazy traveling salesman who wants to visit all possible clients and then return to his home

Page 38: Week 12 - Wednesday.  What did we talk about last time?  Matching  Stable marriage  Started Euler paths

What’s the Shortest Tour? Find the shortest TSP tour:

Starts anywhere Visits all nodes Has the shortest length of such a tour

E

A

D

B

F

C

56

44

3

52

7

3

3

Page 39: Week 12 - Wednesday.  What did we talk about last time?  Matching  Stable marriage  Started Euler paths

How Can We Always Find the Shortest Tour?

Strategies: Always add the nearest neighbor Randomized approaches Try every possible combination

Why are we only listing strategies?

Page 40: Week 12 - Wednesday.  What did we talk about last time?  Matching  Stable marriage  Started Euler paths

Best Solution to TSP

So far we haven’t given a foolproof solution to TSP

Let’s look at two possibilities: Greedy Solution: Pick the closest

neighbor Brute Force: Try all possibilities

Page 41: Week 12 - Wednesday.  What did we talk about last time?  Matching  Stable marriage  Started Euler paths

Upcoming

Page 42: Week 12 - Wednesday.  What did we talk about last time?  Matching  Stable marriage  Started Euler paths

Next time…

Finish NP-completeness Elementary sorts Merge sort

Page 43: Week 12 - Wednesday.  What did we talk about last time?  Matching  Stable marriage  Started Euler paths

Reminders

Start on Project 4 Form teams by Friday

Start on Assignment 6 Read sections 2.1 and 2.2