week 12 - wednesday. what did we talk about last time? matching stable marriage started euler...
TRANSCRIPT
CS221Week 12 - Wednesday
Last time
What did we talk about last time? Matching Stable marriage Started Euler paths
Questions?
Project 4
Assignment 6
Euler Paths and Tours
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
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
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
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
The Blue Prince: Solution
Put the bridge from the Bishop’s land to the Red Prince’s land
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
The Red Prince: Solution
Put the bridge from the Red Prince’s land to the Blue Prince’s land
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
The Bishop: Solution
Put the bridge from the Center Island to the Red Prince’s land, making all pieces of land have even degree
Euler Practice
Is there an Euler tour?
87
9 5
6
0
3
4
2
1
What about here?
1
5 2
4 3
B-treesStudent Lecture
B-trees
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
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
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
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
Easy add
12
5 813
15
Insert 7 1
2
5 7 813
15
Tougher add
12
2 5 7 813
15
Insert 6 6
12
2 513
15
7 8
B-tree practice
Insert the following numbers: 86 69 81 15 100 94 8 27 56 68 92 89 38
53 88
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
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
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
Are you ready to get Hamiltonian with it?
Hamiltonian Style
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?
Find the Hamiltonian Cycle
On this graph:
E
A
C
B
D
E
A
C
B
D
Find the Hamiltonian Cycle
Now, on this graph: There isn’t one!
E
A
C
B
D
F
Find the Hamiltonian Cycle
Now, on this graph: Hmmm…
TSP
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
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
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?
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
Upcoming
Next time…
Finish NP-completeness Elementary sorts Merge sort
Reminders
Start on Project 4 Form teams by Friday
Start on Assignment 6 Read sections 2.1 and 2.2