-
Notifications
You must be signed in to change notification settings - Fork 0
/
Graph color new and updated.c
96 lines (83 loc) · 1.31 KB
/
Graph color new and updated.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include<stdio.h>
#include<stdlib.h>
#define N 100
int adjm[N][N],n;
void ColorTheGraph(){
int nodeColor[N],i,j,colorUsed[N],col;
char colList[N][20]={"Red", "Blue", "Green", "Violet","Purple", "Yellow","Black","Beige","White","Cyan","Grey","Indigo"};
for(i=0;i<N;i++)nodeColor[i]=(colorUsed[i]=0)-1;
nodeColor[0]=0;
for(i=1;i<n;i++){
for(j=0;j<n;j++){
if(adjm[i][j] && nodeColor[j]!=-1)
colorUsed[nodeColor[j]]=1;
}
col=-1;
for(j=0;j<n;j++){
if(colorUsed[j]==0){
//printf("j = %d\n",j);
col=j;
break;
}
}
nodeColor[i]=col;
for(j=0;j<n;j++){
if(adjm[i][j] && nodeColor[j]!=-1)
colorUsed[nodeColor[j]]=0;
}
}
printf("\n\nThe Color used in nodes are:\n");
for(i=0;i<n;i++){
printf("Node %d is colored ",i);
if(nodeColor[i]>=12)
printf("Color%d",(nodeColor[i]-12));
else printf("%s",colList[nodeColor[i]]);
puts("");
}
}
int main(){
int i,j,e,a,b;
for(i=0;i<N;i++)for(j=0;j<N;j++)adjm[i][j]=0;
printf("Enter the number of Nodes: ");
scanf("%d",&n);
printf("Enter the number of Edges: ");
scanf("%d",&e);
printf("Enter the Edges:\n");
for(i=0;i<e;i++){
scanf("%d%d",&a,&b);
adjm[a][b]=adjm[b][a]=1;
}
ColorTheGraph();
}
/*
4
5
0 1
0 2
2 3
3 1
0 3
0----1
|\\ |
| \\|
2----3
9
17
0 1
0 4
0 5
0 3
1 5
1 6
1 7
2 5
2 6
3 7
4 5
4 8
5 6
5 8
6 7
6 8
7 8
*/