Daa 3
Daa 3
Optimisation Problems
An optimization problem involves finding the best solution from all feasible solutions,
according to a specific criterion or objective function.
1. Traveling Salesman Problem (TSP): Finding the shortest possible route that
visits a set of cities and returns to the origin city.
1. Knapsack Problem: Given a set of items, each with a weight and a value,
determine the number of each item to include in a collection so that the total
weight is less than or equal to a given limit and the total value is maximized.
Common Techniques:
1. Constraints: The conditions or limitations that the solution must satisfy. For
example, budget limits, resource availability, or time constraints.
1
17-09-2024
Greedy Technique
• The Greedy Technique is an algorithmic approach used to solve optimization
problems by making a series of choices, each of which looks the most
immediately beneficial.
• The technique follows the principle of making the best possible decision at each
step (locally optimal choice), with the hope that these choices will lead to a
globally optimal solution.
• Most of the problem have n input and require us to obtain a subset that satisfy
some constraints. Any subset that satisfy these constraint is called feasible
solution. We need to find a feasible solution that either maximizes or minimizes
a given objective function which is declared as a optimal solution.
1. Solution space : All possible solution ( for given n input all possible
solution may or may not be correct.
2. Feasible solution : Any subset of solution that satisfy our constraints is
called feasible solution.
3. Optimal solution : The feasible solution, which optimise our problem by
minimising or maximising.
Key Characteristics:
1. Local Optimality: At each step, the algorithm picks the best option available
without considering the future consequences.
1. Feasibility: The algorithm ensures that the choice made at each step is
feasible within the problem’s constraints.
2
17-09-2024
• It is a classic optimization problem where we are given a set of items, each with a
weight and a profit value, and we need to determine the most profitable combination
of items to include in a knapsack that has a weight capacity limit.
• Given the weights and profits of N items, in the form of {profit, weight} put these
items in a knapsack of capacity W to get the maximum total profit in the knapsack.
• Example 1: Items with profit , weight = {{60, 30}, {100, 20}, {120, 30}}, W = 60
• Brute-force approach: The brute-force approach tries all the possible solutions
with all the different fractions but it is a time-consuming approach.
Let Xi fraction of Ith item put into knapsack then the remaining capacity equal to
M - Wi * Xi
Total Weight
Total Profit
Constraint : M >=
3
17-09-2024
Objects: 1 2 3 4 5 6 7 Objects: 1 5 7 2 6 4 3
Profit (P): 5 10 15 7 8 9 4 Profit (P): 5 8 4 10 9 7 15
Weight(w): 1 3 5 4 1 3 2 Weight(w): 1 1 2 3 3 4 5
Approach 1: Low Weighted Object Taken First. Approach 2: High Weighted Object Taken First.
4 4 1 7 10 - 4 = 6
5 1 1 8 14 - 1 = 13
6 3 1 9 6-3=3
7 2 1 4 13 - 2 = 11
2 3 1 10 3-3=0
2 3 1 10 11 - 3 = 8
4 4 1 7 5-4=1
Objects: 1 2 3 4 5 6 7 Objects: 3 2 6 5 4 1 7
Profit (P): 5 10 15 7 8 9 4 Profit (P): 15 10 9 8 7 5 4
Weight(w): 1 3 5 4 1 3 2 Weight(w): 5 3 3 1 4 1 2
Approach 3: Low Profits Object Taken First. Approach 4: High Profits Object Taken First.
2 3 1 10 10 - 3 = 7
1 1 1 5 13 - 1 = 12
6 3 1 9 7-3=4
4 4 1 7 12 - 4 = 8
5 1 1 8 4-1=3
5 1 1 8 8 -1=7
4 3 3/4 ¾ * 7 = 5.25 3-3=0
6 3 1 9 7-3=4
The total profit = 47.25
2 3 1 10 4-3=1
4
17-09-2024
2 3 4 7 1 5 6
Approach 5: High Profit / Weight ratio (P/W) based Object Taken First.
5 1 1 8 15 - 1 = 14
1 1 1 5 14 - 1 = 13
2 3 1 10 13 - 3 = 10
3 5 1 15 10 - 5 = 5
6 3 1 9 5-3=2
7 2 1 4 2-2=0
1. Calculate Profit per Weight: For each item, calculate its profit per unit of
weight.
2. Sort Items: Sort all items in decreasing order based on their profit per unit
of weight.
3. Add to Knapsack:
a) Start adding items to the knapsack,
b) beginning with the item with the highest profit per unit of weight.
c) If the whole item can’t be added due to weight constraints,
d) add as much of it as possible (i.e., take a fraction of it).
5
17-09-2024
Problem Statement
• You are given n jobs, where each job i has:
Job Profit Deadline
P [ i ]: Profit of job i
J1 100 2
D [ i ]: Deadline of job i J2 19 1
6
17-09-2024
7
17-09-2024
Huffman Coding
It is used for lossless data compression.
It efficiently, reduce the average size of character in a message by
assigning value length code to input character based on their
frequency.
Message of total A 00 A 50 A 0
100 characters that B 01 B 40 B 10
includes only four C 10 C 5 C 110
distinct characters D 11 D 5 D 111
Total bits = 50 x 1 + 40 x 2
Total bits = 100 x 2 = 200
+ 5 x 3 + 5 x 3 = 160
8
17-09-2024
Message Characters A B C D E F
Frequency 45 13 12 16 9 5
Hoffman(m)
{
n = |m|
Make a min heap Q with m.
For i = 1 to n
Allocate a new node Z
Z.left=x=Extract-min(Q)
Z.right=y=Extract-min(Q)
Z.freq=x.freq+y.freq
Insert(Q,Z)
Return(Extract-min(Q))
}
9
17-09-2024
Example 2 : for seven message, there are relative frequencies given below
Message M1 M2 M3 M4 M5 M6 M7
Frequency 4 5 7 8 10 12 20
Example 2 : for seven message, there are relative frequencies given below
Message M1 M2 M3 M4 M5 M6 M7
Frequency 4 5 7 8 10 12 20
10
17-09-2024
Exam. 3: suppose the letter ABCDEF have probability 1/2 , 1/4, 1/8 , 1/16 , 1/32 , 1/32
i. Find out the Huffman code for the given letters.
ii. Find out average code length per letter. A. 1.8579 B. 1.9375 C. 2.087 D. Non
Letters A B C D E F
Frequency 1/2 1/4 1/8 1/16 1/32 1/32
16 8 4 2 1 1
Example 4 : for seven message, there are relative frequencies given below
Message A E I O U S T
Frequency 10 15 12 3 4 13 1
11
17-09-2024
Spanning Tree
Let G = ( V , E ) is the undirected graph.
A subgraph t = ( V , E’ ) of G is a spanning tree of G iff T is tree.
Any connected subgraph with n vertices must have n-1 edges and all
connected subgraphs with n-1 edges are spanning tree.
12
17-09-2024
Step 1.1 Step 2.1 Step 3.1 Step 4.1 Step 5.1
Step 1.2 Step 2.2 Step 3.2 Step 4.2 Step 5.2
Step 6
13
17-09-2024
• Overall Time Complexity: O(E log E), dominated by the heap operations.
• In the worst case, E = O(V²) for a dense graph, giving O(V² log V).
14
17-09-2024
15
17-09-2024
16