// File: /workspace/github_cpp_makefile_star2_O1/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 0xa819
movq (%r14), %rax
movq %r14, %rdi
callq *(%rax)
addq $0x10, %r14
movq %r14, %rdi
callq 0x4d02
movl %eax, %ebp
subl %r15d, %ebp
jl 0xaa72
leaq 0x60(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x4e00
xorpd %xmm0, %xmm0
movapd %xmm0, (%rsp)
movq $0x0, 0x10(%rsp)
movq 0x10(%r15), %r13
testq %r13, %r13
je 0xa909
movq %rsp, %r15
leaq 0x98(%rsp), %r12
movq 0x8(%r13), %rax
movq %rax, 0x98(%rsp)
movq 0x8(%rsp), %rsi
cmpq 0x10(%rsp), %rsi
je 0xa88d
movq %rax, (%rsi)
addq $0x8, 0x8(%rsp)
jmp 0xa898
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 0xa8f5
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 0xa8f5
cmpq %rcx, %rdi
jl 0xa8e6
ucomisd %xmm0, %xmm1
jbe 0xa8f5
movq %rdi, (%rax,%rdx,8)
cmpq $0x2, %rdx
movq %rsi, %rdx
jg 0xa8bb
jmp 0xa8f8
movq %rdx, %rsi
movq %rcx, (%rax,%rsi,8)
movq (%r13), %r13
testq %r13, %r13
jne 0xa86a
leaq 0x20(%rsp), %rdi
movq %r14, %rsi
callq 0x4d06
leaq 0x38(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0xaf1a
leaq 0x98(%rsp), %rdi
leaq 0x38(%rsp), %rsi
callq 0xdb40
movq 0x38(%rsp), %rdi
testq %rdi, %rdi
je 0xa94e
movq 0x48(%rsp), %rsi
subq %rdi, %rsi
callq 0x3290
testl %ebp, %ebp
jle 0xa9ac
leaq 0x98(%rsp), %r14
movq (%rsp), %rax
movq (%rax), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
movq %r14, %rdi
callq 0xde5a
movq (%rsp), %rdi
movq 0x8(%rsp), %rax
movq %rax, %rcx
subq %rdi, %rcx
cmpq $0x9, %rcx
jl 0xa9a2
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 0xae39
addq $-0x8, 0x8(%rsp)
decl %ebp
jne 0xa95a
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 0xaa19
leaq 0x98(%rsp), %r14
leaq 0x50(%rsp), %r15
movq (%r13), %r12
movq %r14, %rdi
movq %r12, %rsi
callq 0xddea
movq %r12, 0x50(%rsp)
movq %rax, 0x58(%rsp)
movq %rbx, %rdi
movq %r15, %rsi
callq 0xb0d6
addq $0x8, %r13
cmpq %rbp, %r13
jne 0xa9ec
leaq 0x98(%rsp), %rdi
callq 0xdc62
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0xaa3d
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x3290
movq (%rsp), %rdi
testq %rdi, %rdi
je 0xaa53
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x3290
leaq 0x60(%rsp), %rdi
callq 0x52b6
movq %rbx, %rax
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x6c07(%rip), %rdi      # 0x11680
leaq 0x6c31(%rip), %rsi      # 0x116b1
leaq 0x6c40(%rip), %rcx      # 0x116c7
movl $0x5e, %edx
callq 0x3170
movq %rax, %r14
movq 0x38(%rsp), %rdi
testq %rdi, %rdi
je 0xaad8
movq 0x48(%rsp), %rsi
subq %rdi, %rsi
callq 0x3290
jmp 0xaad8
movq %rax, %r14
jmp 0xaad8
jmp 0xaab4
movq %rax, %r14
jmp 0xaaef
jmp 0xaabb
movq %rax, %r14
movq %rbx, %rdi
callq 0x91c8
jmp 0xaacb
movq %rax, %r14
leaq 0x98(%rsp), %rdi
callq 0xdc62
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0xaaef
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x3290
movq (%rsp), %rdi
testq %rdi, %rdi
je 0xab05
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x3290
leaq 0x60(%rsp), %rdi
callq 0x52b6
movq %r14, %rdi
callq 0x34a0
nop