You are on page 1of 46

SUBJECT NAME : DESIGN AND ANALYSIS OF ALGORITHM

SUBJECT CODE : CCS53

SEMESTER :V

CLASS : III B.SC

UNIT – III: THE GREEDY METHOD

1.The General Method

2.Container Loading

3.Knapsack Problem

4. Tree Vertex Splitting

5. Job Sequencing with Deadlines

6. Minimum Cost Spanning Trees

7. Prim’s Algorithm

8. Kruskal’s Algorithm

9. An optimal Randomized Algorithm

10. Optimal Storage on Tapes

11. Optimal Merge Pattern

12. Single Source Shortest Paths.


1.The general method of Greedy

Greedy Method:
Following are a few points about the greedy method.
 The first note point is that we have to find the best method/option out of many
present ways.
 In this method, we will be deciding the output by focusing on the first stage. We
don’t think about the future.
 The greedy method may or may not give the best output.
A greedy Algorithm solves problems by making the choice that seems to be
the best at that particular moment. There are many optimization problems that
can be determined using a greedy algorithm. A greedy algorithm may provide
a solution that is close to optimal to some issues that might have no efficient
solution. A greedy algorithm works if a problem has the following two
properties:
1. Greedy Choice Property: By creating a locally optimal solution we can reach a
globally optimal solution. In other words, by making “greedy” choices we can
obtain an optimal solution.
2. Optimal substructure: Optimal solutions will always contain optimal
subsolutions. In other words, we say that the answers to subproblems of an optimal
solution are optimal.

Examples:
Following are a few examples of Greedy algorithms
 Machine scheduling
 Fractional Knapsack Problem
 Minimum Spanning Tree
 Huffman Code
 Job Sequencing
 Activity Selection Problem
Components of Greedy Algorithm
Greedy algorithms consist of the following five components −
 A candidate set − A solution is created with the help of a candidate set.
 A selection function − It is used to choose the best candidate that is to be added to
the solution.
 A feasibility function − A feasibility function is useful in determining whether a
candidate can be used to contribute to the solution or not.
 An objective function − This is used to assign a value to a solution or a partial
solution.
 A solution function − A solution function is used to indicate whether a complete
solution has been reached or not.

Areas of Application
The greedy approach is used to solve many problems. Out of all the
problems, here we have a few of them as follows:
 One of the applications could be finding the shortest path between two vertices
using Dijkstra’s algorithm.
 Another is finding the minimal spanning tree in a graph using Prim’s /Kruskal’s
algorithm

Greedy Algorithm:
getOptimal(Item, array[], int num)
Initialize empty result as, result = {}
While (All items are not considered)

// In order to select an item we make a greedy here.


j = SelectAnItem()

// If j is feasible, add j to the result


if (feasible(j))
result = result U j
return result

Why should we choose a greedy algorithm?


The greedy approach has a few characteristics that might make it suitable for
optimization. One reason to choose a greedy algorithm is to achieve the most
feasible solution immediately. If we take the activity selection problem as a
reference that is explained below. We can observe that if more activities can
be done before finishing the current activity, then these activities can be
performed within the same time.
Another reason to choose this algorithm is to divide a problem recursively
based on a condition. This ensures that there is no need to combine all the
solutions. While considering the activity selection problem, we achieve the
“recursive division” step by scanning a list of items only once and
considering certain activities.
Greedy choice property: Greedy Choice property tells us that we can obtain
a globally optimal solution by obtaining a solution that is either a locally
optimal solution or a Greedy solution. The choice made by a Greedy
algorithm may depend on the earlier choices but will never depend on the
future. A Greedy choice is made one after another and this helps to reduce the
given problem to a smaller one.
Optimal substructure: A problem is said to exhibit an optimal substructure
if and only if an optimal solution to the problem also contains the optimal
solutions to the subproblems. That means we can solve subproblems and
build up the solutions which could help us to solve larger problems.
NOTE: Making locally optimal choices might not work always. Hence, from
this point, it is understandable that Greedy algorithms might not always give
the best solutions.

Characteristics of Greedy approach


1. The greedy approach consists of an ordered list of resources(profit, cost, value,
etc.)
2. The greedy approach takes the maximum of all the resources(max profit, max
value, etc.)
3. For example, in the case of the fractional knapsack problem, the maximum
value/weight is taken first based on the available capacity.
Applications of Greedy Algorithms
1. To find an optimal solution (Activity selection, Fractional Knapsack, Job
Sequencing, Huffman Coding).
2. To find close to the optimal solution for NP-Hard problems like TSP.

Advantages and Disadvantages of Greedy Approach


Following are the various advantages and disadvantages of the greedy
approach.
Advantages
 It is easy to implement.
 Has fewer time complexities.
 Can be used for the purpose of optimization or finding close to optimization in the
case of NP-Hard problems.
Disadvantages
 One disadvantage of this algorithm is that the local optimal solution may not
always be globally optimal.

2. CONTAINER LOADING

