Sie sind auf Seite 1von 7

CS170Spring 2015 Solutions to Homework n

Ryan Ma, SID 23597524, cs170-pf

Collaborators: Anita Chan, Derrick Hu

1. Getting started
(a) We can clearly see that a graph of (G)=1 has no edges. And we will the rest of the proof off
of that fact.
If the edge e is part of a loop, then we will just delete the edge so that now (G) = (G\e). If
e is an edge that splits the current graph into a disconnected graph, then we will contract the
graph and run the process again.
If the edge e is neither part of a loop or cut edge, then we see that every spanning tree T of G
either does not contain e (G) or contains e (G\e).
(b) For a tree: (G) = (G)
For a cycle: (G) = (G-e) + (G\e)
For a graph with unique cycle: Let G be the graph that is part of G but does not contain the
unique cycle. Thus (G) = ((G-G)-e) + ((G-G)\e) + (G)

CS170Spring 2015 Homework n Ryan Ma, cs170-pf

2. Compare growth rates


Main idea: The main idea here is to note that each vertex u in U must have at least one
neighbor in V not in U. If a tree T is the optimal tree in G, then T not in U must be a spanning
tree of G not in U. It also must be a MST because the nodes in U can be attached as the leaves
of any spanning tree.
Pseudocode:
1:
2:
3:
4:
5:
6:
7:
8:

function DesignatedLeaves(Graph, edge weights, subset U)


if any u in U does not have neighbor in V then
return Fail
Run Prims to find MST in G\U
for each edge weight dealing with vertices in U do
Add on the lightest edge weights to G
end for
return Lightest spanning tree
Proof: We see that if any vertex u in U does not have a neighbor in V, then we obviously
cannot add on a vertex connected to a vertex in V because we do not know the edge weight of
it. Running Prims algorithm is essentialy finding the lightest (shortest) path from one point
of the graph to the other. We use Prims on the original graph without the subset of vertices in
U. Once we have found the MST of G without U, then we add on the lightest edge connecting
to v in G with u.
Run Time: O(ElogV)
Analysis: We are just running Prims algorithm which takes O(ElogV) time and then adding
u to the graph in O(E) time. Adding them together we get O(E+ElogV) O(ElogV).

CS170Spring 2015 Homework n Ryan Ma, cs170-pf

3.
(a) Starting from the 18th number, we would have b a b b b a.
Instead of recognizing the 7, it would recognize ba and it would become 4 instead. Then
the 12th index would become bab. Next it would recognize bb as 3 and would add bbb
to the 13th index. Next it would recognize ba and assign it 4. Thus ends the animation.
(b) Once the dictionary is completed, the Huffman algorithm can just use the binary conformation of the indices to make the tree. This can make the formation of the tree faster.

CS170Spring 2015 Homework n Ryan Ma, cs170-pf

4.
(a) Main idea: What were doing here is to do a DFS starting at a fruit f and for each fruit
we run into, we add a (key,value) pair (fk , R(k)) where R(k) is the product of the exchange
rates up until k.
Pseudocode:
function Fruits(trades, requests, fruits)
R = 1, Create HashMapfruit, R(k) fruitMap
Add validTrade edges into matrix for DFS
while DFS do
for (f,g,r) validTrades do
if not visited(g) then R = R*r Put corresponding values in fruitMap explore(g)
end if
end for
After visit, R = R/r
end while
if fm and fn exist in same fruitMap then
return fruitMap.get(fn )/fruitMap.get(fm )
end if
return Invalid
Proof: First we add our list of trades t into a matrix to do DFS. AS we traverse the tree
with DFS, for every new fruit (vertex) that we reach, we add (fruit, R(k)) key value pair
to our fruitMap where R(k) is the product of the product of exchange rates. Another way
of looking at R(k) is seeing that it is the product of all edge weights from the first fruit
to the current fruit. As we go back up the edge, we must divide by the rate in order to get
the correct weight for that fruit. When we see a fruit that we havent encountered before,
then we initialize a whole new fruitMap and repeat the steps over again until complete. To
check if this work, lets look at if the fruit the customer wants (fw ) is in the same tree as
the fruit that they are trading (ft ). If
R(fw )
R(ft )
rstart,a ra,b ... ru,w
=
rstart,a ra,b ... rs,t

f ruits(ft , fw ) =

(1)

