Shortest Paths
Input: weighted, directed graph G = (V, E), with weight function w :
E R.
The weight of path p =< v0, v1, . . . , vk > is the sum of the weights of its
constituent edges:
w(p) =
k
i=1
w(vi1, vi) .
The shortest-path weight from u to v is
CS4231: Analysis of Algorithms, I
Midterm Exam Solutions, Thursday October 27, 2016
Problem 1 [18 points, 6 points per part] (Graded by Srikar)
Give asymptotic solutions T(n) =(f(n) for the following recurrences. Assume that T(n)
is constant for sufficien
Midterm Exam Guidelines
The midterm exam will be held on Thursday October 27, at 11:40-12:55.
Students in Section 001 will take the exam in the usual classroom, Pupin 428.
Students in Section H01 will take the exam in Pupin 420.
Make sure you arrive early
Randomization
CS 4231, Fall 2016
Mihalis Yannakakis
1
Randomized algorithms
Make random choices (coin flips, random numbers .)
Different random choices are assumed independent
Outcome of algorithm and running time depends on
random choices (besides inp
Selection
CS 4231, Fall 2016
Mihalis Yannakakis
1
Selection (Order Statistics)
Input: Set A of n numbers (or more
generally, elements from an ordered
domain), number i, 1in
Output: i-th smallest (rank i) element of A
i=1:
minimum
i=n:
maximum
i=(n+1)
CS4231: Analysis of Algorithms, I
Midterm Solutions, Tuesday October 27, 2015
Problem 1 [18 points, 6 points per part]
Give asymptotic solutions T(n) =(f(n) for the following recurrences. Assume that T(n)
is constant for sufficiently small n. State in eac
CSOR W4231-2: Midterm Practice Problems
Fall 2016
These problems are ungraded, and are intended as a study aid. Solutions will also be posted
on canvas. They are very similar to the problems that will appear on the midterm. The
midterm is closed book, clo
Depth First Search
Acyclicity
Graph Components
CS 4231, Fall 2015
Mihalis Yannakakis
1
Depth-First Search from a source s
R
s
N-R
u
v
Policy: Choose edge (u,v) from R (reached nodes) to N-R
(unreached) where u is latest node added to reachable
set R
Can
Dynamic Programming
CS 4231, Fall 2016
Mihalis Yannakakis
1
Dynamic Programming
General method that applies to a class of problems, often
a class of optimization problems (but not only
optimization)
Problem reduced to smaller (possibly overlapping)
proble
Counting Sort, Radix Sort,
Bucket Sort
CS 4231, Fall 2016
Mihalis Yannakakis
1
Counting Sort
Restricted domain: D=cfw_1,k
Idea: Count how many input elements for each i in D
Example: Input A = [ 1, 2, 1, 3, 2, 5, 3, 2, 5]
Counts: C(1)=2, C(2)=3, C(3)=2,
CSOR W4231-2: Midterm Practice Problems
Fall 2016
These problems are ungraded, and are intended as a study aid. Solutions will also be posted
on canvas. They are very similar to the problems that will appear on the midterm. The
midterm is closed book, clo
How do you model the future?
Stochastic
approach: The future can be
modeled as a distribution over possible
events.
Very successful in many contexts.
Alternative:
Think of the future as an
adversary, do well against all possible
future outcomes.
Toy Ex
NP-Completeness
Goal: We want some way to classify problems that are hard to solve, i.e.
problems for which we can not nd polynomial time algorithms.
For many interesting problems
we cannot nd a polynomial time algorithm
we cannot prove that no polynomi
Graphs
Graph G = (V, E) has vertices (nodes) V and edges (arcs) E.
Graph can be directed or undirected
Graph can represent any situation with objects and pairwise relationships.
1
2
3
4
Representations
1
2
3
4
Adjacency Matrix
1
2
3
4
1
0
1
1
1
2
1
0
0
Randomized Selection
Same start as for deterministic selection
Select(A,i,n)
1
2
if (n = 1)
then return A[1]
3
4
5
p = median(A)
6
L = cfw_x A : x p
H = cfw_x A : x > p
7
8
9
if i |L|
then Select(L, i, |L|)
else Select(H, i |L|, |H|)
Choose pivot p random
Human Codes
Coding is the problem of representing data in another representation.
Typically, we want that representation to be concise. We will encode in
binary in this lecture. We call the encoding of a character a codeword .
Dierent types of codes
xed
Quicksort
Quicksort(A, p, r) 1 2 3 4 if p < r then q Partition(A, p, r) Quicksort(A, p, q - 1) Quicksort(A, q + 1, r)
Partition(A, p, r) 1 2 3 4 5 6 7 8 9 10 y random(p, r) Exchange A[y] and A[r] x A[r] ip-1 for j p to r - 1 do if A[j] x then i i + 1 exch
Randomization in Algorithms
Randomization is a tool for designing good algorithms.
Two kinds of algorithms
Las Vegas - always correct, running time is random.
Monte Carlo - may return incorrect answers, but running time is
deterministic.
Hiring Proble
Dynamic Programming
Wed like to have generic algorithmic paradigms for solving problems Example: Divide and conquer
Break problem into independent subproblems Recursively solve subproblems (subproblems are smaller instances of main problem) Combine solut