Lab 6 (Midterm Review)- Solution

CS 330: Algorithms
Spring 2016
Lab 6 (Midterm Review)- Solution
Instructor: John Byers
Question 1. You are given n coins. They all look identical. They should all be the same weight, but
one is a fake coin and it is made of a lighter metal. It is your job to find the fake coin and we give you an
old-fashioned balance scale that enables you to compare any two sets of coins. If it tips either to the left
or to the right, you will know which one of the sets is the heavier one. However, we are going to charge
you each time you weigh anything. Therefore, you’d ideally like to use the balance as few times as possible.
Design an algorithm to find the fake coin in the fewest number of weighings. How many times do you need
to use the scale?
Solution:
A first approach will be to use a binary-search type of method. This could work, but we can do better than a
factor of 2. Instead of dividing up the coins into two piles, consider dividing up the coins into piles of three
where at least two of the piles have an equal number of coins. After weighing the equal-sized parts, we can
eliminate close to 2/3 of the coins. Consider the following code with a weight function that returns 1 if the
first input is heavier, 0 if the second input is heavier, -1 if both are equal :
def f i n d F a k e C o i n ( c o i n s ) :
i f len ( c o i n s ) == 1 :
return c o i n s [ 0 ] #T h i s i s t h e f a k e c o i n !
else :
#d i v i d e t h e c o i n s i n t o t h r e e p i l e s
A = coins [ 0 : c e i l i n g (n /3)]
B = coins [ c e i l i n g (n /3): c e i l i n g (n/3)∗2]
C = coins [ c e i l i n g (n /3)∗2:]
r e s u l t = w e i g h t (A, B)
i f r e s u l t == −1:
return f i n d F a k e C o i n (C)
else :
if result :
return f i n d F a k e C o i n (A)
else :
return f i n d F a k e C o i n (B)
Let us now consider how many weighings this requires of us; we will do so by writing a recurrence relation.
First, let us make the simplifying assumption that n = 3k . Let C(n) be the number of weighings required
to find the fake coin in n coins. We can see that there will be n/3 weighings required on top of the initial
weighing. Therefore, C(n) = C(n/3) + 1. Let us try to solve this recurrence relation.
C(30 ) = 0
C(31 ) = C(1) + 1 = 1
C(32 ) = C(3) + 1 = C(1) + 1 + 1 = 2
C(33 ) = C(9) + 1 = C(1) + 1 + 1 + 1 = 3
..
.
C(n) = C(1) + log3 (n) = log3 (n)
-1
Question 2. Consider this graph:
A
3
3
1
F
5
B
C
1
1
5
2
4
D
E
(a) Show the result of running Dijkstra’s algorithm on the above graph starting at node A.
(b) Show the sequence of edges that are added to the MST when running Prim’s algorithm on the above
graph. Start at node A.
(c) Show the sequence of edges that are added to the MST when running Kruskal’s algorithm on the above
graph. When there are multiple choices, choose lexicographically.
Solution:
(a)
Node
A
B
C
F
D
E
Distance
0
1
3
3
2
4
Parent
–
A
A
A
B
C
(b) AB, BD, DC, CE, AF
(c) AB, BD, CE, DC, AF
Question 3.
(a) Show the resulting tree produced from running BFS on the above graph starting at node A. Choose
alphabetically.
(b) Show the resulting tree produced form running DFS on the above graph starting at node A. Choose
alphabetically.
(c) Show the resulting tree produced from running BFS and DFS starting at A on the following graph:
-2
A
F
B
C
D
E
(d) Show that BFS and DFS produce the same tree on a connected, undirected graph G if and only if the
graph is a tree.
Solution:
A
3
3
1
F
B
C
1
1
D
(a)
E
A
1
B
F
5
C
5
2
(b)
D
4
E
(c) The result of DFS and BFS is the graph itself.
(d) We want to show that if G is a tree T , then DFS and BFS will produce the same tree. We suppose that
G is an undirected tree T . Because DFS and BFS must produce a tree, they must contain all the edges
of T . Since two trees must be identical if they have the same root and same edges, both DFS and BFS
will produce T .
-3
Conversely, we want to show that if BFS and DFS produce the same tree, then G is a tree. We will
prove this by proving the contrapositive: if G is not a tree, then BFS and DFS will not produce the same
tree. Since G is not a tree, then we know that it must contain some cycle C. Suppose that the cycle
contains k nodes n1 , n2 , . . . , nk . During DFS, we know that the nodes n1 , . . . , nk will all be on the same
path. This is because once DFS visits any node in the cycle ni , then it will end up visiting every other
node in the cycle because they are all strongly connected. In other words, it will be the case that these
nodes will be at different levels of the DFS tree, since all the unvisited nodes will always be explored as
children of the visited nodes (recursively). In contrast, for BFS, once the first node is visited (at level i),
its two neighbors will either be level i + 1 (or also at level i) of the BFS tree. Thus the BFS tree has two
nodes of this cycle at the same level.Thus, the trees produced in BFS and DFS will not be the same and
we have proven that BFS and DFS produce the same tree if and only if G is a tree.
Question 4. Consider the following rankings:
1st
Name/Rank
Xavier
Amy
Men’s Preference Profile:
Brenda
Yuri
Zoran
Amy
Women’s Preference Profile:
Name/Rank
Amy
Brenda
Cindy
1st
Yuri
Xavier
Xavier
2nd
Brenda
Amy
Brenda
2nd
Xavier
Yuri
Yuri
3rd
Cindy
Cindy
Cindy
3rd
Zoran
Zoran
Zoran
(a) Is the matching {(Xavier, Cindy), (Yuri, Brenda), (Zoran, Amy)} stable?
(b) Is the matching {(Xavier, Amy), (Yuri, Brenda), (Zoran, Cindy)} stable?
Solution:
(a) No, Brenda and Xavier prefer each other to their assigned partner.
(b) Yes.
-4