// File: /workspace/github_cpp_makefile_star2_O3/sebastienmeyer2[P]mst-clustering/src/mst_algorithm.cpp
// Function: MSTAlgorithm::compute_clustering(int)
std::unordered_map<Node*, Node*> MSTAlgorithm::compute_clustering(int k)
{
    /*!
     * @brief Creates a mapping between nodes of the MST (and initial) graph 
     * after it is built, and their representatives where we have eliminated 
     * the k-1 most expansive edges from the MST graph. 
     * 
     * @param k The number of clusters to build 
     * 
     * @return A mapping between nodes of the graph(s) and their representatives 
     * after removing k-1 edges
     * 
     */

    if (!this->treated) {
        this->compute_mst();
    }

    int n = this->mst_graph.get_number_of_nodes();
    assert(("There are less points than clusters!", k <= n));

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

            if (w1 < w2) {
                return false;
            } else if (w1 > w2) {
                return true;
            } else { // Allows multiple keys for same weight values
                intptr_t p1 = reinterpret_cast<intptr_t>(e1);
                intptr_t p2 = reinterpret_cast<intptr_t>(e2);

                return p1 < p2;
            }
        };

    // builds an ordered queue of the edges of the MST graph
    std::unordered_set<Edge*> mst_edges = this->mst_graph.get_edges();

    std::priority_queue<Edge*, std::vector<Edge*>, decltype(cmp)> pq(cmp);

    for (Edge* e : mst_edges) {
        pq.push(e);
    }

    // selects only the E-(k-1) lightest edges of the MST graph
    std::vector<Node*> nodes = this->mst_graph.get_nodes();
    UnionFind uf = UnionFind(nodes);

    int nb_edges = (n-1) - (k-1); // MST is a tree 

    for (int i = 0; i < nb_edges; i ++) {
        Edge* min_edge = pq.top();
        uf.Union(min_edge->p1, min_edge->p2);
        pq.pop();
    }

    // builds a map from all the nodes to their representatives 
    std::unordered_map<Node*, Node*> clusters;

    for (Node* node : nodes) {
        Node* rep = uf.Find(node);

        clusters.insert(std::pair<Node*, Node*>(node, rep));
    }

    return clusters;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x148, %rsp            # imm = 0x148
movl %edx, %r15d
movq %rsi, %r14
movq %rdi, %rbx
cmpb $0x0, 0x100(%rsi)
jne 0xa9d9
movq (%r14), %rax
movq %r14, %rdi
callq *(%rax)
addq $0x10, %r14
movq %r14, %rdi
callq 0x4cc8
movl %eax, %ebp
subl %r15d, %ebp
jl 0xac39
leaq 0x60(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x4dc6
xorpd %xmm0, %xmm0
movapd %xmm0, (%rsp)
movq $0x0, 0x10(%rsp)
movq 0x10(%r15), %r13
testq %r13, %r13
je 0xaac8
movq %rsp, %r15
leaq 0x98(%rsp), %r12
movq 0x8(%r13), %rax
movq %rax, 0x98(%rsp)
movq 0x8(%rsp), %rsi
cmpq 0x10(%rsp), %rsi
je 0xaa55
movq %rax, (%rsi)
movq 0x8(%rsp), %rsi
addq $0x8, %rsi
movq %rsi, 0x8(%rsp)
jmp 0xaa65
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 0xaab7
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 0xaab7
cmpq %rcx, %r8
jl 0xaaa8
ucomisd %xmm0, %xmm1
jbe 0xaab7
movq %r8, (%rax,%rdx,8)
movq %rdi, %rdx
cmpq $0x1, %rsi
ja 0xaa83
xorl %edx, %edx
movq %rcx, (%rax,%rdx,8)
movq (%r13), %r13
testq %r13, %r13
jne 0xaa2a
leaq 0x20(%rsp), %rdi
movq %r14, %rsi
callq 0x4ccc
leaq 0x38(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0xb0c4
leaq 0x98(%rsp), %rdi
leaq 0x38(%rsp), %rsi
callq 0xdeac
movq 0x38(%rsp), %rdi
testq %rdi, %rdi
je 0xab0d
movq 0x48(%rsp), %rsi
subq %rdi, %rsi
callq 0x3290
testl %ebp, %ebp
jle 0xab73
leaq 0x98(%rsp), %r14
movq (%rsp), %rax
movq (%rax), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
movq %r14, %rdi
callq 0xe15c
movq (%rsp), %rdi
movq 0x8(%rsp), %rax
movq %rax, %rcx
subq %rdi, %rcx
cmpq $0x9, %rcx
jl 0xab66
leaq -0x8(%rax), %rdx
movq -0x8(%rax), %rcx
movq (%rdi), %rsi
movq %rsi, -0x8(%rax)
subq %rdi, %rdx
sarq $0x3, %rdx
xorl %esi, %esi
callq 0xafeb
movq 0x8(%rsp), %rax
addq $-0x8, %rax
movq %rax, 0x8(%rsp)
decl %ebp
jne 0xab19
leaq 0x30(%rbx), %rax
movq %rax, (%rbx)
movq $0x1, 0x8(%rbx)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x10(%rbx)
movl $0x3f800000, 0x20(%rbx) # imm = 0x3F800000
movupd %xmm0, 0x28(%rbx)
movq 0x20(%rsp), %r13
movq 0x28(%rsp), %rbp
cmpq %rbp, %r13
je 0xabe0
leaq 0x98(%rsp), %r14
leaq 0x50(%rsp), %r15
movq (%r13), %r12
movq %r14, %rdi
movq %r12, %rsi
callq 0xe0e0
movq %r12, 0x50(%rsp)
movq %rax, 0x58(%rsp)
movq %rbx, %rdi
movq %r15, %rsi
callq 0xb280
addq $0x8, %r13
cmpq %rbp, %r13
jne 0xabb3
leaq 0x98(%rsp), %rdi
callq 0xdfce
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0xac04
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x3290
movq (%rsp), %rdi
testq %rdi, %rdi
je 0xac1a
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x3290
leaq 0x60(%rsp), %rdi
callq 0x529c
movq %rbx, %rax
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x6a80(%rip), %rdi      # 0x116c0
leaq 0x6aaa(%rip), %rsi      # 0x116f1
leaq 0x6ab9(%rip), %rcx      # 0x11707
movl $0x5e, %edx
callq 0x3170
movq %rax, %r14
movq 0x38(%rsp), %rdi
testq %rdi, %rdi
je 0xac9f
movq 0x48(%rsp), %rsi
subq %rdi, %rsi
callq 0x3290
jmp 0xac9f
movq %rax, %r14
jmp 0xac9f
jmp 0xac7b
movq %rax, %r14
jmp 0xacb6
jmp 0xac82
movq %rax, %r14
movq %rbx, %rdi
callq 0x90a2
jmp 0xac92
movq %rax, %r14
leaq 0x98(%rsp), %rdi
callq 0xdfce
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0xacb6
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x3290
movq (%rsp), %rdi
testq %rdi, %rdi
je 0xaccc
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x3290
leaq 0x60(%rsp), %rdi
callq 0x529c
movq %r14, %rdi
callq 0x34a0