Daa
1) Selection Sort
package progame1;
import java.util.Scanner;
public class selectionsort {
public static void main(String[] args) {
// TODO Auto-generated method stub
int n;
int a[];
a=new int[20];
Scanner sc=new Scanner(System.in);
System.out.println("enter the size of array");
n=sc.nextInt();
System.out.println("enter the elements of array");
for(int i=0;i<=n-1;i++)
{
a[i]=sc.nextInt();
}
for(int i=0;i<n;i++)
{
int min=i;
for(int j=i+1;j<=n-1;j++)
{
if(a[j]<a[min])
{
min=j;
}
}
int temp=a[i];
a[i]=a[min];
a[min]=temp;
}
System.out.println("The sorted array");
for(int i=0;i<=n-1;i++)
{
System.out.println(a[i]);
}
}
}
3) KnsapSack Program
package program4;
import java.util.Scanner;
public class KnapSack {
int n;
double c;
double[] p;
double[] w;
public KnapSack(int n, double c, double[] p, double[] w) {
this.n = n;
this.c = c;
this.p = p;
this.w = w;
}
public void compute() {
int i;
double[] x = new double[n];
double rc = c;
double netProfit = 0.0;
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++) {
netProfit += x[i] * p[i];
}
System.out.println("Net profit: " + netProfit);
System.out.println("The fractions of objects picked into the knapsack:");
for (i = 0; i < n; i++) {
System.out.println("Object " + (i + 1) + ": " + x[i]);
}
}
public static void main(String[] args) {
int n;
double c;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of objects:");
n = sc.nextInt();
double[] p = new double[n];
double[] w = new double[n];
System.out.println("Enter the capacity of the knapsack:");
c = sc.nextDouble();
System.out.println("Enter profits for each of the " + n + " objects:");
for (int i = 0; i < n; i++) {
p[i] = sc.nextDouble();
}
System.out.println("Enter weights for each of the " + n + " objects:");
for (int i = 0; i < n; i++) {
w[i] = sc.nextDouble();
}
KnapSack gk = new KnapSack(n, c, p, w);
gk.compute();
sc.close();
}
}
4) DFS Program
package program5;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.Stack;
public class Graphimp {
private LinkedList<Integer> adjacency[];
// Constructor to initialize the adjacency list
public Graphimp(int v) {
adjacency = new LinkedList[v];
for (int i = 0; i < v; i++) {
adjacency[i] = new LinkedList<>();
}
}
// Method to insert an edge between two vertices
public void insertEdge(int s, int d) {
adjacency[s].add(d);
adjacency[d].add(s); // Remove this line if the graph is directed
}
// DFS traversal
public void dfs(int source) {
boolean visited[] = new boolean[adjacency.length];
Stack<Integer> q = new Stack<>();
q.add(source);
visited[source] = true;
System.out.println("DFS Traversal:");
while (!q.isEmpty()) {
int current = q.pop();
System.out.println(current);
for (int neighbor : adjacency[current]) {
if (!visited[neighbor]) {
visited[neighbor] = true;
q.push(neighbor);
}
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of vertices and edges:");
int v = sc.nextInt();
int e = sc.nextInt();
Graphimp g = new Graphimp(v);
System.out.println("Enter the edges (source and destination):");
for (int i = 0; i < e; i++) {
int s = sc.nextInt();
int d = sc.nextInt();
g.insertEdge(s, d);
}
System.out.println("Enter the source vertex for DFS:");
int source = sc.nextInt();
g.dfs(source);
sc.close();
}
}
5)BFS Progaram
package program4;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.Queue;
public class Graph {
private LinkedList<Integer> adjacency[];
// Constructor to initialize the adjacency list
public Graph(int v) {
adjacency = new LinkedList[v];
for (int i = 0; i < v; i++) {
adjacency[i] = new LinkedList<>();
}
}
// Method to insert an edge between two vertices
public void insertEdge(int s, int d) {
adjacency[s].add(d);
adjacency[d].add(s);
}
// BFS traversal
public void bfs(int source) {
boolean visited[] = new boolean[adjacency.length];
int parent[] = new int[adjacency.length];
Queue<Integer> q = new LinkedList<>();
q.add(source);
visited[source] = true;
parent[source] = -1;
System.out.println("BFS Traversal:");
while (!q.isEmpty()) {
int current = q.poll();
System.out.println(current);
for (int neighbor : adjacency[current]) {
if (!visited[neighbor]) {
visited[neighbor] = true;
q.add(neighbor);
parent[neighbor] = current;
}
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of vertices and edges:");
int v = sc.nextInt();
int e = sc.nextInt();
Graph g = new Graph(v);
System.out.println("Enter the edges (source and destination):");
for (int i = 0; i < e; i++) {
int s = sc.nextInt();
int d = sc.nextInt();
g.insertEdge(s, d);
}
System.out.println("Enter the source vertex for BFS:");
int source = sc.nextInt();
g.bfs(source);
sc.close();
}
}
6) Maximum and Minimum
package progame1;
import java.util.Scanner;
public class maxMin {
static int max, min;
static int size;
static int a[];
static Scanner sc = new Scanner(System.in);
static void maxMin(int i, int j) {
int max1, min1, mid;
if (i == j) {
max = min = a[i];
} else if (i == j - 1) {
if (a[i] < a[j]) {
min = a[i];
max = a[j];
} else {
min = a[j];
max = a[i];
}
} else {
mid = (i + j) / 2;
maxMin(i, mid);
max1 = max;
min1 = min;
maxMin(mid + 1, j);
// Combine results
if (max < max1) max = max1;
if (min > min1) min = min1;
}
}
public static void input() {
a = new int[size];
for (int i = 0; i < size; i++) {
a[i] = sc.nextInt();
}
}
public static void main(String[] args) {
System.out.println("Enter the size of the array:");
size = sc.nextInt();
System.out.println("Enter the elements of the array:");
input();
maxMin(0, size - 1);
// Output results
System.out.println("The maximum is: " + max);
System.out.println("The minimum is: " + min);
sc.close();
}
}
7)Quick Sort Program
package progame1;
import java.util.Scanner;
public class QuickSort {
static int max = 200;
public int partition(int[] a, int low, int high) {
int pivot = a[low];
int i = low + 1;
int j = high;
int temp;
while (true) {
while (i <= high && a[i] <= pivot) {
i++;
}
while (a[j] > pivot) {
j--;
}
if (i >= j) {
break;
}
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
temp = a[low];
a[low] = a[j];
a[j] = temp;
return j;
}
void sort(int[] a, int low, int high) {
if (low < high) {
int partitionIndex = partition(a, low, high); // Partitioning
sort(a, low, partitionIndex - 1); // Sort left part
sort(a, partitionIndex + 1, high); // Sort right part
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size of the array:");
int n = sc.nextInt();
// Declare array with the given size
int[] a = new int[n];
System.out.println("Enter array elements:");
for (int i = 0; i < a.length; i++) {
a[i] = sc.nextInt();
}
// Create an instance of QuickSort
QuickSort sorter = new QuickSort();
sorter.sort(a, 0, n - 1);
System.out.println("Array elements after sorting:");
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
sc.close();
}
}
8) MERGE SORT
package program8;
import java.util.*;
public class MergeSort {
public static void main(String[] args) {
// TODO Auto-generated method stub
int a[];
Scanner scan=new Scanner(System.in);
System.out.println("Enter the size for merge sort...:");
int n=scan.nextInt();
a=new int[n];
System.out.println("enter array elements:");
for(int i=0;i<n;i++)
a[i]=scan.nextInt();
msortdiv(a,0,n-1);
System.out.println("sorted array are:");
for(int i=0;i<n;i++)
System.out.println(a[i]+" ");
}
static void msortdiv(int a[],int low,int high)
{
int mid;
if(low<high)
{
mid=(low+high)/2;
msortdiv(a,low,mid);
msortdiv(a,mid+1,high);
mergesort(a,low,mid,high);
}
}
static void mergesort(int a[],int low,int mid,int high)
{
int i,j,k,b[]=new int[20];
i=low;
j=mid+1;
k=low;
while(i<=mid && j<=high)
{
if(a[i]>=a[j])
{
b[k]=a[j];
j++;
k++;
}
else
{
b[k]=a[i];
k++;
i++;
}
}
while(i<=mid)
{
b[k++]=a[i++];
}
while(j<=high)
{
b[k++]=a[j++];
}
for( i=low;i<=high;i++)
{
a[i]=b[i];
}
}
}
9) PRIMS ALGORITHEM PROGRAME
package program9;
import java.util.Scanner;
public class PrimProgram {
//static Scanner scan;
final static int MAX=20;
static int n;
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan=new Scanner(System.in);
int[][] matrix=new int[MAX][MAX];
int[] visited=new int[10];
int min;
int u=0;
int v=0;
int total=0;
System.out.println("Enter number of nodes");
int n=scan.nextInt();
System.out.println("Enter cost matrix");
for(int i=0;i<n;i++)
{
visited[i]=0;
for(int j=0;j<n;j++)
{
matrix[i][j]=scan.nextInt();
if(matrix[i][j]==0)
{
matrix[i][j]=999;
}
}
}
visited[0]=1;
//start algorithm
for(int counter =0; counter<n-1;counter++)
{
min=999;
for(int i=0;i<n;i++)
{
if(visited[i]==1)
{
for(int j=0;j<n;j++)
{
if(visited[j]!=1)
{
if(min>matrix[i][j])
{
min=matrix[i][j];
u=i;
v=j;
}
}}}}
visited[v]=1;
total+=min;
System.out.println("Edge found:"+u+"->"+v+": Weight:"+min);
}
System.out.println("The weight of the minimum spanning tree is"+total);
}
}
10) Kruskal's Program
package program10;
import java.util.*;
public class KruskalProgram {
final static int MAX=20;
static int n; // no of vertices
static int cost[][];//cost matrix
static int parent[]=new int[9];
static Scanner scan=new Scanner(System.in);
public static void main(String[] args) {
// TODO Auto-generated method stub
ReadMatrix();//read input
Kruskals();//construct minimum spanning tree
}
static void ReadMatrix()
{
int i,j;
cost=new int[MAX][MAX];
System.out.println("\nEnter the number of nodes");
n=scan.nextInt();
System.out.println("\nEnter cost matrix");
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;
}
}
}
}
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");
while(ne<n)
{
for(i=1,min=999;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(cost[i][j]<min)
{
min=cost[i][j];
a=u=i;
b=v=j;
}
}
}
while(parent[a]>0)
{
a=parent[a];
}
while(parent[b]>0)
{b=parent[b];
}
if(v!=u)
{
uni(u,v);
mincost+=min;
System.out.println(ne++ +"edge("+u+","+v+")=" +min);
}
cost[a][b]=cost[b][a]=999;
}
System.out.println("minimum cost is:"+mincost);
}
static void uni(int i,int j)
{
parent[j]=i;
}
}


Comments
Post a Comment