hw6_cs170

7
CS170–Spring 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’) 1

Upload: ryan-ma

Post on 17-Jan-2016

11 views

Category:

Documents


0 download

DESCRIPTION

blah

TRANSCRIPT

Page 1: hw6_cs170

CS170–Spring 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 offof that fact.If the edge e is part of a loop, then we will just delete the edge so that now τ(G) = τ(G\e). Ife is an edge that splits the current graph into a disconnected graph, then we will contract thegraph 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 Geither 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 theunique cycle. Thus τ(G) = τ((G-G’)-e) + τ((G-G’)\e) + τ(G’)

1

Page 2: hw6_cs170

CS170–Spring 2015 Homework n Ryan Ma, cs170-pf 2

2. Compare growth rates

Main idea: The main idea here is to note that each vertex u in U must have at least oneneighbor in V not in U. If a tree T is the optimal tree in G, then T not in U must be a spanningtree of G not in U. It also must be a MST because the nodes in U can be attached as the leavesof any spanning tree.Pseudocode:

1: function DesignatedLeaves(Graph, edge weights, subset U)2: if any u in U does not have neighbor in V then3: return Fail4: Run Prim’s to find MST in G\U5: for each edge weight dealing with vertices in U do6: Add on the lightest edge weights to G7: end for8: return Lightest spanning tree

Proof: We see that if any vertex u in U does not have a neighbor in V, then we obviouslycannot add on a vertex connected to a vertex in V because we do not know the edge weight ofit. Running Prim’s algorithm is essentialy finding the lightest (shortest) path from one pointof the graph to the other. We use Prim’s on the original graph without the subset of vertices inU. Once we have found the MST of G without U, then we add on the lightest edge connectingto v in G with u.

Run Time: O(ElogV)

Analysis: We are just running Prim’s algorithm which takes O(ElogV) time and then addingu to the graph in O(E) time. Adding them together we get O(E+ElogV) ≡ O(ElogV).

Page 3: hw6_cs170

CS170–Spring 2015 Homework n Ryan Ma, cs170-pf 3

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. Thenthe 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 confor-mation of the indices to make the tree. This can make the formation of the tree faster.

Page 4: hw6_cs170

CS170–Spring 2015 Homework n Ryan Ma, cs170-pf 4

4.

(a) Main idea: What we’re doing here is to do a DFS starting at a fruit f and for each fruitwe run into, we add a (key,value) pair (fk, R(k)) where R(k) is the product of the exchangerates up until k.

Pseudocode:

function Fruits(trades, requests, fruits)R = 1, Create HashMap¡fruit, R(k)¿ fruitMapAdd validTrade edges into matrix for DFSwhile DFS do

for (f,g,r) ∈ validTrades doif not visited(g) then R = R*r Put corresponding values in fruitMap explore(g)end if

end forAfter visit, R = R/r

end whileif fm and fn exist in same fruitMap then

return fruitMap.get(fn)/fruitMap.get(fm)end ifreturn Invalid

Proof: First we add our list of trades t into a matrix to do DFS. AS we traverse the treewith DFS, for every new fruit (vertex) that we reach, we add (fruit, R(k)) key value pairto our fruitMap where R(k) is the product of the product of exchange rates. Another wayof looking at R(k) is seeing that it is the product of all ’edge weights’ from the first fruitto the current fruit. As we go back up the edge, we must divide by the rate in order to getthe correct weight for that fruit. When we see a fruit that we haven’t encountered before,then we initialize a whole new fruitMap and repeat the steps over again until complete. Tocheck if this work, let’s look at if the fruit the customer wants (fw) is in the same tree asthe fruit that they are trading (ft). If

fruits(ft, fw) =R(fw)

R(ft)

=rstart,a ∗ ra,b ∗ ... ∗ ru,wrstart,a ∗ ra,b ∗ ... ∗ rs,t

(1)

But we notice that as we go down the tree, both R(fw) and R(ft) will have some of thesame ancestors. Thus their ancestral exchange rates will cancel out and thus we will havethe 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 thecustomer wants moves to a different node in the start. However, these two fruits have nocommon ancestors. This means when we divide by R(ft), it is basically the same thing astraversing the tree from ft to fstart instead of fstart to ft. This works because we knowthe exchange rate going one way is the equivalent to the reciprocal of the exchange rategoing the other way.

Run Time: O(m+n+k)

Page 5: hw6_cs170

CS170–Spring 2015 Homework n Ryan Ma, cs170-pf 5

Analysis: We know that our DFS takes O(m+n) time. When we go through the fruitMapto check if the fruits exist, it takes about O(4k). But when we add them together, it isbasically O(m+n+k) time, ignoring the constants.

(b) Main idea: This is very similar to our previous problem but instead this time we willinclude disjoint sets. We start with n fruits in n disjoint sets and every time there is anedge from fruit to fruit, we will say that there is a union of the two endpoints of the edgestogether.

Pseudocode:

1: function MoreFruits(fm, fn)2: Initialization: create n disjoint sets for n fruits3: for every edge do4: Union edges5: Set values of parent pointers to R6: end for7: if fm and fn have same parent then8: return R(fn)

R(fm)9: end if

10: return Invalid11: end function=0

Proof: Since we start with each fruit at its own disjoint set, we will have fruits in thesame set only if they are connected. Our method works because we’re working undirectedgraphs. If we run the MoreFruits operation and they both share a same root, then theremust be a path from the first fruit to the other fruit. Using what we know from part a, ifthere exists a path between two fruits, then we can easily calculate R(fn)

R(fm) to determine theexchange 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 tomake the m unions. O(klogn) time is needed to calculate the rates. Thus the total isO((m+n+k)logn).

Page 6: hw6_cs170

CS170–Spring 2015 Homework n Ryan Ma, cs170-pf 6

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) andthe 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 thisordering will not have any penalty at all.

(c) Let the jl be the job with the highest penalty among unschedule jobs. We will show thatthere is an optimal schedule B that schedules jl before its deadline. Once this property isestablished, 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 itat the latest available time before the deadline. If in schedule A the spot supposedly takenby 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 scheduleA.Case 2: If A then schedules jl at a bad time where there would be a penalty, then B willschedule 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.

Page 7: hw6_cs170

CS170–Spring 2015 Homework n Ryan Ma, cs170-pf 7

6.

Main idea: We obviously want the smallest weights for this problem but the way we incorpo-rate the weight and sets together is to create a ratio of number of elements missing in the setto the weight of the set. The larger the number means that there is greater numbers encasedin the set to weight ratio.

Pseudocode:

function WeightedSetCover(B, sets, weights)SetList, list = [], []Assign ratio to each set where each ratio = number of items missing from universe

weight of setwhile list != universe B do

Use the largest ratio and add set to list if not on the listCut and paste set to SetList

end whilereturn SetList

end function

Proof: What we’re doing here is first assigning each set a new ’weight’. This new ’weight’ isthe 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 weightto see which has the highest new weight. This in sense is also a greedy algorithm but modifieda bit to accomodate the weights.

Run Time: O(mlogn)

Analysis: This is just like the normal set cover problem except with weights. We’re justlooking at the weights instead of the number of things each set covers. Thus this algorithmruns the same time as the normal set cover problem.