{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

hw1Sol.pdf - CSE 5500 HW1 Solutions Answer 1 We assume that...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
CSE 5500: HW1 Solutions Answer 1 We assume that the graph is given to us as an adjacency list. (Can’t use adjacency matrix since just reading the matrix would require O ( | V 2 | ) time.) We perform a breadth-first (or depth-first) traversal of the graph, mark nodes as “visited” when they are encountered, and also keep track of the search path by including, at each node, back-pointers to the previous node. If the search encounters a node that has been previously “visited” then a cycle exists, otherwise no cycle exists. If a previously visited node is encountered, we backtrack using the back-pointers to output the nodes of the cycle. This is a simple problem and further details and proof of correctness are omitted (left to the reader). The BFS (or DFS) itself takes O ( | V | + | E | ) time and the backtracking step, if executed, requires another O ( | V | ) time, yielding an O ( | V | + | E | )-time algorithm for the problem. Answer 2 Part (a): Here, we have k = 2 jars available for testing. Consider the following strategy. We divide the ladder with n rungs into different levels, each containing n rungs (except for the top-most level which may contain fewer than n rungs). This would result in at most n levels. E.g., if n = 10, then we would have 3-levels with 4, 4, and 2 rungs, respectively, from bottom to top. We now drop the first jar from the bottommost rung of each level, going from the bottom to top, until the jar breaks. We now have three possible cases: (i) The jar breaks at level x where x = 1. (ii) The jar breaks at level x for x > 1. And (iii) The jar does not break on any of the tested rungs. For case (i), we can conclude that the jar breaks on the first rung of the ladder. For case (ii), we test the second jar from each rung of level x 1 from bottom to top and find the first rung where the second jar breaks. For case (iii), the first jar did not yet break, so we continue testing that jar from each rung of the topmost level, from bottom to top, and find the first rung where it breaks. Thus, in either case, we break no more than 2 jars to find the highest safe rung. We claim that the total number of drop required in this strategy is O ( n ). This is easy to verify using the three possible cases above, as follows. In case 1, we needed only 1 drop. In case 2, we needed x drops for the first jar and at most n drops for the second jar. Since x can be no greater than the total number of levels, we have x n . Thus, the total number of drops in this case is O ( n . In case 3, we have up to n drops of the first jar to get to the topmost level and an additional, at most, n drops to find the highest safe rung in the topmost level. Thus, the total number of drops is O ( n ), and lim n →∞ n n = 0, as required.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}