#include
#include
// Swap two integers
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
// Heapify to maintain heap property
void heapify(int *heap, int size, int i, int is_min_heap) {
int extreme = i, left = 2 * i + 1, right = 2 * i + 2;
if (is_min_heap) { // Min Heap
if (left < size && heap[left] < heap[extreme]) extreme = left;
if (right < size && heap[right] < heap[extreme]) extreme = right;
} else { // Max Heap
if (left < size && heap[left] > heap[extreme]) extreme = left;
if (right < size && heap[right] > heap[extreme]) extreme = right;
}
if (extreme != i) {
swap(&heap[i], &heap[extreme]);
heapify(heap, size, extreme, is_min_heap);
}
}
// Build a heap from an array
void buildHeap(int *heap, int size, int is_min_heap) {
for (int i = (size - 2) / 2; i >= 0; i--)
heapify(heap, size, i, is_min_heap);
}
// Delete an element from the heap
void deleteElement(int *heap, int *size, int key, int is_min_heap) {
int i;
for (i = 0; i < *size; i++) {
if (heap[i] == key) break;
}
if (i == *size) {
printf("Element %d not found.\n", key);
return;
}
heap[i] = heap[--(*size)];
heapify(heap, *size, i, is_min_heap);
}
// Display the heap
void displayHeap(int *heap, int size) {
for (int i = 0; i < size; i++)
printf("%d ", heap[i]);
printf("\n");
}
int main() {
int minHeap[] = {3, 2, 1, 7, 8, 4, 10, 16};
int maxHeap[] = {10, 8, 16, 14, 7, 9, 3, 2};
int minSize = sizeof(minHeap) / sizeof(minHeap[0]);
int maxSize = sizeof(maxHeap) / sizeof(maxHeap[0]);
// Build and display Min Heap
buildHeap(minHeap, minSize, 1);
printf("Min Heap:\n");
displayHeap(minHeap, minSize);
// Build and display Max Heap
buildHeap(maxHeap, maxSize, 0);
printf("Max Heap:\n");
displayHeap(maxHeap, maxSize);
// Delete elements and display heaps
deleteElement(minHeap, &minSize, 4, 1);
printf("Min Heap after deleting 4:\n");
displayHeap(minHeap, minSize);
deleteElement(maxHeap, &maxSize, 16, 0);
printf("Max Heap after deleting 16:\n");
displayHeap(maxHeap, maxSize);
return 0;
}
Output:-
Min Heap:
1 2 3 7 8 4 10 16
Max Heap:
16 14 10 8 7 9 3 2
Min Heap after deleting 4:
1 2 3 7 8 16 10
Max Heap after deleting 16:
14 8 10 2 7 9 3
#include
// Utility function to get maximum of two integers
int max(int a, int b) {
return (a > b) ? a : b;
}
// Function to solve the 0/1 Knapsack problem
void knapsack(int weights[], int values[], int n, int capacity) {
int dp[n + 1][capacity + 1];
// Build the dp table
for (int i = 0; i <= n; i++) {
for (int w = 0; w <= capacity; w++) {
if (i == 0 || w == 0) {
dp[i][w] = 0; // Base case
} else if (weights[i - 1] <= w) {
dp[i][w] = max(values[i - 1] + dp[i - 1][w - weights[i - 1]], dp[i - 1][w]);
} else {
dp[i][w] = dp[i - 1][w];
}
}
}
// Output the maximum value
printf("Maximum value in Knapsack = %d\n", dp[n][capacity]);
// Traceback to find included items
printf("Items included:\n");
int w = capacity;
for (int i = n; i > 0 && dp[i][w] > 0; i--) {
if (dp[i][w] != dp[i - 1][w]) {
printf("Item %d (Weight: %d, Value: %d)\n", i, weights[i - 1], values[i - 1]);
w -= weights[i - 1];
}
}
}
int main() {
int n, capacity;
// Input number of items and their weights and values
printf("Enter the number of items: ");
scanf("%d", &n);
int weights[n], values[n];
printf("Enter the weights of the items:\n");
for (int i = 0; i < n; i++) scanf("%d", &weights[i]);
printf("Enter the values of the items:\n");
for (int i = 0; i < n; i++) scanf("%d", &values[i]);
// Input knapsack capacity
printf("Enter the capacity of the knapsack: ");
scanf("%d", &capacity);
// Solve the knapsack problem
knapsack(weights, values, n, capacity);
return 0;
}
/*input*/
Enter the number of items: 3
Enter the weights of the items:
10 20 30
Enter the values of the items:
60 100 120
Enter the capacity of the knapsack: 50
//output
Maximum value in Knapsack = 220
Items included:
Item 3 (Weight: 30, Value: 120)
Item 2 (Weight: 20, Value: 100)
1>
A Min Heap is a binary tree data structure where:
Min Heaps are typically implemented using arrays for efficiency. The key advantage is that we can find the smallest element (root) in constant time O(1)O(1)O(1), making Min Heaps useful for priority queues.
Indexing:
Operations in Min Heap:
Alternatively, if the input array is known in advance:
Consider the array:
3,1,6,5,2,8,73, 1, 6, 5, 2, 8, 73,1,6,5,2,8,7
Construct Min Heap:
After constructing, the heap may look like:
1,2,6,5,3,8,71, 2, 6, 5, 3, 8, 71,2,6,5,3,8,7
Delete Element (e.g., 6):
Display the Heap Content:
The heap array remains ready for display or further operations.
The Single Source Shortest Path (SSSP) problem aims to find the shortest paths from a source vertex to all other vertices in a graph. A popular algorithm to solve this problem is Dijkstra’s Algorithm, which employs the greedy method. The performance of Dijkstra’s Algorithm depends significantly on the graph’s representation—Adjacency Matrix or Adjacency List.
Structure:
The graph is represented as a V×VV \times VV×V matrix, where VVV is the number of vertices.
Advantages:
Disadvantages:
Structure:
The graph is represented as an array of lists. Each list contains the neighbors of a vertex and their corresponding edge weights.
Advantages:
Disadvantages:
Aspect | Adjacency Matrix | Adjacency List |
---|---|---|
Space Complexity | O(V2)O(V^2)O(V2) | O(V+E)O(V + E)O(V+E) |
Initialization | O(V2)O(V^2)O(V2) | O(V+E)O(V + E)O(V+E) |
Extract Min (Greedy) | O(V)O(V)O(V) with array, O(logV)O(\log V)O(logV) with heap | O(V)O(V)O(V) with array, O(logV)O(\log V)O(logV) with heap |
Relaxation of Edges | O(V)O(V)O(V) | O(degree of u)O(\text{degree of } u)O(degree of u) |
Overall Time Complexity | O(V2)O(V^2)O(V2) with simple array; O(V2)O(V^2)O(V2) with binary heap | O(E+VlogV)O(E + V\log V)O(E+VlogV) with binary heap |
Adjacency Matrix:
Adjacency List:
Heap Usage: