// File: /workspace/github_cpp_makefile_star2_O1/sebastienmeyer2[P]mst-clustering/src/mst_boruvka.cpp
// Function: BoruvkaAlgorithm::compute_mst()
void BoruvkaAlgorithm::compute_mst() 
{
    /*!
     * @brief Computes the Minimum Spanning Tree of the graph associated to 
     * this instance of MSTAlgorithm, using Boruvka's algorithm. More specifically,
     * initializes a Union-Find data structure with all the nodes of the graph, 
     * and unions connected components of the MST with respect to their edge of 
     * minimal weight, until there is only one component left.
     * 
     */

    this->mst_weight = 0.0;
    std::cout << "\nComputing MST using Boruvka's algorithm...";
    std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();

    // initialization of the algorithm
    std::unordered_set<Edge*> edges = this->initial_graph->get_edges();
    std::vector<Node*> nodes = this->initial_graph->get_nodes();
    for(Node* n : nodes) {
        this->mst_graph.add_node(n);
    }

    UnionFind uf = UnionFind(nodes);

    // filling the MST with edges until there is only one component left
    int k = uf.get_num_classes();
    while (k > 1) {

        // for each component, we will keep track of the external edge with minimal weight
        std::unordered_set<Node*> representatives = uf.get_representatives();
        std::unordered_map<Node*, Edge*> min_edges;
        for (Node* rep : representatives) {
            min_edges.insert(std::pair<Node*, Edge*>(rep, NULL));
        }

        // getting all the edges with minimal weights according to components (not best complexity here)
        for (Edge*  e : edges) {

            Node* n_in = e->p1;
            Node* n_out = e->p2;

            Node* rep_in = uf.Find(n_in);
            Node* rep_out = uf.Find(n_out);

            if (rep_in == rep_out) {
                continue;
            }

            if (min_edges.at(rep_in) == NULL || min_edges.at(rep_in)->weight > e->weight) {
                min_edges.erase(rep_in);
                min_edges.insert(std::pair<Node*, Edge*>(rep_in, e));
            }

            if (min_edges.at(rep_out) == NULL || min_edges.at(rep_out)->weight > e->weight) {
                min_edges.erase(rep_out);
                min_edges.insert(std::pair<Node*, Edge*>(rep_out, e));
            }
        
        }

        // linking components
        for (Node* rep : representatives) {
            Edge* min_edge = min_edges.at(rep);

            Node* rep1 = uf.Find(min_edge->p1);
            Node* rep2 = uf.Find(min_edge->p2);

            if (rep1 != rep2) {
                this->mst_graph.add_edge(min_edge);
                this->mst_weight += min_edge->weight;
                uf.Union(rep1, rep2);
            }
        }

        k = uf.get_num_classes();
    }

    std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
    std::cout << "[OK]" << std::endl;
    this->treatment_done();

    std::cout << "Total weight of the MST: " << this->mst_weight << std::endl;

    std::cout << "Time spent by the algorithm: " << std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count() << " µs" << std::endl;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1c8, %rsp            # imm = 0x1C8
movq %rdi, %rbx
movq $0x0, 0xf8(%rdi)
movq 0xa970(%rip), %rdi      # 0x16fb8
leaq 0x5156(%rip), %rsi      # 0x117a5
movl $0x2b, %edx
callq 0x32d0
callq 0x3380
movq %rax, 0x48(%rsp)
movq 0x8(%rbx), %rsi
leaq 0xe0(%rsp), %rdi
callq 0x4e00
movq %rbx, 0x8(%rsp)
movq 0x8(%rbx), %rsi
leaq 0x30(%rsp), %rdi
callq 0x4d06
movq 0x30(%rsp), %r14
movq 0x38(%rsp), %r15
cmpq %r15, %r14
je 0xc6b3
movq 0x8(%rsp), %rax
leaq 0x10(%rax), %rbx
movq (%r14), %rsi
movq %rbx, %rdi
callq 0x4ba4
addq $0x8, %r14
cmpq %r15, %r14
jne 0xc69f
leaq 0x58(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0xaf1a
leaq 0x118(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0xdb40
movq 0x58(%rsp), %rdi
testq %rdi, %rdi
je 0xc6eb
movq 0x68(%rsp), %rsi
subq %rdi, %rsi
callq 0x3290
leaq 0x118(%rsp), %rdi
callq 0xdde2
cmpl $0x2, %eax
jl 0xc947
movq 0x8(%rsp), %rax
addq $0x10, %rax
movq %rax, 0x50(%rsp)
leaq 0xa8(%rsp), %rbx
leaq 0x118(%rsp), %r13
leaq 0x70(%rsp), %rbp
movq %rbx, %rdi
movq %r13, %rsi
callq 0xdd92
leaq 0xa0(%rsp), %rax
movq %rax, 0x70(%rsp)
movq $0x1, 0x78(%rsp)
xorpd %xmm0, %xmm0
movupd %xmm0, -0x20(%rax)
movl $0x3f800000, 0x90(%rsp) # imm = 0x3F800000
movupd %xmm0, -0x8(%rax)
movq 0xb8(%rsp), %rbx
testq %rbx, %rbx
je 0xc78f
movq 0x8(%rbx), %rax
movq %rax, 0x10(%rsp)
movq $0x0, 0x18(%rsp)
movq %rbp, %rdi
leaq 0x10(%rsp), %rsi
callq 0xc366
movq (%rbx), %rbx
jmp 0xc766
movq 0xf0(%rsp), %r15
testq %r15, %r15
leaq 0x28(%rsp), %rbx
je 0xc894
movq 0x8(%r15), %r12
movq (%r12), %rsi
movq 0x8(%r12), %r14
movq %r13, %rdi
callq 0xddea
movq %rax, 0x28(%rsp)
movq %r13, %rdi
movq %r14, %rsi
callq 0xddea
movq %rax, 0x20(%rsp)
cmpq %rax, 0x28(%rsp)
je 0xc888
movq %rbp, %rdi
movq %rbx, %rsi
callq 0xc23e
cmpq $0x0, (%rax)
je 0xc807
movq %rbp, %rdi
movq %rbx, %rsi
callq 0xc23e
movq (%rax), %rax
movsd 0x10(%rax), %xmm0
ucomisd 0x10(%r12), %xmm0
jbe 0xc82e
movq %rbp, %rdi
movq %rbx, %rsi
callq 0xc280
movq 0x28(%rsp), %rax
movq %rax, 0x10(%rsp)
movq %r12, 0x18(%rsp)
movq %rbp, %rdi
leaq 0x10(%rsp), %rsi
callq 0xc366
movq %rbp, %rdi
leaq 0x20(%rsp), %rsi
callq 0xc23e
cmpq $0x0, (%rax)
je 0xc85f
movq %rbp, %rdi
leaq 0x20(%rsp), %rsi
callq 0xc23e
movq (%rax), %rax
movsd 0x10(%rax), %xmm0
ucomisd 0x10(%r12), %xmm0
jbe 0xc888
movq %rbp, %rdi
leaq 0x20(%rsp), %rsi
callq 0xc280
movq 0x20(%rsp), %rax
movq %rax, 0x10(%rsp)
movq %r12, 0x18(%rsp)
movq %rbp, %rdi
leaq 0x10(%rsp), %rsi
callq 0xc366
movq (%r15), %r15
testq %r15, %r15
jne 0xc7a5
movq 0xb8(%rsp), %rbx
testq %rbx, %rbx
je 0xc91a
movq 0x8(%rbx), %rax
movq %rax, 0x10(%rsp)
movq %rbp, %rdi
leaq 0x10(%rsp), %rsi
callq 0xc23e
movq (%rax), %r12
movq (%r12), %rsi
movq %r13, %rdi
callq 0xddea
movq %rax, %r15
movq 0x8(%r12), %rsi
movq %r13, %rdi
callq 0xddea
movq %rax, %r14
cmpq %rax, %r15
je 0xc915
movq 0x50(%rsp), %rdi
movq %r12, %rsi
callq 0x47c2
movsd 0x10(%r12), %xmm0
movq 0x8(%rsp), %rax
addsd 0xf8(%rax), %xmm0
movsd %xmm0, 0xf8(%rax)
movq %r13, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0xde5a
movq (%rbx), %rbx
jmp 0xc89c
movq %r13, %rdi
callq 0xdde2
movl %eax, %r14d
movq %rbp, %rdi
callq 0xc186
leaq 0xa8(%rsp), %rbx
movq %rbx, %rdi
callq 0x50cc
cmpl $0x1, %r14d
jg 0xc724
callq 0x3380
movq %rax, %rbx
movq 0xa662(%rip), %r14      # 0x16fb8
leaq 0x4df6(%rip), %rsi      # 0x11753
movl $0x4, %edx
movq %r14, %rdi
callq 0x32d0
movq (%r14), %rax
addq -0x18(%rax), %r14
movq %r14, %rdi
movl $0xa, %esi
callq 0x3270
movsbl %al, %esi
movq 0xa630(%rip), %rdi      # 0x16fb8
callq 0x3040
movq %rax, %rdi
callq 0x31a0
movq 0x8(%rsp), %rdi
callq 0xa7e6
movq 0xa612(%rip), %rdi      # 0x16fb8
leaq 0x4dab(%rip), %rsi      # 0x11758
movl $0x19, %edx
callq 0x32d0
movq 0x8(%rsp), %rax
movsd 0xf8(%rax), %xmm0
movq 0xa5ed(%rip), %rdi      # 0x16fb8
callq 0x3420
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x3270
movsbl %al, %esi
movq %r14, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31a0
movq 0xa5b7(%rip), %rdi      # 0x16fb8
leaq 0x4d6a(%rip), %rsi      # 0x11772
movl $0x1d, %edx
callq 0x32d0
subq 0x48(%rsp), %rbx
movabsq $0x20c49ba5e353f7cf, %rcx # imm = 0x20C49BA5E353F7CF
movq %rbx, %rax
imulq %rcx
movq %rdx, %rsi
shrq $0x3f, %rsi
sarq $0x7, %rdx
addq %rdx, %rsi
movq 0xa57c(%rip), %rdi      # 0x16fb8
callq 0x3260
movq %rax, %rbx
leaq 0x4d45(%rip), %rsi      # 0x11790
movl $0x4, %edx
movq %rax, %rdi
callq 0x32d0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3270
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31a0
leaq 0x118(%rsp), %rdi
callq 0xdc62
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0xcaa3
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x3290
leaq 0xe0(%rsp), %rdi
callq 0x52b6
addq $0x1c8, %rsp            # imm = 0x1C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xcaed
movq %rax, %rbx
movq 0x58(%rsp), %rdi
testq %rdi, %rdi
je 0xcb2c
movq 0x68(%rsp), %rsi
subq %rdi, %rsi
callq 0x3290
jmp 0xcb2c
jmp 0xcaf2
movq %rax, %rbx
jmp 0xcb43
jmp 0xcaed
jmp 0xcaed
jmp 0xcb05
movq %rax, %rbx
jmp 0xcb1f
movq %rax, %rbx
jmp 0xcb2c
jmp 0xcb05
jmp 0xcb05
jmp 0xcb05
jmp 0xcb05
jmp 0xcb05
jmp 0xcb05
jmp 0xcb05
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0xc186
leaq 0xa8(%rsp), %rdi
callq 0x50cc
leaq 0x118(%rsp), %rdi
callq 0xdc62
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0xcb43
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x3290
leaq 0xe0(%rsp), %rdi
callq 0x52b6
movq %rbx, %rdi
callq 0x34a0