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
Minimum Spanning Trees
G = (V, E) is an undirected graph with non-negative edge weights w : E Z +
We assume wlog that edge weights are distinct
A spanning tree is a tree with V 1 edges, i.e. a tree that connects
all the vertices.
The total cost (weigh
Maximum Flows
A ow network G = (V, E) is a directed graph in which each edge
(u, v) E has a nonnegative capacity .
If (u, v) E, we assume that c(u, v) = 0.
We distinguish two vertices in a ow network: a source s and a sink
t.
A ow in G is a real-valued
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
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
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
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)
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
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
All Pairs 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
Deterministic Selection
Select(A,i,n)
1
2
if (n = 1)
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|
Select(L, i, |L|)
else Select(H, i |L|, |H|)
Deterministic Selection (2)
Select(A,i,n)
1
2
if (n = 1)
return A
3
Sp
Basics of Algorithm Analysis
We measure running time as a function of n, the size of the input (in
bytes assuming a reasonable encoding).
We work in the RAM model of computation. All reasonable operations take 1 unit of time. (e.g. +, *, -, /, array acc
Sorting restricted ranges of numbers
If the range is restricted, we can sort using more than comparisons and
swaps.
Assume each of the n input elements is an integer in the range 1 . . . k .
Sorting restricted ranges of numbers
If the range is restrict
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
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
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
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
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
Greedy
Consider a set of requests for a room. Only one person can reserve the
room at a time, and you want to allow the maximum number of requests.
The requests for periods (si, fi) are:
(1, 4), (3, 5), (0, 6), (5, 7), (3, 8), (5, 9), (6, 10), (8, 11), (8
Amortized Analysis
Multipop(S, k) 1 2 3 while not Stack-Empty(S) and k = 0 do Pop(S) k k1
Binary Counter
Increment(A) 1 2 3 4 5 6 i0 while i < length[A] and A[i] = 1 do A[i] 0 ii+1 if i < length[A] then A[i] 1
Table Insert
Table-Insert(T, x) 1 2 3 4 5 6 7
Strongly Connected Components
Definition A strongly connected component of a directed graph G is a maximal set of vertices C V such that for every pair of vertices u and v, there is a directed path from u to v and a directed path from v to u. Strongly-Con