I created a greedy coloring program in C++. While running this code I receive the error: identifier V is undefined on line 39 code 'int result[V];'. I have the same variable on line 46 'bool available[V];' but i don't get an error there. I think it has something to do with the vectors i used but i am not sure. Any help is greatly appreciated.
#include "stdafx.h"
#include <iostream>
#include <list>
#include <vector>
using namespace std;
class Graph
{
vector<list<int>> adj;
public:
Graph(int V);
~Graph(){}
void addEdge(int v, int w);
void greedyColoring(vector<bool>& available);
};
Graph::Graph(int V)
{
adj.resize(V);
}
void Graph::addEdge(int v, int w)
{
adj[v].push_back(w);
adj[w].push_back(v);
}
void Graph::greedyColoring(vector<bool>& available)
{
int result[V];
result[0] = 0;
for (int u = 1; u < V; u++)
result[u] = -1;
bool available[V];
for (int cr = 0; cr < V; cr++)
available[cr] = false;
for (int u = 1; u < V; u++)
{
list<int>::iterator i;
for (i = adj[u].begin(); i != adj[u].end(); ++i)
if (result[*i] != -1)
available[result[*i]] = true;
int cr;
for (cr = 0; cr < V; cr++)
if (available[cr] == false)
break;
result[u] = cr;
for (i = adj[u].begin(); i != adj[u].end(); ++i)
if (result[*i] != -1)
available[result[*i]] = false;
}
for (int u = 0; u < V; u++)
cout << "Vertex " << u << " ---> Color"
<< result[u] << endl;
}
int main()
{
Graph g(6);
g.addEdge(0, 1);
g.addEdge(0, 4);
g.addEdge(0, 5);
g.addEdge(1, 3);
g.addEdge(1, 4);
g.addEdge(2, 3);
g.addEdge(2, 4);
g.addEdge(4, 5);
cout << "Coloring of the graph \n";
g.greedyColoring();
return 0;
}