COSC3320 Homework 1 Spr2025
COSC3320 Homework 1 Spr2025
Homework 1
COSC 3320
Algorithms and Data Structures
Due: Sunday, February 16, 2025
11:59 PM
Instructions
Answer the questions below. Your submission must be typed. We prefer you use LATEX to type
your solutions — LATEX is the standard way to type works in mathematical sciences, like computer
science, and is highly recommended; for more information on using LATEX, please see this post on
Overleaf — but any method of typing your solutions (e.g., MS Word, Google Docs, Markdown) is
acceptable. For exercises asking for a drawing or diagram (e.g. a graph), a hand-drawn figure is
acceptable. Your submission must be in pdf format.
By submitting this assignment, you affirm that you have followed the Academic
Honesty Policy. By submitting this assignment, you affirm that you have followed
the Academic Honesty Policy and the Honor Code as mentioned in the syllabus
document. All submitted work should be your own. Copying or using other people’s
work (including from the Web or using AI tools) will result in −MAX points, where
MAX is the maximum possible number of points for that assignment/homework/exam.
Repeat offenses will result in a failing grade for the course and will be reported to
the Chair. If you have any questions, please contact the professor and the TAs.
Reading
Sections 2.1, 2.2, 2.4, 2.5, 2.6, 3.1, 3.2, 4.1, 4.2, 4.3, 5.1, and 5.2 of the Algorithms textbook (https:
//sites.google.com/site/gopalpandurangan/home/algorithms-course). Please note that
the textbook might be updated periodically (mostly minor changes, correcting typos and such,
but, sometimes, more). So it is best to look at the textbook before each homework assignment.
Also, while reading if you find any typos or errors, feel free to post in piazza.
Several worked exercises with solutions are provided at the end of each chapter. Attempting
to solve the worked exercises before seeing their solutions is a good learning technique.
1 Exercises
Exercise 1 (20 Points)
Rank the listed functions by order of growth. That is, find an arrangement f1 , f2 , . . . , f8 of the
functions satisfying
f1 = O(f2 )
f2 = O(f3 )
..
.
f7 = O(f8 )
Give arguments that justify your ordering. Note that logk n is the usual way of writing (log n)k .
n 1
n2 , , n log n, (1.001)n , 2 , log100 n, n!, nlg lg n
log n n
There are n adults in town A and they all need to go to town B. There is only a single motorbike
available which is owned by two boys. The motorbike can carry exactly one adult or one or two
boys at a time. Show how all adults can be moved from town A to town B while, at the end,
leaving the motorbike with the two boys in town A.
Show the correctness of your algorithm by using mathematical induction and analyze the number
of trips needed. (Hint: Use decrease and conquer to reduce the problem size.)
You are given n stones (assume that n is a power of 2) each having a distinct weight. You are also
given a two-pan balance scale. Given two stones, the scale can determine which stone is heavier
and which is lighter, but cannot determine the actual weight of the stones. Give a divide and
conquer algorithm that uses only 3n2 − 2 weighings to find the heaviest and lightest stones and
prove that it uses that many weightings. Explain your algorithm and give pseudocode, proof of
correctness (using mathematical induction), and analysis of the weightings. (Hint: See Worked
Exercise 4.2.)
1
Exercise 5 (20 Points)
You are given an array A[1..n] consisting of n integers. Your goal is to output an array B[1..n]
such that B[i] is the number of elements in A[i + 1...n] that are smaller than A[i]. Describe an
O(n log n) time algorithm for the problem, show its correctness, and prove that it indeed takes
O(n log n) time. (Hint: Use the merging technique of MergeSort. See Worked Exercise 5.3 for a
similar problem.)
Given any positive number n, your task is to write a divide and conquer algorithm to output an
array A consisting of the first n numbers in a way that satisfies the following property: for every
pair of indices i and j (i < j), there is no index k such that i < k < j and 2A[k] = A[i] + A[j].
For example, if n = 3, then a valid answer is A = [1, 3, 2]. Note that 2 ∗ 3 ̸= 1 + 2. However,
A = [1, 2, 3] is not a valid answer, since 2 ∗ 2 = 1 + 3.
If n = 4, then a valid answer is A = [2, 1, 4, 3].
Explain your algorithm, argue its correctness, and analyze the running time of your algorithm.