Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
10 views

Module 4 Ada New

Uploaded by

GS SINDHU
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Module 4 Ada New

Uploaded by

GS SINDHU
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

PES Institute of Technology and Management

NH-206, Sagar Road,Shivamogga-577204

Department of Computer Science and Engineering


Affiliated to

VISVESVARAYA TECHNOLOGICAL UNIVERSITY


Jnana Sangama, Belagavi, Karnataka –590018c

Lecture Notes
on

Module 3-Analysis and Design of Algorithms


(BCS401)
2022 Scheme

Prepared By,
Mrs. Prathibha S ,
Assistant Professor,
Department of CSE,PESITM
Module 4-ADA (BCS401)

MODULE -4
Dynamic Programming
Dynamic Programming is a method used in mathematics and computer science to
solve complex problems by breaking them down into simpler subproblems. By solving each
subproblem only once and storing the results, it avoids redundant computations, leading to

more efficient solutions for a wide range of problems.

0/1 Knapsack using Dynamic Programming


Given N items where each item has some weight and profit associated with it and also given
a bag with capacity W, [i.e., the bag can hold at most W weight in it]. The task is to put the
items into the bag such that the sum of profits associated with them is the maximum
possible.

Examples:
Input: N = 3, W = 4, weight[] = {1, 2, 3}, profit[] = {4, 5, 1}
Output: Maximum Profit =5, Item 1 and 3 are chosen.

Explanation: There are two items 1 and 3 whose tota weight is equal to 4. So the
maximum possible profit is 5. Note that we cannot put only 1 and 3 because both the items
fit to the capacity 4.
Optimal Substructure: To consider all subsets of items, there can be two cases for every
item.
 Case 1: The item is included in the optimal subset.(say 1)
 Case 2: The item is not included in the optimal set.(say 0)

PESITM, Dept of CSE Prepared By, Prathibha S Page 2


Module 4-ADA (BCS401)
Solving 0/1 Knapack using Dynamic Programming

Given N=3, weight[] = {1, 2, 3}, profit[] = {10, 15, 40}, Capacity = 6

 If no element is filled, then the possible profit is 0.


weight⇢
item⇣ / 0 1 2 3 4 5 6

0 0 0 0 0 0 0 0

1
w=1,v=10

3
 For filling the first item in the bag: If we follow the above mentioned procedure, the
table will look like the following.
weight⇢
item⇣ / 0 1 2 3 4 5 6

0 0 0 0 0 0 0 0

1 0 10 10 10 10 10 10

3
 For filling the second item:
When jthWeight = 2,
then maximum possible profit is max (10, DP[1][2-2] + 15) = max(10, 15) = 15.
When jthWeight = 3, then maximum possible profit is max(2 not put, 2 is put into bag) =
max(DP[1][3], 15+DP[1][3-2]) = max(10, 25) = 25.

weight⇢
item⇣ / 0 1 2 3 4 5 6

0 0 0 0 0 0 0 0

1
0 10 10 10 10 10 10
w=1,v=10

2
0 10 15 25 25 25 25
w=2,v=15

PESITM, Dept of CSE Prepared By, Prathibha S Page 3


Module 4-ADA (BCS401)
 For filling the third item:
When jthWeight = 3,
the maximum possible profit is max(DP[2][3], 40+DP[2][3-3]) = max(25, 40) = 40.
When jthWeight = 4, the maximum possible profit is max(DP[2][4], 40+DP[2][4-3]) =
max(25, 50) = 50.
When jthWeight = 5, the maximum possible profit is max(DP[2][5], 40+DP[2][5-3]) =
max(25, 55) = 55.
When jthWeight = 6, the maximum possible profit is max(DP[2][6], 40+DP[2][6-3]) =
max(25, 65) = 65.

weight⇢
item⇣ / 0 1 2 3 4 5 6

0 0 0 0 0 0 0 0

1
0 10 10 10 10 10 10
w=1,v=10

2
0 10 15 25 25 25 25
w=2,v=15

3
w=3,v=40
0 10 15 40 50 55 65
Total capacity =6, Max Profit =65

Transitive Closure by Warshal’ Algorithm


Given a directed graph, determine if a vertex j is reachable from another vertex i for all
vertex pairs (i, j) in the given graph. Here reachable means that there is a path from vertex i
to j. The reach-ability matrix is called the transitive closure of a graph.
Example:

