Last time, we compared the playing ability of two algorithms for the End Game. This time we compare the time performance of two algorithms for the board-coloring problem, which we encountered in the board reconstruction puzzle to open Session 11.
The Implementation Notes from Homework 2 apply here as well. In particular, in the description below, I refer to lists of integers as specifying the row and column counts. I mean 'list' in a generic sense. You may represent these lists in any way suitable to your language: as an array, a list, a vector, an object, ....
Tasks 4 through 6 ask you to report "the average time it takes" to execute a function. See this short section on timing execution in Python, Java, Ruby, and Racket. For other languages, please ask.
If you are game, there is a chance to earn some extra credit. Don't attempt the extra credit until you have made substantial progress on the basic tasks. I won't spend time grading extra credit if the primary assignment isn't in good shape.
Write the following functions.
Demonstrate that your function works as expected for the six boards used as examples in Session 11.
Example:
rows [1 1 2] output [1 0 0] cols [1 2 1] [0 1 0] [0 1 1]
Demonstrate that your function works as expected for the six boards used as examples in Session 11.
Example:
rows [1 1 3] output [0 0 1] cols [1 1 3] [0 0 1] [1 1 1]
Example:
N 3 output [1 1 2] [1 2 1]
Create a readme.txt file that tells me:
Implement the brute-force approach to coloring a board.
Include it in your experiments and report its performance data along side those of the greedy and zoom-in approaches.
First, note that the time to execute a single function is often so small that it rounds to zero in a variable. Do not run a function once and save its execution time. Run the function many times (in this assignment, 1000), save the execution time of the for-loop, and then divide by the number of iterations.
Python
import time start_time = time.time() ... stop_time = time.time() elapsed_time = stop_time - start_time # in seconds
....... Or, for short expressions:
import timeit timeit.timeit('"-".join(str(n) for n in range(100))', number=10000)
Java
long startTime = System.currentTimeMillis(); ... long stopTime = System.currentTimeMillis(); long elapsedTime = stopTime - startTime; # in milliseconds
Ruby
start_time = Time.now ... stop_time = Time.now elapsed_time = stop_time - start_time # in seconds
....... Or, for some real Ruby goodness:
def time start = Time.now yield Time.now - start end # then reuse with: time do ... end
Racket
(time body ...+) ; returns value of last exp ; prints three times: full time, real time, GC time (time-apply proc lst) ; returns four values: ; result of (apply proc lst) ; the three times from (time ...)
If you are interested in using another language, let me know. I can't guarantee help, but I can try.
By the due time and date, submit a zipped archive named homework03 containing:
Be sure that your submission follows all homework submission requirements. Note that the only file you need to print is your readme.txt.