// File: /workspace/github_cpp_makefile_star2_O1/sebastienmeyer2[P]mst-clustering/src/mst_kruskal.cpp
// Function: KruskalAlgorithm::compute_mst()
void KruskalAlgorithm::compute_mst()
{
    /*!
     * @brief Computes the Minimum Spanning Tree of the graph associated to 
     * this instance of MSTAlgorithm, using Kruskal's algorithm. More specifically,
     * sorts all the edges by their weight, and uses an Union-Find data structure
     * to select which components to union at each iteration, until there is 
     * only one left, which is the MST.
     * 
     */

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

    int n = this->initial_graph->get_number_of_nodes();
    std::vector<Node*> nodes = this->initial_graph->get_nodes();
    std::unordered_set<Edge*> edges = this->initial_graph->get_edges();

    auto cmp = [](const Edge* e1, const Edge* e2) 
        { 
            double w1 = e1->weight;
            double w2 = e2->weight;

            if (w1 < w2) {
                return false; // reversed in order to follow priority queue's implementation
            } else if (w1 > w2) {
                return true;
            } else { // allows multiple keys for same weight values (arbitrary comp)
                intptr_t p1 = reinterpret_cast<intptr_t>(e1);
                intptr_t p2 = reinterpret_cast<intptr_t>(e2);

                return p1 < p2;
            }
        };

    // initialization of the algorithm
    std::priority_queue<Edge*, std::vector<Edge*>, decltype(cmp)> pq(cmp);
    for (Edge* e : edges) {
        pq.push(e);
    }

    UnionFind uf = UnionFind(nodes);

    // while there is more than one component, we add edges that are shared 
    int k = uf.get_num_classes();
    while (k > 1) {

        Edge* min_edge = pq.top();
        pq.pop();

        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 $0x148, %rsp            # imm = 0x148
movq %rdi, %r14
movq $0x0, 0xf8(%rdi)
movq 0xa424(%rip), %rdi      # 0x16fb8
leaq 0x4c49(%rip), %rsi      # 0x117e4
movl $0x2f, %edx
callq 0x32d0
callq 0x3380
movq %rax, 0x28(%rsp)
movq 0x8(%r14), %rdi
callq 0x4d02
movq 0x8(%r14), %rsi
leaq 0x48(%rsp), %rdi
callq 0x4d06
movq 0x8(%r14), %rsi
leaq 0x60(%rsp), %rdi
callq 0x4e00
xorpd %xmm0, %xmm0
movapd %xmm0, (%rsp)
movq $0x0, 0x10(%rsp)
movq 0x70(%rsp), %rbx
testq %rbx, %rbx
je 0xcc9d
movq %rsp, %r15
leaq 0x98(%rsp), %r12
movq 0x8(%rbx), %rax
movq %rax, 0x98(%rsp)
movq 0x8(%rsp), %rsi
cmpq 0x10(%rsp), %rsi
je 0xcc22
movq %rax, (%rsi)
addq $0x8, 0x8(%rsp)
jmp 0xcc2d
movq %r15, %rdi
movq %r12, %rdx
callq 0xaf9a
movq (%rsp), %rax
movq 0x8(%rsp), %rsi
movq -0x8(%rsi), %rcx
subq %rax, %rsi
sarq $0x3, %rsi
leaq -0x1(%rsi), %rdx
cmpq $0x2, %rsi
jl 0xcc8a
movsd 0x10(%rcx), %xmm0
leaq -0x1(%rdx), %rsi
shrq $0x3f, %rsi
addq %rdx, %rsi
decq %rsi
sarq %rsi
movq (%rax,%rsi,8), %rdi
movsd 0x10(%rdi), %xmm1
ucomisd %xmm1, %xmm0
ja 0xcc8a
cmpq %rcx, %rdi
jl 0xcc7b
ucomisd %xmm0, %xmm1
jbe 0xcc8a
movq %rdi, (%rax,%rdx,8)
cmpq $0x2, %rdx
movq %rsi, %rdx
jg 0xcc50
jmp 0xcc8d
movq %rdx, %rsi
movq %rcx, (%rax,%rsi,8)
movq (%rbx), %rbx
testq %rbx, %rbx
jne 0xcbff
leaq 0x30(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0xaf1a
leaq 0x98(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0xdb40
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0xccd5
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x3290
leaq 0x98(%rsp), %rdi
callq 0xdde2
leaq 0x10(%r14), %r15
leaq 0x98(%rsp), %r12
cmpl $0x2, %eax
jl 0xcd90
movq (%rsp), %rdi
movq 0x8(%rsp), %rax
movq (%rdi), %r13
movq %rax, %rcx
subq %rdi, %rcx
cmpq $0x9, %rcx
jl 0xcd29
leaq -0x8(%rax), %rdx
movq -0x8(%rax), %rcx
movq %r13, -0x8(%rax)
subq %rdi, %rdx
sarq $0x3, %rdx
xorl %esi, %esi
callq 0xcf97
addq $-0x8, 0x8(%rsp)
movq (%r13), %rsi
movq %r12, %rdi
callq 0xddea
movq %rax, %rbp
movq 0x8(%r13), %rsi
movq %r12, %rdi
callq 0xddea
movq %rax, %rbx
cmpq %rax, %rbp
je 0xcd83
movq %r15, %rdi
movq %r13, %rsi
callq 0x47c2
movsd 0x10(%r13), %xmm0
addsd 0xf8(%r14), %xmm0
movsd %xmm0, 0xf8(%r14)
movq %r12, %rdi
movq %rbp, %rsi
movq %rbx, %rdx
callq 0xde5a
movq %r12, %rdi
callq 0xdde2
jmp 0xccee
callq 0x3380
movq %rax, %r15
movq 0xa219(%rip), %rbx      # 0x16fb8
leaq 0x49ad(%rip), %rsi      # 0x11753
movl $0x4, %edx
movq %rbx, %rdi
callq 0x32d0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x3270
movsbl %al, %esi
movq 0xa1e7(%rip), %rdi      # 0x16fb8
callq 0x3040
movq %rax, %rdi
callq 0x31a0
movq %r14, %rdi
callq 0xa7e6
movq 0xa1cb(%rip), %rdi      # 0x16fb8
leaq 0x4964(%rip), %rsi      # 0x11758
movl $0x19, %edx
callq 0x32d0
movsd 0xf8(%r14), %xmm0
movq 0xa1aa(%rip), %rdi      # 0x16fb8
callq 0x3420
movq %rax, %rbx
movq (%rax), %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
movq 0xa174(%rip), %rdi      # 0x16fb8
leaq 0x4927(%rip), %rsi      # 0x11772
movl $0x1d, %edx
callq 0x32d0
subq 0x28(%rsp), %r15
movabsq $0x20c49ba5e353f7cf, %rcx # imm = 0x20C49BA5E353F7CF
movq %r15, %rax
imulq %rcx
movq %rdx, %rsi
shrq $0x3f, %rsi
sarq $0x7, %rdx
addq %rdx, %rsi
movq 0xa139(%rip), %rdi      # 0x16fb8
callq 0x3260
movq %rax, %rbx
leaq 0x4902(%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 0x98(%rsp), %rdi
callq 0xdc62
movq (%rsp), %rdi
testq %rdi, %rdi
je 0xcee5
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x3290
leaq 0x60(%rsp), %rdi
callq 0x52b6
movq 0x48(%rsp), %rdi
testq %rdi, %rdi
je 0xcf06
movq 0x58(%rsp), %rsi
subq %rdi, %rsi
callq 0x3290
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xcf48
movq %rax, %rbx
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0xcf58
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x3290
jmp 0xcf58
jmp 0xcf3f
movq %rax, %rbx
jmp 0xcf78
jmp 0xcf48
movq %rax, %rbx
jmp 0xcf58
jmp 0xcf48
jmp 0xcf48
movq %rax, %rbx
leaq 0x98(%rsp), %rdi
callq 0xdc62
movq (%rsp), %rdi
testq %rdi, %rdi
je 0xcf6e
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x3290
leaq 0x60(%rsp), %rdi
callq 0x52b6
movq 0x48(%rsp), %rdi
testq %rdi, %rdi
je 0xcf8f
movq 0x58(%rsp), %rsi
subq %rdi, %rsi
callq 0x3290
movq %rbx, %rdi
callq 0x34a0