DAA LAB SM
DAA LAB SM
https://www.youtube.com/watch?v=FbOfNoXrevY
Part A:
{
int i, j, temp;
int n = sc.nextInt();
temp = arr[i];
arr[i] = arr[min];
arr[min] = temp;
Part A: Write a program to find minimum and maximum value in an array using
divide and conquer.
import java.util.Scanner;
else{
mid = (i+j)/2;
MaxMin(i, mid);
max1 = max;
min1 = min;
MaxMin(mid+1, j);
if(max < max1) max = max1;//updating here
if(min > min1) min = min1;
}
}
}
OutPut:
}
public static void main(String[] args)
{
int mincost,n,i,j;
Scanner s = new Scanner(System.in);
System.out.println("enter the no of cities");
n=s.nextInt();
int path[] =new int[n+1];
cost = new int[n+1][n+1];
System.out.println("Enter the cost matrix");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
cost[i][j]=s.nextInt();
for(i=1;i<=n;i++)
path[i]=i;
Tsp obj = new Tsp();
mincost=obj.tsp(path,1,n);
System.out.println("tsp tour");
for(i=1;i<=n;i++)
System.out.print(path[i] + "--->");
System.out.println("1");
System.out.println("Tourcost=" + mincost);
}
}
OUTPUT:
Enter the no of cities
4
```````````````````````````````````````````
void compute()
{
for ( int i = 0; i <= n; ++ i)
{
for ( int j = 0; j <= c; ++ j)
{
if ( i == 0 || j == 0 )
{
v[i][j] = 0;
}
void traceback()
{
System.out.println("The objects picked up into knapsack are:");
int i = n;
int j = c;
while( i > 0)
{
if(v[i][j] != v[i-1][j])
{
System.out.print(i + " ");
j = j - w[i];
i--;
}
else
{
i--;
}
}
}
OUTPUT:
Enter number of objects
5
10
Enter weight for each 5 objects
2
Optimal Solution: 26
The objects picked up into knapsack are:
5431
void compute()
{
int i;
double[] x= new double[n+1];
for (i=0; i<n; i++)
{
x[i] = 0.0;
}d
ouble rc = c;
for(i=0; i<n; i++)
{
if(w[i] > rc) break;
x[i] = 1;
rc = rc - w[i];
} if
(i<=n)
{
x[i] = rc/w[i];
}
for ( i = 0; i < n; ++ i)
{
if ( x[i] > 0.0)
{
netProfit = netProfit + x[i] * p[i];
}
}S
ystem.out.println("Net Profit: " + netProfit);
System.out.println("The objects picked up into knapsack are:");
for ( i = 0; i < n; ++ i)
{
System.out.println(x[i] + " ");
}
}
}
OUTPUT:
Enter number of objects
7
4
5
A6: Write a program to find minimum and maximum value in an array using
divide and conquer.
import java.util.Scanner;
else{
mid = (i+j)/2;
MaxMin(i, mid);
max1 = max;
min1 = min;
MaxMin(mid+1, j);
if(max < max1) max = max1;//updating here
if(min > min1) min = min1;
}
}
}
EXPERIMENT 4
PROGRAM STATEMENT
Sort a given set of n integer elements using Quick Sort method and compute its time complexity.
Run the program for varied values of n > 5000 and record the time taken to sort. Plot a graph of
the time taken versus n on graph sheet. The elements can be read from a file or can be generated
using the random number generator. Demonstrate using Java how the divide- and-conquer
method works along with its time complexity analysis: worst case, average case and best case.
CONCEPT
Divide and Conquer: In divide and conquer approach, the problem in hand, is divided into
smaller sub-problems and then each problem is solved independently. When we keep on dividing
the subproblems into even smaller sub-problems, we may eventually reach a stage where no
more division is possible. Those "atomic" smallest possible sub-problem (fractions) are solved.
The solution of all sub-problems is finally merged in order to obtain the solution of an original
problem.