Algorithms Letures Part1
Algorithms Letures Part1
2
Brute Force Algorithms – Example
Traveling Salesman Problem (TSP)
3
Backtracking Algorithms
Backtracking.
◦ is used to solve problems in which a sequence of objects
is chosen from a specified set so that the sequence
satisfies some criterion.
◦ is a modified depth-first search of a tree.
Example:
Traversing a maze.
5
Backtracking Algorithms
We call a node non-promising if when visiting the node we
determine that it cannot possibly lead to a solution. Otherwise,
we call it promising.
This is called pruning the state space tree, and the subtree
consisting of the visited nodes is called the pruned state space
tree.
6
Example 1: Solving a Maze
Clearly, at a single junction you
could have even more than 2
choices.
8
Example 2: Eight Queens Problem
Find an arrangement of 8 queens on a
single chess board such that no two
queens are attacking one another.
In chess, queens can move all the way
down any row, column or diagonal (so
long as no pieces are in the way).
◦ Due to the first two restrictions, it's
clear that each row and column of
the board will have exactly one
queen.
9
Example 2: Eight Queens Problem
Number of Possible placements
= 64 * 63 * 62 * 61 * 60 * 59 * 58 * 57
= 178,462, 987, 637, 760 / 8!
= 4,426,165,368
10
Example: Eight Queens Problem
The backtracking strategy is as Q
follows:
Q
Q
1. Place a queen on the first Q
available square in row 1. Q Q
2. Move onto the next row, Continue…
placing a queen on the
first available square there
Animated Example:
(that doesn't conflict with http://www.hbmeyer.de/backtrack/a
chtdamen/eight.htm#up
the previously placed
queens).
11
Example: Eight Queens Problem
3. Continue in this fashion until Q
either: Q
Q
a. you have solved the
Q
problem, or Q Q
b. you get stuck.
Continue…
When you get stuck, remove
the queens that got you
there, until you get to a row
Animated Example:
where there is another valid http://www.hbmeyer.de/backtrack/a
chtdamen/eight.htm#up
square to try.
12
Divide-and-Conquer: Main Idea
Divide and Conquer is a designing algorithms
technique to analyze the running time of such
algorithms
Basic Procedure
◦ divide problem into subproblems
◦ For subproblems that are really small (trivial),
solve them directly. Else solve them recursively.
(conquer)
◦ combine solutions to subproblems to get solution
to original problem
A good example of this approach is Mergesort.
13
Divide-and-conquer algorithms
Divide-and-conquer algorithms often follow a generic
pattern.
They tackle a problem of size n by recursively
solving, say, a subproblems of size n/b and then
combining these answers in O(nd) time, for some a; b;
d > 0 (in the multiplication algorithm, a = 3, b = 2,
and d = 1).
Their running time can therefore be captured by the
equation T(n) = a T (n/b) + O(nd).
derive a closed-form solution to this general
recurrence so that we no longer have to solve it
explicitly in each new instance.
Greedy Algorithm
Used to solve optimization problem
◦ Recursion
◦ is one in which you want to find, not just a solution, but the best
solution
◦ You take the best you can get right now, without regard for future
consequences
◦ We hope that by choosing a local optimum at each step, you will end
up at a global optimum
15
Greedy Algorithm
Idea: When we have a choice to make, make the one
that looks best right now
◦ Make a locally optimal choice in hope of getting a
globally optimal solution
◦ a decision is made that appears to be good, without
regard for future consequences
Greedy algorithms don’t always yield an optimal
solution
Makes the choice that looks best at the moment in order
to get optimal solution 16
Examples of Greedy Algorithms
• Counting Money
• Activity Selection
• Minimum Spanning Tree
• Dijkstra Shortest Path
• Huffman Codes
• Fractional Knapsack
17
Example: Counting Money
Suppose you want to count out a certain amount of
money, using the fewest possible bills and coins
A greedy algorithm would do this would be:
At each step, take the largest possible bill or coin that
does not overshoot
◦ Example: To make $6.39, you can choose:
a $5 bill
a $1 bill, to make $6
a 25¢ coin, to make $6.25
A 10¢ coin, to make $6.35
four 1¢ coins, to make $6.39
For US money, the greedy algorithm always gives the
optimum solution
18
Example: Counting Money
Consider the problems of making n cents change with 50
cents, 20 cents, 10 cents, 5 cents and 1 cent using the least
total number of coins.
At each step we choose the coin of the largest denomination
possible to add to the group of change without exceeding n
cents
Pseudocode:The Greedy Change-Making Algorithm
procedure change(c1,c2,…,cr: values of denomination of
coins where c1 > c2 >…> cr ; n: a positive integer)
for i := 1 to r
while n ≥ ci
begin
add a coin with value ci to the change
n := n - ci
end
19
A Failure of Greedy Algorithm
In some (fictional) monetary system, “krons” come in 1
kron, 7 kron, and 10 kron coins
Using a greedy algorithm to count out 15 krons, you
would get
◦ A 10 kron piece
◦ Five 1 kron pieces, for a total of 15 krons
◦ This requires six coins
A better solution would be to use two 7 kron pieces and
one 1 kron piece
◦ This only requires three coins
The greedy algorithm results in a solution, but not in an
optimal solution
20
A Scheduling Problem
You have to run nine jobs(processes), with running times of 3,
5, 6, 10, 11, 14, 15, 18, and 20 minutes
You have three processors on which you can run these jobs
You decide to do the longest-running jobs first, on whatever
processor is available
P1 20 10 3
P2
18 11 6
P3
15 14 5
P1 3 10 15
P2
5 11 18
P3
6 14 20
P1 20 14
P2
18 11 5
P3
15 10 6 3
23
Data Compression
Q. Given a text that uses 32 symbols (26 different letters, space,
and
some punctuation characters), how can we encode this text in bits?
Ans. We can encode 25 different symbols using a fixed length of 5
bits per symbol. This is called fixed length encoding.
Q. Some symbols (e, t, a, o, i, n) are used far more often than
others.
How can we use this to reduce our encoding?
Ans. Encode these characters with fewer bits, and the others with
more bits.
Q. How do we know when the next symbol begins?
Ans. Use a separation symbol (like the pause in Morse), or make
sure that there is no ambiguity by ensuring that no code is a prefix
of another one.
Ex. c(a) = 01, c(b) = 010 and c(e) = 1?
◦ What is 0101
24
Prefix Codes
Definition. A prefix code for a set S is a function c that maps
each x ∈ S to 1s and 0s in such a way that for x, y∈S, x≠y,
c(x) is not a prefix of c(y).
Easy Definition: Prefix(-free) code: no codeword is also a
prefix of some other codewords (Un-ambiguous)
Example:
◦ c(a) = 11, c(e) = 01, c(k) = 001, c(l) = 10, c(u) = 000
Q. What is the meaning of 1001000001 ?
Ans: “Leuk:
Suppose frequencies are known in a text of 1G, where fa=0.4,
fe=0.2, fk=0.2, fl=0.1, fu=0.1
Q. What is the size of the encoded text?
Ans. 2*fa + 2*fe + 3*fk + 2*fl + 3*fu = 2.4G
25
Optimal Prefix Codes
Definition. The average bits per letter of a prefix code c
is the sum over all symbols of its frequency times the
number of bits of its encoding:
ABL(c ) = ∑ f x ⋅ c ( x )
x∈S
26
Representing Prefix Codes using
Binary Trees
Q. What is the meaning of 111010001111101000 ?
Ans. 111010001111101000
s I m p e l
ABL(c ) = ∑ f x ⋅ c ( x )
x∈S
ABL(T ) = ∑ f x ⋅ depth T ( x)
x∈S
27
Optimal Prefix Codes: Huffman
Encoding
Observation. Lowest frequency items should be at the
lowest level in tree of optimal prefix code.
Observation. For n > 1, the lowest level always contains
at least two leaves.
Observation.The order in which items appear in a level
does not matter.
Claim. There is an optimal prefix code with tree T* where
the two lowest-frequency letters are assigned to leaves
that are siblings in T*.
Greedy template. [Huffman, 1952] Create tree bottom-up.
Make two leaves for two lowest-frequency letters y and z.
Recursively build tree for the rest using a meta-letter for
yz.
28
Huffman Encoding: Greedy Analysis
Huffman codes: compressing data (savings of 20% to 90%)
Huffman’s greedy algorithm uses a table of the frequencies of
occurrence of each character to build up an optimal way of
representing each character as a binary string
29
Huffman Encoding: Example
30
Example, Step 1
Assume that relative frequencies are:
◦ A: 40
◦ B: 20
◦ C: 10
◦ D: 10
◦ R: 20
(We chose simpler numbers than the real frequencies)
Smallest number are 10 and 10 (C and D), so connect those
31
Example, Step 5
Assign 0 to left branches, 1 to right branches
Each encoding is a path from the root
A=0
B = 100
C = 1010
D = 1011
R = 11
Each path
terminates at a
leaf
Do you see
why encoded
strings are
decodable?
32
Spanning Tree
6 4 6 4
5 5
7 8 7 8
G = (V, E)
T = (V, F)
33
Minimum Spanning Trees
•A spanning tree for G is a free tree that connects all the
vertices in V.
•The cost of a spanning tree is the sum of the costs of the edges
in the tree.
•Given: a Connected, undirected, weighted
b c graph, G =(V, E) in which each edge (u, v) in
11 5
E has a cost c (u, v) attached to it.
a 1 -3 •Find: Minimum - weight spanning tree, T
3
d e f
0 2 b c
5
a 1
3 -3
d e f
34 0
Minimum Spanning Trees
Minimum spanning tree. Given connected graph G with real-
valued arc weights ce, an MST is a spanning tree of G whose
sum of arc weights is minimized.
2 24 3 2 3
4 4
1 1
23 9 9
6 18 6
6 6
5 4 5 4
16 11 11
8 5 8 5
7 7
10 14
7 21 8 7 8
35
Applications
Designing physical networks.
◦ telephone, electrical, hydraulic, TV cable, computer, road
Cluster analysis.
◦ delete long edges leaves connected components
◦ finding clusters of quasars and Seyfert galaxies
◦ analyzing fungal spore spatial patterns
Approximate solutions to NP-hard problems.
◦ metric TSP, Steiner tree
Indirect applications.
◦ describing arrangements of nuclei in skin cells for cancer
research
◦ learning salient features for real-time face verification
◦ modeling locality of particle interactions in turbulent fluid flow
◦ reducing data storage in sequencing amino acids in a protein
36
Minimum Spanning Tree
There can be many spanning trees of a graph
37
Generic Algorithm
“Grows” a set A.
A := ∅;
while A not complete tree do
find a safe edge (u, v);
A := A ∪ {(u, v)}
do
38
Fundamental Cycle
Fundamental cycle.
◦ Adding any non-tree arc e to T forms unique cycle C.
◦ Deleting any arc f ∈ C from T ∪ {e} results in new
spanning tree.
2 3
1
6 4
5 f
9
e 8
7
6 4
f
5
9
e 8
7
Cut optimality conditions: For every tree arc f, and for every
non-tree arc e in its fundamental cut: ce ≥ cf.
Observation: If ce < cf then T not a MST.
40
Greedy Algorithms for Minimum
Spanning Trees
1. Kruskal's algorithm. Start with T = φ. Consider edges in
ascending order of cost. Insert edge e in T unless doing so
would create a cycle.
2. Reverse-Delete algorithm. Start with T = E. Consider edges
in descending order of cost. Delete edge e from T unless
doing so would disconnect T.
3. Prim's algorithm. Start with some root node s and greedily
grow a tree T from s outward. At each step, add the cheapest
edge e to T that has exactly one endpoint in T.
4. Remark. All three algorithms produce an MST.
41
Greedy Algorithms for Minimum
Spanning Trees
Simplifying assumption. All edge costs ce are distinct.
Cut property. Let S be any subset of nodes, and let e be the min
cost edge with exactly one endpoint in S. Then the MST
contains e.
Cycle property. Let C be any cycle, and let f be the max cost
edge belonging to C. Then the MST does not contain f.
C
f
S
e
42
Kruskal's Algorithm:
43
Kruskal's Algorithm: Proof of
Correctness
Kruskal's algorithm.
◦ Consider edges in ascending order of weight.
◦ Case 1: If adding e to T creates a cycle, discard e according
to cycle property.
◦ Case 2: Otherwise, insert e = (u, v) into T according to cut
property where S = set of nodes in u's connected component.
v S
e
e u
Case 1 Case 2
44
Kruskal's Algorithm
16
5 4 11
12
7
3 14
6 9 2
10 8
15
17
13 18
45
Prim’s Algorithm
MST-Prim(G, w, r)
6 4
Q = V[G];
5 9
for each u ∈ Q
key[u] = ∞;
14
10 2
key[r] = 0;
15
p[r] = NULL;
while (Q not empty) 3 8
u = ExtractMin(Q);
Run on example gr aph
for each v ∈ Adj[u]
if (v ∈ Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);
46
Prim’s Algorithm
MST-Prim(G, w, r)
6 4 4
Q = V[G];
5 9
for each u ∈ Q
5 2 9
key[u] = ∞;
14
10 2 u
key[r] = 0;
15
p[r] = NULL; 0 8 15
while (Q not empty) 3 8
3
u = ExtractMin(Q);
for each v ∈ Adj[u]
if (v ∈ Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);
47
Minimum Spanning Tree
Suppose in contradiction there are TWO MSTs,
M1 and M2.
48
Minimum Spanning Tree
e: in M1 but not M2
M2:
e': in M2 but not M1;
wt is less than wt of e
49
Correctness of Kruskal's Alg.
Let e1, e2, …, en-1 be sequence of edges chosen
50
Correctness of Kruskal's Algorithm
51
Barůvka‘s Algorithm
1. For all vertices search the edge with the smallest weight
of this vertex and mark these edges
5
A B
4 6 2
2 D 3
C
3 1 2
E F
4
Barůvka‘s Algorithm
A B
2
2 D
C
3 1 2
E F
The correctness of Barůvka‘s Algorithm
57
Minimum Spanning Trees
◦ Objective: maximize
∑b
i∈T
i
◦ Constraint: ∑w
i∈T
i ≤W
59
Knapsack Problem
Knapsack Problem: Given n items, with i th item worth bi
dollars and weighing wi pounds, a thief wants to take as valuable
a load as possible, but can carry at most W pounds in his
knapsack.
Given: A set S of n items, with each item i having
If we are not allowed to take fractional amounts, then this is the 0/1
knapsack problem (Each item is either taken or not taken)
60
Knapsack Problem: Example
Given: A set S of n items, with each item i having
◦ bi - a positive benefit, wi - a positive weight
Goal: Choose items with maximum total benefit but with
weight at most W.
“knapsack”
Solution:
Items:
• 5 (2 in)
1 2 3 4 5
• 3 (2 in)
• 1 (4 in)
Weight: 4 in 2 in 2 in 6 in 2 in
Benefit: 9 in
$20 $3 $6 $25 $80
61
Knapsack Problem
Knapsack problem.
◦ Given n objects and a "knapsack."
◦ Item i weighs wi > 0 kilograms and has value vi > 0.
◦ Knapsack has capacity of W kilograms.
◦ Goal: fill knapsack so as to maximize total value.
Item Value Weight
Ex: { 3, 4 } has value 40. 1 1 1
2 6 2
W = 11
3 18 5
4 22 6
5 28 7
62 147
Review: Greedy Algorithm
Fib(n):
if n = 0 or 1 then return n
65
Computing Fibonacci Numbers
Recursion Tree for Fib(5)
Computing the nth Fibonacci
number recursively (top-down): Fib(5)
Fib(4) Fib(3)
Fib(1) Fib(0)
66
How Many Recursive Calls?
• Run time: T(n) = T(n-1) + T(n-2)
• If all leaves had the same depth, then there would be about 2n
recursive calls.
68
More Efficient Recursive Algorithm
F[0] := 0; F[1] := 1; F[n] := Fib(n);
Fib(n):
if n = 0 or 1 then return F[n]
if F[n-1] = NIL then F[n-1] := Fib(n-1)
if F[n-2] = NIL then F[n-2] := Fib(n-2)
return (F[n-1] + F[n-2])
69
Automated Memoization
Automated memoization. Many functional programming languages
(e.g., Lisp) have built-in support for memoization.
Q. Why not in imperative languages (e.g., Java)?
F(40)
F(39) F(38)
70
Example of Memoized Fib
1 1
fills in F[3] with 2,
2 1
NIL Fib(4) returns 2+1 = 3
3 2
NIL
4 3
NIL
Fib(3)
fills in F[2] with 1,
returns 1+1 = 2
5 5
NIL
71
Dynamic Programming for Fibonacci
DP Solution for Fibonacci
Fib(n):
F[0] := 0; F[1] := 1;
for i := 2 to n do
F[i] := F[i-1] + F[i-2]
◦ return F[n]
Can perform application-specific
optimizations
e.g., save space by only keeping last
two numbers computed
72
Computing a Binomial Coefficient by DP
Binomial coefficients are coefficients of the binomial formula:
(a + b)n = C(n,0)anb0 + . . . + C(n,k)an-kbk + . . . + C(n,n)a0bn
73
The General Dynamic Programming
Technique
Applies to a problem that at first seems to require a lot of
time (possibly exponential), provided we have:
Simple subproblems: the subproblems can be defined in
terms of a few variables, such as j, k, l, m, and so on.
Subproblem optimality: the global optimum value can
be defined in terms of optimal subproblems
Subproblem overlap: the subproblems are not
independent, but instead they overlap (hence, should be
constructed bottom-up).
74
Dynamic Programming
75 160
Dynamic Programming
76 161
Computing C(n, k)
Pseudocode and Analysis
Time efficiency: HW
Space efficiency: HW
77
References
Cormen, C. Leiserson, R. Rivest, and C. Stein,
Introduction to Algorithms, MIT Press and
McGraw-Hill Book Company, 2009
Anany Levitin, Introduction to The Design &
Analysis of Algorithms.,Addison-Wesley , 2002
J. Kleinberg, E. Tardos, Algorithm Design,
Addison-Wesley, 2005.
78