DAA - Unit 3 (PG)
DAA - Unit 3 (PG)
An optimal solution is one that provides the best possible outcome based on a specific
objective or metric.
A feasible solution, on the other hand, is one that meets all the constraints or
requirements of the problem without necessarily being the best possible solution
KNAPSACK PROBLEM
The knapsack problem (or also called as fractional knapsack problem) is one
of the techniques which uses Greedy Method. In fractional knapsack, the items are
broken in order to maximize the profit. The problem in which we break the item is
known as a Fractional knapsack problem.
In Greedy approach, we calculate the ratio of profit/weight, and accordingly,
we will select the item. The item with the highest ratio would be selected first.
There are basically three approaches to solve the problem:
The first approach is to select the item based on the maximum profit.
The second approach is to select the item based on the minimum weight.
The third approach is to calculate the ratio of profit/weight.
In general, the ratio of profit / weight will give the best result rather than the two
approaches. The main time taking step is the sorting of all items in decreasing order
of their value / weight ratio. If the items are already arranged in the required order,
then while loop takes O(n) time. Therefore, total time taken including the sort is
O(nlogn).
Step 1
Calculate the p/w
After object 5, object 1 has the maximum profit/weight ratio, i.e., 5. So, we select
object 1
Object Profit Weight Remaining Weight
5 8 1 15 – 1 = 14
1 5 1 14 – 1 = 13
After object 1, object 2 has the maximum profit/weight ratio, i.e., 3.3. So, we select
object 2 having profit/weight ratio as 3.3.
Object Profit Weight Remaining Weight
5 8 1 15 – 1 = 14
1 5 1 14 – 1 = 13
2 10 3 13 – 3 = 10
After object 2, object 3 has the maximum profit/weight ratio, i.e., 3. So, we select
object 3 having profit/weight ratio as 3.
Object Profit Weight Remaining Weight
5 8 1 15 – 1 = 14
1 5 1 14 – 1 = 13
2 10 3 13 – 3 = 10
3 15 5 10 – 5 = 5
As we can observe in the above table that the remaining weight is zero which
means that the knapsack is full. We cannot add more objects in the knapsack.
Therefore, the total profit would be equal to (8 + 5 + 10 + 15 + 9 + 4), i.e., 51.
2) Let us consider that the capacity of the knapsack W = 60 and the list of provided
items are shown in the following table –
We found three spanning trees off one complete graph. A complete undirected
graph can have maximum nn-2 number of spanning trees, where n is the number of
nodes. In the above addressed example, n is 3, hence 33−2 = 3 spanning trees are
possible.
General Properties of Spanning Tree
A connected graph G can have more than one spanning tree.
All possible spanning trees of graph G, have the same number of edges and
vertices.
The spanning tree does not have any cycle (loops).
Removing one edge from the spanning tree will make the graph disconnected,
i.e. the spanning tree is minimally connected.
Example:
The set mstSet is initially empty and keys assigned to vertices are {0, INF, INF, INF,
INF, INF, INF, INF} where INF indicates infinite. Now pick the vertex with
minimum key value. The vertex 0 is picked, include it in mstSet. So mstSet becomes
Pick the vertex with minimum key value and not already
included in MST (not in mstSET). We can either pick vertex 7
or vertex 2, let vertex 7 is picked. So mstSet now becomes {0,
1, 7}. Update the key values of adjacent vertices of 7. The key
value of vertex 6 and 8 becomes finite (1 and 7 respectively).
Step 3:
Pick the vertex with minimum key value and not already
included in MST (not in mstSET). Vertex 6 is picked. So
mstSet now becomes {0, 1, 7, 6}. Update the key values of
adjacent vertices of 6. The key value of vertex 5 and 8 are
updated.
Step 4:
KRUSKAL'S ALGORITHM
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. This algorithm was
written by Joseph Kruskal.
Example:
Step 1:
Step 2:
Pick edge 8-2: No cycle is formed, include it.
Step 3:
Pick edge 6-5: No cycle is formed, include it.
Step 4:
Pick edge 0-1: No cycle is formed, include it
Step 6:
Pick edge 8-6: Since including this edge results in cycle, discard it.
Step 7:
Pick edge 2-3: No cycle is formed, include it.
Step 8:
Pick edge 7-8: Since including this edge results in cycle, discard it.
Step 9:
Pick edge 0-7: No cycle is formed, include it.
Step 10:
Pick edge 1-2: Since including this edge results in cycle, discard it.
Step 11:
Pick edge 3-4: No cycle is formed, include it.
This is the final MST of the given tree using Kruskal’s Algorithm.
Try it yourself:
Find the minimum shortest path for the following graphs using Dijsktra’s Algorithm