Uploaded by Vijaya Kumar Thirthahalli

DAA Lab Programs 6, 8 and 9

advertisement
 Program – 6
Implement in Java, the 0/1 Knapsack
problem using
(a) Dynamic Programming method
(b) Greedy method.
import java.util.Scanner;
class KObject
{
details
float w;
float p;
float r;
}
public class KnapsackGreedy
{
static final int MAX = 20;
static int n;
static float M;
// Knapsack object
// max. no. of objects
// no. of objects
// capacity of Knapsack
public static void main(String args[ ])
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter number of objects: ");
n = scanner.nextInt( );
// n=3
KObject[ ] obj = new KObject[n]; Obj[0]
Obj[1]
Obj[2]
for(int i = 0; i<n;i++)
// allocate memory
for members
Obj[i]
ReadObjects(obj);
Knapsack(obj);
scanner.close();
}
W
P
r
0
1
2
static void ReadObjects(KObject obj[ ])
{
KObject temp = new KObject( );
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the max capacity of knapsack: ");
M = scanner.nextFloat( );
//m=20
System.out.println("Enter Weights: ");
0
1
2
p
obj[i].w = scanner.nextFloat( );
25
24
15
w
18
15
10
for (int i = 0; i < n; i++)
System.out.println("Enter Profits: ");
for (int i = 0; i < n; i++)
r
obj[i].p = scanner.nextFloat( );
temp
for (int i = 0; i < n; i++)
0
1
2
p
25
24
15
w
18
15
10
r
1.38
1.6
1.5
1
2
temp
obj[i].r = obj[i].p / obj[i].w;
temp
// sort objects in descending order, based on p/w ratio
0
for(int i = 0; i<n-1; i++)
for(int j=0; j<n-1-i; j++)
if(obj[j].r < obj[j+1].r)
{
p
24
15
25
w
15
10
18
r
1.6
1.5
1.38
0
1
2
temp = obj[j];
p
24
25
15
obj[j] = obj[j+1];
w
15
18
10
r
1.6
1.38
1.5
obj[j+1] = temp;
0
1
2
p
24
15
25
w
15
10
18
r
1.6
1.5
1.38
}
scanner.close( );
}
temp
temp
static void Knapsack(KObject kobj[ ])
{
float x[ ] = new float[MAX];
float totalprofit;
int i;
float U; // U place holder for M
U = M;
//rc is represented as U so U=20
ie., M=20
totalprofit = 0;
for (i = 0; i < n; i++)
x[i] = 0; // intialize x to 0
// solution vector x[]={ 0 , 0 , 0}
for (i = 0; i < n; i++)
{
if (kobj[i].w > U)
break;
else
{
}
1
2
p
24
15
25
w
15
10
18
r
1.6
1.5
1.38
temp
i=0 If(15>20) F
Else
X[0]=1
Totalprofit=0+24=24
x[i] = 1;
U=20-15=5
totalprofit = totalprofit + kobj[i].p;
U = U - kobj[i].w;
}
0
Solution vector X[i]={1, 0, 0}
i=1
If(10>5) T
Print
i=1
System.out.println("i = " + i);
if (i < n)
If(10>5) T
If(1<3) T
X[1]=5/10=0.5
Totalprofit=24+(0.5*15)=31.5
{
x[i] = U / kobj[i].w;
Solution vector X[i]={1, 0.5, 0}
totalprofit = totalprofit + (x[i] * kobj[i].p);
}
System.out.println("The Solution vector, x[ ]: ");
for (i = 0; i < n; i++)
System.out.print(x[i] + " ");
System.out.println("\nTotal profit is = " + totalprofit);
0
1
2
p
24
15
25
w
15
10
18
r
1.6
1.5
1.38
}
}
temp
Enter number of objects:
3
Enter the max capacity of knapsack:
20
Enter Weights:
18 15 10
Enter Profits:
25 24 15
i=1
The Solution vector, x[ ]:
1.0 0.5 0.0
Total profit is = 31.5
OUTPUT 2:
Enter number of objects:
3
Enter the max capacity of knapsack:
30
Enter Weights:
20 15 15
Enter Profits:
40 25 25
i=1
The Solution vector, x[ ]:
1.0 0.6666667 0.0
Total profit is = 56.666668
Program-8
Kruskals Lab Program
Find
of a given connected undirected graph
using Kruskal's algorithm. Use Union-Find algorithms in your program.
import java.util.Scanner;
public class KruskalsClass
{
final static int MAX = 20;
static int n; // No. of vertices of G
static int cost[ ][ ]; // Cost matrix
static int parent[ ] = new int[9];
P[1]
P[2]
P[3]
P[4]
P[5]
0
0
0
0
0
static Scanner scan = new Scanner(System.in);
public static void main(String[ ] args)
{
ReadMatrix( );
//to read the inputs
Kruskals( ); //to construct minimum spanning tree
}
static void ReadMatrix( )
{
int i, j;
cost = new int[MAX][MAX];
System.out.println("\n Enter the number of nodes:");
n = scan.nextInt( );
//n=5
System.out.println("\n Enter the cost adjacency matrix:\n");
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
{
cost[i][j] = scan.nextInt( );
if (cost[i][j] == 0)
cost[i][j] = 999;
}
}
1
b
5
3
6
a
4
c
6
2
d
e
a
b
c
d
e
a
0
5
999
6
999
b
5
0
1
3
999
c
999
1
0
4
6
d
6
3
4
0
2
e
999
999
6
2
0
Diagonal values (0) are replaced
with (999)
a
b
c
d
e
a
999
5
999
6
999
b
5
999
1
3
999
c
999
1
999
4
6
d
6
3
4
999
2
e
999
999
6
2
999
static void Kruskals( )
{
int a = 0, b = 0, u = 0, v = 0;
int i, j, ne = 1, min, mincost = 0;
System.out.println("The edges of Minimum Cost Spanning Tree are");
a
b
c
d
e
while (ne < n)
a
999 5
999
6
999
{
b
5
999 1
3
999
for (i = 1, min = 999; i <= n; i++)
c
999 1
999
4
6
for (j = 1; j <= n; j++)
d
6
3
4
999
2
if (cost[i][j] < min)
e
999 999 6
2
999
{
min = cost[i][j];
a = u = i;
2 4
2 3 1
b = v = j;
3 5
4 4 2
}
u = find(u);
//u=find(2)=2 4 2 2 1
v = find(v);
//v=find(3)=3 5 4 2 2
if (u != v)
{
uni(u, v);
System.out.println(ne++ + "edge (" + a + "," + b + ") =" + min);
mincost += min;
}
cost[a][b] = cost[b][a] = 999;
}//end while
System.out.println("Minimum cost :" + mincost);
}//end kruskals
i=1, j=1
if(cost[1][1]<min)
if(999<999)F
j++
i=1, j=2
if(cost[1][2]<min)
if(5<999)T
min=cost[1][2]=5
a=u=i (a=u=1)
b=v=j (b=v=2)
j++
i=1,j=3
if(cost[1][3]<min)
if(999<5)F
j++
i=1, j=4
if(cost[1][4]<min)
if(6<5) F
j++
i=2, j=1
if(cost[2][1]<min)
If(5<5)F
j++
i=2, j=2
if(cost[2][2]<min)
If(999<5)F
j++
i=2,j=3
if(cost[2][3]<min)
if(1<5)1
min=cost[2][3]=1
a=u=i (a=u=2)
b=v=j (b=v=3)
j++
i=2, j=4
if(cost[2][4]<min)
if(3<1)F
j++
i=1, j=5
If(cost[1][5]<min)
If(999<5) F
j++
i=2, j=5
If(cost[2][5]<min)
If(999<1)F
j++
(j<=n) F end for loop
i++
(j<=n) F end for loop
i++
if(2!=3)
{uni(2,3)
ne++=2
Print(edge(2,3)=1)
mincost=0+1=1}
cost[2][3]=cost[3][2]=999
a
b
c
d
e
a
999
5
999
6
999
b
5
999
999
3
999
c
999
999
999
4
6
d
6
3
4
999
2
e
999
999
6
2
999
static int find(int i)
{
while (parent[i] > 0)
i = parent[i];
return i;
}
static void uni(int i, int j)
{
parent[j] = i;
}
} // class
a
b
c
d
e
a
999
5
999
6
999
b
5
999
999
999
999
c
999
999
999
4
6
d
6
999
4
999
999
e
999
999
6
999
999
P[1]
P[2]
P[3]
P[4]
P[5]
0
0
0
0
0
find(2)
while(p[2]>0)
while(0>0) F
Return 2;
Uni(2,3)
{
P[3]=2;
}
while(4<5)
{
i=3,j=4
if(cost[3][4]<min)
if((4<5) T
min=4
a=u=3
b=v=4
find(3)
while(p[3]>0)F
while(0>0) F
Return 3;
P[1]
P[2]
P[3]
P[4]
P[5]
0
0
2
0
0
0
0
2
0
4
0
0
2
2
4
find[3]
{
while(P[3]>0)
while(2>0)T
i=P[2]=2
return 2;
}
find[4]
{
while(P[4]>0)
while(2>0)T
i=P[2]=2
return 2;
}
If(u!=v)
If(2!=2) F
This indicates that including that edge leads to form
a cycle
ne is not incremented so for the next while loop ne
is still 4 (ne=4)
cost[3][4]=cost[4][3]=999 } end while
P[1]
P[2]
P[3]
P[4]
P[5]
0
0
2
2
4
Tree vertices
Remaining vertices
-
(b,c)=1 (d,e)=2 (b,d)=3 (c,d)=4
(a,b)=5 (a,d)=6 (c,e)=6
(b,c)=1
(d,e)=2
(d,e)=2 (b,d)=3 (c,d)=4 (a,b)=5
(a,d)=6 (c,e)=6
(b,d)=3 (c,d)=4 (a,b)=5 (a,d)=6
(c,e)=6
Illustration
b
c
a
d
1
b
a
a
(a,b)=5
c
2
d
a
(c,d)=4
e
1
b
(c,d)=4 (a,b)=5 (a,d)=6 (c,e)=6
Adding this edge eads to cycle
(a,b)=5 (a,d)=6 (c,e)=6
c
d
b
(b,d)=3
e
1
3
e
c
2
d
e
No changes in the graph
No of count of edges is not
incremented
5
(a,d)=6 (c,e)=6
a
b
1
3
d
c
2
e
Program-9
Prims Lab Program
Find Minimum Cost Spanning Tree of a given connected
undirected graph using Prim's algorithm.
import java.util.Scanner;
public class PrimsClass
{
final static int MAX = 20;
static int n; // No. of vertices of G
static int cost[ ][ ]; // Cost matrix
static Scanner scan = new Scanner(System.in);
public static void main(String[ ] args)
{
ReadMatrix( );
Prims( );
}
//to read the inputs
//to construct minimum spanning tree
static void ReadMatrix( )
{
int i, j;
cost = new int[MAX][MAX];
1
7
3
5
2
2
4
3
4
System.out.println("\n Enter the number of nodes:");
n = scan.nextInt( );
//n=4
System.out.println("\n Enter the adjacency matrix:\n");
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
{
cost[i][j] = scan.nextInt( );
Diagonal values (0) are replaced
with (999)
if (cost[i][j] == 0)
cost[i][j] = 999;
}
}
static void Prims( )
{
int visited[ ] = new int[10];
V[1]
V[2]
V[3]
V[4]
0
0
0
0
int ne = 1, i, j, min, a = 0, b = 0, u = 0, v = 0;
int mincost = 0;
visited[1] = 1;
V[1]
V[2]
V[3]
V[4]
1
0
0
0
i=1, j=1 , min=999
if(cost[1][1]<min) if(999<999)F j++
1
while (ne < n)
i=1, j=2
2
1
999
5
7
if(cost[1][2]<min)=(5<999)T
2
5
999 999
3
{
if(visited[1] != 0) T
3
7
999 999
4
for (i = 1, min = 999; i <= n; i++)
2
999
min=cost[1][2]=5
4
3
4
for (j = 1; j <= n; j++)
a=u=i (a=u=1)
b=v=j (b=v=2)
j++
if (cost[i][j] < min)
i=1,j=3
if (visited[i] != 0)
if(cost[1][3]<min) if(7<5)F
j++
{
i=1, j=4
min = cost[i][j];
if(cost[1][4]<min) if(2<5)T
a = u = i;
1 4 4
if(visited[1] != 0) T
min=cost[1][2]=2
b = v = j;
4 2 3
a=u=i (a=u=1)
}
b=v=j (b=v=4)
j++
if (visited[u] == 0 || visited[v] == 0)
(j<=n) F ends j loop
i++
{
V[1]
V[2]
V[3]
V[4]
System.out.println("Edge" + ne++ + ":(" + a + "," + b + ")" + "cost:" + min);
1
0
0
1
if(visited[1] == 0 || visited[4] == 0)
mincost += min;
Edge : ( 1, 4) cost : 2 ne++ (ne=2)
1
1
1
visited[b] = 1;
2
3
4
1
mincost=0+2=2;
1
1
1
1
999
}
1
999
5
7
visited[4] = 1;
2
5
999 999
3
cost[a][b] = cost[b][a] = 999;
3
7
999 999
4
}//end while
999
999
4
3
4
cost[1][4] = cost[4][1] = 999;
2
3
4
System.out.println("\n Minimum cost" + mincost);
} // Prims method ends
} // class ends
5
1
2
2
7
3
4
Tree vertices
a(-,-)
1
MST
2
2
3
3
4
Remaining vertices
4
3
4
Illustration
1
2
3
4
b(a,5) c(a,7) d(a,2)
1
d(a,2)
b(d,3)
2
2
b(d,3) c(d,4)
3
4
1
2
2
3
4
2
2
3
4
c(d,4)
3
1
c(d,4)
------3
4
Download