The greedy algorithm constructs the loading plan of a single container layer by
layer from the bottom up. At the initial stage, the list of available surfaces
contains only the initial surface of size L x W with its initial position at height 0.
At each step, the algorithm picks the lowest usable surface and then determines
the box type to be packed onto the surface, the number of the boxes and the
rectangle area the boxes to be packed onto, by the procedure select layer.
The procedure select layer calculates a layer of boxes of the same type with the
highest evaluation value. The procedure select layeruses breadth-limited tree
search heuristic to determine the most promising layer, where the breadth is
different depending on the different depth level in the tree search. The advantage
is that the number of nodes expanded is polynomial to the maximal depth of the
problem, instead of exponentially growing with regard to the problem size. After
packing the specified number of boxes onto the surface according to the layer
arrangement, the surface is divided into up to three sub-surfaces by the
procedure divide surfaces.

Then, the original surface is deleted from the list of available surfaces and the
newly generated sub-surfaces are inserted into the list. Then, the algorithm selects
the new lowest usable surface and repeats the above procedures until no surface is
available or all the boxes have been packed into the container. The algorithm
follows a similar basic framework.

The pseudo-code of the greedy algorithm is given by the greedy


heuristic procedure.
procedure greedy heuristic()

list of surface := initial surface of L x W at height 0 list of box type


:= all box types

while (there exist usable surfaces) and (not all boxes are packed) do

select the lowest usable surface as current surface set depth := 0

set best layer := select layer(list of surface, list of box type, depth)

pack best layer on current surface


reduce the number of the packed box type by the packed amount

set a list of new surfaces := divide surfaces(current surface, best


layer, list of box type)

delete current surface from the list of surfaces

insert each surface in list of new surfaces into list of surfaces

end while

Given a layer of boxes of the same type arranged by the G4-heuristic, the layer is
always packed in the bottom-left corner of the loading surface.

As illustrated in above Figure, up to three sub-surfaces are to be created from the


original loading surface by the procedure divide surfaces, including the top
surface, which is above the layer just packed, and the possible spaces that might
be left at the sides.

If l = L or w = W, the original surface is simply divided into one or two sub-


surfaces, the top surface and a possible side surface. Otherwise, two possible
division variants exist, i.e., to divide into the top surface, the surface (B,C,E,F)
and the surface (F,G,H, I), or to divide into the top surface, the surface (B,C,D, I)
and the surface (D,E,G,H).

The divisions are done according to the following criteria, which are similar to
those in [2] and [5]. The primary criterion is to minimize the total unusable area of
the division variant. If none of the remaining boxes can be packed onto a sub-
surface, the area of the sub-surface is unusable. The secondary criterion is to avoid
the creation of long narrow strips.

―The underlying rationale is that narrow areas might be difficult to fill


subsequently‖. More specifically, if L−l ≥ W −w, the loading surface is divided
into the top surface, the surface (B,C,E,F) and the surface (F,G,H, I). Otherwise, it
is divided into the top surface, the surface (B,C,D, I) and the surface (D,E,G,H).
Algorithm for Container Loading

3.Knapsack Problem

Given a set of items, each with a weight and a value, determine a subset of items to
include in a collection so that the total weight is less than or equal to a given limit
and the total value is as large as possible.
The knapsack problem is in combinatorial optimization problem. It appears as a
subproblem in many, more complex mathematical models of real-world problems.
One general approach to difficult problems is to identify the most restrictive
constraint, ignore the others, solve a knapsack problem, and somehow adjust the
solution to satisfy the ignored constraints.

Knapsack Problem Using Greedy Method: The selection of some things, each
with profit and weight values, to be packed into one or more knapsacks with
capacity is the fundamental idea behind all families of knapsack problems. The
knapsack problem had two versions that are as follows:
Fractional Knapsack Problem

1. 0 /1 Knapsack Problem

The fractional Knapsack problem using the Greedy Method is an efficient


method to solve it, where you need to sort the items according to their ratio of
value/weight. In a fractional knapsack, we can break items to maximize the
knapsack's total value. This problem in which we can break an item is also called
the Fractional knapsack problem. Here, we will see Knapsack Problem using
Greedy method in detail, along with its algorithm and examples.

What is Knapsack Problem Using Greedy Method?

In this method, the Knapsack's filling is done so that the maximum capacity of the
knapsack is utilized so that maximum profit can be earned from it. The knapsack
problem using the Greedy Method is referred to as:
Given a list of n objects, say {I1, I2,……, In) and a knapsack (or bag).
The capacity of the knapsack is M.
Each object Ij has a weight wj and a profit of pj
If a fraction xj (where x ∈ {0...., 1)) of an object Ij is placed into a knapsack, then a
profit of pjxj is earned.
The problem (or Objective) is to fill the knapsack (up to its maximum capacity M),
maximizing the total profit earned.

Mathematically:
Note that the value of xj will be any value between 0 and 1 (inclusive). If any
object Ij is completely placed into a knapsack, its value is 1 (xj = 1). If we do not
pick (or select) that object to fill into a knapsack, its value is 0 ( xj = 0). Otherwise,
if we take a fraction of any object, then its value will be any value between 0 and
1.
Knapsack Problem Algorithm Using Greedy Method

Knapsack Problem Using Greedy Method Pseudocode

A pseudo-code for solving knapsack problems using the greedy method is;
greedy fractional-knapsack (P[1...n], W[1...n], X[1..n]. M)
/*P[1...n] and W[1...n] contain the profit and weight of the n-objects ordered such
that X[1...n] is a solution set and M is the capacity of knapsack*/
{

For j ← 1 to n do
X[j]← 0
profit ← 0 // Total profit of item filled in the knapsack
weight ← 0 // Total weight of items packed in knapsacks
j ← 1
While (Weight < M) // M is the knapsack capacity

4.Tree vertex spilitting


Let T= (V, E, W) be a directed tree. A weighted tree can be used to model a
distribution network in which electrical signals are transmitted. Nodes in the tree
correspond to receiving stations & edges correspond to transmission lines. In the
process of transmission some loss is occurred. Each edge in the tree is labeled with
the loss that occurs in traversing that edge.

The network model may not able tolerate losses beyond a certain level. In places
where the loss exceeds the tolerance value boosters have to be placed. Given a
networks and tolerance value, the TVSP problem is to determine an optimal
placement of boosters. The boosters can only placed at the nodes of the tree.

d (u) = Max { d(v) + w(Parent(u), u)} d(u) – delay of node v-set of all edges & v
belongs to child(u) δ tolerance value

TVSP (Tree Vertex Splitting Problem)


If d (u)>= δ than place the booster

. d (7)= max{0+w(4,7)}=1

d (8)=max{0+w(4,8)}=4

d (9)= max{0+ w(6,9)}=2

d (10)= max{0+w(6,10)}=3

d(5)=max{0+e(3.3)}=1

d (4)= max{1+w(2,4), 4+w(2,4)}=max{1+2,4+3}=6> δ ->booster

d (6)=max{2+w(3,6),3+w(3,6)}=max{2+3,3+3}=6> δ->booster

d (2)=max{6+w(1,2)}=max{6+4)=10> δ->booster

d (3)=max{1+w(1,3), 6+w(1,3)}=max{3,8}=8> δ ->booster

Note: No need to find tolerance value for node 1 because from source only power
is transmitting.

3.9 Single-source Shortest Paths


Let G=(V,E) be a directed graph and a main function is C(e)(c=cost, e=edge) for
the edges of graph ‘G’ and a source vertex it will represented with V0 the vertices
represents cities and weights represents distance between 2 cities. The objective of
the problem find shortest path from source to destination. The length of path is
defined to be sum of weights of edges on the path. S[i] =T if vertex i present in
set‘s’. S[i] =F if vertex i is not present in set‘s’

Formula

Min {distance[w],distance[u]+cost[u, w]}

u-recently visited node w-unvisited node

Step-1 s[1]

s[1]=T dist[2]=10

s[2]=F dist[3]=α

s[3]=F dist[4]= α

s[4]=F dist[5]= α

s[5]=F dist[6]= 30

s[6]=F dist[7]= α
S[7]=F

Kruskal's algorithm to find the minimum cost spanning tree uses the greedy
approach. This algorithm treats the graph as a forest and every node it has as an
individual tree. A tree connects to another only and only if, it has the least cost
among all available options and does not violate MST properties.
To understand Kruskal's algorithm let us consider the following example −

Step 1 - Remove all loops and Parallel Edges

Remove all loops and parallel edges from the given graph.
In case of parallel edges, keep the one which has the least cost associated and
remove all others.

Step 2 - Arrange all edges in their increasing order of weight

The next step is to create a set of edges and weight, and arrange them in an
ascending order of weightage (cost).

Step 3 - Add the edge which has the least weightage

Now we start adding edges to the graph beginning from the one which has the least
weight. Throughout, we shall keep checking that the spanning properties remain
intact. In case, by adding one edge, the spanning tree property does not hold then
we shall consider not to include the edge in the graph.
The least cost is 2 and edges involved are B,D and D,T. We add them. Adding
them does not violate spanning tree properties, so we continue to our next edge
selection.
Next cost is 3, and associated edges are A,C and C,D. We add them again −

Next cost in the table is 4, and we observe that adding it will create a circuit in the
graph. –
5.Job Sequencing problem with deadline

Problem Statement

In job sequencing problem, the objective is to find a sequence of jobs, which is


completed within their deadlines and gives maximum profit.

Solution

Let us consider, a set of n given jobs which are associated with deadlines and
profit is earned, if a job is completed by its deadline. These jobs need to be ordered
in such a way that there is maximum profit.
It may happen that all of the given jobs may not be completed within their
deadlines.
Assume, deadline of ith job Ji is di and the profit received from this job is pi.
Hence, the optimal solution of this algorithm is a feasible solution with maximum
profit.
Thus, D(i)>0D(i)>0 for 1⩽i⩽n1⩽i⩽n.
Initially, these jobs are ordered according to profit,
i.e. p1⩾p2⩾p3⩾...⩾pnp1⩾p2⩾p3⩾...⩾pn.
Algorithm: Job-Sequencing-With-Deadline (D, J, n, k)
D(0) := J(0) := 0
k := 1
J(1) := 1 // means first job is selected
for i = 2 … n do
r := k
while D(J(r)) > D(i) and D(J(r)) ≠ r do
r := r – 1
if D(J(r)) ≤ D(i) and D(i) > r then
for l = k … r + 1 by -1 do
J(l + 1) := J(l)
J(r + 1) := i
k := k + 1

Analysis

In this algorithm, we are using two loops, one is within another. Hence, the
complexity of this algorithm is O(n2)O(n2).
Example

Let us consider a set of given jobs as shown in the following table. We have to find
a sequence of jobs, which will be completed within their deadlines and will give
maximum profit. Each job is associated with a deadline and profit.

Job J1 J2 J3 J4 J5

Deadline 2 1 3 2 1

Profit 60 100 20 40 20

Solution

To solve this problem, the given jobs are sorted according to their profit in a
descending order. Hence, after sorting, the jobs are ordered as shown in the
following table.

Job J2 J1 J4 J3 J5

Deadline 1 2 2 3 1

Profit 100 60 40 20 20

From this set of jobs, first we select J2, as it can be completed within its deadline
and contributes maximum profit.
 Next, J1 is selected as it gives more profit compared to J4.
 In the next clock, J4 cannot be selected as its deadline is over, hence J3 is
selected as it executes within its deadline.
 The job J5 is discarded as it cannot be executed within its deadline.
Thus, the solution is the sequence of jobs (J2, J1, J3), which are being executed
within their deadline and gives maximum profit.
Total profit of this sequence is 100 + 60 + 20 = 180.

6. Methods of Minimum Spanning Tree


There are two methods to find Minimum Spanning Tree
1. Kruskal's Algorithm
2. Prim's Algorithm

7. Kruskal's Algorithm:
An algorithm to construct a Minimum Spanning Tree for a connected weighted
graph. It is a Greedy Algorithm. The Greedy Choice is to put the smallest weight
edge that does not because a cycle in the MST constructed so far.

If the graph is not linked, then it finds a Minimum Spanning Tree.

Steps for finding MST using Kruskal's Algorithm:

1. arrange the edge of G in order of increasing weight.


2. Starting only with the vertices of G and proceeding sequentially add each
edge which does not result in a cycle, until (n - 1) edges are used.
3. EXIT.

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 non decreasing order by weight w
5. for each edge (u, v) ∈ E, taken in non decreasing order by weight
6. do if FIND-SET (μ) ≠ if FIND-SET (v)
7. then A ← A ∪ {(u, v)}
8. UNION (u, v)
9. return A

Analysis: Where E is the number of edges in the graph and V is the number of
vertices, Kruskal's Algorithm can be shown to run in O (E log E) time, or simply,
O (E log V) time, all with simple data structures. These running times are
equivalent because:
o E is at most V2 and log V2= 2 x log V is O (log V).
o If we ignore isolated vertices, which will each their components of the
minimum spanning tree, V ≤ 2 E, so log V is O (log E).

Thus the total time is

1. O (E log E) = O (E log V).

For Example: Find the Minimum Spanning Tree of the following graph using
Kruskal's algorithm.

Solution: First we initialize the set A to the empty set and create |v| trees, one
containing each vertex with MAKE-SET procedure. Then sort the edges in E into
order by non-decreasing weight.

There are 9 vertices and 12 edges. So MST formed (9-1) = 8 edges


Now, check for each edge (u, v) whether the endpoints u and v belong to the same
tree. If they do then the edge (u, v) cannot be supplementary. Otherwise, the two
vertices belong to different trees, and the edge (u, v) is added to A, and the vertices
in two trees are merged in by union procedure.

Step1: So, first take (h, g) edge

Step 2: then (g, f) edge.


Step 3: then (a, b) and (i, g) edges are considered, and the forest becomes

Step 4: Now, edge (h, i). Both h and i vertices are in the same set. Thus it creates a
cycle. So this edge is discarded.

Then edge (c, d), (b, c), (a, h), (d, e), (e, f) are considered, and the forest
becomes.
Step 5: In (e, f) edge both endpoints e and f exist in the same tree so discarded this
edge. Then (b, h) edge, it also creates a cycle.

Step 6: After that edge (d, f) and the final spanning tree is shown as in dark lines.

Step 7: This step will be required Minimum Spanning Tree because it contains all
the 9 vertices and (9 - 1) = 8 edges

1. e → f, b → h, d → f [cycle will be formed]


8. Prim's Algorithm
It is a greedy algorithm. It starts with an empty spanning tree. The idea is to
maintain two sets of vertices:

o Contain vertices already included in MST.


o Contain vertices not yet included.

At every step, it considers all the edges and picks the minimum weight edge. After
picking the edge, it moves the other endpoint of edge to set containing MST.

Steps for finding MST using Prim's Algorithm:


1. Create MST set that keeps track of vertices already included in MST.
2. Assign key values to all vertices in the input graph. Initialize all key values
as INFINITE (∞). Assign key values like 0 for the first vertex so that it is
picked first.
3. While MST set doesn't include all vertices.
a. Pick vertex u which is not is MST set and has minimum key value.
Include 'u'to MST set.
b. Update the key value of all adjacent vertices of u. To update, iterate
through all adjacent vertices. For every adjacent vertex v, if the
weight of edge u.v less than the previous key value of v, update key
value as a weight of u.v.
MST-PRIM (G, w, r)
1. for each u ∈ V [G]
2. do key [u] ← ∞
3. π [u] ← NIL
4. key [r] ← 0
5. Q ← V [G]
6. While Q ? ∅
7. do u ← EXTRACT - MIN (Q)
8. for each v ∈ Adj [u]
9. do if v ∈ Q and w (u, v) < key [v]
10. then π [v] ← u
11. key [v] ← w (u, v)

Example: Generate minimum cost spanning tree for the following graph using
Prim's algorithm.

Solution: In Prim's algorithm, first we initialize the priority Queue Q. to contain all
the vertices and the key of each vertex to ∞ except for the root, whose key is set to
0. Suppose 0 vertex is the root, i.e., r. By EXTRACT - MIN (Q) procure, now u = r
and Adj [u] = {5, 1}.
Removing u from set Q and adds it to set V - Q of vertices in the tree. Now, update
the key and π fields of every vertex v adjacent to u but not in a tree.

1. Taking 0 as starting vertex


2. Root = 0
3. Adj [0] = 5, 1
4. Parent, π [5] = 0 and π [1] = 0
5. Key [5] = ∞ and key [1] = ∞
6. w [0, 5) = 10 and w (0,1) = 28
7. w (u, v) < key [5] , w (u, v) < key [1]
8. Key [5] = 10 and key [1] = 28
9. So update key value of 5 and 1 is:
Now by EXTRACT_MIN (Q) Removes 5 because key [5] = 10 which is minimum
so u = 5.

1. Adj [5] = {0, 4} and 0 is already in heap


2. Taking 4, key [4] = ∞ π [4] = 5
3. (u, v) < key [v] then key [4] = 25
4. w (5,4) = 25
5. w (5,4) < key [4]
6. date key value and parent of 4.

Now remove 4 because key [4] = 25 which is minimum, so u =4

1. Adj [4] = {6, 3}


2. Key [3] = ∞ key [6] = ∞
3. w (4,3) = 22 w (4,6) = 24
4. w (u, v) < key [v] w (u, v) < key [v]
5. w (4,3) < key [3] w (4,6) < key [6]

Update key value of key [3] as 22 and key [6] as 24.

And the parent of 3, 6 as 4.

1. π[3]= 4 π[6]= 4

1. u = EXTRACT_MIN (3, 6) [key [3] < key [6]]


2. u = 3 i.e. 22 < 24

Now remove 3 because key [3] = 22 is minimum so u =3.

1. Adj [3] = {4, 6, 2}


2. 4 is already in heap
3. 4 ≠ Q key [6] = 24 now becomes key [6] = 18
4. Key [2] = ∞ key [6] = 24
5. w (3, 2) = 12 w (3, 6) = 18
6. w (3, 2) < key [2] w (3, 6) < key [6]
Now in Q, key [2] = 12, key [6] = 18, key [1] = 28 and parent of 2 and 6 is 3.

1. π [2] = 3 π[6]=3

Now by EXTRACT_MIN (Q) Removes 2, because key [2] = 12 is minimum.

1. u = EXTRACT_MIN (2, 6)
2. u = 2 [key [2] < key [6]]
3. 12 < 18
4. Now the root is 2
5. Adj [2] = {3, 1}
6. 3 is already in a heap
7. Taking 1, key [1] = 28
8. w (2,1) = 16
9. w (2,1) < key [1]

So update key value of key [1] as 16 and its parent as 2.

1. π[1]= 2
Now by EXTRACT_MIN (Q) Removes 1 because key [1] = 16 is minimum.

1. Adj [1] = {0, 6, 2}


2. 0 and 2 are already in heap.
3. Taking 6, key [6] = 18
4. w [1, 6] = 14
5. w [1, 6] < key [6]

Update key value of 6 as 14 and its parent as 1.

1. Π [6] = 1

Now all the vertices have been spanned, Using above the table we get Minimum
Spanning Tree.

1. 0 → 5 → 4 → 3 → 2 → 1 → 6
2. [Because Π [5] = 0, Π [4] = 5, Π [3] = 4, Π [2] = 3, Π [1] =2, Π [6] =1]

Thus the final spanning Tree is

Total Cost = 10 + 25 + 22 + 12 + 16 + 14 = 99

10.Optimal Storage on Tapes Problem:

Given n programs P1, P2, …, Pn of length L1, L2, …, Ln respectively, store them on
a tap of length L such that Mean Retrieval Time (MRT) is a minimum. The
retrieval time of the jth program is a summation of the length of first j programs on
tap. Let Tj be the time to retrieve program Pj. The retrieval time of P j is computed
as,

Mean retrieval time of n programs is the average time required to retrieve any
program. It is required to store programs in an order such that their Mean Retrieval
Time is minimum. MRT is computed as,
Storage on Single Tape
 In this case, we have to find the permutation of the program order which
minimizes the MRT after storing all programs on single tape only.
 There are many permutations of programs. Each gives a different MRT.
Consider three programs (P1, P2, P3) with a length of (L1, L2, L3) = (5, 10,
2).
 Let’s find the MRT for different permutations. 6 permutations are possible for
3 items. The Mean Retrieval Time for each permutation is listed in the
following table.
Ordering Mean Retrieval Time (MRT)
P1, P2, P3 ( (5) + (5 + 10) + (5 + 10 + 2) ) / 3 = 37 / 3
P1, P3, P2 ( (5) + (5 + 2) + (5 + 2 + 10) ) = 29 / 3
P2, P1, P3 ( (10) + (10 + 5) + (10 + 5 + 2) ) = 42 / 3
P2, P3, P1 ( (10) + (10 + 2) + (10 + 2 + 5) ) = 39 / 3
P3, P1, P2 ( (2) + (2 + 5) + (2 + 5 + 10) ) = 26 / 3
P3, P2, P1 ( (2) + (2 + 10) + (2 + 10 + 5) ) = 31 / 3
 It should be observed from the above table that the MRT is 26/3, which is
achieved by storing the programs in ascending order of their length.
 Thus, greedy algorithm stores the programs on tape in non-decreasing order of
their length, which ensures the minimum MRT.

Algorithm for Optimal Storage on Tapes


Let L be the array of program length in ascending order. The greedy
algorithm finds the MRT as following:

Algorithm Algorithm MRT_SINGLE_TAPE(L)


// Description : Find storage order of n programs to such that mean retrieval time is
minimum
// Input : L is array of program length sorted in ascending order
// Output : Minimum Mean Retrieval Time

Tj ← 0
for i ← 1 to n do
for j ← 1 to i do
Tj ← Tj + L[j]
end
end
MRT ← Tj/ n

Complexity analysis of Optimal Storage on Tapes


Primitive operation in above algorithm is the addition of program length, which is
enclosed within two loops. The running time of algorithm is given by,
This algorithm runs in O(n2) time.
Storage on Multiple Tapes
 This is the problem of minimizing MRT on retrieval of the program from
multiple tapes.
 Instead of a single tape, programs are to be stored on multiple tapes. Greedy
algorithm solves this problem in a similar way. It sorts the programs according
to increasing length of program and stores the program in one by one in each
tape.
 The working of this approach is explained in the following example.
Example
Example: Given the program lengths L = {12, 34, 56, 73, 24, 11, 34, 56, 78, 91,
34, 91, 45}. Store them on three taps and minimize MRT
Solution:
Given data :

Pi P1 P2 P3 P4 P5 P6 P7 P8 P9 P10 P11 P12 P13


Li 12 34 56 73 24 11 34 56 78 91 34 91 45
First sort the programs in increasing order of their size.

Sorted data:

Pi P6 P1 P5 P2 P7 P11 P13 P3 P8 P4 P9 P10 P12


Li 11 12 24 34 34 34 45 56 56 73 78 91 91
Now distribute the files among all three tapes.

Tape 1 P6 P2 P13 P4 P12


Tape 2 P1 P7 P3 P9
Tape 3 P5 P11 P8 P10
MRTTape1 = ((11) + (11 + 34) + (11 + 34 + 45) + ((11 + 34 + 45 + 73)) + (11 + 34 +
45 + 73 + 91) ) / 4
= 563 / 5

= 112.6

MRTTape2 = ((12) + (12 + 34) + (12 + 34 + 56) + (12 + 34 + 56 + 78) ) / 4


= 340 / 4

= 85

MRTTape3 = ((24) + (24 + 34) + (24 + 34 + 56) + (24 + 34 + 56 + 91) ) / 4


= 353 / 4

= 88.25

MRT = (MRTTape3+ MRTTape3+ MRTTape3) / 3


= (112.6 + 85 + 88.25) / 3

= 95.28

11.Optimal merge pattern


Merge a set of sorted files of different length into a single sorted file. We need to find
an optimal solution, where the resultant file will be generated in minimum time.
If the number of sorted files are given, there are many ways to merge them into a
single sorted file. This merge can be performed pair wise. Hence, this type of merging
is called as 2-way merge patterns.
As, different pairings require different amounts of time, in this strategy we want to
determine an optimal way of merging many files together. At each step, two shortest
sequences are merged.
To merge a p-record file and a q-record file requires possibly p + q record moves,
the obvious choice being, merge the two smallest files together at each step.
Two-way merge patterns can be represented by binary merge trees. Let us consider a
set of n sorted files {f1, f2, f3, …, fn}. Initially, each element of this is considered as a
single node binary tree. To find this optimal solution, the following algorithm is used.
Algorithm: TREE (n)
for i := 1 to n – 1 do
declare new node
node.leftchild := least (list)
node.rightchild := least (list)
node.weight) := ((node.leftchild).weight) + ((node.rightchild).weight)
insert (list, node);
return least (list);
At the end of this algorithm, the weight of the root node represents the optimal
cost.

