The Hong Kong University of Science and Technology
Algorithm Design and Analysis
COMP 3711

Spring 2014
COMP 3711 Design and Analysis of Algorithms
Fall 2014 Final Exam
1. Time Complexity (8 pts)
We have two algorithms, A and B. Let TA (n) and TB (n) denote the time complexities
of algorithm A and B respectively, with respect to the input size n. Below ther
The Hong Kong University of Science and Technology
Algorithm Design and Analysis
COMP 3711

Fall 2014
COMP 3711 Design and Analysis of Algorithms
2014 Fall
Solutions to Assignment 1
1. (a) A = (B);
(b) A = O(B), A = (B), A = (B);
(c) A = O(B);
(d) A = O(B), A = (B), A = (B);
(e) A = (B).
2. (a) T (n) = O(log n).
(b) T (n) = O(n2).
(c) T (n) = O(log n).
(d
The Hong Kong University of Science and Technology
Algorithm Design and Analysis
COMP 3711

Fall 2014
COMP 3711 Design and Analysis of Algorithms
Fall 2014
Assignment 1
1. For each pair of expressions (A, B) below, indicate whether A is O, , or of B. Note
that zero, one, or more of these relations may hold for a given pair. List all correct
ones. It often
The Hong Kong University of Science and Technology
Algorithm Design and Analysis
COMP 3711

Fall 2014
COMP 3711 Design and Analysis of Algorithms
Fall 2014
Assignment 3
1. Lets consider a long river, along which n houses are scattered. You can think of
this river as an axis, and the houses are given by their coordinates on this axis in
a sorted order. You
The Hong Kong University of Science and Technology
Algorithm Design and Analysis
COMP 3711

Fall 2014
COMP3711: Design and Analysis of Algorithms
Tutorial 12
1/6
COMP3711: Design and Analysis of Algorithms
Tutorial 12
Tutorial 12 ()
COMP3711: Design and Analysis of Algorithms
1/6
COMP3711: Design and Analysis of Algorithms
Tutorial 12
2/6
Question 1
Suppo
The Hong Kong University of Science and Technology
Algorithm Design and Analysis
COMP 3711

Fall 2014
COMP3711: Design and Analysis of Algorithms
Tutorial 2
HKUST
Tutorial 2
COMP3711: Design and Analysis of Algorithms
Question 1
Give asymptotic upper bounds for T (n) by recursion tree
approach. Make your bounds as tight as possible.
(a)
T (1) = 1
T (n) =
The Hong Kong University of Science and Technology
Algorithm Design and Analysis
COMP 3711

Fall 2014
COMP3711: Design and Analysis of Algorithms
Tutorial 3
HKUST
Tutorial 3
COMP3711: Design and Analysis of Algorithms
Question 1
Given a sorted array A[1.n] of n distinct integers (positive or
negative), give an algorithm to nd the index i such that A[i] =
The Hong Kong University of Science and Technology
Algorithm Design and Analysis
COMP 3711

Fall 2014
COMP3711: Design and Analysis of Algorithms
Tutorial 5
HKUST
Tutorial 5
COMP3711: Design and Analysis of Algorithms
Question 1
Given n elements, we have discussed an O(n log n) time algorithm
to build a minheap of these n elements. Can you design an
O(n)
The Hong Kong University of Science and Technology
Algorithm Design and Analysis
COMP 3711

Fall 2014
COMP3711: Design and Analysis of Algorithms
Tutorial 4
HKUST
Tutorial 4
COMP3711: Design and Analysis of Algorithms
Question 1
Let A[1 . . . n] be an array of n distinct numbers. A pair (A[i], A[j])
is said to be an inversion if these numbers are out of o
The Hong Kong University of Science and Technology
Algorithm Design and Analysis
COMP 3711

Spring 2014
Overview of Materials After Midter
m
1
Greedy Algorithms
General idea: make the choice that looks best now with
out thinking too much about the future
Examples
Sometimes it produces the optimal solution
Interval scheduling
Interval partitioning
Fractional
The Hong Kong University of Science and Technology
Algorithm Design and Analysis
COMP 3711

Fall 2014
COMP3711: Design and Analysis of Algorithms
Tutorial 1
COMP3711: Design and Analysis of Algorithms
Tutorial 1
Tutorial 1
COMP3711: Design and Analysis of Algorithms
1/15
COMP3711: Design and Analysis of Algorithms
Tutorial 1
Asymptotic notations
Asymptoti
The Hong Kong University of Science and Technology
Algorithm Design and Analysis
COMP 3711

Fall 2014
COMP3711: Design and Analysis of Algorithms
Tutorial 10
HKUST
Tutorial 10
COMP3711: Design and Analysis of Algorithms
Question 1
Let G = (V , E ) be an undirected graph where V is the set of
vertices and E is the set of edges.
a) What is the maximum numbe
The Hong Kong University of Science and Technology
Algorithm Design and Analysis
COMP 3711

