Lecture Notes
CMSC 251
Lecture 19: Second Midterm Exam
(Tuesday, April 7, 1998)
Second midterm exam today. No lecture.
Lecture 20: Introduction to Graphs
(Thursday, April 9, 1998)
Read: Sections 5.4, 5.5. Graph Algorithms: For the next few weeks, we will

Lecture Notes
CMSC 251
Using Decision Trees for Analyzing Sorting: Consider any sorting algorithm. Let T (n) be the maximum number of comparisons that this algorithm makes on any input of size n. Notice that the running time fo the algorithm must be at le

Lecture Notes
CMSC 251
To bound this, recall the integration formula for bounding summations (which we paraphrase here). For any monotonically increasing function f (x)
b-1 b
f (i)
i=a a
f (x)dx.
The function f (x) = x ln x is monotonically increasing, a

Lecture Notes
CMSC 251
Heapify(A, 1, m)
/ fix things up
An example of HeapSort is shown in Figure 7.4 on page 148 of CLR. We make n 1 calls to Heapify, each of which takes O(log n) time. So the total running time is O(n 1) log n) = O(n log n).
Lecture 1

Lecture Notes
CMSC 251
A heap is represented as an left-complete binary tree. This means that all the levels of the tree are full except the bottommost level, which is lled from left to right. An example is shown below. The keys of a heap are stored in so

Lecture Notes
CMSC 251
Divide-and-conquer: Understand how to design algorithms by divide-and-conquer. Understand the divide-and-conquer algorithm for MergeSort, and be able to work an example by hand. Also understand how the sieve technique works, and how

