Write a program using C++: The smallest last ordering: choose a vertex with the
ID: 3604866 • Letter: W
Question
Write a program using C++:
The smallest last ordering: choose a vertex with the lowest degree and put it in front of the sequence fterwards, temporarily remove the vertex from the graph. in this way, if a vertex v is removed from the graph so is each edge(vu), which lowers the degree of each neighbor of v. After the sequence of vertices is established, the links temporarily removed are restored and the for-loop is executed to assign colors to vertices. sequentialColoringAlgorithm (graph = ( V, E)) | put vertices in a certain order Vt, V2, Vn put colors in a certain order ci, C2, .,C for i = 1 to P1 j= the smallest index of col0r that does not appear in any neighbor of vi; color(V);Explanation / Answer
#include<iostream>
#include<string>
#include<malloc.h>
using namespace std;
class graph{
int no_vertex;
int **adj;
int *node_order;
string *color;
string *node_color;
bool *flag;
public:
graph(int n)
{
int i;
no_vertex=n;
color=new string[n];
adj=(int**)malloc(n*sizeof(int*));
for(i=0;i<n;i++)
adj[i]=(int*)malloc(n*sizeof(int));
node_color=new string[n];
node_order=new int[n];
flag=new bool[n];
}
void input()
{
int i,j;
cout<<"Enter theAdjacency Matrix of size: "<<no_vertex<<"X"<<no_vertex;
for(i=0;i<no_vertex;i++)
{
node_order[i]=0;
flag[i]=0;
}
for(i=0;i<no_vertex;i++)
for(j=0;j<no_vertex;j++)
{
cin>>adj[i][j];
if(adj[i][j]>0)
node_order[i]++;
}
cout<<"Enter the color: ";
for(i=0;i<no_vertex;i++)
cin>>color[i];
}
void find_color()
{
int i,j,min=100000,index=-1;
for(i=0;i<no_vertex;i++)
{
for(j=0;j<no_vertex;j++)
if((min>node_order[j])&&(flag[j]==0))
{
index=j;
min=node_order[j];
}
if(min!=100000)
{
node_color[index]=color[i];
min=100000;
flag[index]=1;
for(j=0;j<no_vertex;j++)
{
if(adj[j][index]>0)
{
adj[j][index]--;
node_order[j]--;
}
}
index=-1;
}
cout<<"Node Order Status of each iterartion ";
for(j=0;j<no_vertex;j++)
cout<<node_order[j]<<" ";
cout<<endl;
}
}
display()
{
int i;
cout<<"Node order ";
for(i=0;i<no_vertex;i++)
cout<<node_order[i]<<" ";
cout<<" color's of NodeAssigned: ";
for(i=0;i<no_vertex;i++)
cout<<node_color[i]<<" ";
}
};
int main()
{
graph G(4);
G.input();
//G.display();
G.find_color();
G.display();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.