But we notice that as we go down the tree, both R(fw ) and R(ft ) will have some of the
same ancestors. Thus their ancestral exchange rates will cancel out and thus we will have
the exchange rate that differs between the two fruits.
If the fruits are in different subtrees, then we still use the same formula but the fruit the
customer wants moves to a different node in the start. However, these two fruits have no
common ancestors. This means when we divide by R(ft ), it is basically the same thing as
traversing the tree from ft to fs tart instead of fs tart to ft . This works because we know
the exchange rate going one way is the equivalent to the reciprocal of the exchange rate
going the other way.
Run Time: O(m+n+k)

CS170Spring 2015 Homework n Ryan Ma, cs170-pf

Analysis: We know that our DFS takes O(m+n) time. When we go through the fruitMap
to check if the fruits exist, it takes about O(4k). But when we add them together, it is
basically O(m+n+k) time, ignoring the constants.
(b) Main idea: This is very similar to our previous problem but instead this time we will
include disjoint sets. We start with n fruits in n disjoint sets and every time there is an
edge from fruit to fruit, we will say that there is a union of the two endpoints of the edges
together.
Pseudocode:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:

function MoreFruits(fm , fn )
Initialization: create n disjoint sets for n fruits
for every edge do
Union edges
Set values of parent pointers to R
end for
if fm and fn have same parent then
R(fn )
return R(f
m)
end if
return Invalid
end function=0
Proof: Since we start with each fruit at its own disjoint set, we will have fruits in the
same set only if they are connected. Our method works because were working undirected
graphs. If we run the MoreFruits operation and they both share a same root, then there
must be a path from the first fruit to the other fruit. Using what we know from part a, if
R(fn )
there exists a path between two fruits, then we can easily calculate R(f
to determine the
m)
exchange rate between the fruits.
Run time: O((m+n+k)logn)
Analysis: We need O(nlogn) time to make disjoint sets. O(mlogn) time is needed to
make the m unions. O(klogn) time is needed to calculate the rates. Thus the total is
O((m+n+k)logn).

CS170Spring 2015 Homework n Ryan Ma, cs170-pf

5.
(a) Consider a counterexample where the listing is (deadline, penalty). If there are listings
(1,1), (2,10), (2,15). Here we would list (1,1) first but the next job would be (2,15) and
the last would be (2,10). This incurs a penalty of 5. However, if we start with (2,10) then
(2,15) then (1,1), then we will have a penalty of 1 instead of 5.
(b) If there are listings (1,1) and (2,10), then the jobs would be ordered as (2,10) then (1,1)
with a penalty of 1. However, a better job ordering would be (1,1) then (2,10) as this
ordering will not have any penalty at all.
(c) Let the jl be the job with the highest penalty among unschedule jobs. We will show that
there is an optimal schedule B that schedules jl before its deadline. Once this property is
established, then the rest of the algorithm will correctly give the optimal schedule.
Consider an optimal schedule A. Now we construct schedule B by taking jl and putting it
at the latest available time before the deadline. If in schedule A the spot supposedly taken
by jl is taken by some job jx , then we just switch jl with jx . There are two cases:
Case 1: If A schedules jl no later than its deadline, then B has schedules jx earlier than jl .
This means that schedule B will have just the same amount of penalty (if any) as schedule
A.
Case 2: If A then schedules jl at a bad time where there would be a penalty, then B will
schedule jl on time. We will be switching jl with jx , but since jl has the greatest penalty,
scheduling jx late will make schedule B have no more than a penalty than schedule A.

CS170Spring 2015 Homework n Ryan Ma, cs170-pf

6.
Main idea: We obviously want the smallest weights for this problem but the way we incorporate the weight and sets together is to create a ratio of number of elements missing in the set
to the weight of the set. The larger the number means that there is greater numbers encased
in the set to weight ratio.
Pseudocode:
function WeightedSetCover(B, sets, weights)
SetList, list = [], []
missing from universe
Assign ratio to each set where each ratio = number of items
weight of set
while list != universe B do
Use the largest ratio and add set to list if not on the list
Cut and paste set to SetList
end while
return SetList
end function
Proof: What were doing here is first assigning each set a new weight. This new weight is
the ratio of number of elements missing from the universe in the set over the weight of the set.
As we go on to construct the universe from the sets we are given, we go through each weight
to see which has the highest new weight. This in sense is also a greedy algorithm but modified
a bit to accomodate the weights.
Run Time: O(mlogn)
Analysis: This is just like the normal set cover problem except with weights. Were just
looking at the weights instead of the number of things each set covers. Thus this algorithm
runs the same time as the normal set cover problem.

Das könnte Ihnen auch gefallen