0% found this document useful (0 votes)
10 views8 pages

Assignment 5 Solution

The document contains solutions for three programming assignments in C: the first implements Kruskal's Algorithm to find the minimum cost spanning tree, the second solves the n-queens problem using backtracking, and the third colors the vertices of a graph using a greedy algorithm. Each solution includes the necessary code and a brief explanation of its functionality. Sample outputs are also provided for each program.

Uploaded by

bastenvan389
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)
10 views8 pages

Assignment 5 Solution

The document contains solutions for three programming assignments in C: the first implements Kruskal's Algorithm to find the minimum cost spanning tree, the second solves the n-queens problem using backtracking, and the third colors the vertices of a graph using a greedy algorithm. Each solution includes the necessary code and a brief explanation of its functionality. Sample outputs are also provided for each program.

Uploaded by

bastenvan389
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/ 8

Assignment-5 Solution

1. Write a C Program to find minimum cost spanning tree using Kruskal’s Algorithm for
the following graph.

Solution:
#include <stdio.h>

int i, j, k, a, b, u, v, n, ne = 1;
int min, mincost = 0, cost[9][9], parent[9];

int find(int);
int uni(int, int);

void main() {
printf("\n\tImplementation of Kruskal's Algorithm\n");

printf("\nEnter the no. of vertices: ");


scanf("%d", &n);

printf("\nEnter the cost adjacency matrix:\n");


for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
scanf("%d", &cost[i][j]);
if (cost[i][j] == 0)
cost[i][j] = 999;
}
}

printf("The edges of Minimum Cost Spanning Tree are\n");


while (ne < n) {
for (i = 1, min = 999; i <= n; i++) {
for (j = 1; j <= n; j++) {
if (cost[i][j] < min) {
min = cost[i][j];
a = u = i;
b = v = j;
}
}
}

u = find(u);
v = find(v);

if (uni(u, v)) {
printf("%d edge (%d,%d) =%d\n", ne++, a, b, min);
mincost += min;
}

cost[a][b] = cost[b][a] = 999;


}

printf("\n\tMinimum cost = %d\n", mincost);


}

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

int uni(int i, int j) {


if (i != j) {
parent[j] = i;
return 1;
}
return 0;
}

2) Write a C Program to implement n-queen’s problem.


Solution:
#include <stdio.h>
#include <math.h>
#include<stdlib.h>

int board[20], count;

int main() {
int n, i, j;
void queen(int row, int n);
printf(" - N Queens Problem Using Backtracking -");
printf("\n\nEnter number of Queens: ");
scanf("%d", &n);
queen(1, n);
return 0;
}

// function for printing the solution


void print(int n) {
int i, j;
printf("\n\nSolution %d:\n\n", ++count);

for (i = 1; i <= n; ++i)


printf("\t%d", i);

for (i = 1; i <= n; ++i) {


printf("\n\n%d", i);
for (j = 1; j <= n; ++j) { // for nxn board
if (board[i] == j)
printf("\tQ"); // queen at i,j position
else
printf("\t-"); // empty slot
}
}
}

/* function to check conflicts


If no conflict for desired position returns 1 otherwise returns 0 */
int place(int row, int column) {
int i;
for (i = 1; i <= row - 1; ++i) {
// checking column and diagonal conflicts
if (board[i] == column)
return 0;
else if (abs(board[i] - column) == abs(i - row))
return 0;
}
return 1; // no conflicts
}

// function to check for proper positioning of queen


void queen(int row, int n) {
int column;
for (column = 1; column <= n; ++column) {
if (place(row, column)) {
board[row] = column; // no conflicts so place queen
if (row == n) // dead end
print(n); // printing the board configuration
else // try queen with next position
queen(row + 1, n);
}
}
}Sample Output:

3)
Solution;
#include <stdio.h>

int G[50][50], x[50];

void next_color(int k) {
int i, j;
x[k] = 1; // coloring vertex with color1
for (i = 0; i < k; i++) {
// checking all k-1 vertices - backtracking
if (G[i][k] != 0 && x[k] == x[i]) // if connected and has same color
x[k] = x[i] + 1; // assign higher color than x[i]
}
}

int main() {
int n, e, i, j, k, l;

printf("Enter no. of vertices : ");


scanf("%d", &n);

printf("Enter no. of edges : ");


scanf("%d", &e);

for (i = 0; i < n; i++)


for (j = 0; j < n; j++)
G[i][j] = 0; // assign 0 to all indices of adjacency matrix

printf("Enter indexes where value is 1 -->\n");


for (i = 0; i < e; i++) {
scanf("%d %d", &k, &l);
G[k][l] = 1;
G[l][k] = 1;
}

for (i = 0; i < n; i++)


next_color(i); // coloring each vertex
printf("Colors of vertices -->\n");
for (i = 0; i < n; i++) // displaying color of each vertex
printf("Vertex[%d] : %d\n", i + 1, x[i]);

return 0;
}

Sample Output:

You might also like