Docsity

CS170 Spring 1993 Midterm Exam: Problem 3 - Sorting Problem, Exams of Algorithms and Programming

Toggle sidebar
bg1
CS170 Spring 1993
Midterm
Professor M. Blum
CLOSED BOOK. CALCULATORS ALLOWED.
You have two hours to complete this exam.
****DO ANY TWO OF THE THREE PROBLEMS. (Try to do all three if you can.)****
Problem #1 (Find An Element Above the Median)
Give upper and lower bounds on the number of comparisons to solve the following problems:
INPUT: An array A=[a1...aN] of real numbers. N = even integer. The array is NOT sorted.
OUTPUT: An element a1 contained in A that is greater than the MEDIAN, where the median is the biggest
element in the bottom half.
EXAMPLE: A = [1, 5, 3, 6]; MEDIAN = 3; RETURN 5 or 6.
Use the decision tree model of computation. (Each comparison counts 1 step.)
Make your bounds as tight as you can make them, but no tighter.
Problem #2 (Celebrity Problem)
DEFINITION: A celebrity is someone whom everyone knows, but who knows no one (else).
THE PROBLEM: You are to determine if a party of N persons, N >= 2, has a celebrity by asking questions of
the form "Do you know that person over there?"
STEPS: Each question counts one step. All other computations are free.
ASSUME: Each person (including the celebrity, if any) answers every (such) question asked of him, and
answers it honestly.
YOUR MISSION: Give upper and lower bounds on the number of steps (questions) to determine if a party
has a celebrity. Make your bounds as tight as you can make them, but no tighter.
HINT: Each answer to a question fills one entry of the MxN matrix [aij] defined by:
aij = 1 if i knows j, 0 if i does not know j, -1 if i=j
Problem #3 (A Sorting Problem)
In this problem, f:Z+->Z+ is a given function. (see I below).
An algorithm: Consider the following algorithm for computing "FOO [a1...aN]":
INPUT: An array [a1...aN] of reals; N contained in Z+ (positive integers)
OUTPUT: [a1...aN] a permutation of the input.
BEGIN: 1 If Nthen sort input & return; else
2 2.1 Do FOO [a1...af(N)] [Foo of top f(N) elements.]
2.2 Do FOO [aN-f(N)+1...aN] [Foo of bottom f(N) elements.]
2.3 Do FOO [a1...af(N)] [Foo of top f(N) elements.]
END
CS170, Midterm, Spring 1993
CS170 Spring 1993 Midterm Professor M. Blum 1
pf2

Partial preview of the text

Download CS170 Spring 1993 Midterm Exam: Problem 3 - Sorting Problem and more Exams Algorithms and Programming in PDF only on Docsity!

CS170 Spring 1993

Midterm

Professor M. Blum

CLOSED BOOK. CALCULATORS ALLOWED.

You have two hours to complete this exam. ****DO ANY TWO OF THE THREE PROBLEMS. (Try to do all three if you can.)****

Problem #1 (Find An Element Above the Median)

Give upper and lower bounds on the number of comparisons to solve the following problems:

INPUT: An array A=[a 1 ...aN] of real numbers. N = even integer. The array is NOT sorted. OUTPUT: An element a 1 contained in A that is greater than the MEDIAN, where the median is the biggest element in the bottom half. EXAMPLE: A = [1, 5, 3, 6]; MEDIAN = 3; RETURN 5 or 6.

Use the decision tree model of computation. (Each comparison counts 1 step.) Make your bounds as tight as you can make them, but no tighter.

Problem #2 (Celebrity Problem)

DEFINITION: A celebrity is someone whom everyone knows, but who knows no one (else). THE PROBLEM: You are to determine if a party of N persons, N >= 2, has a celebrity by asking questions of the form "Do you know that person over there?" STEPS: Each question counts one step. All other computations are free. ASSUME: Each person (including the celebrity, if any) answers every (such) question asked of him, and answers it honestly. YOUR MISSION: Give upper and lower bounds on the number of steps (questions) to determine if a party has a celebrity. Make your bounds as tight as you can make them, but no tighter. HINT: Each answer to a question fills one entry of the MxN matrix [aij] defined by: aij = 1 if i knows j, 0 if i does not know j, -1 if i=j

Problem #3 (A Sorting Problem)

In this problem, f:Z+->Z+^ is a given function. (see I below). An algorithm: Consider the following algorithm for computing "FOO [a 1 ...aN]": INPUT: An array [a 1 ...aN] of reals; N contained in Z+^ (positive integers) OUTPUT: [a 1 ...aN] a permutation of the input. BEGIN: 1 If Nthen sort input & return; else 2 2.1 Do FOO [a 1 ...af(N)] [Foo of top f(N) elements.] 2.2 Do FOO [aN-f(N)+1...aN] [Foo of bottom f(N) elements.] 2.3 Do FOO [a 1 ...af(N)] [Foo of top f(N) elements.] END

CS170, Midterm, Spring 1993

CS170 Spring 1993 Midterm Professor M. Blum 1

NOTES

I.

For which of the following choices of f does FOO sort the input array? PROVE YOUR ANSWERS A) f(N) = N-1 B) f(N) = ceiling(2/3 N) C) f(N) = ceiling((N+1)/2)

II.

Analyze the running times of A, B, C (from part I). Let S(N) = number of steps FOO takes on input [a 1 ...aN].

Posted by HKN (Electrical Engineering and Computer Science Honor Society)

University of California at Berkeley

If you have any questions about these online exams

please contact [email protected].

CS170, Midterm, Spring 1993

Problem #3 (A Sorting Problem) 2