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
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
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
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
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 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
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 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 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 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
COMP 3711 Design and Analysis of Algorithms
Fall 2015 Assignment 4
1. Let G be a connected undirected graph with weights on the edges. Assume that all the edge
weights are distinct. Let ei be the edge with the i-th smallest weight. Does the MST have to
co
COMP 3711 Design and Analysis of Algorithms
2015 Fall Semester
Solutions to Assignment 4
1. e1 and e2 must always be in the MST, while e3 may not. For the counter example on e3 ,
simply consider a triangle with three edges. The MST only contains e1 and e2
COMP3711: Design and Analysis of Algorithms
Tutorial 5
HKUST
Tutorial 5
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
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.
COMP3711: Design and Analysis of Algorithms
Tutorial 9
HKUST
Tutorial 9
COMP3711: Design and Analysis of Algorithms
Question 1
The adjacency list representation of a graph G , which has 7
vertices and 10 edges, is:
a : d, e, b, g
c : f, e, b, d
e : a, c,
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
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
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
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
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
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
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
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 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
COMP 3711 Design and Analysis of Algorithms (Fall 2015)
Written 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 a
COMP3711 PA2
Name: ZHAO Lucen
Student ID: 20256435
Section: L1
The size of encoded message for uniform distribution is: 66098 bits
The size of encoded message for non-uniform distribution is: 52123 bits
/The code is attached on the next page.
/*
* PA2.cpp
Lecture 3: The Maximum Subarray Problem
Version of September 12, 2016
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
Lecture 4: Integer and Matrix Multiplication
More complicated examples of divide-and-conquer
Revision of September 14b, 2016
Integer Arithmetic
Add. Given two -bit integers and , compute + .
() time
Multiply. Given two -bit integers and , compute .
Primar
COMP 3711 Design and Analysis of Algorithms
Lecture 1: Introduction
Version of September 6, 2016
1
Where are we?
Advanced Topics:
Database, Networking, Graphics, AI, Data Mining, Machine Learning,
COMP 3111:
Software Engineering
COMP 2012:
OOP and Data s
Lecture 2: Merge sort
Version of Sept 7, 2016
Divide-and-Conquer intro: Binary search
Main idea of DaC: Solve a problem of size n by breaking into smaller
problems of size less than n.
Example: Binary Search
Input: An array of elements in sorted order, an