// File: /workspace/github_cpp_makefile_star2_O3/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 0xa15c(%rip), %rdi      # 0x16fb8
leaq 0x49c1(%rip), %rsi      # 0x11824
movl $0x2f, %edx
callq 0x32d0
callq 0x3380
movq %rax, 0x28(%rsp)
movq 0x8(%r14), %rdi
callq 0x4cc8
movq 0x8(%r14), %rsi
leaq 0x48(%rsp), %rdi
callq 0x4ccc
movq 0x8(%r14), %rsi
leaq 0x60(%rsp), %rdi
callq 0x4dc6
xorpd %xmm0, %xmm0
movapd %xmm0, (%rsp)
movq $0x0, 0x10(%rsp)
movq 0x70(%rsp), %rbx
testq %rbx, %rbx
je 0xcf64
movq %rsp, %r15
leaq 0x98(%rsp), %r12
movq 0x8(%rbx), %rax
movq %rax, 0x98(%rsp)
movq 0x8(%rsp), %rsi
cmpq 0x10(%rsp), %rsi
je 0xcef2
movq %rax, (%rsi)
movq 0x8(%rsp), %rsi
addq $0x8, %rsi
movq %rsi, 0x8(%rsp)
jmp 0xcf02
movq %r15, %rdi
movq %r12, %rdx
callq 0xb144
movq 0x8(%rsp), %rsi
movq (%rsp), %rax
movq -0x8(%rsi), %rcx
subq %rax, %rsi
sarq $0x3, %rsi
leaq -0x1(%rsi), %rdx
cmpq $0x2, %rsi
jl 0xcf54
movsd 0x10(%rcx), %xmm0
leaq -0x1(%rdx), %rsi
movq %rsi, %rdi
shrq %rdi
movq (%rax,%rdi,8), %r8
movsd 0x10(%r8), %xmm1
ucomisd %xmm1, %xmm0
ja 0xcf54
cmpq %rcx, %r8
jl 0xcf45
ucomisd %xmm0, %xmm1
jbe 0xcf54
movq %r8, (%rax,%rdx,8)
movq %rdi, %rdx
cmpq $0x1, %rsi
ja 0xcf20
xorl %edx, %edx
movq %rcx, (%rax,%rdx,8)
movq (%rbx), %rbx
testq %rbx, %rbx
jne 0xcec7
leaq 0x30(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0xb0c4
leaq 0x98(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0xdeac
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0xcf9c
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x3290
leaq 0x98(%rsp), %rdi
callq 0xe0d8
leaq 0x10(%r14), %r15
leaq 0x98(%rsp), %r12
cmpl $0x2, %eax
jl 0xd05f
movq (%rsp), %rdi
movq 0x8(%rsp), %rax
movq (%rdi), %r13
movq %rax, %rcx
subq %rdi, %rcx
cmpq $0x9, %rcx
jl 0xcff5
leaq -0x8(%rax), %rdx
movq -0x8(%rax), %rcx
movq %r13, -0x8(%rax)
subq %rdi, %rdx
sarq $0x3, %rdx
xorl %esi, %esi
callq 0xd266
movq 0x8(%rsp), %rax
addq $-0x8, %rax
movq %rax, 0x8(%rsp)
movq (%r13), %rsi
movq %r12, %rdi
callq 0xe0e0
movq %rax, %rbp
movq 0x8(%r13), %rsi
movq %r12, %rdi
callq 0xe0e0
movq %rax, %rbx
cmpq %rax, %rbp
je 0xd052
movq %r15, %rdi
movq %r13, %rsi
callq 0x477c
movsd 0x10(%r13), %xmm0
addsd 0xf8(%r14), %xmm0
movsd %xmm0, 0xf8(%r14)
movq %r12, %rdi
movq %rbp, %rsi
movq %rbx, %rdx
callq 0xe15c
movq %r12, %rdi
callq 0xe0d8
jmp 0xcfb5
callq 0x3380
movq %rax, %r15
movq 0x9f4a(%rip), %rbx      # 0x16fb8
leaq 0x471e(%rip), %rsi      # 0x11793
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 0x9f18(%rip), %rdi      # 0x16fb8
callq 0x3040
movq %rax, %rdi
callq 0x31a0
movq %r14, %rdi
callq 0xa9a6
movq 0x9efc(%rip), %rdi      # 0x16fb8
leaq 0x46d5(%rip), %rsi      # 0x11798
movl $0x19, %edx
callq 0x32d0
movsd 0xf8(%r14), %xmm0
movq 0x9edb(%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 0x9ea5(%rip), %rdi      # 0x16fb8
leaq 0x4698(%rip), %rsi      # 0x117b2
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 0x9e6a(%rip), %rdi      # 0x16fb8
callq 0x3260
movq %rax, %rbx
leaq 0x4673(%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 0x98(%rsp), %rdi
callq 0xdfce
movq (%rsp), %rdi
testq %rdi, %rdi
je 0xd1b4
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x3290
leaq 0x60(%rsp), %rdi
callq 0x529c
movq 0x48(%rsp), %rdi
testq %rdi, %rdi
je 0xd1d5
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 0xd217
movq %rax, %rbx
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0xd227
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x3290
jmp 0xd227
jmp 0xd20e
movq %rax, %rbx
jmp 0xd247
jmp 0xd217
movq %rax, %rbx
jmp 0xd227
jmp 0xd217
jmp 0xd217
movq %rax, %rbx
leaq 0x98(%rsp), %rdi
callq 0xdfce
movq (%rsp), %rdi
testq %rdi, %rdi
je 0xd23d
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x3290
leaq 0x60(%rsp), %rdi
callq 0x529c
movq 0x48(%rsp), %rdi
testq %rdi, %rdi
je 0xd25e
movq 0x58(%rsp), %rsi
subq %rdi, %rsi
callq 0x3290
movq %rbx, %rdi
callq 0x34a0