Fall 2014
COMP3711: Design and Analysis of Algorithms
Tutorial 11
Tutorial 11
COMP3711: Design and Analysis of Algorithms
Question 1
Let G = (V , E ) be a connected undirected graph with weights on
the edges. Assume that all the edge weights are distinct.
Let C be
The Hong Kong University of Science and Technology
Algorithm Design and Analysis
COMP 3711

Fall 2014
COMP 3711 Design and Analysis of Algorithms
Fall 2014
Assignment 2
1. Smallest k numbers in sorted order
Given a set of n numbers, we wish to nd the k smallest numbers in sorted order using a
comparison based algorithm. Below are some possible algorithms
The Hong Kong University of Science and Technology
Algorithm Design and Analysis
COMP 3711

Fall 2014
COMP3711: Design and Analysis of Algorithms
Tutorial 6
HKUST
Tutorial 6
COMP3711: Design and Analysis of Algorithms
Question 1
Design an O(n log k)time algorithm to merge k sorted lists into
one sorted list by making use of priority queues, where n is th
The Hong Kong University of Science and Technology
Algorithm Design and Analysis
COMP 3711

Fall 2014
COMP3711: Design and Analysis of Algorithms
Tutorial 7
HKUST
Tutorial 7
COMP3711: Design and Analysis of Algorithms
Question 1
Show the steps of inserting 2,1,4,5,9,3,6,7 into an initially empty
AVL tree.
Tutorial 7
COMP3711: Design and Analysis of Algori
The Hong Kong University of Science and Technology
Algorithm Design and Analysis
COMP 3711

Fall 2014
COMP 3711 Design and Analysis of Algorithms
Fall 2014 Midterm Exam Solutions
1. 1.1 (a) (b), (a) (c), (d) (a), (a) (e), (b) (c), (d) (b), (d) (c), (d) (e)
1.2 correct comparisonbased sorting algorithm, c > 0, n0 > 0, such that n > n0 ,
an array of n ele
The Hong Kong University of Science and Technology
Algorithm Design and Analysis
COMP 3711

Fall 2014
COMP3711: Design and Analysis of Algorithms
Tutorial 6
HKUST
Tutorial 6
COMP3711: Design and Analysis of Algorithms
Question 1
Design an O(n log k)time algorithm to merge k sorted lists into
one sorted list by making use of priority queues, where n is th
The Hong Kong University of Science and Technology
Algorithm Design and Analysis
COMP 3711

Fall 2014
COMP3711: Design and Analysis of Algorithms
Tutorial 4
HKUST
Tutorial 4
COMP3711: Design and Analysis of Algorithms
Question 1
Let A[1 . . . n] be an array of n distinct numbers. A pair (A[i], A[j])
is said to be an inversion if these numbers are out of o
The Hong Kong University of Science and Technology
Algorithm Design and Analysis
COMP 3711

Fall 2014
COMP3711: Design and Analysis of Algorithms
Tutorial 5
HKUST
Tutorial 5
COMP3711: Design and Analysis of Algorithms
Question 1
Given n elements, we have discussed an O(n log n) time algorithm
to build a minheap of these n elements. Can you design an
O(n)
The Hong Kong University of Science and Technology
Algorithm Design and Analysis
COMP 3711

Fall 2014
COMP3711: Design and Analysis of Algorithms
Tutorial 3
HKUST
Tutorial 3
COMP3711: Design and Analysis of Algorithms
Question 1
Given a sorted array A[1.n] of n distinct integers (positive or
negative), give an algorithm to nd the index i such that A[i] =
The Hong Kong University of Science and Technology
Algorithm Design and Analysis
COMP 3711

Fall 2014
COMP3711: Design and Analysis of Algorithms
Tutorial 12
1/23
COMP3711: Design and Analysis of Algorithms
Tutorial 12
Tutorial 12 ()
COMP3711: Design and Analysis of Algorithms
1 / 23
COMP3711: Design and Analysis of Algorithms
Tutorial 12
2/23
Question 1
The Hong Kong University of Science and Technology
Algorithm Design and Analysis
COMP 3711

