0% found this document useful (0 votes)
19 views16 pages

L6

rf

Uploaded by

topu md
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views16 pages

L6

rf

Uploaded by

topu md
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

DFS

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>

void dfs(int graph[][7], int start, int visited[]) {


visited[start] = 1;
printf("%d ", start);

for (int i = 0; i < 7; i++) {


if (graph[start][i] == 1 && visited[i] == 0) {
dfs(graph, i, visited);
}
}
}

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

for (int j = 0; j < numVertices; j++) {


scanf("%d", &graph[i][j]);
}
}

int visited[7] = {0};


printf("Visiting Office: ");
dfs(graph, 0, visited);

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];

void dfs(int node) {


visited[node] = true;
printf("%d ", node);

for (int i = 0; i < N; i++) {


if (graph[node][i] == 1 && !visited[i]) {
dfs(i);
}
}
}

int main() {
printf("Enter the number of clothes: ");
scanf("%d", &N);

printf("Enter the connection matrix:\n");


for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
scanf("%d", &graph[i][j]);
}
}
3

printf("Wearing list: ");


dfs(0);
printf("\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>

#define INF 999

typedef struct {
int u, v, weight;
} Edge;

typedef struct {
int V;
Edge *edges;
5

int numEdges;
} Graph;

int findRoot(int parent[], int i) {


while (parent[i] != i) {
parent[i] = parent[parent[i]];
i = parent[i];
}
return i;
}

void unionSets(int parent[], int x, int y) {


int rootX = findRoot(parent, x);
int rootY = findRoot(parent, y);
parent[rootX] = rootY;
}

void kruskalMST(Graph *graph) {


int i, j;
Edge *edges = graph->edges;
int numEdges = graph->numEdges;
int V = graph->V;

for (i = 0; i < numEdges - 1; i++) {


6

for (j = i + 1; j < numEdges; j++) {


if (edges[i].weight > edges[j].weight) {
Edge temp = edges[i];
edges[i] = edges[j];
edges[j] = temp;
}
}
}

int *parent = (int *)malloc((V + 1) * sizeof(int));


for (i = 1; i <= V; i++) {
parent[i] = i;
}

int mstWeight = 0;
for (i = 0; i < numEdges; i++) {
int u = edges[i].u;
int v = edges[i].v;
int weight = edges[i].weight;

int rootU = findRoot(parent, u);


int rootV = findRoot(parent, v);

if (rootU != rootV) {
7

mstWeight += weight;
unionSets(parent, rootU, rootV);
prin ("%d Edge (%d, %d) = %d\n", i + 1, u, v, weight);
}
}

prin ("Total = %d\n", mstWeight);


free(parent);
}

int main() {
int numConnec ons;
prin ("Enter number of Connec ons: ");
scanf("%d", &numConnec ons);

Graph graph;
graph.V = numConnec ons;
graph.numEdges = 0;

int **adjacencyMatrix = (int **)malloc(numConnec ons * sizeof(int *));


for (int i = 0; i < numConnec ons; i++) {
adjacencyMatrix[i] = (int *)malloc(numConnec ons * sizeof(int));
}
8

prin ("Enter the adjacency matrix:\n");


for (int i = 0; i < numConnec ons; i++) {
for (int j = 0; j < numConnec ons; j++) {
scanf("%d", &adjacencyMatrix[i][j]);
}
}

Edge *edges = (Edge *)malloc(numConnec ons * numConnec ons *


sizeof(Edge));
for (int i = 0; i < numConnec ons; i++) {
for (int j = i + 1; j < numConnec ons; j++) {
if (adjacencyMatrix[i][j] != INF) {
edges[graph.numEdges].u = i + 1;
edges[graph.numEdges].v = j + 1;
edges[graph.numEdges].weight = adjacencyMatrix[i][j];
graph.numEdges++;
}
}
}

graph.edges = edges;

kruskalMST(&graph);

for (int i = 0; i < numConnec ons; i++) {


9

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>

#define INF 999

typedef struct {
int u, v, weight;
} Edge;

typedef struct {
int V;
11

int **adjMatrix;
} Graph;

void primMST(Graph *graph) {


int V = graph->V;
int parent[V + 1];
int key[V + 1];
bool inMST[V + 1];
Edge mstEdges[V];

for (int i = 1; i <= V; i++) {


key[i] = INF;
inMST[i] = false;
}

key[1] = 0;
parent[1] = -1;

for (int count = 1; count < V; count++) {


int min = INF, u;

for (int v = 1; v <= V; v++) {


if (!inMST[v] && key[v] < min) {
min = key[v];
12

u = v;
}
}

inMST[u] = true;

for (int v = 1; v <= V; v++) {


if (graph->adjMatrix[u][v] && !inMST[v] && graph->adjMatrix[u][v] <
key[v]) {
parent[v] = u;
key[v] = graph->adjMatrix[u][v];
}
}
}

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

for (int i = 0; i < edgeCount - 1; i++) {


for (int j = 0; j < edgeCount - i - 1; j++) {
if (mstEdges[j].weight > mstEdges[j + 1].weight) {
Edge temp = mstEdges[j];
mstEdges[j] = mstEdges[j + 1];
mstEdges[j + 1] = temp;
}
}
}

prin ("Minimum Cost:\n");


for (int i = 0; i < edgeCount; i++) {
prin ("Edge %d : (%d,%d) Cost= %d\n", i + 1, mstEdges[i].u, mstEdges[i].v,
mstEdges[i].weight);
}
prin ("Minimum Cost: %d\n", mstWeight);
}

int main() {
int numConnec ons;
prin ("Enter number of Clients: ");
scanf("%d", &numConnec ons);

Graph graph;
graph.V = numConnec ons;
14

graph.adjMatrix = (int **)malloc((numConnec ons + 1) * sizeof(int *));


for (int i = 1; i <= numConnec ons; i++) {
graph.adjMatrix[i] = (int *)malloc((numConnec ons + 1) * sizeof(int));
}

prin ("Enter the adjacency matrix:\n");


for (int i = 1; i <= numConnec ons; i++) {
for (int j = 1; j <= numConnec ons; j++) {
scanf("%d", &graph.adjMatrix[i][j]);
}
}

primMST(&graph);

for (int i = 1; i <= numConnec ons; i++) {


free(graph.adjMatrix[i]);
}
free(graph.adjMatrix);

return 0;
}
15

Output:

You might also like