CMPSC 465, Spring 2009 Homework 5: Josephus Problem, Mergesort, Graph Theory, Assignments of Computer Science

Homework problems from a computer science course, including solutions to the josephus problem using mathematical induction, implementing a mergesort algorithm, and identifying strongly connected components in a graph. Problem 1 deals with the josephus problem, where participants stand in a circle and one is eliminated at each round. Problem 2 focuses on mergesort, a divide-and-conquer sorting algorithm. Problem 3 involves graph theory, determining if a graph remains strongly connected when edges are given a single direction.

Typology: Assignments

Pre 2010

Uploaded on 09/24/2009

koofers-user-kw5-1
koofers-user-kw5-1 🇺🇸

10 documents

1 / 1

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CMPSC 465, Spring 2009, Homework 5
Due: Monday,March 2, in class or before 2 pm on the following Tuesday (drop-offbox
will be in front of IST 337 when the deparment is closed).
Problem 1. In our description of the Josephus problem we have participants standing in
acircle and one sword. One participant has a sword. In one move,the participant with
the sword kills the next participant in the counterclockwise direction, the cycle closes and
the sword is passed to the next participant in the counterclockwise direction. Our goal is
to determine who will survive ifnparticipants start and we number/name them from 0 to
n1Assume that participants number 0 is the one who has the sword at the beginning.
(a) Showthat if nis a power of 2, participant 0 is the survivor. Use mathematical
induction.
(b) Showthat if n=p+rwhere pis a power of 2 and r<pthen the survivorispartici-
pant 2r.Hint: who gets the sword when the number of participants drops to p?
Problem 2. Recursive mergesort can be more efficient than iterative margesort but copy-
ing before sorting is a major drain of running time. We use notation ( A,i,j)for array
sector A[i], A[i+1], ...,A[j].
(a) Write code of a function that merges sorted sectors (A,a,b)and (A,c,d)onto
(A,c,d+ba), assuming abcd.Itshould move each array entry from
these sectors exactly once.
(b) Write recursive code that sorts fragment (A,a,b)sothat the result is in fragment
(A,b1, 2ba1).Itshould move array entries only using the calls to the merg-
ing function.
(c) Explain howlarge array Ashould be to sort ( A,0,n)using this method.
Problem 3. Youhav e as the input a connected undirected graph.
(a) Can you always give a single direction to each edge so the graph remains strongly
connected? Showacounterexample.
(b) Sketch an algorithm to identify edges that makethe graph not-strongly connected if
we give them a single direction.
Problem 4. Order the following functions so when fiis followed by fjthen fi=O(fj).
Indicate the cases when fi(fj).
f1(n)=(ln n)nf4(n)=5n
f2(n)=(ln n)! f5(n)=n
f3(n)=(n)ln nf6(n)=2ln n
Note: Toreceive partial credit for a not entirely correct answer,you must showyour work
neatly,and your work should not simply consist of evaluating the functions at a large
value of˜nin the hope that this value is large enough to reveal the correct answer.

Partial preview of the text

Download CMPSC 465, Spring 2009 Homework 5: Josephus Problem, Mergesort, Graph Theory and more Assignments Computer Science in PDF only on Docsity!

CMPSC 465, Spring 2009, Homework 5

Due: Monday, March 2, in class or before 2 pm on the following Tuesday (drop-off box will be in front of IST 337 when the deparment is closed).

Problem 1. In our description of the Josephus problem we have participants standing in a circle and one sword. One participant has a sword. In one move, the participant with the sword kills the next participant in the counterclockwise direction, the cycle closes and the sword is passed to the next participant in the counterclockwise direction. Our goal is to determine who will survive if n participants start and we number/name them from 0 to n − 1 Assume that participants number 0 is the one who has the sword at the beginning.

(a) Show that if n is a power of 2, participant 0 is the survivor. Use mathematical induction.

(b) Show that if n = p + r where p is a power of 2 and r < p then the survivor is partici- pant 2 r. Hint: who gets the sword when the number of participants drops to p?

Problem 2. Recursive mergesort can be more efficient than iterative margesort but copy- ing before sorting is a major drain of running time. We use notation ( A , i , j ) for array sector A [ i ], A [ i + 1],... , A [ j −].

(a) Write code of a function that merges sorted sectors ( A , a , b ) and ( A , c , d ) onto ( A , c , d + ba ), assuming abcd. It should move each array entry from these sectors exactly once.

(b) Write recursive code that sorts fragment ( A , a , b ) so that the result is in fragment ( A , b − 1, 2 ba − 1 ). It should move array entries only using the calls to the merg- ing function.

(c) Explain how large array A should be to sort ( A , 0, n ) using this method.

Problem 3. You have as the input a connected undirected graph.

(a) Can you always give a single direction to each edge so the graph remains strongly connected? Show a counterexample.

(b) Sketch an algorithm to identify edges that make the graph not-strongly connected if we give them a single direction.

Problem 4. Order the following functions so when fi is followed by f (^) j then fi = O ( f (^) j ). Indicate the cases when fi = Θ( f (^) j ). f 1 ( n ) = (ln n ) n^ f 4 ( n ) = 5 n f 2 ( n ) = (ln n )! f 5 ( n ) = n f 3 ( n ) = ( n )ln^ n^ f 6 ( n ) = 2 ln^ n Note: To receive partial credit for a not entirely correct answer, you must show your work neatly, and your work should not simply consist of evaluating the functions at a large value of˜ n in the hope that this value is large enough to reveal the correct answer.