Chapter Three
Chapter Three
Chapter Three
Greedy Algorithm
outline
Greedy Algorithm
• The greedy method has that each decision is locally optimal. These
locally optimal solutions will finally add up to a globally optimal
solution.
Prove one of the optimal choices is the greedy choice yet safe
Show that all but one of sub-problems are empty after greedy choice
• f1 f2 … fn.
i 1 2 3 4 5 6 7 8 9 10 11
si 1 3 0 5 3 5 6 8 8 2 12
fi 4 5 6 7 8 9 10 11 12 13 14
The solution set = {1, 4, 8, 11}
Time complexity: O(nlogn)
Activity selection problem
Solution of the example:
i si fi accept
1 1 4 Yes
2 3 5 No
3 0 6 No
4 5 7 Yes
5 3 8 No
7 6 10 No
8 8 11 Yes
9 8 12 No
10 2 13 No
11 12 14 Yes
Solution = {1, 4, 8, 11}
Job sequencing with deadline
• Given n jobs. Associated with job I is an integer deadline Di≧0.
• For any job I the profit Pi is earned; iff the job is completed by its
deadline.
• To complete a job, one has to process the job on a machine for one unit
of time.
• A feasible solution is a subset J of jobs such that each job in the subset
can be completed by its deadline.
Step 2: Add the next job ai to the solution set if ai can be completed by
its deadline. Assign i to time slot [r-1, r], where r is the largest integer
such that 1 r di and [r-1, r] is free.
i 1 2 3 4 5
pi 20 15 10 5 1
di 2 2 1 3 3
solution = {1, 2, 4}
total profit = 20 + 15 + 5 = 40
The 2-way merging problem
# of comparisons required for the linear 2-way merge algorithm is m1+
respectively.
2-way merging example
2 3 5 6
1 4 7 8
The problem: There are n sorted lists, each of length mi. What is the
optimal sequence of merging process to merge these n lists into one
sorted list ?
The 2-way merging problem
An extended binary tree representing a 2-way
merge
The 2-way merging problem
An example of 2-way merging
2 3 5 7 11 13
The 2-way merging problem
50 10 30 30
30 20 20 10
Merge L1 and L2, then with L3 Merge L2 and L3, then with
merge cost = sum of all weightedL1
external path lengths
Huffman codes
• Suppose we wish to save a text (ASCII) file on the disk or to transmit
it through a network using an encoding scheme that minimizes the
number of bits required.
• Example:
• Symbols: A, B, C, D, E, F, G
freq. : 2, 3, 5, 8, 13, 15, 18
• Huffman codes:
A: 10100 B: 10101 C: 1011
D: 100 E: 00 F: 01
G: 11
1 2 1 2 1 2
3 4 3 4 3 4
A directed graph B undirected graph C Cyclic graph
1 2 1 3
2
5
1 2
3 4 3 6
4 3 4
D Acyclic graph E weighted graph F unweight graph
Graphs (review)
Definition. A directed graph (digraph)
G = (V, E) is an ordered pair consisting of
• a set V of vertices (singular: vertex),
• a set E ⊆ V × V of edges.
Definition. an undirected graph G = (V, E),
the edge set E consists of unordered pairs of
vertices.
In either case, we have | E | = O(V 2).Moreover,
if G is connected, then | E | ≥ | V | – 1, which
implies that log | E | = Θ(log V).
Minimum Spanning Trees
a 1
3 -3
d e f
0
Minimum Spanning Trees
Given a weighted (undirected) graph G = (V, E), where each edge e
has a positive weight w(e).
A spanning tree of G is a tree (connected graph without cycles, or
circuits) which has V as its vertex set,
i.e., the tree connects all vertices of the graph G. If |V| = n, then the
tree has n – 1 edges (this is a fact which can be proved by induction).
A minimum spanning tree of G is a spanning tree that has the
minimum total edge weight. In MST weighted must be p+?
1 1 A minimum spanning
3 6 3 6
8 tree (of 4 edges),
2 3 2 3
5 weight = 3 + 2 + 4 + 6
4 4
7
5 4 5 4
2 2 = 15.
A weighted graph of no parallel edges or self-loops
Prim’s algorithm for finding MST
Input A connected, undirected graph G = (V, E)
with weight function w : E → R.
Step 1: x V, Let A = {x}, B = V - {x}.
Step 2: Select (u, v) E, u A, v B such that (u,
v) has the smallest weight between A and B.
Step 3: Put (u, v) in the tree. A = A {v}, B = B - {v}
Step 4: If B = , stop; otherwise, go to Step 2.
QQ:=
:=V[G];
V[G];
for eachuuQQdo
foreach do Complexity:
key[u]
key[u]:= := Using binary heaps: O(E lg
do;
do; V).
key[r]
key[r]:=
:=0;0;
[r] := NIL;
Initialization – O(V).
[r] := NIL;
Building initial queue –
whileQQ
while dodo
uu:=
:=Extract
Extract--Min(Q);
Min(Q); O(V).
for eachvvAdj[u]
foreach Adj[u]dodo V Extract-Min’s – O(V
ififvvQQw(u,
w(u,v)
v)<<key[v]
key[v]then
then lgV).
[v]
[v]:=
:=u;u; E Decrease-Key’s –
key[v] := w(u, v)
key[v] := w(u, v)
ifif O(E lg operation
decrease-key V).
do
do
do
do Using Fibonacci heaps:
= O(E + V lg V).
Note: A = {(v, [v]) : v v - {r} - Q}.
Prim’s MST Algorithm
Example .
Step Next edge selected Partial tree
1
3 Initially
6 1
8
2 3 3 1
5 4 1 (1,5), weight=3
7 5
5
4
2 1
2 (5,4), weight=2
5 4
A weighted graph 2
1
3 (4,2), weight=4 5 2
4
4
1
6
5 2
4 (1,3), weight=6 3
4
Example Prim’s Algorithm
6 12
5 9
14 7
8 15
3 10
Kruskal’s algorithm for finding MST
Input: A connected, undirected graph G = (V, E)
with weight function w : E
Step→ R. all edges into non-decreasing order.
1: Sort
Step 2: Add the next smallest weight edge to the forest if it will
not cause a cycle.
Step 3: Stop if n-1 edges. Otherwise, go to Step2.
For each of the remaining nodes, find a shortest path connected from the source
(assuming the direction of the edges along the paths are respected).
1. find the shortest among all shortest paths (from the source), then find the
second shortest, etc.,
2. breaking ties arbitrarily, until all shortest paths are found. During the process,
the collection of all the shortest paths determined so far form a tree;
3. the next shortest path is selected by finding a node that is one edge away from
the current tree and has the shortest distance measured from the source.
Single source shortest path problem
5 30 2 Initially 1 C = [ 2, 3, 4, 5]
100
20 D = [50,30,100,10]
10 5 Choose 1 [ 2, 3, 4] Changed
4 3
50
node 5 5 [50,30, 20] from 100
3 1000 800 0
4 1200 0
5 1500 0 250
6 1000 0 900 1400
7 0 1000
8 1700 0 4 -42
Vertex
Iteration S Selected (1) (2) (3) (4) (5) (6) (7) (8)
Initial ----
1 5 6 + + + 1500 0 250 + +
2 5,6 7 + + + 1250 0 250 1150 1650
3 5,6,7 4 + + + 1250 0 250 1150 1650
4 5,6,7,4 8 + + 2450 1250 0 250 1150 1650
5 5,6,7,4,8 3 3350 + 2450 1250 0 250 1150 1650
6 5,6,7,4,8,3 2 3350 3250 2450 1250 0 250 1150 1650
5,6,7,4,8,3,2 3350 3250 2450 1250 0 250 1150 1650
4 -43
• Time complexity : O(n ) 2