// File: /workspace/github_cpp_makefile_star2_O2/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 $0x158, %rsp            # imm = 0x158
movl %edx, %r15d
movq %rsi, %r14
movq %rdi, %rbx
cmpb $0x0, 0x100(%rsi)
jne 0x947f
movq (%r14), %rax
movq %r14, %rdi
callq *(%rax)
addq $0x10, %r14
movq %r14, %rdi
callq 0x465a
movl %eax, %ebp
subl %r15d, %ebp
jl 0x965b
leaq 0x68(%rsp), %rdi
movq %r14, %rsi
callq 0x46e4
xorps %xmm0, %xmm0
movq %rsp, %r15
movaps %xmm0, (%r15)
andq $0x0, 0x10(%r15)
leaq 0xa0(%rsp), %r12
movaps %xmm0, (%r12)
andq $0x0, 0x10(%r12)
movq %r12, %rdi
callq 0x9aba
leaq 0x78(%rsp), %r13
movq (%r13), %r13
testq %r13, %r13
je 0x9512
movq 0x8(%r13), %rax
movq %rax, 0xa0(%rsp)
movq %r15, %rdi
movq %r12, %rsi
callq 0x9aee
movq (%rsp), %rdi
movq 0x8(%rsp), %rsi
movq -0x8(%rsi), %rcx
subq %rdi, %rsi
sarq $0x3, %rsi
decq %rsi
xorl %edx, %edx
callq 0x9a29
jmp 0x94d2
leaq 0x28(%rsp), %rdi
movq %r14, %rsi
callq 0x465e
leaq 0x50(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0x9a6c
leaq 0xa0(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0xc084
leaq 0x50(%rsp), %rdi
callq 0x5d78
xorl %eax, %eax
testl %ebp, %ebp
cmovlel %eax, %ebp
leaq 0xa0(%rsp), %r14
subl $0x1, %ebp
jb 0x95b6
movq (%rsp), %rax
movq (%rax), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
movq %r14, %rdi
callq 0xc2c4
movq (%rsp), %rdi
movq 0x8(%rsp), %rax
movq %rax, %rcx
subq %rdi, %rcx
cmpq $0x9, %rcx
jl 0x95ab
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 0x999c
movq 0x8(%rsp), %rax
addq $-0x8, %rax
movq %rax, 0x8(%rsp)
jmp 0x9559
leaq 0x30(%rbx), %rax
movq %rax, (%rbx)
movq $0x1, 0x8(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rbx)
movl $0x3f800000, 0x20(%rbx) # imm = 0x3F800000
movups %xmm0, 0x28(%rbx)
movq 0x28(%rsp), %r13
movq 0x30(%rsp), %rbp
leaq 0xa0(%rsp), %r14
leaq 0x40(%rsp), %r15
cmpq %rbp, %r13
je 0x961d
movq (%r13), %r12
movq %r14, %rdi
movq %r12, %rsi
callq 0xc24a
movq %r12, 0x40(%rsp)
movq %rax, 0x48(%rsp)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x9c76
addq $0x8, %r13
jmp 0x95ee
leaq 0xa0(%rsp), %rdi
callq 0xc19a
leaq 0x28(%rsp), %rdi
callq 0x5d78
movq %rsp, %rdi
callq 0x9aba
leaq 0x68(%rsp), %rdi
callq 0x4cfe
movq %rbx, %rax
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x602e(%rip), %rdi      # 0xf690
leaq 0x6058(%rip), %rsi      # 0xf6c1
leaq 0x6067(%rip), %rcx      # 0xf6d7
pushq $0x5e
popq %rdx
callq 0x31b0
movq %rax, %r14
leaq 0x50(%rsp), %rdi
callq 0x5d78
jmp 0x96ad
movq %rax, %r14
jmp 0x96ad
jmp 0x96b9
jmp 0x9690
movq %rax, %r14
movq %rbx, %rdi
callq 0x7e78
jmp 0x96a0
movq %rax, %r14
leaq 0xa0(%rsp), %rdi
callq 0xc19a
leaq 0x28(%rsp), %rdi
callq 0x5d78
jmp 0x96bc
movq %rax, %r14
movq %rsp, %rdi
callq 0x9aba
leaq 0x68(%rsp), %rdi
callq 0x4cfe
movq %r14, %rdi
callq 0x34b0