// File: /workspace/github_cpp_makefile_star2_O2/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, %r12
andq $0x0, 0xf8(%rdi)
movq 0xc043(%rip), %rdi      # 0x16fb0
leaq 0x4841(%rip), %rsi      # 0xf7b5
callq 0x32a0
callq 0x33a0
movq %rax, %r15
movq 0x8(%r12), %rsi
leaq 0xe0(%rsp), %rdi
callq 0x46e4
movq 0x8(%r12), %rsi
leaq 0x40(%rsp), %rdi
callq 0x465e
movq 0x40(%rsp), %rbx
movq 0x48(%rsp), %r14
movq %r12, 0x28(%rsp)
leaq 0x10(%r12), %rax
movq %rax, 0x30(%rsp)
cmpq %r14, %rbx
je 0xafd3
movq (%rbx), %rsi
movq 0x30(%rsp), %rdi
callq 0x4556
addq $0x8, %rbx
jmp 0xafbb
leaq 0x58(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x9a6c
movq %r15, 0x38(%rsp)
leaq 0x118(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0xc084
leaq 0x58(%rsp), %rdi
callq 0x5d78
leaq 0x118(%rsp), %rdi
callq 0xc242
movl %eax, %r15d
leaq 0xa8(%rsp), %rbx
leaq 0x118(%rsp), %r13
leaq 0x70(%rsp), %rbp
cmpl $0x2, %r15d
jl 0xb249
movq %rbx, %rdi
movq %r13, %rsi
callq 0xc230
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)
leaq 0xb8(%rsp), %rbx
movq (%rbx), %rbx
testq %rbx, %rbx
je 0xb09a
movq 0x8(%rbx), %rax
movq %rax, 0x8(%rsp)
andq $0x0, 0x10(%rsp)
movq %rbp, %rdi
leaq 0x8(%rsp), %rsi
callq 0xac1c
jmp 0xb074
leaq 0xf0(%rsp), %r15
leaq 0x20(%rsp), %rbx
movq (%r15), %r15
testq %r15, %r15
je 0xb19b
movq 0x8(%r15), %r12
movq (%r12), %rsi
movq 0x8(%r12), %r14
movq %r13, %rdi
callq 0xc24a
movq %rax, 0x20(%rsp)
movq %r13, %rdi
movq %r14, %rsi
callq 0xc24a
movq %rax, 0x18(%rsp)
cmpq %rax, 0x20(%rsp)
je 0xb0a7
movq %rbp, %rdi
movq %rbx, %rsi
callq 0xaafc
cmpq $0x0, (%rax)
je 0xb111
movq %rbp, %rdi
movq %rbx, %rsi
callq 0xaafc
movq (%rax), %rax
movsd 0x10(%rax), %xmm0
ucomisd 0x10(%r12), %xmm0
jbe 0xb138
movq %rbp, %rdi
movq %rbx, %rsi
callq 0xab1a
movq 0x20(%rsp), %rax
movq %rax, 0x8(%rsp)
movq %r12, 0x10(%rsp)
movq %rbp, %rdi
leaq 0x8(%rsp), %rsi
callq 0xac1c
movq %rbp, %rdi
leaq 0x18(%rsp), %rsi
callq 0xaafc
cmpq $0x0, (%rax)
je 0xb16d
movq %rbp, %rdi
leaq 0x18(%rsp), %rsi
callq 0xaafc
movq (%rax), %rax
movsd 0x10(%rax), %xmm0
ucomisd 0x10(%r12), %xmm0
jbe 0xb0a7
movq %rbp, %rdi
leaq 0x18(%rsp), %rsi
callq 0xab1a
movq 0x18(%rsp), %rax
movq %rax, 0x8(%rsp)
movq %r12, 0x10(%rsp)
movq %rbp, %rdi
leaq 0x8(%rsp), %rsi
callq 0xac1c
jmp 0xb0a7
leaq 0xb8(%rsp), %rbx
movq (%rbx), %rbx
testq %rbx, %rbx
je 0xb221
movq 0x8(%rbx), %rax
movq %rax, 0x8(%rsp)
movq %rbp, %rdi
leaq 0x8(%rsp), %rsi
callq 0xaafc
movq (%rax), %r12
movq (%r12), %rsi
movq %r13, %rdi
callq 0xc24a
movq %rax, %r15
movq 0x8(%r12), %rsi
movq %r13, %rdi
callq 0xc24a
movq %rax, %r14
cmpq %rax, %r15
je 0xb1a3
movq 0x30(%rsp), %rdi
movq %r12, %rsi
callq 0x4250
movsd 0x10(%r12), %xmm0
movq 0x28(%rsp), %rax
addsd 0xf8(%rax), %xmm0
movsd %xmm0, 0xf8(%rax)
movq %r13, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0xc2c4
jmp 0xb1a3
movq %r13, %rdi
callq 0xc242
movl %eax, %r15d
movq %rbp, %rdi
callq 0xa9d8
leaq 0xa8(%rsp), %rbx
movq %rbx, %rdi
callq 0x4a48
jmp 0xb028
callq 0x33a0
movq %rax, %rbx
movq 0xbd58(%rip), %rdi      # 0x16fb0
leaq 0x4504(%rip), %rsi      # 0xf763
callq 0x32a0
movq 0x38(%rsp), %r14
movq 0x28(%rsp), %r15
movq %rax, %rdi
callq 0x3130
movq %r15, %rdi
callq 0x944c
movq 0xbd2b(%rip), %rdi      # 0x16fb0
leaq 0x44dc(%rip), %rsi      # 0xf768
callq 0x32a0
movsd 0xf8(%r15), %xmm0
movq %rax, %rdi
callq 0x3430
movq %rax, %rdi
callq 0x3130
movq 0xbcff(%rip), %rdi      # 0x16fb0
leaq 0x44ca(%rip), %rsi      # 0xf782
callq 0x32a0
movq %rax, %rdi
subq %r14, %rbx
movl $0x3e8, %ecx            # imm = 0x3E8
movq %rbx, %rax
cqto
idivq %rcx
movq %rax, %rsi
callq 0x3290
leaq 0x44c1(%rip), %rsi      # 0xf7a0
movq %rax, %rdi
callq 0x32a0
movq %rax, %rdi
callq 0x3130
leaq 0x118(%rsp), %rdi
callq 0xc19a
leaq 0x40(%rsp), %rdi
callq 0x5d78
leaq 0xe0(%rsp), %rdi
callq 0x4cfe
addq $0x1c8, %rsp            # imm = 0x1C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xb343
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0x5d78
jmp 0xb382
jmp 0xb348
movq %rax, %rbx
jmp 0xb38c
jmp 0xb343
jmp 0xb343
jmp 0xb35b
movq %rax, %rbx
jmp 0xb375
movq %rax, %rbx
jmp 0xb382
jmp 0xb35b
jmp 0xb35b
jmp 0xb35b
jmp 0xb35b
jmp 0xb35b
jmp 0xb35b
jmp 0xb35b
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0xa9d8
leaq 0xa8(%rsp), %rdi
callq 0x4a48
leaq 0x118(%rsp), %rdi
callq 0xc19a
leaq 0x40(%rsp), %rdi
callq 0x5d78
leaq 0xe0(%rsp), %rdi
callq 0x4cfe
movq %rbx, %rdi
callq 0x34b0
nopl (%rax)