Transitive closure of above graphs is


1111
1111
1111
0001

PESITM, Dept of CSE Prepared By, Prathibha S Page 4


Module 4-ADA (BCS401)
To find the Transitive Closure by Wrashal’s Algorithm

Transitive Closure is

PESITM, Dept of CSE Prepared By, Prathibha S Page 5


Module 4-ADA (BCS401)

All Pair Shortest Path Problem using Floyd Algorithm


The all pair shortest path algorithm is also known as Floyd-Warshall algorithm is used to
find all pair shortest path problem from a given weighted graph. As a result of this algorithm,
it will generate a matrix, which will represent the minimum distance from any node to all
other nodes in the graph.
To find all pair shortest path using Floyd Algorithm

PESITM, Dept of CSE Prepared By, Prathibha S Page 6


Module 4-ADA (BCS401)
All Pair shortest Path Matrix is
0 3 4 1
5 0 1 6
4 7 0 5
7 2 3 0

GREEDY METHOD

General method
 The greedy method is the straight forward design technique applicable to variety of
applications. The greedy approach 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. On each step the choice made
must be:
 feasible, i.e., it has to satisfy the problem’s constraints
 locally optimal, i.e., it has to be the best local choice among all feasible choices
available on that step
 irrevocable, i.e., once made, it cannot be changed on subsequent steps of the
algorithm