Example

Let us consider the given files, f1, f2, f3, f4 and f5 with 20, 30, 10, 5 and 30 number
of elements respectively.
If merge operations are performed according to the provided sequence, then
M1 = merge f1 and f2 => 20 + 30 = 50
M2 = merge M1 and f3 => 50 + 10 = 60
M3 = merge M2 and f4 => 60 + 5 = 65
M4 = merge M3 and f5 => 65 + 30 = 95
Hence, the total number of operations is
50 + 60 + 65 + 95 = 270
Now, the question arises is there any better solution?
Sorting the numbers according to their size in an ascending order, we get the
following sequence −
f4, f3, f1, f2, f5
Hence, merge operations can be performed on this sequence
M1 = merge f4 and f3 => 5 + 10 = 15
M2 = merge M1 and f1 => 15 + 20 = 35
M3 = merge M2 and f2 => 35 + 30 = 65
M4 = merge M3 and f5 => 65 + 30 = 95
Therefore, the total number of operations is
15 + 35 + 65 + 95 = 210
Obviously, this is better than the previous one.
In this context, we are now going to solve the problem using this algorithm.
Initial Set

Step-1

Step-2
Step-3

Step-4

Hence, the solution takes 15 + 35 + 60 + 95 = 205 number of comparisons.

12.Single Source Shortest Path

