quiz2sol

Quiz2sol - Introduction to Algorithms May 6 2005 Massachusetts Institute of Technology 6.046J/18.410J Professors Charles E Leiserson and Ronald L

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Introduction to Algorithms May 6, 2005 Massachusetts Institute of Technology 6.046J/18.410J Professors Charles E. Leiserson and Ronald L. Rivest Quiz 2 Solutions Quiz 2 Solutions Problem 1. Static Graph Representation Let G = ( V, E ) be a sparse undirected graph, where V = { 1 , 2 , . . . , n } . For a vertex v V and for i = 1 , 2 , . . . , out-degree( v ) , define v s i th neighbor to be the i th smallest vertex u such that ( v, u ) E , that is, if you sort the vertices adjacent to v , then u is the i th smallest. Construct a representation of the graph G to support the following queries: D EGREE ( v ) : returns the degree of vertex v . L INKED ( u, v ) : output TRUE if an edge connects vertices u and v , and FALSE otherwise. N EIGHBOR ( v, i ) : returns v s i th neighbor. Your data structure should use asymptotically as little space as possible, and the operations should run asymptotically as fast as possible, but space is more important than time. Analyze your data structure in terms of both space and time. Solution: We give a solution that uses ( E ) space for the data structure and takes (1) time for each of the three operations. Create a hash table that contains key ( u, v ) if ( u, v ) E or key ( u, i ) if vertex u has an i-th neighbor. It is possible that for some u and v = i , vertex u has both an adjacent vertex v and an i-th neighbor. This is handled by storing satellite data with each record of the hash table. For the record with key ( u, i ) in the hash table, if u has a neighbor v = i , then indicate so using a bit in the record; if u has an i-th neighbor, then store the corresponding neighbor vertex index in the record. Also, for every vertex u that is connected to some other vertex, store its degree in the record for key ( u, 1) . Thus, for each vertex u in the first coordinate of the key, the hash table has at most degree( u ) + degree( u ) = 2 degree( u ) entries. The total number of entries is thus at most n u =1 2 degree( u ) = 4 | E | . By using perfect hashing , and choosing a suitable hash function through a small number of random samplings (during data structure construction), we can make the lookup time (1) and space requirement linear in the number of entries stored, i.e., ( E ) (see CLRS, page 249, Corollary 11.12). We can use the same family of hash functions as in CLRS by converting each 2-tuple ( u, v ) into a distinct number ( u- 1) n + v in the range of [1 . . . n 2 ] . The total space requirement of the degree array and hash table is thus ( V + E ) . Then, D EGREE ( v ) looks up the key ( v, 1) in the hash table. If found, it returns the degree value stored in the record. Otherwise, it returns , since v is not adjacent to any vertex. This takes (1) time. L INKED ( u, v ) looks up the key ( u, v ) in the hash table and returns TRUE if it exists and the associated bit in the record is set. This is (1) time. N EIGHBOR ( v, i ) looks up the key ( v, i ) in the hash table if it exists and a neighbor vertex is stored in the record, it returns its index. This isthe hash table if it exists and a neighbor vertex is stored in the record, it returns its index....
View Full Document

This note was uploaded on 01/20/2012 for the course CS 6.006 taught by Professor Erikdemaine during the Spring '08 term at MIT.

Page1 / 11

Quiz2sol - Introduction to Algorithms May 6 2005 Massachusetts Institute of Technology 6.046J/18.410J Professors Charles E Leiserson and Ronald L

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online