title: welsh powell algorithm code giving segmentation fault
body: <p>I've found an implementation of famous welsh Powell algorithm code in c++, and just to check it's working I compiled the code and executed it. unfortunately, it's giving me a segmentation fault and I'm not able to figure this out. Any helps would be highly appreciated.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;list&gt;
  #include &lt;algorithm&gt;
  using namespace std;
  
  // A class that represents an undirected graph
  class Graph
  {
      int V;    // No. of vertices
      list&lt;int&gt; *adj;    // A dynamic array of adjacency lists
  public:
      // Constructor and destructor
      Graph(int V)
      {
          this-&gt;V = V;
          adj = new list&lt;int&gt;[V];
      }
      ~Graph()
      {
          delete [] adj;
      }
  
      // function to add an edge 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 undirected
  }
  
  struct vertexDegree
  {
      int v;
      int degree;
  };
  
  bool compareByDegree(const vertexDegree&amp; x, const vertexDegree&amp; y)
  {
      return x.degree &gt; y.degree;
  }
  
  // Assigns colors (starting from 0) to all vertices and prints
  // the assignment of colors
  void Graph::greedyColoring()
  {
      int result[V];
  
      // Initialize remaining V-1 vertices as unassigned
      for (int u = 1; u &lt; V; u++)
          result[u] = -1;  // no color is assigned to u
  
      // A temporary array to store the available colors. True
      // value of available[cr] would mean that the color cr is
      // assigned to one of its adjacent vertices
      bool available[V];
  
      for (int cr = 0; cr &lt; V; cr++)
          available[cr] = false;
  
      vertexDegree arr[V];
      for (int i = 0; i &lt; V; i++)
      {
          arr[i].v = i;
          arr[i].degree = adj[i].size();
      }
  
  
      sort(arr, arr+V, compareByDegree);
  
      cout &lt;&lt; "Sorted vertices \n";
      for (int i = 0; i &lt;V; i++)
      {
          cout &lt;&lt; arr[i].v &lt;&lt; " ";
      }
      cout &lt;&lt; "\n";
  
      // Assign the first color to first vertex in sorted array
      result[arr[0].v]  = 0;
  
  
      // Assign colors to remaining V-1 vertices
      for (int x = 1; x &lt; V; x++)
      {
          int u = arr[x].v;
  
          // Process all adjacent vertices and flag their colors
          // as unavailable
          list&lt;int&gt;::iterator i;
          for (i = adj[u].begin(); i != adj[u].end(); ++i)
              if (result[*i] != -1)
                  available[result[*i]] = true;
  
          // Find the first available color
          int cr;
          for (cr = 0; cr &lt; V; cr++)
              if (available[cr] == false)
                  break;
  
          result[u] = cr; // Assign the found color
  
          // Reset the values back to false for the next 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 &lt; V; u++)
          cout &lt;&lt; "Vertex " &lt;&lt; u &lt;&lt; " ---&gt;  Color "
               &lt;&lt; result[u] &lt;&lt; endl;
  }
  
  // Driver program to test above function
  int main()
  {
      Graph g1(5);
      g1.addEdge(0, 1);
      g1.addEdge(0, 2);
      g1.addEdge(1, 2);
      g1.addEdge(1, 3);
      g1.addEdge(2, 3);
      g1.addEdge(3, 4);
      cout &lt;&lt; "Coloring of Graph 1 \n";
      g1.greedyColoring();
  
      Graph g2(5);
      g2.addEdge(0, 1);
      g2.addEdge(0, 2);
      g2.addEdge(1, 2);
      g2.addEdge(1, 4);
      g2.addEdge(2, 4);
      g2.addEdge(4, 3);
      cout &lt;&lt; "\nColoring of Graph 2 \n";
      g2.greedyColoring();
  
      return 0;
  }   
  </code></pre>