DAA ASS2
DAA ASS2
import java.util.List;
int length = 0;
int i = 1;
lps[0] = 0;
if (pattern.charAt(i) == pattern.charAt(length)) {
length++;
lps[i] = length;
i++;
} else {
if (length != 0) {
} else {
lps[i] = 0;
i++;
}
return lps;
int i = 0;
int j = 0;
if (pattern.charAt(j) == text.charAt(i)) {
i++;
j++;
if (j == pattern.length()) {
result.add(i - j);
j = lps[j - 1];
if (j != 0) {
j = lps[j - 1];
} else {
i++;
return result;
}
Output:
2] Given a large dataset of customer records. Each record contains customer details such as
name, age, and purchase amount. Design an application using Quick Sort to sort these records
by purchase amount in descending order. Discuss the performance of your program with this
large dataset.
import java.util.Arrays;
class Customer {
String name;
int age;
double purchaseAmount;
this.name = name;
this.age = age;
this.purchaseAmount = purchaseAmount;
return "Customer{name='" + name + "', age=" + age + ", purchaseAmount=" + purchaseAmount + '}';
quickSort(customers, pi + 1, high);
}
}
i++;
customers[i] = customers[j];
customers[j] = temp;
customers[i + 1] = customers[high];
customers[high] = temp;
return i + 1;
Customer[] customers = {
};
System.out.println("Before sorting:");
System.out.println(Arrays.toString(customers));
System.out.println(Arrays.toString(customers));
Output:
3] Design and Implement an application that considers the problem of scheduling n jobs of
known durations t1, t2,..., tn for execution by a single processor. The jobs can be executed in
any order, one job at a time. Find and display the schedule that minimizes the total time spent
by all the jobs in the system by maximizing the profit.
import java.util.Arrays;
import java.util.Comparator;
class Job {
String jobId;
int duration;
int profit;
this.jobId = jobId;
this.duration = duration;
this.profit = profit;
@Override
return "Job{jobId='" + jobId + "', duration=" + duration + ", profit=" + profit + '}';
Job[] jobs = {
System.out.println("Before scheduling:");
System.out.println(Arrays.toString(jobs));
System.out.println(Arrays.toString(jobs));
scheduleJobs(jobs);
int n = jobs.length;
waitingTime[0] = 0;
totalWaitingTime += waitingTime[i];
totalTurnaroundTime += turnaroundTime[i];
System.out.println("\nJob Schedule:");
int totalProfit = 0;
totalProfit += jobs[i].profit;
Output:
4] Construct a Optimal binary search tree for a given sorted array key [0.. n-1] of search keys
and an array freq[0.. n-1] of frequency counts, where freq[i] is the number of searches for
keys[i]. Design and implement Java Program to find the total cost of all the searches is as
small as possible.
int n = keys.length;
cost[i][i] = freq[i];
sum[i][i] = freq[i];
int j = i + length - 1;
cost[i][j] = Integer.MAX_VALUE;
for (int r = i; r <= j; r++) {
cost[i][j] = totalCost;
OUTPUT:
5. Design and Implement Java Program to find all Hamiltonian Cycles in a connected
undirected Graph G of n vertices using backtracking principle.
import java.util.ArrayList;
import java.util.List;
this.graph = graph;
this.n = graph.length;
visited[0] = true;
if (path.size() == n) {
if (graph[vertex][path.get(0)] == 1) {
return;
visited[v] = true;
path.add(v);
visited[v] = false;
path.remove(path.size() - 1);
if (allCycles.isEmpty()) {
} else {
System.out.println("Hamiltonian Cycles:");
System.out.println(cycle);
// Example graph
int[][] graph = {
{0, 1, 1, 1, 1},
{1, 0, 1, 1, 0},
{1, 1, 0, 1, 1},
{1, 1, 1, 0, 1},
{1, 0, 1, 1, 0}
};
hc.findAllHamiltonianCycles();
hc.printAllCycles();
OUTPUT: