34424tpnews_11082020
34424tpnews_11082020
34424tpnews_11082020
Algorithms (BCS-28)
[B Tech IIIrd Year, Vth Sem, Session: 2020-21]
• Areas of Application
– Greedy approach is used to solve many problems, such as
– Finding the shortest path between two vertices using Dijkstra’s algorithm.
– Finding the minimal spanning tree in a graph using Prim’s /Kruskal’s algorithm, etc.
Greedy Algorithm: Properties
• "A greedy algorithm works if a problem exhibits the following two properties:
• Example:
– machine scheduling
– Fractional Knapsack Problem
– Minimum Spanning Tree
– Huffman Code
– Job Sequencing
– Activity Selection Problem
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.
• Applications
– In many cases of resource allocation along with some constraint, the problem can be
derived in a similar way of Knapsack problem. Following is a set of example.
– Finding the least wasteful way to cut raw materials
– portfolio optimization
– Cutting stock problems
Knapsack Problem: Problem Scenario
• Problem Scenario
– A thief is robbing a store and can carry a maximal weight of W into his knapsack. There
are n items available in the store and weight of ith item is wi and its profit is pi. What
items should the thief take?
– In this context, the items should be selected in such a way that the thief will carry those
items for which he will gain maximum profit. Hence, the objective of the thief is to
maximize the profit.
• Based on the nature of the items, Knapsack problems are categorized as
– Fractional Knapsack
– Knapsack
Fractional Knapsack: Generalized
Solution to the problem
• In this case, items can be broken into smaller pieces, hence the thief can select
fractions of items.
• According to the problem statement,
There are n items in the store
Weight of ith item wi>0
Profit for ith item pi>0
and
Capacity of the Knapsack is W
• In this version of Knapsack problem, items can be broken into smaller pieces. So,
the thief may take only a fraction xi of ith item.
0⩽xi⩽1
Fractional Knapsack: Generalized
Solution to the problem
The ith item contributes the weight xi.wi
to the total weight in the knapsack and profit xi.pito the total profit.
Hence, the objective of this algorithm is to
subject to constraint,
It is clear that an optimal solution must fill the knapsack exactly, otherwise we could
add a fraction of one of the remaining items and increase the overall profit.
Thus, an optimal solution can be obtained by
In this context, first we need to sort those items according to the value of pi/wi, so
that pi+1/wi+1 ≤ pi/wi . Here, x is an array to store the fraction of items.
• Analysis
– If the provided items are already sorted into a decreasing order of pi/wi then the
whileloop takes a time in O(n); Therefore, the total time including the sort is in O(n
logn).
Fractional Knapsack
• Fractions of items can be taken rather than having to make binary (0-1) choices for
each item.
• Steps to solve the Fractional Problem:
– Compute the value per pound for each item.
– Obeying a Greedy Strategy, we take as possible of the item with the highest value per
pound.
– If the supply of that element is exhausted and we can still carry more, we take as much
as possible of the element with the next value per pound.
– Sorting, the items by value per pound, the greedy algorithm run in O (n log n) time.
Fractional Knapsack : Method
• Fractions of items can be taken rather than having to make binary (0-1) choices for
each item.
• Fractional Knapsack Problem can be solvable by greedy strategy whereas 0 - 1
problem is not.
• Steps to solve the Fractional Problem:
– Compute the value per pound for each item.
– Obeying a Greedy Strategy, we take as possible of the item with the highest value per
pound.
– If the supply of that element is exhausted and we can still carry more, we take as much
as possible of the element with the next value per pound.
– Sorting, the items by value per pound, the greedy algorithm run in O (n log n) time.
Fractional Knapsack : Ist Example
Let us consider that the capacity of the knapsack W = 60 and the list of provided items are shown
in the following table −
As the provided items are not sorted based on piwi. After sorting, the items are as shown in the
following table.
Fractional Knapsack : Example (Contd…)
• Solution
After sorting all the items according to pi/wi.
First all of B is chosen as weight of B is less than the capacity of the knapsack. Next,
item A is chosen, as the available capacity of the knapsack is greater than the
weight of A. Now, C is chosen as the next item. However, the whole item cannot be
chosen as the remaining capacity of the knapsack is less than the weight of C.
Hence, fraction of C (i.e. (60 − 50)/20) is chosen.
Now, the capacity of the Knapsack is equal to the selected items. Hence, no more item
can be selected.
The total weight of the selected items is 10 + 40 + 20 * (10/20) = 60
And the total profit is 100 + 280 + 120 * (10/20) = 380 + 60 = 440
This is the optimal solution. We cannot gain more profit selecting any different
combination of items.
Fractional Knapsack : 2nd Example
(Contd…)
• 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)>0 for 1⩽i⩽n.
Initially, these jobs are ordered according to profit, i.e. p1⩾p2⩾p3⩾...⩾pn.
Job Sequencing with deadline
Algorithm
Job Sequencing with deadline
• Analysis
– In this algorithm, we are using two loops, one is within another. Hence, the complexity
of this algorithm is O(n^2)
• 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 Sequencing with deadline
• 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.
• 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.
Optimal Cost Binary Search Tree
• A Binary Search Tree (BST) is a tree where the key values are stored in the internal
nodes. The external nodes are null nodes. The keys are ordered lexicographically,
i.e. for each internal node all the keys in the left sub-tree are less than the keys in
the node, and all the keys in the right sub-tree are greater.
• Search time of an element in a BST is O(n), whereas in a Balanced-BST search time
is O(log n). Again the search time can be improved in Optimal Cost Binary Search
Tree, placing the most frequently used data in the root and closer to the root
element, while placing the least frequently used data near leaves and in leaves.
• Here, the Optimal Binary Search Tree Algorithm is presented. First, we build a BST
from a set of provided n number of distinct keys < k1, k2, k3, ... kn >. Here we
assume, the probability of accessing a key Ki is pi. Some dummy keys (d0, d1, d2, ...
dn) are added as some searches may be performed for the values which are not
present in the Key set K. We assume, for each dummy key di probability of access
is qi.
• Analysis
– The algorithm requires O (n3) time, since three nested for loops are used. Each of these
loops takes on at most n values.
Optimal Cost Binary Search Tree:
Example
• Considering the following tree, the
cost is 2.80, though this is not an
optimal result.
Optimal Cost Binary Search Tree:
Example
• To get an optimal solution, using the algorithm discussed in this chapter, the
following tables are generated.
• In the following tables, column index is i and row index is j.
Optimal Cost Binary Search Tree:
Example
• Analysis
– There are at the most 2^n.n
– sub-problems and each one takes linear time to solve. Therefore, the total running time
is O(2^n.n^2).
Travelling Salesman Problem (Contd…)
• Example
In the following example, we will illustrate the steps to solve the travelling salesman problem.
• In case of parallel edges, keep the one which has the least cost associated and
remove all others.
Kruskal’s Spanning Tree (Contd…)
• 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).
• Next cost in the table is 4, and we observe that adding it will create a circuit in the
graph. −
Kruskal’s Spanning Tree (Contd…)
• We ignore it. In the process we shall ignore/avoid all edges that create a circuit.
• We observe that edges with cost 5 and 6 also create circuits. We ignore them and
move on.
• Now we are left with only one node to be added. Between the two least cost
edges available 7 and 8, we shall add the edge with cost 7.
Kruskal’s Spanning Tree (Contd…)
• By adding edge S,A we have included all the nodes of the graph and we now have
minimum cost spanning tree
Prim’s Spanning Tree
• Prim's algorithm to find minimum cost spanning tree (as Kruskal's algorithm) uses
the greedy approach. Prim's algorithm shares a similarity with the shortest path
first algorithms.
• Prim's algorithm, in contrast with Kruskal's algorithm, treats the nodes as a single
tree and keeps on adding new nodes to the spanning tree from the given graph.
• To contrast with Kruskal's algorithm and to understand Prim's algorithm better, we
shall use the same example −
Prim’s Spanning Tree (Contd…)
• 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.
Prim’s Spanning Tree (Contd…)
• Step 2 - Choose any arbitrary node as root node
• In this case, we choose S node as the root node of Prim's spanning tree. This node
is arbitrarily chosen, so any node can be the root node. One may wonder why any
video can be a root node. So the answer is, in the spanning tree all the nodes of a
graph are included and because it is connected then there must be at least one
edge, which will join it to the rest of the tree.
• Step 3 - Check outgoing edges and select the one with less cost
• After choosing the root node S, we see that S,A and S,C are two edges with weight
7 and 8, respectively. We choose the edge S,A as it is lesser than the other.
Prim’s Spanning Tree (Contd…)
• Now, the tree S-7-A is treated as one node and we check for all edges going out
from it. We select the one which has the lowest cost and include it in the tree.
• After this step, S-7-A-3-C tree is formed. Now we'll again treat it as a node and will
check all the edges again. However, we will choose only the least cost edge. In this
case, C-3-D is the new edge, which is less than other edges' cost 8, 6, 4, etc.
Prim’s Spanning Tree (Contd…)
• After adding node D to the spanning tree, we now have two edges going out of it
having the same cost, i.e. D-2-T and D-2-B. Thus, we can add either one. But the
next step will again yield edge 2 as the least cost. Hence, we are showing a
spanning tree with both edges included.
• We may find that the output spanning tree of the same graph using two different
algorithms is same.
Shortest Path
• Dijkstra’s Algorithm
– Dijkstra’s algorithm solves the single-source shortest-paths problem on a directed
weighted graph G = (V, E), where all the edges are non-negative (i.e., w(u, v) ≥ 0 for each
edge (u, v) Є E).
– In the following algorithm, we will use one function Extract-Min(), which extracts the
node with the smallest key.
Shortest Path: Dijkstra’s Algorithm
• Analysis
• The complexity of this algorithm is fully dependent on the implementation of
Extract-Min function. If extract min function is implemented using linear search,
the complexity of this algorithm is O(V2 + E).
• In this algorithm, if we use min-heap on which Extract-Min() function works to
return the node from Q with the smallest key, the complexity of this algorithm can
be reduced further.
• Example
• Let us consider vertex 1 and 9 as the start and destination vertex respectively.
Initially, all the vertices except the start vertex are marked by ∞ and the start
vertex is marked by 0.
Shortest Path: Dijkstra’s Algorithm
• Example
– Let us consider vertex 1 and 9 as the start and destination vertex respectively. Initially, all
the vertices except the start vertex are marked by ∞ and the start vertex is marked by 0.
Shortest Path: Dijkstra’s Algorithm
• Hence, the minimum distance of vertex 9 from vertex 1 is 20. And the path is
1→ 3→ 7→ 8→ 6→ 9
• This path is determined based on predecessor information.
Shortest Path: Bellman Ford Algorithm
• This algorithm solves the single source shortest path problem of a directed graph
G = (V, E) in which the edge weights may be negative. Moreover, this algorithm can
be applied to find the shortest path, if there does not exist any negative weighted
cycle.
Shortest Path: Bellman Ford Algorithm
• Analysis
– The first for loop is used for initialization, which runs in O(V) times. The next for loop
runs |V - 1| passes over the edges, which takes O(E) times.
– Hence, Bellman-Ford algorithm runs in O(V, E) time.
• Example
– The following example shows how Bellman-Ford algorithm works step by step. This
graph has a negative edge but does not have any negative cycle, hence the problem can
be solved using this technique.
– At the time of initialization, all the vertices except the source are marked by ∞ and the
source is marked by 0.
Shortest Path: Bellman Ford
Algorithm
• In the first step, all the vertices which are reachable from the source are updated
by minimum cost. Hence, vertices a and h are updated
• Overlapping Sub-Problems
– Similar to Divide-and-Conquer approach, Dynamic Programming also
combines solutions to sub-problems. It is mainly used where the solution of
one sub-problem is needed repeatedly. The computed solutions are stored in
a table, so that these don’t have to be re-computed. Hence, this technique is
needed where overlapping sub-problem exists.
– For example, Binary Search does not have overlapping sub-problem. Whereas
recursive program of Fibonacci numbers have many overlapping sub-
problems.
Dynamic Programming (Contd…)
• Optimal Sub-Structure
– A given problem has Optimal Substructure Property, if the optimal solution of the given
problem can be obtained using optimal solutions of its sub-problems.
– For example, the Shortest Path problem has the following optimal substructure property
−
– If a node x lies in the shortest path from a source node u to destination node v, then the
shortest path from u to v is the combination of the shortest path from u to x, and the
shortest path from x to v.
– The standard All Pair Shortest Path algorithms like Floyd-Warshall and Bellman-Ford are
typical examples of Dynamic Programming.
Dynamic Programming: Steps and
Application Areas
• Steps of Dynamic Programming Approach
– Dynamic Programming algorithm is designed using the following four steps −
– Characterize the structure of an optimal solution.
– Recursively define the value of an optimal solution.
– Compute the value of an optimal solution, typically in a bottom-up fashion.
– Construct an optimal solution from the computed information.
• Hence, it can be concluded that Greedy approach may not give an optimal
solution.
• To solve 0-1 Knapsack, Dynamic Programming approach is required.
Knapsack: Dynamic Approach (Contd…)
• Problem Statement
– A thief is robbing a store and can carry a maximal weight of W into his knapsack. There
are n items and weight of ith item is wi and the profit of selecting this item is pi. What
items should the thief take?
• Dynamic-Programming Approach
– Let i be the highest-numbered item in an optimal solution S for W dollars.
Then S' = S - {i} is an optimal solution for W - wi dollars and the value to the
solution S is Vi plus the value of the sub-problem.
– We can express this fact in the following formula: define c[i, w] to be the
solution for items 1,2, … , i and the maximum weight w.
– The algorithm takes the following inputs
• The maximum weight W
• The number of items n
• The two sequences v = <v1, v2, …, vn> and w = <w1, w2, …, wn>
Knapsack: Dynamic Approach (Contd…)
• The set of items to take can be deduced from the table, starting at c[n, w] and
tracing backwards where the optimal values came from.
• If c[i, w] = c[i-1, w], then item i is not part of the solution, and we continue tracing
with c[i-1, w]. Otherwise, item i is part of the solution, and we continue tracing
with c[i-1, w-W].
• Analysis
– This algorithm takes θ(n, w) times as table c has (n + 1).(w + 1) entries, where each entry
requires θ(1) time to compute.
Multistage Graph: Dynamic Approach
• A multistage graph G = (V, E) is a directed graph where vertices are partitioned into
k (where k > 1) number of disjoint subsets S = {s1,s2,…,sk} such that edge (u, v) is in
E, then u Є si and v Є s1 + 1 for some subsets in the partition and |s1| = |sk| = 1.
• The vertex s Є s1 is called the source and the vertex t Є sk is called sink.
• G is usually assumed to be a weighted graph. In this graph, cost of an edge (i, j) is
represented by c(i, j). Hence, the cost of path from source s to sink t is the sum of
costs of each edges in this path.
• The multistage graph problem is finding the path with minimum cost from source s
to sink t.
Multistage Graph Example
• Consider the following example to understand the concept of multistage graph.
• According to the formula, we have to calculate the cost (i, j) using the following
steps
Multistage Graph Example (Contd…)
• Step-1: Cost (K-2, j)
– In this step, three nodes (node 4, 5. 6) are selected as j. Hence, we have three options to
choose the minimum cost at this step.
– Cost(3, 4) = min {c(4, 7) + Cost(7, 9),c(4, 8) + Cost(8, 9)} = 7
– Cost(3, 5) = min {c(5, 7) + Cost(7, 9),c(5, 8) + Cost(8, 9)} = 5
– Cost(3, 6) = min {c(6, 7) + Cost(7, 9),c(6, 8) + Cost(8, 9)} = 5
• Step-2: Cost (K-3, j)
– Two nodes are selected as j because at stage k - 3 = 2 there are two nodes, 2 and 3. So,
the value i = 2 and j = 2 and 3.
– Cost(2, 2) = min {c(2, 4) + Cost(4, 8) + Cost(8, 9),c(2, 6) +
– Cost(6, 8) + Cost(8, 9)} = 8
– Cost(2, 3) = {c(3, 4) + Cost(4, 8) + Cost(8, 9), c(3, 5) + Cost(5, 8)+ Cost(8, 9), c(3, 6) +
Cost(6, 8) + Cost(8, 9)} = 10
Multistage Graph Example (Contd…)
• Step-3: Cost (K-4, j)
– Cost (1, 1) = {c(1, 2) + Cost(2, 6) + Cost(6, 8) + Cost(8, 9), c(1, 3) + Cost(3, 5) + Cost(5, 8) +
Cost(8, 9))} = 12
– c(1, 3) + Cost(3, 6) + Cost(6, 8 + Cost(8, 9))} = 13
• Solution:
Example: Floyd Warshall Algorithm
• Step (i) When k = 0
• Table: Number of patients served (left) and boundary conditions for Sk (x) (right)
Resource allocation Problem:
Numerical
Resource allocation Problem:
Numerical