L6
L6
Problem 01:
Suppose, you working on a networking company. In this company, your work
is finding which sub network office are visited by their priority. Then you draw
a da for visi ng office. Now your job writes the code to solve the problem.
Input Output
Enter Number of Sub-Office: 7 Visi ng Office:
Enter Their Connec on using Matrix: 0
0110000 1
1000000 2
1001001 3
0010110 4
0001000 5
0001001 6
0010010
Solu on:
#include <stdio.h>
int main() {
int numVertices;
printf("Enter Number of Sub-Office: ");
scanf("%d", &numVertices);
int graph[7][7];
printf("Enter Their Connection using Matrix:\n");
for (int i = 0; i < numVertices; i++) {
1
return 0;
}
Output:
Problem 02:
2
We know our daily life have a rou ne. A rou ne can makes a man punctually.
Suppose, you go to your office, so wear all things sequen ally like you didn’t
wear shocks a er shoe. That’s why you have maintain a par cular rule. Here is
a graph and your work are finding the sequent to wear all things. So, write the
code for solu on.
Input Output
Enter Number of Cloths: 4 Wearing list: 0 1 2 3
Their Connec on:
0101
0010
0000
0010
Solu on:
#include <stdio.h>
#include <stdbool.h>
#define MAX_N 10
int N, M;
int graph[MAX_N][MAX_N];
bool visited[MAX_N];
int main() {
printf("Enter the number of clothes: ");
scanf("%d", &N);
return 0;
}
Output:
4
MST
Problem 01:
Suppose, you working on a cable connec ng company. Your job is controlling
the connec on and connect the new client. But every me you see your
company losses a huge amount for unplanned connec on. Now write the
code (Kruskal’s Algorithm) to solve the problem and find the minimum cost.
(Consider unconnected graph=999).
Input Output
Enter number of Connec ons: 4 Minimum Cost:
Enter the adjacency of matrix: 1 edge (2,3) = 4
0 5 999 999 2 edge (1,2) = 5
5 0 4 999 3 edge (3,4) = 5
999 4 0 5 Total = 14
999 999 5 0
Solu on:
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int u, v, weight;
} Edge;
typedef struct {
int V;
Edge *edges;
5
int numEdges;
} Graph;
int mstWeight = 0;
for (i = 0; i < numEdges; i++) {
int u = edges[i].u;
int v = edges[i].v;
int weight = edges[i].weight;
if (rootU != rootV) {
7
mstWeight += weight;
unionSets(parent, rootU, rootV);
prin ("%d Edge (%d, %d) = %d\n", i + 1, u, v, weight);
}
}
int main() {
int numConnec ons;
prin ("Enter number of Connec ons: ");
scanf("%d", &numConnec ons);
Graph graph;
graph.V = numConnec ons;
graph.numEdges = 0;
graph.edges = edges;
kruskalMST(&graph);
free(adjacencyMatrix[i]);
}
free(adjacencyMatrix);
free(edges);
return 0;
}
Output:
10
Problem 02:
Suppose, you have a telephone company and have many clients. Every year
connec on is growing up. But your working process being very difficult day by
day. You want to op mal solu on for that and you rearrange the all
connec on by using prim’s algorithm. Now, your job writes the code to solve
this problem and find minimum cost for your company.
Input Output
Enter number of Clients: 4 Minimum Cost:
Enter the adjacency of matrix: Edge 1 : (1,4) Cost= 1
0 3 999 1 Edge 2 : (4,2) Cost= 2
3042 Edge 3 : (2,3) Cost= 4
999 4 0 6 Minimum Cost : 7
1260
Solu on:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef struct {
int u, v, weight;
} Edge;
typedef struct {
int V;
11
int **adjMatrix;
} Graph;
key[1] = 0;
parent[1] = -1;
u = v;
}
}
inMST[u] = true;
int mstWeight = 0;
int edgeCount = 0;
for (int i = 2; i <= V; i++) {
mstEdges[edgeCount].u = parent[i];
mstEdges[edgeCount].v = i;
mstEdges[edgeCount].weight = graph->adjMatrix[i][parent[i]];
mstWeight += graph->adjMatrix[i][parent[i]];
edgeCount++;
}
13
int main() {
int numConnec ons;
prin ("Enter number of Clients: ");
scanf("%d", &numConnec ons);
Graph graph;
graph.V = numConnec ons;
14
primMST(&graph);
return 0;
}
15
Output: