|
1 |
| -## DAA Lab |
| 1 | +## Algorithm Lab 4th Sem |
2 | 2 |
|
3 |
| -# [All Sorting Algorithms](https://www.interviewkickstart.com/learn/sorting-algorithms) |
| 3 | +### [All Sorting Algorithms](https://www.interviewkickstart.com/learn/sorting-algorithms) |
4 | 4 |
|
5 |
| -# [Merge Sort](https://www.interviewkickstart.com/learn/merge-sort) |
| 5 | +### [Merge Sort](https://www.interviewkickstart.com/learn/merge-sort) |
6 | 6 |
|
7 |
| -# [Quick Sort](https://www.interviewkickstart.com/learn/quick-sort) |
| 7 | +### [Quick Sort](https://www.interviewkickstart.com/learn/quick-sort) |
8 | 8 |
|
| 9 | +--- |
| 10 | + |
| 11 | +## Algorithms Code in C |
| 12 | + |
| 13 | +<details><summary><b>Prims</b></summary> |
| 14 | + |
| 15 | +```c |
| 16 | +#include <stdio.h> |
| 17 | +#include <stdlib.h> |
| 18 | + |
| 19 | +#define infinity 9999 |
| 20 | +#define MAX 20 |
| 21 | + |
| 22 | +int G[MAX][MAX], spanning[MAX][MAX], n; |
| 23 | + |
| 24 | +int prims(); |
| 25 | + |
| 26 | +int main() |
| 27 | +{ |
| 28 | + int i, j, total_cost; |
| 29 | + printf("Enter no. of vertices:"); |
| 30 | + scanf("%d", &n); |
| 31 | + printf("\nEnter the adjacency matrix:\n"); |
| 32 | + for (i = 0; i < n; i++) |
| 33 | + for (j = 0; j < n; j++) |
| 34 | + scanf("%d", &G[i][j]); |
| 35 | + total_cost = prims(); |
| 36 | + printf("\nspanning tree matrix:\n"); |
| 37 | + for (i = 0; i < n; i++) |
| 38 | + { |
| 39 | + printf("\n"); |
| 40 | + for (j = 0; j < n; j++) |
| 41 | + printf("%d\t", spanning[i][j]); |
| 42 | + } |
| 43 | + printf("\n\nTotal cost of spanning tree = %d", total_cost); |
| 44 | + return 0; |
| 45 | +} |
| 46 | + |
| 47 | +int prims() |
| 48 | +{ |
| 49 | + int cost[MAX][MAX]; |
| 50 | + int u, v, min_distance, distance[MAX], from[MAX]; |
| 51 | + int visited[MAX], no_of_edges, i, min_cost, j; |
| 52 | + // create cost[][] matrix,spanning[][] |
| 53 | + for (i = 0; i < n; i++) |
| 54 | + for (j = 0; j < n; j++) |
| 55 | + { |
| 56 | + if (G[i][j] == 0) |
| 57 | + cost[i][j] = infinity; |
| 58 | + else |
| 59 | + cost[i][j] = G[i][j]; |
| 60 | + spanning[i][j] = 0; |
| 61 | + } |
| 62 | + // initialise visited[],distance[] and from[] |
| 63 | + distance[0] = 0; |
| 64 | + visited[0] = 1; |
| 65 | + for (i = 1; i < n; i++) |
| 66 | + { |
| 67 | + distance[i] = cost[0][i]; |
| 68 | + from[i] = 0; |
| 69 | + visited[i] = 0; |
| 70 | + } |
| 71 | + min_cost = 0; // cost of spanning tree |
| 72 | + no_of_edges = n - 1; // no. of edges to be added |
| 73 | + while (no_of_edges > 0) |
| 74 | + { |
| 75 | + // find the vertex at minimum distance from the tree |
| 76 | + min_distance = infinity; |
| 77 | + for (i = 1; i < n; i++) |
| 78 | + if (visited[i] == 0 && distance[i] < min_distance) |
| 79 | + { |
| 80 | + v = i; |
| 81 | + min_distance = distance[i]; |
| 82 | + } |
| 83 | + u = from[v]; |
| 84 | + // insert the edge in spanning tree |
| 85 | + spanning[u][v] = distance[v]; |
| 86 | + spanning[v][u] = distance[v]; |
| 87 | + no_of_edges--; |
| 88 | + visited[v] = 1; |
| 89 | + // updated the distance[] array |
| 90 | + for (i = 1; i < n; i++) |
| 91 | + if (visited[i] == 0 && cost[i][v] < distance[i]) |
| 92 | + { |
| 93 | + distance[i] = cost[i][v]; |
| 94 | + from[i] = v; |
| 95 | + } |
| 96 | + min_cost = min_cost + cost[u][v]; |
| 97 | + } |
| 98 | + return (min_cost); |
| 99 | +} |
| 100 | +``` |
| 101 | + |
| 102 | +</details> |
| 103 | + |
| 104 | +<details><summary><b>Krushkal</b></summary> |
| 105 | + |
| 106 | +```c |
| 107 | +#include <stdio.h> |
| 108 | +#include <stdlib.h> |
| 109 | + |
| 110 | +int i, j, k, a, b, u, v, n, ne = 1; |
| 111 | +int min, mincost = 0, cost[9][9], parent[9]; |
| 112 | + |
| 113 | +int find(int); |
| 114 | +int uni(int, int); |
| 115 | + |
| 116 | +void main() |
| 117 | +{ |
| 118 | + printf("Kruskal's algorithm in C\n"); |
| 119 | + printf("========================\n"); |
| 120 | + |
| 121 | + printf("Enter the no. of vertices: "); |
| 122 | + scanf("%d", &n); |
| 123 | + |
| 124 | + printf("\nEnter the cost adjacency matrix:\n"); |
| 125 | + for (i = 1; i <= n; i++) |
| 126 | + { |
| 127 | + for (j = 1; j <= n; j++) |
| 128 | + { |
| 129 | + scanf("%d", &cost[i][j]); |
| 130 | + if (cost[i][j] == 0) |
| 131 | + cost[i][j] = 999; |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + printf("The edges of Minimum Cost Spanning Tree are\n"); |
| 136 | + while (ne < n) |
| 137 | + { |
| 138 | + for (i = 1, min = 999; i <= n; i++) |
| 139 | + { |
| 140 | + for (j = 1; j <= n; j++) |
| 141 | + { |
| 142 | + if (cost[i][j] < min) |
| 143 | + { |
| 144 | + min = cost[i][j]; |
| 145 | + a = u = i; |
| 146 | + b = v = j; |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + u = find(u); |
| 152 | + v = find(v); |
| 153 | + |
| 154 | + if (uni(u, v)) |
| 155 | + { |
| 156 | + printf("%d edge (%d,%d) = %d\n", ne++, a, b, min); |
| 157 | + mincost += min; |
| 158 | + } |
| 159 | + |
| 160 | + cost[a][b] = cost[b][a] = 999; |
| 161 | + } |
| 162 | + |
| 163 | + printf("\nMinimum cost = %d\n", mincost); |
| 164 | +} |
| 165 | + |
| 166 | +int find(int i) |
| 167 | +{ |
| 168 | + while (parent[i]) |
| 169 | + i = parent[i]; |
| 170 | + return i; |
| 171 | +} |
| 172 | + |
| 173 | +int uni(int i, int j) |
| 174 | +{ |
| 175 | + if (i != j) |
| 176 | + { |
| 177 | + parent[j] = i; |
| 178 | + return 1; |
| 179 | + } |
| 180 | + |
| 181 | + return 0; |
| 182 | +} |
| 183 | +``` |
| 184 | +
|
| 185 | +</details> |
| 186 | +
|
| 187 | +## Prims Input: |
| 188 | +
|
| 189 | +Enter no. of vertices: 5 |
| 190 | +
|
| 191 | +Enter the adjacency matrix: |
| 192 | +
|
| 193 | +0 1 2 0 0 |
| 194 | +1 0 2 0 4 |
| 195 | +2 2 0 3 0 |
| 196 | +0 0 3 0 2 |
| 197 | +0 4 0 2 0 |
| 198 | +
|
| 199 | +## Krushkal Input: |
| 200 | +
|
| 201 | +Enter no. of vertices: 5 |
| 202 | +
|
| 203 | +Enter the cost adjacency matrix: |
| 204 | +
|
| 205 | +0 1 2 0 1 |
| 206 | +1 0 3 0 1 |
| 207 | +2 3 0 6 5 |
| 208 | +0 0 6 0 0 |
| 209 | +1 1 5 0 0 |
0 commit comments