Dijkstra’s Algorithm is also known as Single Source Shortest Path (SSSP)


problem. It is used to find the shortest path from source node to destination node in
graph.

The graph is widely accepted data structure to represent distance map. The distance
between cities effectively represented using graph.
 Dijkstra proposed an efficient way to find the single source shortest path from
the weighted graph. For a given source vertex s, the algorithm finds the
shortest path to every other vertex v in the graph.
 Assumption : Weight of all edges is non-negative.
 Steps of the Dijkstra’s algorithm are explained here:
1. Initializes the distance of source vertex to zero and remaining all other
vertices to infinity.

2. Set source node to current node and put remaining all nodes in the list of
unvisited vertex list. Compute the tentative distance of all immediate neighbour
vertex of the current node.

3. If the newly computed value is smaller than the old value, then update it.

For example, C is the current node, whose distance from source S is dist (S, C) = 5.

 Consider N is the neighbour of C and weight of edge


(C, N) is 3. So the distance of N from source via C would be 8.
 If the distance of N from source was already computed and if it is greater than
8 then relax edge (S, N) and update it to 8, otherwise don’t update it.

Weight updating in Dijkstra’s algorithm


4. When all the neighbours of a current node are explored, mark it as visited.
Remove it from unvisited vertex list. Mark the vertex from unvisited vertex list
with minimum distance and repeat the procedure.

5. Stop when the destination node is tested or when unvisited vertex list
becomes empty.

Algorithm for Dijkstra’s Algorithm


Dijkstra’s shortest path algorithm is described below :

Algorithm DIJAKSTRA_SHORTEST_PATH(G, s, t)
// s is the source vertex
// t is the target vertex
// π[u] stores the parent / previous node of u
// V is the set of vertices in graph G

dist[s] ← 0
π[s] ← NIL

for each vertex v ∈ V do


if v ≠ s then
dist[v] ← ∞
π[v] ← undefined
end
ENQUEUE(v, Q) // insert v to queue Q
end

while Q is not empty do


u ← vertex in Q having minimum dist[u]
if u == t then
break
end
DEQUEUE(u, Q) // Remove u from queue Q

for each adjacent node v of u do


val ← dist[u] + weight(u, v)
if val<dist[v] then
dist[v] ← val
π[v] ← u
end
end
end

Complexity analysis of Dijkstra’s Algorithm


First for loop does initialization in O(|V|) time. As there are |V| nodes in the graph,
size of queue Q would be V, and hence while loop iterates |V| times in worst case.
For loop inside while loop run maximum |V| time, because a node can have
maximum |V| – 1 neighbours. The worst case upper bound running time of this
algorithm is described as O(|V2|).
Note: Dijkstra’s algorithm cannot handle negative weight
Examples

