0% found this document useful (0 votes)
22 views5 pages

Exp8 1

This C code implements a graph coloring algorithm to color the vertices of a graph using a specified maximum number of colors such that no adjacent vertices share the same color. The code takes user input for the number of vertices, adjacency matrix, and maximum number of colors. It then uses a recursive backtracking algorithm to try all possible color combinations and print out any valid colorings it finds.

Uploaded by

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

Exp8 1

This C code implements a graph coloring algorithm to color the vertices of a graph using a specified maximum number of colors such that no adjacent vertices share the same color. The code takes user input for the number of vertices, adjacency matrix, and maximum number of colors. It then uses a recursive backtracking algorithm to try all possible color combinations and print out any valid colorings it finds.

Uploaded by

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

NAME : DISHANT H SHAH ROLL : 16 CLASS:SE-B/COMPS

SUBJECT:AOA EXPERIMENT : 8

CODE:
#include<conio.h>
#include<stdio.h>

char color[10] = {'x','R','G','B','Y','O'};

int x[10]={0,0,0,0,0,0,0,0,0,0};

int weight[10][10];
int m,n;

void NextValue(int k)
{
    int j;
 
    while(1)
    {
        x[k] = (x[k] + 1)  %  (m+1);
        if(x[k] == 0)
            return;
        for(j=1;j<=n;j++)
        {
            if(weight[k][j]!=0 && x[k] == x[j])
            break;
        }
        if(j == n+1)
            return;
    }
}

void graph_color(int k)
{
    int i;

    while(1)
    {
          NextValue(k);
        if(x[k] == 0)
            return;
        if(k == n)
        {
            printf("{");
            for(i=1;i<=n;i++)
            {
                if(i == n)
                printf("%c",color[x[i]]);
                else
                printf("%c,",color[x[i]]);
NAME : DISHANT H SHAH ROLL : 16 CLASS:SE-B/COMPS
SUBJECT:AOA EXPERIMENT : 8

            }
            printf("}");
            printf("\n");
            return;
        }
        else
        graph_color(k+1);
          for(i=k+1;i<=n;i++)
          x[i] = 0;
    }
}

void main()
{
    int i,j;
    n = 4;
    m = 3;
    printf("\nEnter Number of Vertices :");
    scanf("%d",&n);

    printf("\nEnter the weight of the vertices :\n");


    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n;j++)
        {
            printf("w[%d][%d]:",i,j);
            scanf("%d",&weight[i][j]);
        }
    }

    printf("\nEnter the Chromatic Number :");


    scanf("%d",&m);

printf("\nMAtrix of the Graph :\n");


    for(i=1;i<=n;i++)
    {
        printf("\n");
        for(j=1;j<=n;j++)
        {
            printf("%d ",weight[i][j]);
        }
    }
    printf("\n");

    printf("\nSolution :\n");

    graph_color(1);
}
NAME : DISHANT H SHAH ROLL : 16 CLASS:SE-B/COMPS
SUBJECT:AOA EXPERIMENT : 8

OUTPUT for following Graph :


NAME : DISHANT H SHAH ROLL : 16 CLASS:SE-B/COMPS
SUBJECT:AOA EXPERIMENT : 8

OUTPUT for following Graph:


NAME : DISHANT H SHAH ROLL : 16 CLASS:SE-B/COMPS
SUBJECT:AOA EXPERIMENT : 8

You might also like