{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

HW1 Solutions

# HW1 Solutions - CSc 445 Homework Assignment 1 Solutions...

This preview shows pages 1–3. Sign up to view the full content.

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 i =1 n - i = n ( n - 1) - n - 1 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

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

View Full Document
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.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern