This preview shows pages 1–3. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: CSc 445: Homework Assignment 1 Solutions February 5, 2008 1. Consider the problem of determining whether an arbitrary sequence x 1 ,x 2 ,...,x n of n num- bers contains repeated occurences of some number. (a) Design an efficient algorithm for the case where you are not allowed to use additional space (i.e., you can use O (1) storage). What’s the running time? Why? Solution: Since we cannot use O ( n ) space, that means we cannot treat the n input values as variables; we will act as if the input is stored in read-only memory. However, we assume that the number n itself is small enough to fit into a single unit of memory. In that case, one may simply compare each pair of values, like so: for i ← 1 to n- 1 for j ← i + 1 to n if x i = x j then return ‘duplicate exists’ return ‘all elements are unique’ Each iteration of the inner, j-indexed loop uses O (1) time, and for a fixed value of i , the j loop runs n- i times. The outer loop obviously runs n- 1 times, so the entire routine uses time proportional to n- 1 X i =1 n- i = n ( n- 1)- n- 1 X i =1 i = n ( n- 1) 2 = Θ( n 2 ) . (b) By using an extra array of n elements we can copy the data into memory and sort them in place. Then if there are any duplicate data, they will be adjacent in the sorted array, and so we can find them in a single pass, looking at neighboring elements: Heapsort( x 1 ,...,x n ) for i ← 1 to n- 1 if x i = x i +1 then return ‘duplicate exists’ return ‘all elements are unique’ The heapsort routine takes time O ( n log n ), and requires O ( n ) space. The scan clearly runs for n- 1 iterations, each iteration using O (1) time, and so the overall time is O ( n log n + n ) = O ( n log n ). Other approaches that might be suitable are to use a hash table, or to use a linear-time sorting routine such as radix sort. 1 2. Prove by induction that the sum of the degrees of all the nodes in any graph is an even number. We prove this using weak induction. The sketch of the proof is as follows: We observe the stated claim for a base case. Assume, the claim holds for a graph of a said complexity and then prove the induction on an increment of this graph. Note that we only need to prove the theorem for connected graphs. This is because multiple connected components each con- tribute an even number to the total which will therefore be even. Base cases: Graph with single node (no edges, 0 is even). Graph with 2 nodes and an edge connecting them (total degree is 2). Also, a triangle G(V,E): 3 nodes and 3 edges, each node has 2 degrees and 6 overall. Induction case: Assume that the property holds for any connected graph G(V,E) of any type of connection among the edges. We prove the induction by increasing the complexity of this graph incrementally. This is done by either adding an edge among two existing vertices or by adding a new vertex to the graph and connecting this new vertex to one or more existing vertices....
View Full Document
This homework help was uploaded on 04/08/2008 for the course CSC 445 taught by Professor Kobourov during the Spring '08 term at University of Arizona- Tucson.
- Spring '08