10
10
Task 1 Given some negative cycle (v1, v2, v3, ..., vn, v1): we have u2 − u1 <= w21, u3 − u2 <=
w32, ...u1 − u(n) <= w1n. Summing up both sides, we get 0 <= weightof cycle. But the weight
of the cycle is negative, so contradiction.
15-210 SandwichLab Alex Zheng (amzheng), Recitation Z Page 2 of 17
Task 2 Make the weights from s to all the vertices 0. Then run Bellman Ford from s. The
shortest path from s to any u is the satisfiable assignment. Pick arbitrary ui , uj such that there is
a directed edge e of weight w going from ui to uj wLOG. Notice that shortest path from s to uj is
less than or equal to the shortest path from s to ui + w. since our algorithm has uj = shortest path
from s to uj and uj = shortest path from s to ui , we have uj − uj <= w, which is the inequality
we wanted.
15-210 SandwichLab Alex Zheng (amzheng), Recitation Z Page 3 of 17
Task 3 Sort all coordinates from increasing to decreasing. Give each coordinate two labels: Si
and Ti , where each S represents the number of cuts up until that coordinate, inclusive while each T
represents the number of cuts up until that coordinate, exclusive. For each left / right coordinate
of each sandwich, create an inequality Si − Tj <= k, where Si is the number of cuts up until the
right coordinate, inclusive while Tj is the number of cuts up until the left coordinate, exclusive.
The number of these inequalities is the same as the number of sandwiches. This ensures that every
sandwich has less than k cuts. We also do Ti − Ti+1 <= 0 and Si − Si+1 <= 0 to ensure that
we are actually accumulating cuts, and also Tj − Si <= −1 for all sandwiches to ensure that each
sandwich has at least one cut through it. We also do Ti − Si <= 0 for all i and Si − T( i + i) <= 0
Turn into inequalities into graph as described and run Task 2 Algorithm on it. If Bellman Ford
detects negative cycle, return no solution. Else, take all relevant Si and Tj left-right coordinate
pairs and take the differences to get all the relevant cuts in each sandwich.
15-210 SandwichLab Alex Zheng (amzheng), Recitation Z Page 4 of 17
Task 4 Negate all the weights and run Boruvska’s algorithm. Take the resulting tree and revert
the weights to the original. We end up with maximum spanning tree represented by a set of edges.
We convert this set to a sequence, tranpose it, and collect everything to end up with a sequence of
sequencee. The index of the sequence represents the neighbors of that index.
Boruvka’s is O(m log n) work and polylogarithmic span. Collect and toSeq are also both within
the time bounds.
15-210 SandwichLab Alex Zheng (amzheng), Recitation Z Page 5 of 17
Task 5 Perform a DFS starting at t. Stop when s is reached. Once s is reached, we can recursively
concat all the elements in the path to an empty list. We return the list.
DFS is within O(n) work and span. Constructing the list also takes O(n) work and span.
15-210 SandwichLab Alex Zheng (amzheng), Recitation Z Page 6 of 17
Task 6 I create a maximum spanning tree of the graph. The path between any s and any t in
the MST is the path that gives the maximum minimum excitement for those two vertices.
Take the path from some arbitrary s to t in the MST. Take the smallest weight edge in the
graph. Remove that edge so that the tree is bipartitioned. Take the partitions and look at the
corresponding vertices in the original graph. The cut edges are the edges that connect the partitions,
including the one we removed. We assert that the edge we removed is the greatest of all the cut
edges. If this were not the case, we could replace the removed edge with a greater edge, resulting in
a tree with a greater total edge weight than the original. However, that is a contradiction because
our original tree was already a maximum spanning tree. We also assert that any path from s to t
must travel through a cut edge. Since the edge we removed is the largest of the cut edges while
being the smallest edge in the path, any path that satisfies the maximum minimum excitement
requirement must contain the edge we removed, which means that our path in our MST is valid.
Since s and t were arbitrary, the path from any s to any t is a path that satisfies the maximum
minimum excitement requirement.
15-210 SandwichLab Alex Zheng (amzheng), Recitation Z Page 7 of 17
Task 7 Yes. Taking the log of a set of positive number preserves their order, so the algorithm is
correct because there is no difference between the MST of the original edges and the MST of the
edges after lg is applied to all of them.
15-210 SandwichLab Alex Zheng (amzheng), Recitation Z Page 8 of 17
Task 9 Yes. Let e = (u,v). Prim generates the correct MSTs for regular Prim starting at u and
regular Prim starting at v.
Two cases: either e is in the MST or it isn’t. Both cases are covered after we remove e - if it
isn’t in the MST, we replace it with what should be connecting u and v. If it is, then we just add
e back in because there is no other lighter path.
15-210 SandwichLab Alex Zheng (amzheng), Recitation Z Page 10 of 17
Task 10 Doesn’t work. The random edge we pick might not be in the MST but it also might not
be heaviest edge in the MST, so it won’t be removed and we’ll end up with the wrong MST.
V = (1,2,3,4),E=((1,2),(2,3),(3,4),(1,3),(2,4)) with w(1,2)=1,w(2,3)=2,w(3,4)=5,w(2,4)=6, w(1,3)
= 4. The MST is ((1,2),(2,3),(3,4)). However, if we start at (1,3), (1,3) will be in the resulting tree
no matter what, since it’s not the heaviest edge in the graph.
15-210 SandwichLab Alex Zheng (amzheng), Recitation Z Page 11 of 17
Task 12 Works. Prim’s algo returns the same correct MST for any given starting node, so taking
the smallest edge weights from individual vertices results in a valid MST because those edges
would be the first edges in the MST if Prim’s algo started at each of those vertices. Connecting
components simply results in a subtree of the MST, so the algorithm remains valid because the
smallest weight edges coming off of a subtree of the MST is also part of the MST.
15-210 SandwichLab Alex Zheng (amzheng), Recitation Z Page 13 of 17
Task 15 Since it’s a tree, that means we have m = n-1 edges. Handshake lemma gives us that
the sum of all the degrees of the vertices is 2n - 2.
Worst case is that the sum of all the vertices is 3n if all the vertices have degree 3, but we
know the actual sum is 2n - 2, so we take the degree difference and get n + 2. We want to know
minimum number of vertices with degree less than 3. The best way to do that is to make degree-1
vertices, each of which subtracts 2 degrees from our initial assumption. Thus, we have (n+2)/2
as our lower bound. To represent this in n/ k form, k = 2n/(n+2). The upper bound for k as n
approaches infinity is 2, so the lower bound for n / k is simply n / 2. This bound provides the
minimum number of not-3 vertices, which in the worst case we only have degree 1 vertices and the
rest are degree 3 vertices.
15-210 SandwichLab Alex Zheng (amzheng), Recitation Z Page 16 of 17
Task 16 There are at most n - n/2 edges that have degree 3. That means there are at most n/2
- 1 edges that are between two degree 3 vertices. These are the edges that we cannot contract if
we want to maintain the invariant. We assign all these edges tails. The other edges we flip a coin
for. Then we contract all the edges that are heads with all their incident edges being tails.
Assigning coin values to each of the edges is O(n) work and O(1) span. We can do a map to
contract the vertices / edges, which is O(n) work. Filtering out self-loops is O(log n) span.
15-210 SandwichLab Alex Zheng (amzheng), Recitation Z Page 17 of 17