I need help on my C programming homework that is on graphs T
I need help on my C programming homework that is on graphs. The pseudo code can be fairly simple using plain english and same with the next two parts. Thanks in advanced!!
Think of a heuristic for the graph vertex coloring problem (1) write a pseudo code to explain your heuristic (2) Use an example to show how your heuristic works (3) Find a counter-example where your heuristic fails to find an optimal solution.Solution
// A C++ program to implement greedy formula for graph coloring
#include <iostream>
#include <list>
using namespace std;
// a category that represents AN directionless graph
class Graph
closeness lists
public:
// builder and destructor
Graph(int V)
~Graph()
// operate to feature a position to graph
void addEdge(int v, int w);
// Prints greedy coloring of the vertices
void greedyColoring();
};
void Graph::addEdge(int v, int w)
{
adj[v].push_back(w);
adj[w].push_back(v); // Note: the graph is directionless
}
// Assigns colours (starting from 0) to any or all vertices and prints
// the assignment of colours
void Graph::greedyColoring()
initial|the primary} color to first vertex
result[0] = 0;
// Initialize remaining doodlebug vertices as unassigned
for (int u = 1; u < V; u++)
result[u] = -1; // no color is appointed to u
// a short lived array to store the offered colours. True
// price of available[cr] would mean that the colour atomic number 24 is
// appointed to 1 of its adjacent vertices
bool available[V];
for (int atomic number 24 = 0; atomic number 24 < V; cr++)
available[cr] = false;
// Assign colours to remaining doodlebug vertices
for (int u = 1; u < V; u++)
method all adjacent vertices and flag their colours
// as out of stock
list<int>::iterator i;
for (i = adj[u].begin(); i != adj[u].end(); ++i)
if (result[*i] != -1)
available[result[*i]] = true;
// notice the primary offered color
int cr;
for (cr = 0; atomic number 24 < V; cr++)
if (available[cr] == false)
break;
result[u] = cr; // Assign the found color
// Reset the values back to false for ensuing iteration
for (i = adj[u].begin(); i != adj[u].end(); ++i)
if (result[*i] != -1)
available[result[*i]] = false;
}
// print the result
for (int u = 0; u < V; u++)
cout << \"Vertex \" << u << \" ---> Color \"
<< result[u] << endl;
}
// Driver program to check higher than operate
int main()

