Upload
meljun-cortes-mbampa
View
228
Download
0
Embed Size (px)
Citation preview
8/21/2019 Meljun Cortes Algorithm Greedy Algorithm Design Technique II
1/25
Greedy Algorithm Design Technique
Design and Analysis of Algorithms
* Property of STI
Page 1 of 25
Definition of Greedy algorithm
Importance of Greedy algorithm design technique
Types of Greedy algorithm
Prim’s algorithm
Kruskal’s algorithm
Dijktra’s algorithm
Huffman trees
Sample problem using the application of Prim’s
algorithm, Kruskal’s algorithm, Dijktra’s algorithm,
and Huffman trees
8/21/2019 Meljun Cortes Algorithm Greedy Algorithm Design Technique II
2/25
Greedy Algorithm Design Technique
Design and Analysis of Algorithms
* Property of STI
Page 2 of 25
Greedy algorithms are simple and straight forward
[www.personal.kent.edu].
Most greedy algorithms are easy to develop,
implement, and time efficient.
Suggests constructing a solution through a
sequence of steps, each expanding a partially
constructed solution obtained so far, until a
complete solution to the problem is reached.
8/21/2019 Meljun Cortes Algorithm Greedy Algorithm Design Technique II
3/25
Greedy Algorithm Design Technique
Design and Analysis of Algorithms
* Property of STI
Page 3 of 25
On each step, which is the central point of this
technique, the choices made must be:
Feasible
Locally optimal
Irrevocable
Each step, it suggest a “greedy” grab of the best
alternative available.
8/21/2019 Meljun Cortes Algorithm Greedy Algorithm Design Technique II
4/25
Greedy Algorithm Design Technique
Design and Analysis of Algorithms
* Property of STI
Page 4 of 25
Example: Making Change Problem
Consisting of available dollar coins (100 cents),
quarters (25 cents), dimes (10 cents), nickels (5
cents) and pennies (1 cent), make a change of a
given amount using the smallest possible number
of coins.
To solve this problem, we use simple and formal
algorithms as follows:
Informal Algorithm Formal Algorithm
8/21/2019 Meljun Cortes Algorithm Greedy Algorithm Design Technique II
5/25
Greedy Algorithm Design Technique
Design and Analysis of Algorithms
* Property of STI
Page 5 of 25
Greedy algorithm:
Make a change for 2.89 (289 cents) where n =
2.89 and the solution contains 2 dollars, 3
quarters, 1 dime and 4 pennies.
The algorithm is greedy because at every stage it
chooses the largest coin without worrying about
the consequences.
8/21/2019 Meljun Cortes Algorithm Greedy Algorithm Design Technique II
6/25
Greedy Algorithm Design Technique
Design and Analysis of Algorithms
* Property of STI
Page 6 of 25
The greedy algorithm consists of four functions:
1. A function that validates whether the selected
set of items provides a solution.
2. A function that validates the feasibility of a
set.
3. The selection function that tells which of the
choices is the most capable.
4. The objective function which does not appear
explicitly, but gives the value of a solution.
8/21/2019 Meljun Cortes Algorithm Greedy Algorithm Design Technique II
7/25
Greedy Algorithm Design Technique
Design and Analysis of Algorithms
* Property of STI
Page 7 of 25
Prim s algorithm: is an algorithm in graph theory
that finds a minimum spanning tree for a
connected weighted graph.
Spanning tree of a connected graph is its
connected acyclic subgraph like a tree that
contains all the vertices of the graph [LEV07].
Minimum spanning tree (MST) of a weighted
connected graph is its spanning tree of thesmallest weight, where the weight of a tree is
defined as the sum of the weights on all its edges
[LEV07].
Minimum spanning tree problem is the problem of
finding a minimum spanning tree for a givenweighted connected graph [LEV07].
8/21/2019 Meljun Cortes Algorithm Greedy Algorithm Design Technique II
8/25
Greedy Algorithm Design Technique
Design and Analysis of Algorithms
* Property of STI
Page 8 of 25
8/21/2019 Meljun Cortes Algorithm Greedy Algorithm Design Technique II
9/25
Greedy Algorithm Design Technique
Design and Analysis of Algorithms
* Property of STI
Page 9 of 25
The illustration below is an example of minimum
spanning tree:
Minimum spanning tree from the given graph
8/21/2019 Meljun Cortes Algorithm Greedy Algorithm Design Technique II
10/25
Greedy Algorithm Design Technique
Design and Analysis of Algorithms
* Property of STI
Page 10 of 25
Prim’s algorithm efficiency:
O(n2) for weight matrix representation of graph
and array implementation of priority queue
O(m log n) for adjacency lists representation
of graph with n vertices and m edges and min-
heap implementation of the priority queue
8/21/2019 Meljun Cortes Algorithm Greedy Algorithm Design Technique II
11/25
Greedy Algorithm Design Technique
Design and Analysis of Algorithms
* Property of STI
Page 11 of 25
an algorithm in graph theory that finds a minimum
spanning tree for a connected weighted graph
[www.wordiq.com]
introduced by Joseph Kruskal
an algorithm which 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
If in case the graph is not connected, it finds a
minimum spanning forest connected component.
8/21/2019 Meljun Cortes Algorithm Greedy Algorithm Design Technique II
12/25
Greedy Algorithm Design Technique
Design and Analysis of Algorithms
* Property of STI
Page 12 of 25
8/21/2019 Meljun Cortes Algorithm Greedy Algorithm Design Technique II
13/25
Greedy Algorithm Design Technique
Design and Analysis of Algorithms
* Property of STI
Page 13 of 25
Step 1. Observe that in the given graph, the edge
(g, h) is the shortest. In this case, either vertex g or
vertex h could be represented.
Step 2. The edge (c, i) creates the second tree.
Choose vertex c as representative for second tree.
8/21/2019 Meljun Cortes Algorithm Greedy Algorithm Design Technique II
14/25
Greedy Algorithm Design Technique
Design and Analysis of Algorithms
* Property of STI
Page 14 of 25
Step 3. Edge (g, g) is the next shortest edge. Add
this edge and choose vertex g as representative.
Step 4. Edge (a, b) creates a third tree.
8/21/2019 Meljun Cortes Algorithm Greedy Algorithm Design Technique II
15/25
Greedy Algorithm Design Technique
Design and Analysis of Algorithms
* Property of STI
Page 15 of 25
Step 5. Add edge (c, f) and merge two trees. Vertex
c is chosen as the representative.
Step 6. Edge (g, i) is the next cheapest, but if we
add this edge a cycle would be created. Vertex c is
the representative of both.
8/21/2019 Meljun Cortes Algorithm Greedy Algorithm Design Technique II
16/25
Greedy Algorithm Design Technique
Design and Analysis of Algorithms
* Property of STI
Page 16 of 25
Step 7. Instead, add edge (c, d).
Step 8. If we add edge (h, i), this would make a
cycle.
8/21/2019 Meljun Cortes Algorithm Greedy Algorithm Design Technique II
17/25
Greedy Algorithm Design Technique
Design and Analysis of Algorithms
* Property of STI
Page 17 of 25
Step 9. Instead of adding edge (h, i) add edge (a,
h).
Step 10. Again, if we add edge (b, c), it would
create a cycle. Add edge (d, e) instead to complete
the spanning tree.
8/21/2019 Meljun Cortes Algorithm Greedy Algorithm Design Technique II
18/25
Greedy Algorithm Design Technique
Design and Analysis of Algorithms
* Property of STI
Page 18 of 25
introduced in 1959 by Edsger Dijktra, who is a
Dutch computer scientist
a graph search algorithm that solves the single-
source shortest path problem for a graph with
nonnegative edge path costs, producing a shortest
path tree [www.wikipedia.org]
finds the shortest paths to a graph’s vertices in
order of their distance from a given source it finds the shortest path form the source to a vertex
nearest to it, then to the second and so on
8/21/2019 Meljun Cortes Algorithm Greedy Algorithm Design Technique II
19/25
Greedy Algorithm Design Technique
Design and Analysis of Algorithms
* Property of STI
Page 19 of 25
The algorithm repeats the following steps until all
vertices are processed:
8/21/2019 Meljun Cortes Algorithm Greedy Algorithm Design Technique II
20/25
Greedy Algorithm Design Technique
Design and Analysis of Algorithms
* Property of STI
Page 20 of 25
Huffman tree: is a binary tree which are based on
the probability distribution of a symbol set and the
principle that symbols occurring more frequently
will be represented by shorter codes than other,
less probable ones.
Assume that the frequency of the character E in a
text is 15% and the frequency of the character T is
12%, as illustrated in the table below:
8/21/2019 Meljun Cortes Algorithm Greedy Algorithm Design Technique II
21/25
Greedy Algorithm Design Technique
Design and Analysis of Algorithms
* Property of STI
Page 21 of 25
Three basic steps in constructing a tree:
Step 1: Arrange the entire character set into a row
and should be ordered according to its frequency
from lowest to highest or vice versa.
Step 2: Subsequently, we will look for the two
nodes with the smallest combined frequencyweights and combine them to form a third node,
ensuing a simple two-level tree.
Step 3: Repeat Step 2 until all of the nodes on
each level are combined in a single tree.
8/21/2019 Meljun Cortes Algorithm Greedy Algorithm Design Technique II
22/25
Greedy Algorithm Design Technique
Design and Analysis of Algorithms
* Property of STI
Page 22 of 25
The process for building a tree starts with the leaf-
level nodes representing the original characters
arranged in descending order of value.
8/21/2019 Meljun Cortes Algorithm Greedy Algorithm Design Technique II
23/25
Greedy Algorithm Design Technique
Design and Analysis of Algorithms
* Property of STI
Page 23 of 25
Huffman tree – part 2
8/21/2019 Meljun Cortes Algorithm Greedy Algorithm Design Technique II
24/25
Greedy Algorithm Design Technique
Design and Analysis of Algorithms
* Property of STI
Page 24 of 25
Huffman tree – part 3
8/21/2019 Meljun Cortes Algorithm Greedy Algorithm Design Technique II
25/25
Design and Analysis of Algorithms