#include "Graph.h"
#include "FindUnion.h"
#include <iostream>

Graph::Graph(int v, int e)
{
    this->v = v;
    this->e = e;
    edges = new Edge[e];
    for (int i = 0; i < e; i++)
    {
        std::cout << "Enter edge " << i+1 << ":\n";
        edges[i].input();
    }
    arr = new char[v];
    size = new int[v];
    initialise(arr, size, v);
    accepted_edges = new Edge[v - 1];
}

int comp(const void* a, const void* b)
{
    Edge* a1 = (Edge*)a;
    Edge* b1 = (Edge*)b;
    return a1->get_w() > b1->get_w();
}

Edge* Graph::get_accepted_edges()
{
    qsort(edges, e, sizeof(edges[0]), comp);
    int count = 0, i = 0;
    while (count < v - 1)
    {
        if (!(root(arr, edges[i].get_v1(), edges[i].get_v2())))
        {
            accepted_edges[count] = edges[i];
            combine(arr, size, edges[i].get_v1(), edges[i].get_v2());
            count++;
        }
        i++;
    }
    return accepted_edges;
}