1. Draw a table showing the intermediate distance values of all the nodes at each
iteration of the algorithm.
2. Show the final shortest path tree.
Solution:
Here, source vertex is A.

dist[u] indicates distance of vertex u from source

π[u] indicates parent / previous node of u

Initialization:
dist[source] = 0 ⇒ dist[A] = 0

π[source] = undefined ⇒ π[A] = NIL

dist[B] = dist[C] = dist[D] = dist[E] = dist [F] = dist[G]= dist[H] = ∞

π[B] = π[C] = π[D] = π[E] = π[F] = π[G] = π[H]= NIL

Vertex u A B C D E F G H
dist[u] 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞
π[u] NIL NIL NIL NIL NIL NIL NIL NIL
Iteration 1:
u = unprocessed vertex in Q having minimum dist[u] = A

Adjacent[A] = {B, E, F}

val[B] = dist[A] + weight(A, B)

=0+1

=1

Here, val[B] <dist[B], so update dist[B]

dist[B] = 1, and π[B] = A

val[E] = dist[A] + weight(A, E)

=0+4

=4

Here, val[E] < dist[E], so update dist[E]

dist[E] = 4 and π[6] = A

val[F] = dist[A] + weight(A, F)

=0+8

=8

Here, val[F] < dist[F], so update dist[F]

