title: trouble implementing Brovuka's Algorithm for finding a MST
body: <p>i dunno why when running the code , there is access violation in the find function in the 1st loop , i am kinda lost , here is my code, if you guys any exisiting brovuka's c++ or java implementation that exists ?</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  int numVertices,numEdges;
  int *parent,*weight,numTrees;
  int *bestEdgeNum;
  
  struct edge {
      int tail,head,weight;
  };
  typedef struct edge edgeType;
  edgeType *edgeTab;
  
  int find(int x)
  {
      int i,j,root;
  
      for (i=x;parent[i]!=i; i=parent[i]);
          root=i;
      // path compression 
      for (i=x; parent[i]!=i;j=parent[i],parent[i]=root,i=j);
      return root;
  }
  
  void makeEquivalent(int i,int j)
  {
      if (weight[i]&gt;weight[j])
      {
          parent[j]=i;
          weight[i]+=weight[j];
      }
      else
      {
          parent[i]=j;
          weight[j]+=weight[i];
      }
      numTrees--;
  }
  
  int main()
  {
      int i,MSTweight=0;
      int root1,root2;
      int usefulEdges;
  
      cout &lt;&lt; "Enter the number of Vertices\n";
      cin &gt;&gt; numVertices;
      cout &lt;&lt; "Enter the number of Edges\n";
      cin &gt;&gt; numEdges;
  
      edgeTab = new edgeType[numEdges];
      parent = new int[numVertices];
      weight = new int[numVertices];
      bestEdgeNum = new int[numVertices];
  
      if (!edgeTab || !parent || !weight || !bestEdgeNum)
      {
          cout &lt;&lt; "error\n";
      }
  
      cout &lt;&lt; "Enter the undirected edge weights for the graph in the format u v w\n";
      for (i=0;i&lt;numEdges;i++)
      {
          cin &gt;&gt; edgeTab[i].tail &gt;&gt; edgeTab[i].head  &gt;&gt; edgeTab[i].weight;
      }
      for (i=0;i&lt;numVertices;i++)
      {
          parent[i]=i;
          weight[i]=1;
      }
      numTrees=numVertices;  // Each vertex is initially in its own subtree
      usefulEdges=numEdges;  // An edge is useful if the two vertices are separate
      while (numTrees&gt;1 &amp;&amp; usefulEdges&gt;0)
      {
          for (i=0;i&lt;numVertices;i++)
              bestEdgeNum[i]=(-1);
          usefulEdges=0;
          for (i=0;i&lt;numEdges;i++)
          {
              root1=find(edgeTab[i].tail);
                      root2=find(edgeTab[i].head);
                      if (root1==root2)
                          cout &lt;&lt; edgeTab[i].tail &lt;&lt;" , " &lt;&lt;  edgeTab[i].head &lt;&lt; " : "
                               &lt;&lt; edgeTab[i].weight &lt;&lt; " is useless\n";
                      else
                      {
                          usefulEdges++;
                          if (bestEdgeNum[root1] == -1||edgeTab[bestEdgeNum[root1]].weight&gt;edgeTab[i].weight)
                              bestEdgeNum[root1]=i;  // Have a new best edge from this component
  
              if (bestEdgeNum[root2]==(-1)|| edgeTab[bestEdgeNum[root2]].weight&gt;edgeTab[i].weight)
                  bestEdgeNum[root2]=i;  // Have a new best edge from this component
          }
      }
      for (i=0;i&lt;numVertices;i++)
          if (bestEdgeNum[i]!=(-1))
          {
              root1=find(edgeTab[bestEdgeNum[i]].tail);
              root2=find(edgeTab[bestEdgeNum[i]].head);
              if (root1==root2)
                  continue;  // This round has already connected these components.
              MSTweight+=edgeTab[bestEdgeNum[i]].weight;
              cout &lt;&lt; edgeTab[bestEdgeNum[i]].tail &lt;&lt; " " &lt;&lt; edgeTab[bestEdgeNum[i]].head &lt;&lt; " " &lt;&lt; edgeTab[bestEdgeNum[i]].weight &lt;&lt; "included in MST\n";    
              makeEquivalent(root1,root2);
                      }
                      cout &lt;&lt; "numTrees is " &lt;&lt; numTrees &lt;&lt; endl;
  
      }
      if (numTrees!=1)
          cout &lt;&lt; "MST does not exist\n";
      cout &lt;&lt; "Sum of weights of spanning edges is " &lt;&lt; MSTweight &lt;&lt; endl;
  
      return 0;
  }
  </code></pre>