Fall 2014
COMP3711: Design and Analysis of Algorithms
Tutorial 9
Question 1
Subsets of
mutually
compatibles
jobs:
cfw_a, g, cfw_b, e, h,
cfw_c, f, .
Solution 1
Sorting the n jobs based on fi needs O(nlog n) time
Solution 1 cont
Sort ob finish time: b, c, a, e, d, f
The Hong Kong University of Science and Technology
Algorithm Design and Analysis
COMP 3711

Fall 2014
COMP3711: Design and Analysis of Algorithms
Tutorial 10
HKUST
Tutorial 10
COMP3711: Design and Analysis of Algorithms
Question 1
Let G = (V , E ) be an undirected graph where V is the set of
vertices and E is the set of edges.
a) What is the maximum numbe
The Hong Kong University of Science and Technology
Algorithm Design and Analysis
COMP 3711

Fall 2014
COMP3711: Design and Analysis of Algorithms
Tutorial 1
COMP3711: Design and Analysis of Algorithms
Tutorial 1
Tutorial 1
COMP3711: Design and Analysis of Algorithms
1/9
COMP3711: Design and Analysis of Algorithms
Tutorial 1
Asymptotic notations
Asymptotic
The Hong Kong University of Science and Technology
Algorithm Design and Analysis
COMP 3711

Fall 2014
COMP3711: Design and Analysis of Algorithms
Tutorial 11
Tutorial 11
COMP3711: Design and Analysis of Algorithms
Question 1
Let G = (V , E ) be a connected undirected graph with weights on
the edges. Assume that all the edge weights are distinct.
Let C be
The Hong Kong University of Science and Technology
Algorithm Design and Analysis
COMP 3711

Fall 2014
COMP3711: Design and Analysis of Algorithms
Tutorial 2
HKUST
Tutorial 2
COMP3711: Design and Analysis of Algorithms
Question 1
Give asymptotic upper bounds for T (n) by recursion tree
approach. Make your bounds as tight as possible.
(a)
T (1) = 1
T (n) =
The Hong Kong University of Science and Technology
Algorithm Design and Analysis
COMP 3711

Fall 2014
COMP 3711 Design and Analysis of Algorithms
2014 Fall Semester
Solutions to Assignment 3
1. The algorithm: Put the rst base station at x + 4 where x is the coordinate of the rst
house. Remove all the houses that are covered and then repeat if there are st
The Hong Kong University of Science and Technology
Algorithm Design and Analysis
COMP 3711

Fall 2014
COMP 3711 Design and Analysis of Algorithms
Solutions to Assignment 4
1. This problem is similar to the 01 Knapsack problem, except that there is value that
we want to optimize. We just want to check if it possible to fully pack the knapsack.
Thus we use
The Hong Kong University of Science and Technology
Algorithm Design and Analysis
COMP 3711

Spring 2014
COMP3711: Design and Analysis of Algorithms
Tutorial 10
HKUST
Tutorial 10
COMP3711: Design and Analysis of Algorithms
Question 1
Let G = (V , E ) be a connected undirected graph in which all
edges have weight either 1 or 2.
Give an O(V  + E ) algorith
The Hong Kong University of Science and Technology
Algorithm Design and Analysis
COMP 3711

Spring 2014
COMP3711: Design and Analysis of Algorithms
Tutorial 9
HKUST
Tutorial 9
COMP3711: Design and Analysis of Algorithms
Question 1
Let G = (V , E ) be a connected undirected graph. Prove that
log(E ) = (log V ).
Note: we implictly use this fact in many of our
The Hong Kong University of Science and Technology
Algorithm Design and Analysis
COMP 3711

Spring 2014
COMP 3711 Fall 2016
Midterm Exam Revision
(See handout on web page
for full details)
3. Heaps [8 pts]
A new node was added to hold the 7
and it was then bubbled up to
its proper place.
7 and the items that were moved are
in bold.
2 was removed, 25 was
mov
The Hong Kong University of Science and Technology
Algorithm Design and Analysis
COMP 3711

Spring 2014
COMP3711: Design and Analysis of Algorithms
Tutorial 8
Revised Nov 4, 2016
HKUST
Tutorial 8 Revised Nov 4, 2016
COMP3711: Design and Analysis of Algorithms
Question 1
Let G = (V , E ) be an undirected graph where V is the set of
vertices and E is the set
The Hong Kong University of Science and Technology
Algorithm Design and Analysis
COMP 3711

Spring 2014
COMP3711: Design and Analysis of Algorithms
Tutorial 1
COMP3711: Design and Analysis of Algorithms
Tutorial 1
Tutorial 1
COMP3711: Design and Analysis of Algorithms
1/15
COMP3711: Design and Analysis of Algorithms
Tutorial 1
Asymptotic notations
Asymptoti