Professional Documents
Culture Documents
Ans=
maxSize = size;
if (isFull()) {
stackArray[++top] = value;
if (isEmpty()) {
} else {
return poppedValue;
if (isEmpty()) {
System.out.println("Stack is empty.");
} else {
}
System.out.println();
stack.push(1);
stack.push(2);
stack.push(3);
stack.display();
stack.pop();
stack.display();
stack.push(4);
stack.push(5);
stack.display();
(2) Write a java program to sort a list of elements using the quick sort algorithm. The elements can be
read from a file
ans=import java.io.*;
import java.util.ArrayList;
import java.util.List;
try {
writeElementsToFile(outputFileName, elements);
} catch (IOException e) {
e.printStackTrace();
int i = low - 1;
i++;
elements.set(i, elements.get(j));
elements.set(j, temp);
elements.set(i + 1, elements.get(high));
elements.set(high, temp);
return i + 1;
}
public static List<Integer> readElementsFromFile(String fileName) throws IOException {
String line;
elements.add(Integer.parseInt(line));
reader.close();
return elements;
writer.write(element + "\n");
writer.close();
(3)Write a java program to implement a Merge sort algorithm to a list of elements for different values of
n and
import java.util.Random;
public class MergeSortTiming {
mergeSort(arr);
return arr;
}
return; // No need to sort if the array is empty or has only one element
int n = arr.length;
private static void mergeSort(int[] arr, int[] temp, int left, int right) {
private static void merge(int[] arr, int[] temp, int left, int mid, int right) {
temp[i] = arr[i];
}
int i = left;
int j = mid + 1;
int k = left;
arr[k] = temp[i];
i++;
} else {
arr[k] = temp[j];
j++;
k++;
arr[k] = temp[i];
i++;
k++;
}
(4)Find the minimum cost of spanning tree in java using Prim’s algorithms
Ans=import java.util.Arrays;
min = key[v];
minIndex = v;
return minIndex;
System.out.println("Edge \tWeight");
}
// Function to find the minimum spanning tree using Prim's algorithm
Arrays.fill(key, Integer.MAX_VALUE);
key[0] = 0;
parent[0] = -1;
mstSet[u] = true;
parent[v] = u;
key[v] = graph[u][v];
printMST(parent, graph);
}
public static void main(String[] args) {
{0, 2, 0, 6, 0},
{2, 0, 3, 8, 5},
{0, 3, 0, 0, 7},
{6, 8, 0, 0, 9},
{0, 5, 7, 9, 0}
};
prim.primMST(graph);
(5)Find the minimum cost of spanning tree in java using Kruskal’s algorithm.
Ans=import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
class Edge {
this.src = src;
this.dest = dest;
this.weight = weight;
class Graph {
List<Edge> edges;
Graph(int V, int E) {
this.V = V;
this.E = E;
edges.add(edge);
List<Edge> kruskalMST() {
parent[v] = v;
while (e < V - 1) {
if (x != y) {
result.add(nextEdge);
union(parent, x, y);
e++;
return result;
if (parent[i] != i) {
return parent[i];
}
parent[xSet] = ySet;
graph.addEdge(0, 1, 2);
graph.addEdge(0, 3, 6);
graph.addEdge(1, 2, 3);
graph.addEdge(1, 3, 8);
graph.addEdge(1, 4, 5);
graph.addEdge(2, 4, 7);
graph.addEdge(3, 4, 9);
int capacity = 5;
int n = weights.length;
public static int knapsack(int[] weights, int[] values, int capacity, int n) {
if (i == 0 || w == 0) {
dp[i][w] = 0;
} else {
return dp[n][capacity];
(7) Write a java program to find the shortest paths between nodes in a graph using Dijkstra’s algorithm.
Ans=import java.util.Arrays;
int[] dist = new int[V]; // Array to store the shortest distances from the source
dist[source] = 0;
sptSet[u] = true;
printSolution(dist);
min = dist[v];
minIndex = v;
}
return minIndex;
{0, 4, 0, 0, 0},
{4, 0, 8, 0, 0},
{0, 8, 0, 7, 0},
{0, 0, 7, 0, 9},
{0, 0, 0, 9, 0}
};
shortestPaths.dijkstra(graph, 0);
}
(8) Write a java program
Print all the nodes reachable from a starting node in a digraph using BFS method.
Ans=import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Stack;
class Graph {
private int V;
public Graph(int v) {
V = v;
adjList[u].add(v);
}
public void BFS(int startNode) {
visited[startNode] = true;
queue.add(startNode);
while (!queue.isEmpty()) {
if (!visited[neighbor]) {
visited[neighbor] = true;
queue.add(neighbor);
System.out.println();
visited[startNode] = true;
stack.push(startNode);
while (!stack.isEmpty()) {
if (!visited[neighbor]) {
visited[neighbor] = true;
stack.push(neighbor);
if (adjList[i].size() > 0) {
firstNode = i;
break;
}
if (firstNode == -1) {
DFS(firstNode);
return false; // There is an unvisited node with neighbors, graph is not connected
return true;
graph.addEdge(0, 1);
graph.addEdge(0, 2);
graph.addEdge(1, 3);
graph.addEdge(2, 4);
graph.addEdge(3, 5);
graph.addEdge(4, 5);
graph.addEdge(5, 6);
int startNode = 0;
graph.BFS(startNode);
if (graph.isConnected()) {
} else {
(9) Write a java program to implement all pairs shortest paths problem using Floyd’s algorithm
dist[i][j] = graph[i][j];
if (dist[i][j] == INF) {
System.out.print("INF\t");
} else {
System.out.print(dist[i][j] + "\t");
}
System.out.println();
int[][] graph = {
{INF, 0, 3, INF},
};
floydWarshall.floydWarshall(graph, V);
(10) Write a java program to implement N Queen’s problem using Back Tracking.
solveNQueensHelper(board, 0, result);
printSolutions(result);
if (row == board.length) {
result.add(constructSolution(board));
return;
board[row][col] = 'Q';
board[row][col] = '.';
int n = board.length;
// Check the column above
if (board[i][col] == 'Q') {
return false;
if (board[i][j] == 'Q') {
return false;
if (board[i][j] == 'Q') {
return false;
return true;
solution.add(new String(row));
return solution;
System.out.println(row);
System.out.println();
solveNQueens(n);