Session 7 and 8
Session 7 and 8
College of Education
School of Continuing and Distance Education
2020/2021 – 2022/2023
Introduction
• Algorithms for optimization problems typically go through a
sequence of steps, with a set of choices at each step.
• A greedy algorithm always makes the choice that looks best at the
moment (it makes a locally optimal choice in the hope that this
choice will lead to a globally optimal solution).
• Greedy algorithms do not always yield optimal solutions, but for
many problems they do.
• The greedy method is quite powerful and works well for a wide
range of problems
Greedy Technique
Constructs a solution to an optimization problem piece by
piece through a sequence of choices that are:
• feasible
• locally optimal
• irrevocable
• Approximations:
– traveling salesman problem (TSP)
– knapsack problem
– other combinatorial optimization problems
Change-Making Problem
Given unlimited amounts of coins of denominations d1 > … > dm ,
give change for amount n with the least number of coins
Greedy solution:
Greedy solution is
• optimal for any amount and “normal’’ set of denominations
• may not be optimal for arbitrary coin denominations
Minimum Spanning Tree (MST)
• A spanning tree of an undirected connected graph is its connected
acyclic subgraph (i.e., a tree) that contains all the vertices of the
graph.
• If such a graph has weights assigned to its edges, a minimum
spanning tree is its spanning tree of the smallest weight, where the
weight of a tree is defined as the sum of the weights on all its
edges. The minimum spanning tree problem is the problem of
finding a minimum spanning tree for a given weighted connected
graph. 6 c
a
4 1
Example: 2
b d
3
Graphs and its spanning tree
4
c
a
1
6
2
d
b 3
Notes about Prim’s algorithm
• Proof by induction that this construction actually yields MST
• Efficiency
– O(n2) for weight matrix representation of graph and array
implementation of priority queue
– O(m log n) for adjacency list representation of graph with n
vertices and m edges and min-heap implementation of priority
queue
Kruskal's Algorithm
• Kruskal's algorithm is a greedy algorithm in graph theory that finds
a minimum spanning tree for a connected weighted graph.
• It finds a subset of the edges that forms a tree that includes every
vertex, where the total weight of all the edges in the tree is
minimized.
• This algorithm is directly based on the MST( minimum spanning
tree) property.
Kruskal's Algorithm
• MST-KRUSKAL(G, w)
1. A ← Ø
2. for each vertex v V[G]
3. do MAKE-SET(v)
4. sort the edges of E into nondecreasing order by weight w
5. for each edge (u, v) E, taken in nondecreasing order by weight
6. do if FIND-SET(u) ≠ FIND-SET(v)
7. then A ← A {(u, v)}
8. UNION(u, v)
9. return A
Another greedy algorithm for MST: Kruskal’s
• On each iteration, add the next edge on the sorted list unless this
would create a cycle. (If it would, skip the edge.)
In class - Example
Procedure for finding Minimum
Spanning Tree
Step1. Edges are sorted in ascending order by weight.
Add Edge E3
Add Edge E4
Add Edge E5
4
c
a
1
6
2
d
b 3
Notes about Kruskal’s algorithm
• Algorithm looks easier than Prim’s but is harder to implement
(checking for cycles!)
Example a
3
7 2
d
d
5 4
6
4
b c
b(a,3) c(b,3+4) d(b,3+2) e(-,∞) 3 6
2 5
a d e
7 4
4
d(b,5) c(b,7) e(d,5+4) 3
b c
6
2 5
a d e
7 4
4
c(b,7) e(d,9) 3
b c
6
2 5
a d e
7 4
e(d,9)
Notes on Dijkstra’s algorithm
• Doesn’t work for graphs with negative weights
• Efficiency
– O(|V|2) for graphs represented by weight matrix and array
implementation of priority queue
– O(|E|log|V|) for graphs represented by adj. lists and min-heap
implementation of priority queue
Huffman’s algorithm
Initialize n one-node trees with alphabet characters and the tree
weights with their frequencies.
Repeat the following step n-1 times: join two binary trees with smallest
weights into one (as left and right subtrees) and make its weight
equal the sum of the weights of the two trees.
Mark edges leading to left and right subtrees with 0’s and 1’s,
respectively.
0.1 0.15 0.2 0.2 0.35
Example B _ C D A
character A B C D _ 0.1
B
0.15
_
0.1 0.15
B _
1.0
0 1
0.4 0.6
0 1 0 1
0.1 0.15
B _
Reference
Levitin, A. (2012). Introduction to the Design and
Analysis of Algorithms ( 3rd Edition). Harlow: Addison
Wesley.
Acknowledgement