Lecture Notes
CMSC 251
Since n/5 and 3n/4 are both less than n, we can apply the induction hypothesis, giving T (n) c = 3n 1 3 n +c + n = cn + 5 4 5 4 19 19c cn + n = n +1 . 20 20 +n
This last expression will be cn, provided that we select c such that c (

Lecture Notes
CMSC 251
Finally, in the recurrence T (n) = 4T (n/3) + n (which corresponds to Case 1), most of the work is done at the leaf level of the recursion tree. This can be seen if you perform iteration on this recurrence, the resulting summation i

Lecture Notes
CMSC 251
We have this messy summation to solve though. First observe that the value n remains constant throughout the sum, and so we can pull it out front. Also note that we can write 3i /4i and (3/4)i . T (n) = nlog4 3 + n
(log4 n)-1 i=0
3

Lecture Notes
CMSC 251
Analysis: What remains is to analyze the running time of MergeSort. First let us consider the running time of the procedure Merge(A, p, q, r). Let n = r - p + 1 denote the total length of both the left and right subarrays. What is t

Lecture Notes
CMSC 251
Lecture 6: Divide and Conquer and MergeSort
(Thursday, Feb 12, 1998)
Read: Chapt. 1 (on MergeSort) and Chapt. 4 (on recurrences). Divide and Conquer: The ancient Roman politicians understood an important principle of good algorithm

Lecture Notes
CMSC 251
Lecture 5: Asymptotics
(Tuesday, Feb 10, 1998)
Read: Chapt. 3 in CLR. The Limit Rule is not really covered in the text. Read Chapt. 4 for next time. Asymptotics: We have introduced the notion of () notation, and last time we gave a

Lecture Notes
CMSC 251
In summary, there is no one way to solve a summation. However, there are many tricks that can be applied to either nd asymptotic approximations or to get the exact solution. The ultimate goal is to come up with a close-form solution

Lecture Notes
CMSC 251
high school algebra. If c is a constant (does not depend on the summation index i) then
n n n n n
cai = c
i=1 i=1
ai
and
i=1
(ai + bi ) =
i=1
ai +
i=1
bi .
There are some particularly important summations, which you should probably

Lecture Notes
CMSC 251
Lecture 2: Analyzing Algorithms: The 2-d Maxima Problem
(Thursday, Jan 29, 1998)
Read: Chapter 1 in CLR. Analyzing Algorithms: In order to design good algorithms, we must first agree the criteria for measuring algorithms. The emphas

Lecture Notes
CMSC 251
CMSC 251: Algorithms1 Spring 1998 Dave Mount Lecture 1: Course Introduction
(Tuesday, Jan 27, 1998)
Read: Course syllabus and Chapter 1 in CLR (Cormen, Leiserson, and Rivest). What is algorithm design? Our text denes an algorithm to

Lecture Notes
CMSC 251
p x <x q p x qs swap <x x q >= x ? >= x s ?
r Intermediate configuration r Initial configuration
Final configuration
Figure 14: Partitioning intermediate structure. all of the elements have been processed. To nish things off we swap

CMSC 351:Fall 2011
Clyde Kruskal
Information CMSC351
Quadratic Formula.
2
ax + bx + c = 0
x=
b
b2 4ac
2a
Logarithms.
a = blogb a
logb a =
logc (ab) = logc a + logc b
1
logb (1/a) = logb a
logb a =
loga b
logc a
logc b
Stirlings Formula.
n!
n
n
e
logb an

Fall 2011
CMSC 351: Homework 11
Clyde Kruskal
Due at the start of class, Wednesday, December 7, 2011.
Problem 1. Indicate, for each pair of expressions (A, B ) in the table below, whether
A is O,o, , or of B . Your answer should be in the form of the tabl

Fall 2011
CMSC 351: Homework 10
Clyde Kruskal
Due at the start of class Wednesday, November 30, 2011.
Problem 1. Assume that G = (V, E ) is a weighted graph where each edge has nonnegative integer
weight. Let c be the maximum weight of any edge in the gra

Fall 2011
CMSC 351: Homework 9
Clyde Kruskal
Due at the start of class Wednesday, November 16, 2011.
Problem 1. The square of a directed graph G = (V, E ) is the graph G2 = (V, E 2 ) such
that (x, y ) E 2 if and only if for some z V , both (x, z ) E and (

Fall 2011
CMSC 351: Homework 8
Clyde Kruskal
Due at the start of class Wednesday, November 9, 2011.
Problem 1. Illustrate the operation of radix sort on the following list of English words:
ilk, elf, jig, leg, elk, g, ill, gel, eke, egg, gig, ell
Problem

Fall 2011
CMSC 351: Homework 7
Clyde Kruskal
Due at the start of class Wednesday, October 26, 2011.
Problems (1), (2), and (3) deal with merging two lists each of size two: (A < B ) and (C < D).
There are six possible nal orderings: (A < B < C < D), (A <

Fall 2011
CMSC 351: Homework 6
Clyde Kruskal
Due at the start of class Wednesday, October 19, 2011.
Problem 1. Consider an array of size eight with the numbers 50, 70, 10, 20, 60, 40, 80, 30. Assume
you execute quicksort using the version of partition fro

Fall 2011
CMSC 351: Homework 5
Clyde Kruskal
Due at the start of class, Wednesday, October 12, 2011.
Problem 1.
Consider the sum
n
(k 2 2k ) .
k=1
Assume that you guess it is a cubic polynomial an3 + bn2 + cn + d. Use constructive
induction to justify you

Fall 2011
CMSC 351: Homework 4
Clyde Kruskal
Due at the start of class Wednesday, October 5, 2011.
Problem 1. Consider an array of size eight with the numbers in the following order
40, 20, 80, 60, 30, 10, 70, 50.
(a) Form the heap using the standard (Wil

Fall 2011
CMSC 351: Homework 3
Clyde Kruskal
Due at the start of class Wednesday, September 28, 2011.
Problem 1.
(a) Use the iteration method to solve the following recurrence exactly:
T (n) = 5T (n/2) + 2n 1
where T (1) = 7. Assume n is a power of 2.
(b)

Fall 2011
CMSC 351: Homework 2
Clyde Kruskal
Due at the start of class Wednesday, September 21, 2011.
The following algorithm for sorting is called slowsort:
i1
while i < n do
if A[i] > A[i+1] then
A[i] A[i+1]
i1
else
ii+1
end if
end while
Problem 1.
(a)

Fall 2011
Clyde Kruskal
CMSC 351: Homework 0
Do this homework, but do not hand in.
Problem 1. Use mathematical induction to show that
n
n(n + 1)(n + 2)
i(i + 1) =
(a)
3
i=1
n
Problem 2. See bottom of this sheet.
(a) Assume bx = a. What is x (in terms of a