Algorithm Greedy(a,n)
//Input a[1:n] conatins n inputs
solution =𝟇; //Intialize solution vector
for i=1 to n do
{
x=select (a);
If(feasible(solution,x)then
Solution =union(solution,x);
}
return solution

PESITM, Dept of CSE Prepared By, Prathibha S Page 7


Module 4-ADA (BCS401)
Greedy Knapsack

Solve the following Greedy Knapsack.


n=3, m=10, (p1, p2, p3) =(10,15,10,20,8), (w1, w2, w3) =(3,3,2,1,5)
Profit 10 15 10 20 8
Weight 3 3 2 1 5
Ratio=Profit/weight 10/3 15/3 10/2 20/1 8/5
=1.33 =5 =5 =20 =0.4
Arrange the items in descending order of ratio
Profit 20 15 10 10 8
Weight 1 3 2 3 5
Ratio=Profit/weight 20/1=20 15/3=5 10/2=5 10/3=1.33 8/5=0.4

Max profit= 20 x1 + 15x1 + 10x1 + 10x1 + 8x(1/5)


20+15+10+10+1.6
Max Profit=56.5
Capacity= 1x1 + 1x3 + 1x2+ 1x2 + 1x3 + 1x(1/5)
Solution Vector= { 1, 1, 1, 1, 0.2}

PESITM, Dept of CSE Prepared By, Prathibha S Page 8


Module 4-ADA (BCS401)

Minimum spanning tree (MST)


Minimum spanning tree (MST) is a subset of the edges of a connected, edge-weighted
graph that connects all the vertices together without any cycles and with the minimum
possible total edge weight.

Prim’s Algorithm for Minimum Cost Spanning Tree

Step 1 Step 2
Take source node and start spanning
tree

PESITM, Dept of CSE Prepared By, Prathibha S Page 9


Module 4-ADA (BCS401)
Step 3 Step 4

Step 5

Minimum Cost=46

PESITM, Dept of CSE Prepared By, Prathibha S Page 10


Module 4-ADA (BCS401)

Kruskal ‘s Algorithm for Minimum Cost Spanning Tree


Example:

Step1: Sort the edges in ascending order. Here is the list after sorting.
𝑢->v cost
4->5 2
4->6 2
3->4 3
2->3 3
3->5 4
5->6 5
2->5 6
1->2 7
1->3 8

Step 2:

PESITM, Dept of CSE Prepared By, Prathibha S Page 11


Module 4-ADA (BCS401)

Minimum cost of spanning tree= 7+3+3+2+2=17

PESITM, Dept of CSE Prepared By, Prathibha S Page 12


Module 4-ADA (BCS401)

Dijkstra’s Algorithm (Single Source Shoretst Path)

A->B=3
A->C=13
A->D=8
A->E=9
A->F=10

Intially ,Visited Array


[0] [1] [2] [3] d[4] d[5]
A B C D E F
1 0 0 0 0 0

Step 1: d[B]=3 & B not visited


d[C]=inf
Take A as source Node d[D]=8
d[0] d[1] d[2] d[3] d[4] d[5] d[E]=inf
d[A] d[B] d[c] d[d] d[e] d[f] d[F]=inf
A->A A->B A->C A->D A->E A->F
Intially Min=d[B]=A->B and
0 3 inf 8 inf inf B not visited

Min=3; u=B , (mark B visited)


Visited
[0] [1] [2] [3] d[4] d[5]
A B C D E F Take B as intermediate node
1 1 0 0 0 0 d[D]=min+B->D=3+5=8
d[E]=min+B->E=3+6=9

PESITM, Dept of CSE Prepared By, Prathibha S Page 13


Module 4-ADA (BCS401)

Step 2: d[B]=3
d[C]=inf
d[0] d[1] d[3] d[4] d[5] d[6] d[D]=8 &D not viisted
d[A] d[B] d[c] d[d] d[e] d[f] d[E]=9
A->A A->B A->C A->D A->E A->F d[F]=inf

0 3 inf 8 9 10 Min=d[D]=A->D=8
and D not visited
Visited
[0] [1] [2] [3] d[4] d[5] Min=8, u=D(mark D visited)
A B C D E F
1 1 0 1 0 0 Take D as intermediate node
d[E]=min+D->E= 8+3=11
d[F]=min+D->F= 8+2=10

Step 3: d[B]=3
d[C]=inf
Take A as source Node d[D]=8
d[0] d[1] d[2] d[3] d[4] d[5] d[E]=9 &E not visited
d[A] d[B] d[c] d[d] d[e] d[f] d[F]=10
A->A A->B A->C A->D A->E A->F
Intially Min=d[E]=A->E and
0 3 inf 8 9 10 E not visited

Min=9; u=E , (mark E visited)


Visited
[0] [1] [2] [3] d[4] d[5]
A B C D E F Take B as intermediate node
1 1 0 1 1 0 d[C]=min+E->C= 9+9=18
d[F]=min+ E->F=9+1=10

Step 4: d[B]=3
d[C]=18
Take A as source Node d[D]=8
d[0] d[1] d[2] d[3] d[4] d[5] d[E]=9
d[A] d[B] d[c] d[d] d[e] d[f] d[F]=10 &F not visited
A->A A->B A->C A->D A->E A->F
Intially Min=d[F]=A->F and
0 3 18 8 9 10 F not visited

Visited Min=10; u=F , (mark F visited)


[0] [1] [2] [3] d[4] d[5]
A B C D E F Take F as intermediate node
1 1 0 1 1 1 d[C]=min+F->C= 10+3=13

PESITM, Dept of CSE Prepared By, Prathibha S Page 14


Module 4-ADA (BCS401)

Shortest paths fromA as source node


A->B=3
A->C=13
A->D=8
A->E=9
A->F=10

Huffman Tree

Huffman coding is a lossless data compression algorithm. The idea is to assign variable-
length codes to input characters, lengths of the assigned codes are based on the
frequencies of corresponding characters.
The variable-length codes assigned to input characters are Prefix Codes, means the
codes (bit sequences) are assigned in such a way that the code assigned to one character
is not the prefix of code assigned to any other character. This is how Huffman Coding
makes sure that there is no ambiguity when decoding the generated bitstream.

Problem 1:
Construct a Huffmam Coding tree for the following data:
Character A B C D _
Probability 0.4 0.1 0.2 0.15 0.15

a)Encode the text ABACABAD using the code generated


b)Decode the text 100010111001010

PESITM, Dept of CSE Prepared By, Prathibha S Page 15


Module 4-ADA (BCS401)
Solution:
Arrange the characters in ascending order of probability or frequency
Character B D _ C A
Probability 0.1 0.15 0.15 0.2 0.4

Code word generated


Character A B C D _
Probability 0.4 0.1 0.2 0.15 0.15
Code word 1 010 011 000 001

a) Encode the text

A B A C A B A D
1 010 1 011 1 010 1 000
So, 1010101110101000
b) Decode the text
1 000 1 011 1 001 010
A D A C A _ B
So,
ADACA_B

PESITM, Dept of CSE Prepared By, Prathibha S Page 16


Module 4-ADA (BCS401)
Problem 2:

PESITM, Dept of CSE Prepared By, Prathibha S Page 17

You might also like