## CSci 3501 Algorithms and Computability - Lab 1.

#### August 24th. Due Wednesday, August 30th at 11:59pm.

What to submit and when:

• All submissions are electronic: by e-mail to and CC to all your lab partners. Please do not delete your e-mail from "Sent mail" or your mailbox until the end of the semester.
• When working on the lab, please comment your work so that it is clear what contributions of each person are.
• At the end of the lab each group should send me the results of their in-class work. Please indicate if this is your final submission by marking it in the subject as "3501 Lab 1 Final" or "3501 Lab 1 Not Final". Don't forget to answer all the questions below in your final submission.
• If your submission at the end of the lab time was not final, please send me (CC to the lab partner(s)) a final copy before the due time. Please use the subject "3501 Lab 1 Final".

#### Lab assignment

30 points.

Work in pairs.

Design and implement an algorithm for finding all combinations of the first n integers.
Input: n.
Output: all possible combinations of numbers from 1 to n written one combination per line without repetitions (in any order).
Example: input: n = 3. Output:

``````
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
```
```

Time your program using Java time function (time in milliseconds):

``````
long time = System.currentTimeMillis();
```
```

gives you the current time in milliseconds. Measure it before and after printing the output, subtract the two numbers, and print the result.

If you are using another programming language, please time your program with the time function provided by the language or by the system `time` command.

Your solution should not store all possible combinations of n-1 numbers in memory in order to generate all combinations of n. Think of ways to avoid large amounts of storage. Recursion may be a good direction to explore.
Keep in mind that your program should work correctly no matter what n is.

Make sure to test your program thoroughly, in particular make sure that all the combinations are printed with no duplicates. Figure out on paper how many lines you would have for n=4 and n=5, then check that you are getting the right number of lines (a counter may be useful here so that you don't need to count by hand). Make sure to submit your computation for the number of lines.

What is the efficiency of your algorithm in the number of printed lines as a function of n? Give your answer as Big-O, explain how you computed it.

Based on your timing results and on the Big-O approximation please estimate how long your program would run for n = 15 and for n = 20. Show how you have computed the estimate.

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.