// File: 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
movq %rsp, %rbp
subq $0x3c0, %rsp            # imm = 0x3C0
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x2f0(%rbp)
movq $0x0, 0xf8(%rax)
movq 0x22334(%rip), %rdi     # 0x4ffb0
leaq 0x9c77(%rip), %rsi      # 0x378fa
callq 0xa780
callq 0xaa70
movq %rax, %rcx
movq -0x2f0(%rbp), %rax
movq %rcx, -0x10(%rbp)
movq 0x8(%rax), %rsi
leaq -0x48(%rbp), %rdi
callq 0xd660
movq -0x2f0(%rbp), %rax
movq 0x8(%rax), %rsi
leaq -0x60(%rbp), %rdi
callq 0xd530
jmp 0x2dcbe
leaq -0x60(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rdi
callq 0x15bd0
movq %rax, -0x80(%rbp)
movq -0x78(%rbp), %rdi
callq 0x15a20
movq %rax, -0x88(%rbp)
leaq -0x80(%rbp), %rdi
leaq -0x88(%rbp), %rsi
callq 0x27a50
testb $0x1, %al
jne 0x2dcf9
jmp 0x2dd54
leaq -0x80(%rbp), %rdi
callq 0x15fb0
movq -0x2f0(%rbp), %rdi
movq (%rax), %rax
movq %rax, -0x90(%rbp)
addq $0x10, %rdi
movq -0x90(%rbp), %rsi
callq 0xd2f0
jmp 0x2dd25
jmp 0x2dd27
leaq -0x80(%rbp), %rdi
callq 0x27b00
jmp 0x2dce3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0x2e4f6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0x2e4ed
leaq -0x158(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x27940
jmp 0x2dd66
leaq -0x140(%rbp), %rdi
leaq -0x158(%rbp), %rsi
callq 0x30fd0
jmp 0x2dd7b
leaq -0x158(%rbp), %rdi
callq 0xded0
leaq -0x140(%rbp), %rdi
callq 0x31340
movl %eax, -0x2f4(%rbp)
jmp 0x2dd9b
movl -0x2f4(%rbp), %eax
movl %eax, -0x15c(%rbp)
cmpl $0x1, -0x15c(%rbp)
jle 0x2e352
leaq -0x198(%rbp), %rdi
leaq -0x140(%rbp), %rsi
callq 0x31310
jmp 0x2ddc9
leaq -0x1d0(%rbp), %rdi
callq 0x2b4a0
leaq -0x198(%rbp), %rax
movq %rax, -0x1d8(%rbp)
movq -0x1d8(%rbp), %rdi
callq 0x24690
movq %rax, -0x1e0(%rbp)
movq -0x1d8(%rbp), %rdi
callq 0xdd10
movq %rax, -0x1e8(%rbp)
leaq -0x1e0(%rbp), %rdi
leaq -0x1e8(%rbp), %rsi
callq 0x246c0
testb $0x1, %al
jne 0x2de25
jmp 0x2df06
leaq -0x1e0(%rbp), %rdi
callq 0x246f0
movq (%rax), %rax
movq %rax, -0x1f0(%rbp)
movq $0x0, -0x208(%rbp)
leaq -0x200(%rbp), %rdi
leaq -0x1f0(%rbp), %rsi
leaq -0x208(%rbp), %rdx
callq 0x2e510
jmp 0x2de62
leaq -0x1d0(%rbp), %rdi
leaq -0x200(%rbp), %rsi
callq 0x2b5b0
movb %dl, -0x301(%rbp)
movq %rax, -0x300(%rbp)
jmp 0x2de84
movb -0x301(%rbp), %al
movq -0x300(%rbp), %rcx
movq %rcx, -0x218(%rbp)
movb %al, -0x210(%rbp)
leaq -0x1e0(%rbp), %rdi
callq 0x24740
jmp 0x2de09
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0x158(%rbp), %rdi
callq 0xded0
jmp 0x2e4ed
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0x2e4e1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0x1d0(%rbp), %rdi
callq 0x2b710
leaq -0x198(%rbp), %rdi
callq 0xdc60
jmp 0x2e4e1
leaq -0x48(%rbp), %rax
movq %rax, -0x220(%rbp)
movq -0x220(%rbp), %rdi
callq 0xe520
movq %rax, -0x228(%rbp)
movq -0x220(%rbp), %rdi
callq 0xe550
movq %rax, -0x230(%rbp)
leaq -0x228(%rbp), %rdi
leaq -0x230(%rbp), %rsi
callq 0xe580
testb $0x1, %al
jne 0x2df53
jmp 0x2e1a6
leaq -0x228(%rbp), %rdi
callq 0xe5b0
movq (%rax), %rax
movq %rax, -0x238(%rbp)
movq -0x238(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x240(%rbp)
movq -0x238(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x248(%rbp)
movq -0x240(%rbp), %rsi
leaq -0x140(%rbp), %rdi
callq 0x31360
movq %rax, -0x310(%rbp)
jmp 0x2dfa8
movq -0x310(%rbp), %rax
movq %rax, -0x250(%rbp)
movq -0x248(%rbp), %rsi
leaq -0x140(%rbp), %rdi
callq 0x31360
movq %rax, -0x318(%rbp)
jmp 0x2dfd2
movq -0x318(%rbp), %rax
movq %rax, -0x258(%rbp)
movq -0x250(%rbp), %rax
cmpq -0x258(%rbp), %rax
jne 0x2dff5
jmp 0x2e195
leaq -0x1d0(%rbp), %rdi
leaq -0x250(%rbp), %rsi
callq 0x2b550
movq %rax, -0x320(%rbp)
jmp 0x2e011
movq -0x320(%rbp), %rax
cmpq $0x0, (%rax)
je 0x2e057
leaq -0x1d0(%rbp), %rdi
leaq -0x250(%rbp), %rsi
callq 0x2b550
movq %rax, -0x328(%rbp)
jmp 0x2e03a
movq -0x328(%rbp), %rax
movq (%rax), %rax
movsd 0x10(%rax), %xmm0
movq -0x238(%rbp), %rax
ucomisd 0x10(%rax), %xmm0
jbe 0x2e0c4
leaq -0x1d0(%rbp), %rdi
leaq -0x250(%rbp), %rsi
callq 0x2b580
jmp 0x2e06c
leaq -0x268(%rbp), %rdi
leaq -0x250(%rbp), %rsi
leaq -0x238(%rbp), %rdx
callq 0x2b5f0
jmp 0x2e088
leaq -0x1d0(%rbp), %rdi
leaq -0x268(%rbp), %rsi
callq 0x2b5b0
movb %dl, -0x331(%rbp)
movq %rax, -0x330(%rbp)
jmp 0x2e0aa
movb -0x331(%rbp), %al
movq -0x330(%rbp), %rcx
movq %rcx, -0x278(%rbp)
movb %al, -0x270(%rbp)
leaq -0x1d0(%rbp), %rdi
leaq -0x258(%rbp), %rsi
callq 0x2b550
movq %rax, -0x340(%rbp)
jmp 0x2e0e0
movq -0x340(%rbp), %rax
cmpq $0x0, (%rax)
je 0x2e126
leaq -0x1d0(%rbp), %rdi
leaq -0x258(%rbp), %rsi
callq 0x2b550
movq %rax, -0x348(%rbp)
jmp 0x2e109
movq -0x348(%rbp), %rax
movq (%rax), %rax
movsd 0x10(%rax), %xmm0
movq -0x238(%rbp), %rax
ucomisd 0x10(%rax), %xmm0
jbe 0x2e193
leaq -0x1d0(%rbp), %rdi
leaq -0x258(%rbp), %rsi
callq 0x2b580
jmp 0x2e13b
leaq -0x288(%rbp), %rdi
leaq -0x258(%rbp), %rsi
leaq -0x238(%rbp), %rdx
callq 0x2b5f0
jmp 0x2e157
leaq -0x1d0(%rbp), %rdi
leaq -0x288(%rbp), %rsi
callq 0x2b5b0
movb %dl, -0x351(%rbp)
movq %rax, -0x350(%rbp)
jmp 0x2e179
movb -0x351(%rbp), %al
movq -0x350(%rbp), %rcx
movq %rcx, -0x298(%rbp)
movb %al, -0x290(%rbp)
jmp 0x2e195
leaq -0x228(%rbp), %rdi
callq 0xe5e0
jmp 0x2df37
leaq -0x198(%rbp), %rax
movq %rax, -0x2a0(%rbp)
movq -0x2a0(%rbp), %rdi
callq 0x24690
movq %rax, -0x2a8(%rbp)
movq -0x2a0(%rbp), %rdi
callq 0xdd10
movq %rax, -0x2b0(%rbp)
leaq -0x2a8(%rbp), %rdi
leaq -0x2b0(%rbp), %rsi
callq 0x246c0
testb $0x1, %al
jne 0x2e1f6
jmp 0x2e315
leaq -0x2a8(%rbp), %rdi
callq 0x246f0
movq (%rax), %rax
movq %rax, -0x2b8(%rbp)
leaq -0x1d0(%rbp), %rdi
leaq -0x2b8(%rbp), %rsi
callq 0x2b550
movq %rax, -0x360(%rbp)
jmp 0x2e228
movq -0x360(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x2c0(%rbp)
movq -0x2c0(%rbp), %rax
movq (%rax), %rsi
leaq -0x140(%rbp), %rdi
callq 0x31360
movq %rax, -0x368(%rbp)
jmp 0x2e258
movq -0x368(%rbp), %rax
movq %rax, -0x2c8(%rbp)
movq -0x2c0(%rbp), %rax
movq 0x8(%rax), %rsi
leaq -0x140(%rbp), %rdi
callq 0x31360
movq %rax, -0x370(%rbp)
jmp 0x2e286
movq -0x370(%rbp), %rax
movq %rax, -0x2d0(%rbp)
movq -0x2c8(%rbp), %rax
cmpq -0x2d0(%rbp), %rax
je 0x2e302
movq -0x2f0(%rbp), %rdi
addq $0x10, %rdi
movq -0x2c0(%rbp), %rsi
callq 0xcef0
jmp 0x2e2bd
movq -0x2f0(%rbp), %rax
movq -0x2c0(%rbp), %rcx
movsd 0x10(%rcx), %xmm1
movsd 0xf8(%rax), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0xf8(%rax)
movq -0x2c8(%rbp), %rsi
movq -0x2d0(%rbp), %rdx
leaq -0x140(%rbp), %rdi
callq 0x31410
jmp 0x2e300
jmp 0x2e302
jmp 0x2e304
leaq -0x2a8(%rbp), %rdi
callq 0x24740
jmp 0x2e1da
leaq -0x140(%rbp), %rdi
callq 0x31340
movl %eax, -0x374(%rbp)
jmp 0x2e329
movl -0x374(%rbp), %eax
movl %eax, -0x15c(%rbp)
leaq -0x1d0(%rbp), %rdi
callq 0x2b710
leaq -0x198(%rbp), %rdi
callq 0xdc60
jmp 0x2dda7
callq 0xaa70
movq %rax, -0x2d8(%rbp)
movq 0x21c4b(%rip), %rdi     # 0x4ffb0
leaq 0x953c(%rip), %rsi      # 0x378a8
callq 0xa780
movq %rax, -0x380(%rbp)
jmp 0x2e37a
movq -0x380(%rbp), %rdi
movq 0x21c00(%rip), %rsi     # 0x4ff88
callq 0xa7e0
jmp 0x2e38f
movq -0x2f0(%rbp), %rdi
callq 0x265c0
jmp 0x2e39d
movq 0x21c0c(%rip), %rdi     # 0x4ffb0
leaq 0x9502(%rip), %rsi      # 0x378ad
callq 0xa780
movq %rax, -0x388(%rbp)
jmp 0x2e3b9
movq -0x388(%rbp), %rdi
movq -0x2f0(%rbp), %rax
movsd 0xf8(%rax), %xmm0
callq 0xaee0
movq %rax, -0x390(%rbp)
jmp 0x2e3dd
movq -0x390(%rbp), %rdi
movq 0x21b9d(%rip), %rsi     # 0x4ff88
callq 0xa7e0
jmp 0x2e3f2
movq 0x21bb7(%rip), %rdi     # 0x4ffb0
leaq 0x94c7(%rip), %rsi      # 0x378c7
callq 0xa780
movq %rax, -0x398(%rbp)
jmp 0x2e40e
leaq -0x2d8(%rbp), %rdi
leaq -0x10(%rbp), %rsi
callq 0x2b6a0
movq %rax, -0x3a0(%rbp)
jmp 0x2e427
movq -0x3a0(%rbp), %rax
movq %rax, -0x2e8(%rbp)
leaq -0x2e8(%rbp), %rdi
callq 0x2b670
movq %rax, -0x3a8(%rbp)
jmp 0x2e44a
movq -0x3a8(%rbp), %rax
movq %rax, -0x2e0(%rbp)
leaq -0x2e0(%rbp), %rdi
callq 0x2b6f0
movq %rax, -0x3b0(%rbp)
jmp 0x2e46d
movq -0x3b0(%rbp), %rsi
movq -0x398(%rbp), %rdi
callq 0xae50
movq %rax, -0x3b8(%rbp)
jmp 0x2e489
movq -0x3b8(%rbp), %rdi
leaq 0x944e(%rip), %rsi      # 0x378e5
callq 0xa780
movq %rax, -0x3c0(%rbp)
jmp 0x2e4a5
movq -0x3c0(%rbp), %rdi
movq 0x21ad5(%rip), %rsi     # 0x4ff88
callq 0xa7e0
jmp 0x2e4ba
leaq -0x140(%rbp), %rdi
callq 0x311e0
leaq -0x60(%rbp), %rdi
callq 0xded0
leaq -0x48(%rbp), %rdi
callq 0xdf50
addq $0x3c0, %rsp            # imm = 0x3C0
popq %rbp
retq
leaq -0x140(%rbp), %rdi
callq 0x311e0
leaq -0x60(%rbp), %rdi
callq 0xded0
leaq -0x48(%rbp), %rdi
callq 0xdf50
movq -0x68(%rbp), %rdi
callq 0xade0
nopl (%rax,%rax)