Uploaded by MUHAMMAD ZAIN SHAHZAD

DAA Assignment #2 - M.Zain Shahzad

advertisement
Scanned with CamScanner
(Q1)
Yes, it is possible to implement Bubble Sort for linked lists. Here's an algorithm to implement
Bubble Sort for linked lists:




Define a function bubbleSort takes the head of the linklist as input.
Initialize two pointers, current and prev, to the head.
Define a boolean flag swapped to keep track of whether any swaps have occurred.
While swapped is true, do the following:
i) Set swapped to false.
ii) Set current to the head of the linked list.
 While current has a non-null next pointer, do the following:
If the value of current's next node is less than the value of current node, swap their values.
 Set swapped to true.
 Move current and prev pointers to their next nodes.
 Return the head of the sorted linked list.
The time complexity of Bubble Sort for linked lists is O(n^2) in the worst case, where n is the
number of nodes in the linked list. The space complexity is O(1).
CODE:
class Node {
int val;
Node next;
Node(int val) {
this.val = val;
this.next = null;
}
}
public class BubbleSortLinkedList {
public static void main(String[] args) {
// linked list 1-> 4-> 2 -> 3
Node head = new Node(1);
head.next = new Node(4);
head.next.next = new Node(2);
head.next.next.next = new Node(3);
// Sort
Node sortedList = bubbleSort(head);
// Print
while (sortedList != null) {
System.out.print(sortedList.val + " ");
sortedList = sortedList.next;
}}
public static Node bubbleSort(Node head) {
if (head == null || head.next == null) {
return head;
}
boolean swapped;
Node current = null;
Node prev = null;
do {
swapped = false;
current = head;
while (current.next != null) {
if (current.val > current.next.val) {
int temp = current.val;
current.val = current.next.val;
current.next.val = temp;
swapped = true;
}
prev = current;
current = current.next;
}
} while (swapped);
return head;
}
}
Scanned with CamScanner
Scanned with CamScanner
(Q2)
CODE:
class Node {
public class JobAssignment {
private static int[][] costs; // matrix of job costs
private static boolean[] assigned; // array to keep track of assigned jobs
private static int[] assignment; // array to store the current assignment
public static void main(String[] args) {
costs = new int[][]{
{9, 2, 7, 8},
{6, 4, 3, 7},
{5, 8, 1, 8},
{7, 6, 9, 4}
};
int[] result = findOptimalAssignment(costs);
System.out.println("Optimal job assignment: " + Arrays.toString(result));
}
public static int[] findOptimalAssignment(int[][] costs) {
int n = costs.length;
assigned = new boolean[n];
assignment = new int[n];
int[] optimalAssignment = new int[n];
int[] currentAssignment = new int[n];
int minCost = Integer.MAX_VALUE;
assignJob(0, n, currentAssignment);
for (int i = 0; i < n; i++) {
optimalAssignment[i] = assignment[i];
}
minCost = getAssignmentCost(assignment);
System.out.println("Minimum cost: " + minCost);
return optimalAssignment;
}
private static void assignJob(int jobIndex, int n, int[] currentAssignment) {
if (jobIndex == n) {
int currentCost = getAssignmentCost(currentAssignment);
if (currentCost < getAssignmentCost(assignment)) {
for (int i = 0; i < n; i++) {
assignment[i] = currentAssignment[i];
}
}
return;
}
for (int i = 0; i < n; i++) {
if (!assigned[i]) {
assigned[i] = true;
currentAssignment[jobIndex] = i;
assignJob(jobIndex + 1, n, currentAssignment);
assigned[i] = false;
}}}
private static int getAssignmentCost(int[] assignment) {
int cost = 0;
for (int i = 0; i < assignment.length; i++) {
cost += costs[i][assignment[i]];
}
return cost;
}
}
Scanned with CamScanner
Scanned with CamScanner
Scanned with CamScanner
(Q5)
CODE:
import java.util.*;
public class Graph {
private int V; // number of vertices
private LinkedList<Integer>[] adj; // adjacency list
// constructor
public Graph(int V) {
this.V = V;
adj = new LinkedList[V];
for (int i = 0; i < V; i++) {
adj[i] = new LinkedList<Integer>();
}
}
// add an edge between v and w
public void addEdge(int v, int w) {
adj[v].add(w);
adj[w].add(v);
}
// DFS to find connected components and detect cycles
private void DFS(int v, boolean[] visited, int[] parent, Set<Integer> component) {
visited[v] = true;
component.add(v);
for (int w : adj[v]) {
if (!visited[w]) {
parent[w] = v;
DFS(w, visited, parent, component);
} else if (w != parent[v]) {
// found a cycle
System.out.println("Cycle found in component " + component + ": " + v + " -> " + w); }
}}
// find connected components and cycles
public void findComponents() {
boolean[] visited = new boolean[V];
int[] parent = new int[V];
List<Set<Integer>> components = new ArrayList<Set<Integer>>();
for (int v = 0; v < V; v++) {
if (!visited[v]) {
Set<Integer> component = new HashSet<Integer>();
DFS(v, visited, parent, component);
components.add(component);
}
}
// output connected components
System.out.println("Connected components:");
for (Set<Integer> component : components) {
System.out.println(component);
}
if (components.size() == 1) {
System.out.println("The graph is acyclic");
}
}
// test the program
public static void main(String[] args) {
Graph g = new Graph(7);
g.addEdge(0, 1);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(3, 4);
g.addEdge(4, 5);
g.addEdge(5, 3);
g.addEdge(6, 6);
g.findComponents();
}
}
Download