COMP 3711 Design and Analysis of Algorithms
Solutions to Assignment 3
1. Define c[i] to be the length of the longest increasing subsequence that ends at xi . Note
that the length of the longest increasing subsequence in X is max1in c[i]. The longest
incre
COMP3711: Design and Analysis of Algorithms
Tutorial 4
HKUST
Tutorial 4
COMP3711: Design and Analysis of Algorithms
Question 1
Recall the randomized selection algorithm. We pick a pivot
randomly, divide the array into 3 segments: left, pivot, and right,
a
COMP3711: Design and Analysis of Algorithms
Tutorial 4
HKUST
Tutorial 4
COMP3711: Design and Analysis of Algorithms
Question 1
Recall the randomized selection algorithm. We pick a pivot
randomly, divide the array into 3 segments: left, pivot, and right,
a
COMP 3711 Design and Analysis of Algorithms
Fall 2015
Assignment 3
1. Give an O(n2 )-time dynamic programming algorithm to find the longest monotonically increasing subsequence of a sequence of n numbers (i.e, each successive number
in the subsequence is
COMP3711: Design and Analysis of Algorithms
Tutorial 6
HKUST
Tutorial 6
COMP3711: Design and Analysis of Algorithms
Question 1
Consider the problem of making change for n cents using the fewest
number of coins. Assume that each coins value is an integer.
COMP 3711 Design and Analysis of Algorithms
Spring 2016
Written 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
COMP 3711 Design and Analysis of Algorithms
Lecture 1: Introduction
1
So, where are we so far?
Advanced Topics:
Database, Networking, Graphics, AI, Data Mining, Machine Learning,
COMP 3111:
Software Engineering
COMP 2012:
OOP and Data structures
COMP 371
Lecture 4: Integer and Matrix Multiplication
More complicated examples of divide-and-conquer
Integer Arithmetic
Add. Given two -bit integers and , compute + .
() time
Multiply. Given two -bit integers and , compute .
Primary school method: (2 ) time.
1
A
Lecture 7: Heaps and Heapsort
An ( log )-time in-place sorting algorithm
(Binary) Heap
Structure of a heap: An almost complete binary tree
All levels are full except possibly the lowest level
If the lowest level is not full, then nodes must be packed to t
COMP 3711:Fall 2016
COMP 3711: Mathematical Background
Common Log Identities:
log(a b) = log a + log b
log(ab ) = b log a
aloga b = b
alogb c = clogb a
logb n
= (log n)
loga n =
logb a
log(n!) = (n log n)
Common Summations: Let c = 1 be any positive const
COMP3711: Design and Analysis of Algorithms
Tutorial 8
HKUST
Tutorial 8
COMP3711: Design and Analysis of Algorithms
Question 1
A string of parentheses is said to be balanced if the left- and
right-parentheses in the string can be paired off properly. For
COMP3711: Design and Analysis of Algorithms
Tutorial 10
HKUST
Tutorial 10
COMP3711: Design and Analysis of Algorithms
Question 1
Let G be a connected undirected graph with distinct weights on the
edges, and let e be an edge of G . Suppose e is the largest
COMP 3711
Midterm review
Course Intended Learning Outcomes
1. Describe fundamental concepts and techniques for determining the
asymptotic behavior of real-valued functions defined in natural numbers.
2. Explain recurrence equations and solve common recurr
COMP 3711 Design and Analysis of Algorithms
Fall 2015
Programming Assignment 1
Implement bubble sort, insertion sort, and quicksort in your favourite programming language, and perform the following experiments.
1. Generate an array of n = 100, 000 random
COMP3711: Design and Analysis of Algorithms
Tutorial 11
HKUST
Tutorial 11
COMP3711: Design and Analysis of Algorithms
Question 1
Show the execution of the Edmonds-Karp algorithm on the
following flow network.
Edmonton
v1
Vancouver
Saskatoon
v3
12
16
s
20
Lecture 9: Greedy Algorithms
A greedy algorithm always makes the choice that looks best at
the moment and adds it to the current partial solution.
Greedy algorithms dont always yield optimal solutions, but when
they do, theyre usually the simplest and mos
Lecture 5: Randomized Algorithms
The Hiring Problem
Hire-Assistant():
0
for 1 to
interview candidate
if candidate is better than then
fire
hire candidate
Problem: Whats the number of hires?
Worst case: , which happens when you interview the candida
Lecture 3: The Maximum Subarray Problem
Divide-and-Conquer
Divide-and-conquer.
Break up problem into several parts.
Solve each part recursively.
Combine solutions to sub-problems into overall solution.
Most common pattern.
1
Break up problem of size into
Lecture 2: Merge sort
A quick review of recursion and recurrences
Classical example: Tower of Hanoi
Goal: Move discs from peg A to peg C
One disc at a time
Cant put a larger disc on top of a smaller one
MoveTower(, peg1, peg2, peg3):
if = 1 then
move the
Lecture 4: Integer and Matrix Multiplication
More complicated examples of divide-and-conquer
Integer Arithmetic
Add. Given two -bit integers and , compute + .
() time
Multiply. Given two -bit integers and , compute .
Primary school method: (2 ) time.
1
A
COMP 3711 Design and Analysis of Algorithms
Fall 2015
Written 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.
COMP 3711 Design and Analysis of Algorithms
2015 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).
(
COMP 3711 Design and Analysis of Algorithms
2015 Fall
Solutions to Assignment 2
1. (a) O(n log n)
(b) Build the heap: O(n). Call Extract-Min k times: O(k log n). Total time: O(n +
k log n).
(c) Build the heap with repeated insertions: O(n log n). Call Ext
Lecture 13: Dynamic Programming
over Intervals
1
Longest Palindromic Substring
Def: A palindrome is a string that reads the same backward or forward.
Ex:
radar, level, racecar, madam
A man, a plan, a canal Panama! (ignoring space, punctuation, etc.)
Probl
Lecture 14: Introduction to Graphs
The Seven Bridges of Knigsberg
Q: Can you find a path to cross all seven bridges, each exactly once?
Q: (Reformulated as a graph problem) Can you find a path in the graph
that includes every edge exactly once?
A: Not pos
Lecture 10: Huffman Coding
Encoding
Frequency (in thousands)
Fixed-length codeword
Variable-length codeword
a
b
c
d
e
f
45
000
13
001
12
010
16
011
9
100
5
101
0
101
100
111 1101 1100
Encoding: Replace characters by corresponding codewords.
Q: How to desi
Lecture 11: Dynamic Programming
First Example: Stairs Climbing
Problem: Suppose you can take 1 or 2 stairs with one step. How many
different ways can you climb stairs?
Solution: Let () be the number of different ways to climb stairs.
1 = 1, 2 = 2, 3 = 3,
Lecture 12: 2D Dynamic Programming
The 0/1 Knapsack Problem
Input: A set of items, where item has weight and value , and a
knapsack with capacity .
Goal: Find 1 , , cfw_0,1 such that
maximized.
=1
and
=1
is
Recall: Greedy doesnt work
2
The Recurrence
De
Lecture 8: Sorting in Linear Time
Running time of sorting algorithms
Do you still remember what these statements mean?
Sorting algorithm A runs in ( log ) time.
Sorting algorithm A runs in ( log ) time.
So far, all algorithms have running time ( log )
We
COMP3711 HW2
Ching-Heng, WANG 20152407 [email protected]
Question 1.
1.Sort all n numbers, and output the k smallest numbers in sorted order.
To minimize the runtime: Using merge sort, in worst case = O(n log n).
Getting the first k element K
Complexity: O(n
(COMP152)[2010](s)midterm~1980^_10146.pdf downloaded by slliac from http:/petergao.net/ustpastpaper/down.php?course=COMP152&id=9 at 2016-11-03 20:20:18. Academic use within HKUST only.
COMP 152
Spring 2010
Midterm Solutions
1. (a) 10 20
10 10
20 20
(b) x[
Lecture 4: Integer and Matrix Multiplication
More complicated examples of divide-and-conquer
Revision of January 30, 2017
Integer Arithmetic
Add. Given two -bit integers and , compute + .
() time
Multiply. Given two -bit integers and , compute .
Primary s