dist[F] = 8 and π[F] = A

Vertex u A B C D E F G H
dist[u] 0 1 ∞ ∞ 4 8 ∞ ∞
π[u] NIL A NIL NIL A A NIL NIL
Iteration 2:
u = unprocessed vertex in Q having minimum dist[u] = B

Adjacent[B] = {C, F, G}

val[C] = dist[B] + weight(B, C)

=1+2

=3

Here, val[C] < dist[C], so update dist[C]

dist[C] = 3 and π[C] = B

val[F] = dist[B] + weight(B, F)

=1+6

=7

Here, val[F] < dist[F], so update dist[F]

dist[F] = 7 and π[F] = B

val[G] = dist[B] + weight(B, G)

=1+6

=7

Here, val[G] < dist[G], so update dist[G]

dist[G] = 7 and π[G] = B

Vertex u A B C D E F G H
dist[u] 0 1 3 ∞ 4 7 7 ∞
π[u] NIL A B NIL A B B NIL
Iteration 3:
u = unprocessed vertex in Q having minimum dist[u] = C

Adjacent [C] = {D, G}

val[D] = dist[C] + weight(C, D)


=3+1

=4

Here, val[D] < dist[D], so update dist[D]

dist[D] = 4 and π[D] = C

val[G] = dist[C] + weight(C, G)

=3+2

=5

Here, val[G] < dist[G], so update dist[G]

dist[G] = 5 and π[G] = C

Vertex u A B C D E F G H
dist[u] 0 1 3 4 4 7 5 ∞
π[u] NIL A B C A B C NIL
Iteration 4:
u = unprocessed vertex in Q having minimum dist[u] = E

Adjacent[E] = {F}

val[F] = dist[E] + weight(E, F)

=4+5

=9

Here, val[F] > dist[F], so no change in table

Vertex u A B C D E F G H
dist[u] 0 1 3 4 4 7 5 ∞
π[u] NIL A B C A B C NIL
Iteration 5:
u = unprocessed vertex in Q having minimum dist[u] = D

Adjacent[D] = {G, H}
val[G] = dist[D] + weight(D, G)

=4+1

=5

Here, val[G] = dist[G], so don’t update dist[G]

val[H] = dist[D] + weight(D, H)

=4+4

=8

Here, val[H] < dist[H], so update dist[H]

dist[H] = 8 and π[H] = D

Vertex u A B C D E F G H
dist[u] 0 1 3 4 4 7 5 8
π [u] NIL A B C A B D D
Iteration 6:
u = unprocessed vertex in Q having minimum dist[u] = G

Adjacent[G] = { F, H }

val[F] = dist[G] + weight(G, F)

=5+1

=6

Here, val[F] < dist[F], so update dist[F]

dist[F] = 6 and π[F] = G

val[H] = dist[G] + weight(G, H)

=5+1

=6

Here, val[H] < dist[H], so update dist[H]


dist[H] = 6 and π[H] = G

Vertex u A B C D E F G H
dist[u] 0 1 3 4 4 6 5 6
π [u] NIL A B C A G C G
Iteration 7:
u = unprocessed vertex in Q having minimum dist[u] = F

Adjacent[F] = { }

So, no change in table

Vertex u A B C D E F G H
dist[u] 0 1 3 4 4 6 5 6
p[u] NIL A B C A G C G
Iteration 8:
u = unprocessed vertex in Q having minimum dist[u] = H

Adjacent[H] = { }

So, no change in table

Vertex u A B C D E F G H
dist[u] 0 1 3 4 4 6 5 6
p[u] NIL A B C A G C G
We can easily derive the shortest path tree for given graph from above table. In the
table, p[u] indicates the parent node of vertex u. The shortest path tree is shown in
following figure

You might also like