THINK FIRST·CODE LATER

← All labs

Recurrences: measure, then apply the Master theorem

Problem

Write Recurrences. For each triple a b d in the input, consider T(n) = a·T(n/b) + n^d with T(1) = 1.

  1. Print the Master-theorem verdict by comparing a with b^d: a = b^d → Θ(n^d log n); a < b^d → Θ(n^d); a > b^d → Θ(n^(log_b a)) (exponent with 2 decimals).
  2. Evaluate T exactly with a recursive method for n = b⁰ … b¹⁰ and print the last three values with the ratio to the previous one.

For the input

2 2 1

the program prints

T(n) = 2 T(n/2) + n^1
  Master theorem: a = b^d: Theta(n log n)
  T(256) = 2304  (x2.25)
  T(512) = 5120  (x2.22)
  T(1024) = 11264  (x2.20)

Check that the measured ratio agrees with the verdict (for Θ(n log n) and b = 2 the ratio tends to slightly more than 2).

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

d7-4-data-1.txt

2 2 1
What your program must print for d7-4-data-1.txt
T(n) = 2 T(n/2) + n^1
  Master theorem: a = b^d: Theta(n log n)
  T(256) = 2304  (x2.25)
  T(512) = 5120  (x2.22)
  T(1024) = 11264  (x2.20)

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < d7-4-data-1.txt

Windows PowerShell does not understand <:

Get-Content d7-4-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.