Question: Please give the complete java code. CMPT 360 T1 2017 CMPT 360 T1 2017 Question 1: [20 points This algorithm is straightforward (but requires efficient


Please give the complete java code.
CMPT 360 T1 2017 CMPT 360 T1 2017 Question 1: [20 points This algorithm is straightforward (but requires efficient data structures): All non-trivial trees contain a leaf, L. That leaf only has one neighbour, Mapple recently announced their latest product, the jPhone X, going on sale tomorrow They have committed that each of their stores will have one of these new devices available for sale tomorrow but their shipping department messed up and didn't send one to every store. Some stores got none, and others got too many. To meet their promise, Mapple needs the stores with too many to send their surplus to other stores. Even worse, it takes three shipments to send three jPhones. And even worse, Mapple stores are not a complete graph of shipping routes: store can only ship to their neighbouring stores! But one store can receive a shipped jPhone and ship it on again to another store: but this counts as two shipments. Fortunately, there is a unique path from each store to each other store, possibly involving multiple shipment steps. Your task is to implement an algorithm to compute the minium number of shipmentas .if L has exactly one jPhone, then it does nothing if L has more than one jPhone, then it can only do one thing: ship the surplus to N shipments stock-1 stoek[N] +. stock[L1-1 because they will be needed elsewhere. if L doesn't less than one jPhone, then N must ship one (or more, as we will see in moment) to L o ensure every store has one jPhone. shipments +-1-stock' In summary, you know the following details and N will have that many fewer in stock * Mapple has V = {ty, , th-1)stores (that is n stores). stock!-. 1-stock. Every store v received w (with O Sw S) jPhones; and altogether, the stores did receive enough jPhones: and if N doesn't have enough stock, then N will have to get it from some other of its neighbours - but this is okay, because this also means N is not a leaf in the graph. Note that our algorithm allows for stock at N to become negative, which really means that N will eventually have to receive that many jPhones from its other neightbours and then ship them to L. By working from the leaves in, we ensure that N never takes phones unless we know exactly where they must come from. And, because we care only about how many shipments, not their order of shipments, our solution works: it gives . The shipping routes E = {(may), ] form a tree across the stores. the correct count After greedily working on L, we know that L is satisfied, and can be deleted from the graph. G = (V, E) becomes G' = (V', E') = (V-(L), E-{(LN)) This gives us a smaller graph: one fewer vertexes, one fewer edges, and one fewer jPhones. In addition, this means that N has its degree reduced, because its neighbour has vanished Implement an O(V| log(|V)) algorithm to compute the mmnimum number of shipments. degreeN 1 neighbourML After removing L, we either have a trivial tree, meaning it contains only vertex N, and arithmetie shows that N has its needed jPhone, and so we're done; or we still have a tree -G' remains connected: any simple path (v-Un) where ULa, L in the original graph G remains a path in G, because L, as a leaf, can only appear at the start or end of a simple path in G Page 1 Page 2 CMPT 360 T1 2017 CMPT 360 T1 2017 Question 1: [20 points This algorithm is straightforward (but requires efficient data structures): All non-trivial trees contain a leaf, L. That leaf only has one neighbour, Mapple recently announced their latest product, the jPhone X, going on sale tomorrow They have committed that each of their stores will have one of these new devices available for sale tomorrow but their shipping department messed up and didn't send one to every store. Some stores got none, and others got too many. To meet their promise, Mapple needs the stores with too many to send their surplus to other stores. Even worse, it takes three shipments to send three jPhones. And even worse, Mapple stores are not a complete graph of shipping routes: store can only ship to their neighbouring stores! But one store can receive a shipped jPhone and ship it on again to another store: but this counts as two shipments. Fortunately, there is a unique path from each store to each other store, possibly involving multiple shipment steps. Your task is to implement an algorithm to compute the minium number of shipmentas .if L has exactly one jPhone, then it does nothing if L has more than one jPhone, then it can only do one thing: ship the surplus to N shipments stock-1 stoek[N] +. stock[L1-1 because they will be needed elsewhere. if L doesn't less than one jPhone, then N must ship one (or more, as we will see in moment) to L o ensure every store has one jPhone. shipments +-1-stock' In summary, you know the following details and N will have that many fewer in stock * Mapple has V = {ty, , th-1)stores (that is n stores). stock!-. 1-stock. Every store v received w (with O Sw S) jPhones; and altogether, the stores did receive enough jPhones: and if N doesn't have enough stock, then N will have to get it from some other of its neighbours - but this is okay, because this also means N is not a leaf in the graph. Note that our algorithm allows for stock at N to become negative, which really means that N will eventually have to receive that many jPhones from its other neightbours and then ship them to L. By working from the leaves in, we ensure that N never takes phones unless we know exactly where they must come from. And, because we care only about how many shipments, not their order of shipments, our solution works: it gives . The shipping routes E = {(may), ] form a tree across the stores. the correct count After greedily working on L, we know that L is satisfied, and can be deleted from the graph. G = (V, E) becomes G' = (V', E') = (V-(L), E-{(LN)) This gives us a smaller graph: one fewer vertexes, one fewer edges, and one fewer jPhones. In addition, this means that N has its degree reduced, because its neighbour has vanished Implement an O(V| log(|V)) algorithm to compute the mmnimum number of shipments. degreeN 1 neighbourML After removing L, we either have a trivial tree, meaning it contains only vertex N, and arithmetie shows that N has its needed jPhone, and so we're done; or we still have a tree -G' remains connected: any simple path (v-Un) where ULa, L in the original graph G remains a path in G, because L, as a leaf, can only appear at the start or end of a simple path in G Page 1 Page 2
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
