// File: /workspace/github_cpp_makefile_star2_O3/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 0xa7f4(%rip), %rdi      # 0x16fb8
leaq 0x501a(%rip), %rsi      # 0x117e5
movl $0x2b, %edx
callq 0x32d0
callq 0x3380
movq %rax, 0x48(%rsp)
movq 0x8(%rbx), %rsi
leaq 0xe0(%rsp), %rdi
callq 0x4dc6
movq %rbx, 0x8(%rsp)
movq 0x8(%rbx), %rsi
leaq 0x30(%rsp), %rdi
callq 0x4ccc
movq 0x30(%rsp), %r14
movq 0x38(%rsp), %r15
cmpq %r15, %r14
je 0xc82f
movq 0x8(%rsp), %rax
leaq 0x10(%rax), %rbx
movq (%r14), %rsi
movq %rbx, %rdi
callq 0x4ba0
addq $0x8, %r14
cmpq %r15, %r14
jne 0xc81b
leaq 0x58(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0xb0c4
leaq 0x118(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0xdeac
movq 0x58(%rsp), %rdi
testq %rdi, %rdi
je 0xc867
movq 0x68(%rsp), %rsi
subq %rdi, %rsi
callq 0x3290
leaq 0x118(%rsp), %rdi
callq 0xe0d8
cmpl $0x2, %eax
jl 0xcbf3
movq 0x8(%rsp), %rax
addq $0x10, %rax
movq %rax, 0x50(%rsp)
leaq 0xa8(%rsp), %r14
leaq 0x118(%rsp), %r13
leaq 0x70(%rsp), %rbp
movq %r14, %rdi
movq %r13, %rsi
callq 0xe088
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 0xc90b
movq 0x8(%rbx), %rax
movq %rax, 0x10(%rsp)
movq $0x0, 0x18(%rsp)
movq %rbp, %rdi
leaq 0x10(%rsp), %rsi
callq 0xc4f0
movq (%rbx), %rbx
jmp 0xc8e2
movq 0xf0(%rsp), %r14
testq %r14, %r14
je 0xcafd
movq 0x8(%r14), %r15
movq (%r15), %rsi
movq 0x8(%r15), %rbx
movq %r13, %rdi
callq 0xe0e0
movq %rax, 0x28(%rsp)
movq %r13, %rdi
movq %rbx, %rsi
callq 0xe0e0
movq %rax, %rcx
movq %rax, 0x20(%rsp)
movq 0x28(%rsp), %rsi
cmpq %rax, %rsi
je 0xcaf5
movq 0x70(%rsp), %r10
movq 0x78(%rsp), %r9
movq %rsi, %rax
xorl %edx, %edx
divq %r9
movq (%r10,%rdx,8), %rax
testq %rax, %rax
je 0xcd6e
movq %rdx, %rdi
movq (%rax), %r11
movq 0x8(%r11), %rbx
movq %r11, %r12
cmpq %rbx, %rsi
je 0xc9e3
movq (%r12), %r12
testq %r12, %r12
je 0xcd6e
movq 0x8(%r12), %r8
movq %r8, %rax
xorl %edx, %edx
divq %r9
cmpq %rdi, %rdx
jne 0xcd6e
cmpq %r8, %rsi
jne 0xc986
cmpq $0x0, 0x10(%r12)
je 0xc9fb
cmpq %rbx, %rsi
je 0xc9ea
movq (%r11), %r11
testq %r11, %r11
je 0xcd6e
movq 0x8(%r11), %r8
movq %r8, %rax
xorl %edx, %edx
divq %r9
cmpq %rdi, %rdx
jne 0xcd6e
cmpq %r8, %rsi
jne 0xc9bb
jmp 0xc9ea
cmpq $0x0, 0x10(%r11)
je 0xc9fb
movq 0x10(%r11), %rax
movsd 0x10(%rax), %xmm0
ucomisd 0x10(%r15), %xmm0
jbe 0xca33
movq %rbp, %rdi
leaq 0x28(%rsp), %rsi
callq 0xc400
movq 0x28(%rsp), %rax
movq %rax, 0x10(%rsp)
movq %r15, 0x18(%rsp)
movq %rbp, %rdi
leaq 0x10(%rsp), %rsi
callq 0xc4f0
movq 0x20(%rsp), %rcx
movq 0x70(%rsp), %r10
movq 0x78(%rsp), %r9
movq %rcx, %rax
xorl %edx, %edx
divq %r9
movq (%r10,%rdx,8), %rax
testq %rax, %rax
je 0xcd6e
movq %rdx, %rsi
movq (%rax), %r8
movq 0x8(%r8), %r10
movq %r8, %r11
cmpq %r10, %rcx
je 0xcab4
movq (%r11), %r11
testq %r11, %r11
je 0xcd6e
movq 0x8(%r11), %rdi
movq %rdi, %rax
xorl %edx, %edx
divq %r9
cmpq %rsi, %rdx
jne 0xcd6e
cmpq %rdi, %rcx
jne 0xca5a
cmpq $0x0, 0x10(%r11)
je 0xcacc
cmpq %r10, %rcx
je 0xcabb
movq (%r8), %r8
testq %r8, %r8
je 0xcd6e
movq 0x8(%r8), %rdi
movq %rdi, %rax
xorl %edx, %edx
divq %r9
cmpq %rsi, %rdx
jne 0xcd6e
cmpq %rdi, %rcx
jne 0xca8c
jmp 0xcabb
cmpq $0x0, 0x10(%r8)
je 0xcacc
movq 0x10(%r8), %rax
movsd 0x10(%rax), %xmm0
ucomisd 0x10(%r15), %xmm0
jbe 0xcaf5
movq %rbp, %rdi
leaq 0x20(%rsp), %rsi
callq 0xc400
movq 0x20(%rsp), %rax
movq %rax, 0x10(%rsp)
movq %r15, 0x18(%rsp)
movq %rbp, %rdi
leaq 0x10(%rsp), %rsi
callq 0xc4f0
movq (%r14), %r14
jmp 0xc913
movq 0xb8(%rsp), %r12
testq %r12, %r12
je 0xcbc8
movq 0x8(%r12), %rcx
movq 0x70(%rsp), %rdi
movq 0x78(%rsp), %r8
movq %rcx, %rax
xorl %edx, %edx
divq %r8
movq (%rdi,%rdx,8), %rax
testq %rax, %rax
je 0xcd7a
movq %rdx, %rsi
movq (%rax), %r9
cmpq 0x8(%r9), %rcx
je 0xcb64
movq (%r9), %r9
testq %r9, %r9
je 0xcd7a
movq 0x8(%r9), %rdi
movq %rdi, %rax
xorl %edx, %edx
divq %r8
cmpq %rsi, %rdx
jne 0xcd7a
cmpq %rdi, %rcx
jne 0xcb3e
movq 0x10(%r9), %rbx
movq (%rbx), %rsi
movq %r13, %rdi
callq 0xe0e0
movq %rax, %r15
movq 0x8(%rbx), %rsi
movq %r13, %rdi
callq 0xe0e0
movq %rax, %r14
cmpq %rax, %r15
je 0xcbbf
movq 0x50(%rsp), %rdi
movq %rbx, %rsi
callq 0x477c
movsd 0x10(%rbx), %xmm0
movq 0x8(%rsp), %rax
addsd 0xf8(%rax), %xmm0
movsd %xmm0, 0xf8(%rax)
movq %r13, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0xe15c
movq (%r12), %r12
jmp 0xcb05
movq %r13, %rdi
callq 0xe0d8
movl %eax, %ebx
movq %rbp, %rdi
callq 0xc390
leaq 0xa8(%rsp), %r14
movq %r14, %rdi
callq 0x50b2
cmpl $0x1, %ebx
jg 0xc8a0
callq 0x3380
movq %rax, %rbx
movq 0xa3b6(%rip), %r14      # 0x16fb8
leaq 0x4b8a(%rip), %rsi      # 0x11793
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 0xa384(%rip), %rdi      # 0x16fb8
callq 0x3040
movq %rax, %rdi
callq 0x31a0
movq 0x8(%rsp), %rdi
callq 0xa9a6
movq 0xa366(%rip), %rdi      # 0x16fb8
leaq 0x4b3f(%rip), %rsi      # 0x11798
movl $0x19, %edx
callq 0x32d0
movq 0x8(%rsp), %rax
movsd 0xf8(%rax), %xmm0
movq 0xa341(%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 0xa30b(%rip), %rdi      # 0x16fb8
leaq 0x4afe(%rip), %rsi      # 0x117b2
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 0xa2d0(%rip), %rdi      # 0x16fb8
callq 0x3260
movq %rax, %rbx
leaq 0x4ad9(%rip), %rsi      # 0x117d0
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 0xdfce
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0xcd4f
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x3290
leaq 0xe0(%rsp), %rdi
callq 0x529c
addq $0x1c8, %rsp            # imm = 0x1C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x4415(%rip), %rdi      # 0x1118a
callq 0x33e0
leaq 0x4409(%rip), %rdi      # 0x1118a
callq 0x33e0
jmp 0xcdb1
movq %rax, %rbx
movq 0x58(%rsp), %rdi
testq %rdi, %rdi
je 0xcdf2
movq 0x68(%rsp), %rsi
subq %rdi, %rsi
callq 0x3290
jmp 0xcdf2
jmp 0xcdb6
movq %rax, %rbx
jmp 0xce09
jmp 0xcdb1
jmp 0xcdb1
jmp 0xcdcb
movq %rax, %rbx
jmp 0xcde5
movq %rax, %rbx
jmp 0xcdf2
jmp 0xcdcb
jmp 0xcdcb
jmp 0xcdcb
jmp 0xcdcb
jmp 0xcdcb
jmp 0xcdcb
jmp 0xcdcb
jmp 0xcdcb
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0xc390
leaq 0xa8(%rsp), %rdi
callq 0x50b2
leaq 0x118(%rsp), %rdi
callq 0xdfce
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0xce09
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x3290
leaq 0xe0(%rsp), %rdi
callq 0x529c
movq %rbx, %rdi
callq 0x34a0
nop