quiz2sol

quiz2sol - Introduction to Algorithms December 8, 2006...

Info iconThis preview shows pages 1–4. 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

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 December 8, 2006 Massachusetts Institute of Technology 6.046J/18.410J Professors Erik Demaine and Madhu Sudan Handout 7 Quiz 2 Solutions Handout 7: Quiz 2 Solutions 2 Problem 1. Big Bully The year is 2048. The world has been taken over by unruly orcs. Each orc is either a captain or a soldier . Roughly, an orc X turns into a captain if any orc bigger than X in Xs neighborhood is already the solider of another captain, and X is the biggest orc remaining in Xs neighborhood. On the other hand, X ends up being a soldier if there is an orc in Xs neighborhood that is bigger than X and has turned into a captain. Your goal is to find out which orcs are captains and which orcs are soldiers in a one-dimensional cave. More precisely: Given: An array A [1 ..n ] of distinct integers (defining orc size), and an integer k (defining neighborhood size). Compute: B [1 ..n ] , where each B [ i ] { captain , soldier } , such that, for every i , B [ i ] = captain if, for every j { i- k,...,i + k } , either B [ j ] = soldier or A [ j ] A [ i ] . B [ i ] = soldier if there exists a j { i- k,...,i + k } such that B [ j ] = captain and A [ j ] > A [ i ] . (For this definition, assume that A [ i ] =- and B [ i ] = soldier for i < 1 and i > n .) Give an efficient algorithm for this problem. An ideal solution works well for all values of k relative to n . For partial credit, solve the special cases when k = O (1) and/or when k = ( n ) . Solution: We give an O ( n ) time algorithm for this problem. Initially every orc is a captain. The algorithm makes two passes over the array A. The first pass is left to right and the second pass is right to left. Informally, on the first pass we demote orcs that are dominated by their left neighbors and on the second pass we demote orcs that are dominated by their right neighbors. We use a single auxiliary variable p to keep a pointer to the orc that is currently dominating . Below is the pseudocode for our algorithm: 1 for i 1 to n 2 B [ i ] captain 3 p 1 4 for i 1 to n 5 if B [ p ] = captain and i p + k and A [ i ] < A [ p ] 6 then B [ i ] soldier 7 else p i 8 p n 9 for i n to 1 10 if B [ p ] = captain and i p- k and ( A [ i ] < A [ p ] or B [ i ] = soldier ) 11 then B [ i ] soldier 12 else p i Let us argue that the output of our algorithm satisfies both constraints specified in the problem statement. Handout 7: Quiz 2 Solutions 3 It is easy to see that the output of our algorithm can not contain two captains neighboring each other, since in the pass where we first observe the bigger captain we would demote the smaller....
View Full Document

Page1 / 10

quiz2sol - Introduction to Algorithms December 8, 2006...

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

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