ps6sol

ps6sol - Introduction to Algorithms November 6, 2006...

Info iconThis preview shows pages 1–3. 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 November 6, 2006 Massachusetts Institute of Technology 6.046J/18.410J Professors Erik Demaine and Madhu Sudan Problem Set 6 Solutions Problem Set 6 Solutions Problem 6-1. Corporate Chaos You are a qualitative analyst for a financial institution trying to keep track of whos who in todays corporate frenzy of startups, mergers, and acquisitions. Every person in your database is employed by exactly one company. Your job is to implement a data structure that, given such person x , will produce the name of the president of the company employing person x . More precisely, your data structure must efficiently support the following three operations: 1. STARTUP ( x ) creates a new company whose president and only employee is person x (previ- ously unemployed). 2. MERGER ( x, y ) merges the two (distinct) companies employing persons x and y respectively. The president of the resulting company is one of the presidents of the two subcompanies, and the data structure gets to choose which (!). 3. PRESIDENT ( x ) returns the president of the company employing person x . You first idea is to represent each company as a doubly linked list, with each node corresponding to a person, where the head of the list stores the president, and each node x stores a pointer head [ x ] to the head of the list. PRESIDENT ( x ) simply returns head [ x ] , in O (1) time. MERGER ( x, y ) appends y s list to the end of x s list, and then changes all of the head pointers in y s list to head [ x ] . (a) Give a tight asymptotic bound on the worst-case total cost of performing m STARTUP, MERGER, and/or PRESIDENT operations (starting from an empty data structure). Solution: ( m 2 ) . More precisely, its certainly O ( m 2 ) because, after at most m operations, the length of any list (and thus the cost of MERGER) is at most O ( m ) . It can also be ( m 2 ) : if we maintain a single company grown through m/ 2 calls to STARTUP, each followed by a call to MERGER that appends the companys employee list to the length- 1 em- ployee list of the new startup, we pay at least ( m/ 4) for each of the last m/ 4 calls to MERGER. You think of a simple trick to improve the performance of your data structure. You augment the head h of each list with two pieces of information: a counter length [ h ] tracking the length of the list, and a pointer tail [ h ] to the tail of the list. Now, MERGER ( x, y ) always appends the smaller of the two lists to the longer of the two lists (breaking ties arbitrarily), updating the head pointers in the smaller list. 2 Problem Set 6 Solutions (b) Prove that the worst-case total cost of performing m STARTUP, MERGER, and/or PRESIDENT operations (starting from an empty data structure) is ( m lg m ) ....
View Full Document

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

Page1 / 5

ps6sol - Introduction to Algorithms November 6, 2006...

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

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