#include
#include
//library untuk file
#include
void main()
{
char pilih;
do
{
clrscr();
char filename[100];
cout<<"==================="< cout<<"| Program |"< cout<<"| Colouring Graph |"< cout<<"==================="< cout< ifstream infile;
//membuka file
do
{
cout<<"Masukkan nama file sebagai inputan program : ";
cin>>filename;
infile.open(filename);
//validasi file
if (!infile)
{
cout<<"File tidak ada coba perikasa kembali!"< }
}while (!infile);
typedef struct{
int stat,color;
int check;
char name[20];
}idver;
int l[20];
int temp2,n1[20];
int i,j,k,bar,kol,temp;
idver vertex[20][20],vert[20];
//membaca isi file (baris dan kolom
infile>>bar;
infile>>kol;
cout< cout<<"Silahkan beri nama masing - masing vertex"< for (i=0; i {
cout<<"Vertex ke-"<<(i+1)<<" : ";
cin>>vert[i].name;
}
//membaca isi file (status vertex, 0 atau 1)
for (i=0; i {
n1[i]=0;
for (j=0; j {
infile>>temp;
vertex[i][j].stat = temp;
//menghitung nilai satu
if (temp==1)
n1[i]++;
}
}
//mencari baris yang paling banyak memiliki satu
int max = -1;
for (i=0;i { if (n1[i] > max)
{
max=n1[i];
}
}
int res=max;
int incolor =1;
for (i=0; i {
for (j=i+1; j {
vertex[i][j].check =0;
}
vert[i].color=0;
}
max=-1;
for (i=0; i {
/*for (j=0; j {
for (k=j+1; k {
if (vertex[j][k].connect ==0)
if ((vertex[i][j].stat==1) && (vertex[i][k].stat==1))
vertex[i][j].connect =1;
}
}
*/
vert[i].color=0;
}
//pewarnaan
for (i=0; i {
for (j=0; j {
for (k=j+1; k {
if ((n1[i]>=max))
{
if (vertex[j][k].check == 0)
{
if((vertex[i][j].stat ==1) && (vertex[i][k].stat ==1))
{
vertex[j][k].check =1;
if (vert[j].color == 0)
{
vert[j].color = incolor;
vert[k].color = incolor + 1;
}
else
{
vert[k].color = vert[j].color + 1;
}
}
else
{
if ((vertex[i][j].stat==1) && (vertex[i][k].stat==0))
{
if (vert[j].color == 0)
{
vert[j].color = incolor;
vert[k].color = incolor;
}
else
{
vert[k].color = vert[j].color;
}
}
else
{
if (j==0)
vert[k].color = vert[j].color=incolor;
}
}
}
}
}
}
max=n1[i];
}
cout<<"---------------------------"< cout< cout<<"Lanjut untuk detail"< getch();
typedef struct {
char nama[10];
}warna;
//list warna
warna color[10];
strcpy(color[1].nama,"Merah");
strcpy(color[2].nama,"Jingga");
strcpy(color[3].nama,"Kuning");
strcpy(color[4].nama,"Hijau");
strcpy(color[5].nama,"Biru");
strcpy(color[6].nama,"Nila");
strcpy(color[7].nama,"Ungu");
cout< for (i=0; i {
if (res==kol)
cout< else
cout<
}
cout<<"------------------------------------"< cout<<"Keterangan"< for (i=0; i {
if ((vert[i].color == vert[i+1].color) && (kol!=res))
{
cout< }
else if (kol==res)
{
cout<<"Semua vertex harus memiliki warna berbeda"< i=kol;
}
}
infile.close();
cout<<"====================================================================="< cout<<"Ulang program ? (y/n) : ";
cin>>pilih;
}while ((pilih=='y') || (pilih=='Y'));
}
Untuk adk2 smster 3 maap karena program yg di atas gak jalan..Makanya jangan maen copy aj..
Karena ak baek hati ini klik aja di sini untuk donlod program nya, tp pake C++..
Jangan lupa komen yaw...
Baca selengkapnya...