// File: /workspace/github_cpp_makefile_star2_O1/sebastienmeyer2[P]mst-clustering/src/mst_prim_kumar.cpp
// Function: PrimKumarAlgorithm::compute_mst()
void PrimKumarAlgorithm::compute_mst() 
{
    /*!
     * @brief Computes the Minimum Spanning Tree of the initial graph using 
     * Prim-Kumar's algorithm. More specifically, initialize a priority queue to 
     * sort the edges by their weight. At each iteration, it adds the edge with 
     * minimal weight to the MST and checks for new edges to discover. 
     * 
     */


    int n_slaves = this->world_size;
    int n_nodes = this->initial_graph->get_number_of_nodes();

    int min_nodes_per_proc = n_nodes/(n_slaves);
    int split_nodes = n_nodes % n_slaves;

    int nodes_interval[2];

    nodes_interval[0] = this->rank*min_nodes_per_proc + (this->rank < split_nodes ? this->rank : split_nodes);  
    nodes_interval[1] = nodes_interval[0] + min_nodes_per_proc + (this->rank < split_nodes);

    int node_id_min = nodes_interval[0];
    int node_id_max = nodes_interval[1]; 

    struct {double d; int edge_id;} iter_res;
    struct {double d; int edge_id;} reduction_res;


    this->mst_weight = 0.0;

    std::chrono::steady_clock::time_point begin;
    if(this->rank == 0){
        std::cout << "Computing the MST using Prim-Kumar's algorithm...";
        begin = std::chrono::steady_clock::now();
    }

    int n = this->initial_graph->get_number_of_nodes();
    //std::vector<Node*> nodes = this->initial_graph->get_nodes();

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

            if (w1 < w2) {
                return true;
            } else if (w1 > w2) {
                return false;
            } 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;
            }
        };

    // initialization of the priority queue (ie set), best edge per vertex and beginning vertex
    std::unordered_map<Node*, Edge*> min_edge;
    std::set<Edge*, decltype(cmp)> pq(cmp);


    Node* current_node = this->initial_graph->get_node(0);




    this->mst_graph.add_node(current_node); // just to have the source node in the MST

    std::unordered_set<Edge*> connected_edges = this->initial_graph->connected_edges(current_node);

    for (Edge* e : connected_edges) {

        Node* other_node = e->other_node(current_node);
        if(IS_NODE_ALLOCATED_TO_ME(other_node->get_id())){
            if (!this->mst_graph.has_node(other_node)) {
                if (min_edge.find(other_node) != min_edge.end()) 
                {
                    if (*e < *min_edge.at(other_node)) {
                        pq.erase(min_edge.at(other_node));
                        min_edge.erase(other_node);

                        min_edge.insert(std::pair<Node*, Edge*>(other_node, e));
                        pq.insert(e);
                    }
                }
                else 
                {
                    min_edge.insert(std::pair<Node*, Edge*>(other_node, e));
                    pq.insert(e);
                }
            }
        } 
    }

    //std::printf("Proc %d, %d remaning items in queue\n", this->rank, pq.size());

    // filling the MST with new nodes until it forms a tree 
    while (this->mst_graph.get_number_of_nodes() < n) {

        Edge* new_edge;

        if(!pq.empty()){
            // adding the new edge and node to the MST and the visited nodes
            Edge* new_edge = *pq.begin();

            // We shall now send this to the root processor, so that it decides which edge is the best
            iter_res.edge_id = new_edge->id;
            iter_res.d = new_edge->weight;            
        } else {
            iter_res.d = std::numeric_limits<double>::infinity();
        }


        //std::printf("Proc %d, %d remaning items in queue\n", this->rank, pq.size());
        MPI_Barrier(MPI_COMM_WORLD);
        MPI_Allreduce(&iter_res, &reduction_res, 1, MPI_DOUBLE_INT, MPI_MINLOC, MPI_COMM_WORLD);
        MPI_Barrier(MPI_COMM_WORLD);


        // This is the REAL new edge
        new_edge = this->initial_graph->get_edge(reduction_res.edge_id);

        if(reduction_res.d == std::numeric_limits<double>::infinity()){
            printf("ARRRHS\n");
        } else {
            //printf("Elected edge %d\n", reduction_res.edge_id);
        }

        Node* p1 = new_edge->p1;
        Node* p2 = new_edge->p2;
        Node* new_node = (this->mst_graph.has_node(p1)) ? p2 : p1;

        this->mst_graph.add_edge(new_edge);
        this->mst_weight += new_edge->weight;



        this->mst_graph.add_node(new_node);
        
        pq.erase(new_edge);

        if(IS_NODE_ALLOCATED_TO_ME(p1->get_id()) || IS_NODE_ALLOCATED_TO_ME(p2->get_id())){
            // we won't need the best edge for this node anymore
            min_edge.erase(new_node);

        }


        // updating the external nodes best edges
        connected_edges = this->initial_graph->connected_edges(new_node);
            for (Edge* e : connected_edges) {
                Node* other_node = e->other_node(new_node);
                if(IS_NODE_ALLOCATED_TO_ME(other_node->get_id())){
                    if (!this->mst_graph.has_node(other_node)) {
                        if (min_edge.find(other_node) != min_edge.end()) 
                        {
                            if (*e < *min_edge.at(other_node)) {
                                pq.erase(min_edge.at(other_node));
                                min_edge.erase(other_node);

                                min_edge.insert(std::pair<Node*, Edge*>(other_node, e));
                                pq.insert(e);
                            }
                        }
                        else 
                        {
                            min_edge.insert(std::pair<Node*, Edge*>(other_node, e));
                            pq.insert(e);
                        }
                    }
                }
            }
    }
    
    MPI_Barrier(MPI_COMM_WORLD);
    if(this->rank == 0){
        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 $0x118, %rsp            # imm = 0x118
movq %rdi, %r12
movl 0x104(%rdi), %ebx
movq 0x8(%rdi), %rdi
callq 0x4d02
cltd
idivl %ebx
movl %edx, %ebx
movl 0x108(%r12), %ebp
xorl %r14d, %r14d
cmpl %edx, %ebp
cmovll %ebp, %ebx
setl %cl
movq $0x0, 0xf8(%r12)
testl %ebp, %ebp
movl %eax, 0x40(%rsp)
movb %cl, 0xf(%rsp)
je 0xd104
movq $0x0, 0x48(%rsp)
jmp 0xd126
movq 0x9ead(%rip), %rdi      # 0x16fb8
leaq 0x471e(%rip), %rsi      # 0x11830
movl $0x31, %edx
callq 0x32d0
callq 0x3380
movq %rax, 0x48(%rsp)
movq 0x8(%r12), %rdi
callq 0x4d02
movl %eax, 0x44(%rsp)
leaq 0x80(%rsp), %rax
movq %rax, -0x30(%rax)
movq $0x1, -0x28(%rax)
xorpd %xmm0, %xmm0
movupd %xmm0, -0x20(%rax)
movl $0x3f800000, -0x10(%rax) # imm = 0x3F800000
movupd %xmm0, -0x8(%rax)
leaq 0x98(%rsp), %rax
movl $0x0, (%rax)
xorl %ecx, %ecx
movq %rcx, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
movq %rcx, 0x20(%rax)
movq 0x8(%r12), %rdi
xorl %esi, %esi
callq 0x4f6a
movq %rax, %r13
leaq 0x10(%r12), %r15
movq %r15, %rdi
movq %rax, %rsi
callq 0x4ba4
movq %r12, 0x30(%rsp)
movq 0x8(%r12), %rdi
movq %r13, %rsi
callq 0x4cf0
xorl %ecx, %ecx
leaq 0xe0(%rsp), %rdi
movq %rcx, (%rdi)
movq 0x8(%rax), %rdx
movq %rdx, 0x8(%rdi)
movq %rcx, 0x10(%rdi)
movq 0x18(%rax), %rdx
movq %rdx, 0x18(%rdi)
movupd 0x20(%rax), %xmm0
movupd %xmm0, 0x20(%rdi)
movq %rcx, 0x30(%rdi)
leaq 0x20(%rsp), %rdx
movq %rdi, (%rdx)
movq %rax, %rsi
callq 0x543a
movl 0x40(%rsp), %eax
imull %eax, %ebp
addl %ebx, %ebp
movb 0xf(%rsp), %cl
movb %cl, %r14b
addl %eax, %r14d
addl %ebp, %r14d
movq 0xf0(%rsp), %r12
testq %r12, %r12
je 0xd671
movq 0x8(%r12), %rbx
movq %rbx, 0x38(%rsp)
movq %rbx, %rdi
movq %r13, %rsi
callq 0x39f8
movq %rax, 0x10(%rsp)
movq %rax, %rdi
callq 0xa34e
cmpl %ebp, %eax
jl 0xd369
movq 0x10(%rsp), %rdi
callq 0xa34e
cmpl %r14d, %eax
jge 0xd369
movq 0x10(%rsp), %rsi
movq %r15, %rdi
callq 0x4c86
testb %al, %al
jne 0xd369
movq 0x10(%rsp), %rcx
movq 0x50(%rsp), %rdi
movq 0x58(%rsp), %r8
movq %rcx, %rax
xorl %edx, %edx
divq %r8
movq %rdx, %rsi
movq (%rdi,%rdx,8), %rdx
movl $0x0, %eax
testq %rdx, %rdx
je 0xd2c7
movq (%rdx), %r9
movq %rdx, %rax
cmpq 0x8(%r9), %rcx
je 0xd2c7
movq %r9, %r10
movq (%r9), %r9
testq %r9, %r9
je 0xd2c5
movq 0x8(%r9), %rdi
movq %rdi, %rax
xorl %edx, %edx
divq %r8
movl $0x0, %eax
cmpq %rsi, %rdx
jne 0xd2c7
movq %r10, %rax
cmpq %rdi, %rcx
jne 0xd29a
jmp 0xd2c7
xorl %eax, %eax
testq %rax, %rax
je 0xd33e
cmpq $0x0, (%rax)
je 0xd33e
leaq 0x50(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0xc23e
movq (%rax), %rsi
movq %rbx, %rdi
callq 0x3a10
testb %al, %al
je 0xd369
leaq 0x50(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0xc23e
leaq 0x90(%rsp), %rdi
movq %rax, %rsi
callq 0xd858
leaq 0x50(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0xc280
movq 0x10(%rsp), %rax
movq %rax, 0x20(%rsp)
movq %rbx, 0x28(%rsp)
leaq 0x50(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0xc366
jmp 0xd357
movq %rcx, 0x20(%rsp)
movq %rbx, 0x28(%rsp)
leaq 0x50(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0xc366
leaq 0x90(%rsp), %rdi
leaq 0x38(%rsp), %rsi
callq 0xd9ea
movq (%r12), %r12
jmp 0xd20b
cmpq $0x0, 0xb8(%rsp)
je 0xd39a
movq 0xa8(%rsp), %rax
movq 0x20(%rax), %rax
movl 0x18(%rax), %ecx
movl %ecx, 0xd8(%rsp)
movsd 0x10(%rax), %xmm0
jmp 0xd3a2
movsd 0x4486(%rip), %xmm0     # 0x11828
movsd %xmm0, 0xd0(%rsp)
movq 0x9be6(%rip), %rdi      # 0x16f98
callq 0x31c0
movq 0x9c0a(%rip), %rcx      # 0x16fc8
movq 0x9beb(%rip), %r8       # 0x16fb0
movq 0x9bcc(%rip), %r9       # 0x16f98
leaq 0xd0(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
movl $0x1, %edx
callq 0x3340
movq 0x9bab(%rip), %rdi      # 0x16f98
callq 0x31c0
movq 0x8(%r12), %rdi
movl 0xc8(%rsp), %esi
callq 0x4f84
movq %rax, %r12
movq %rax, 0x38(%rsp)
movsd 0xc0(%rsp), %xmm0
ucomisd 0x440c(%rip), %xmm0     # 0x11828
jb 0xd42a
leaq 0x443d(%rip), %rdi      # 0x11862
callq 0x3410
movq (%r12), %r13
movq 0x8(%r12), %rbx
movq %r15, %rdi
movq %r13, %rsi
callq 0x4c86
testb %al, %al
movq %r13, %rax
cmovneq %rbx, %rax
movq %rax, 0x10(%rsp)
movq %r15, %rdi
movq %r12, %rsi
callq 0x47c2
movsd 0x10(%r12), %xmm0
movq 0x30(%rsp), %rax
addsd 0xf8(%rax), %xmm0
movsd %xmm0, 0xf8(%rax)
movq 0x10(%rsp), %rsi
movq %r15, %rdi
callq 0x4ba4
leaq 0x90(%rsp), %rdi
leaq 0x38(%rsp), %rsi
callq 0xd858
movq %r13, %rdi
callq 0xa34e
cmpl %ebp, %eax
jl 0xd4ab
movq %r13, %rdi
callq 0xa34e
cmpl %r14d, %eax
jl 0xd4c4
movq %rbx, %rdi
callq 0xa34e
cmpl %ebp, %eax
jl 0xd4d3
movq %rbx, %rdi
callq 0xa34e
cmpl %r14d, %eax
jge 0xd4d3
leaq 0x50(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0xc280
movq 0x30(%rsp), %rax
movq 0x8(%rax), %rdi
movq 0x10(%rsp), %rsi
callq 0x4cf0
leaq 0xe0(%rsp), %rcx
cmpq %rcx, %rax
je 0xd503
leaq 0xe0(%rsp), %rdi
movq %rax, %rsi
callq 0xbe84
movq 0xf0(%rsp), %r12
testq %r12, %r12
je 0xd671
movq 0x8(%r12), %rbx
movq %rbx, 0x88(%rsp)
movq 0x10(%rsp), %rsi
movq %rbx, %rdi
callq 0x39f8
movq %rax, 0x18(%rsp)
movq %rax, %rdi
callq 0xa34e
cmpl %ebp, %eax
jl 0xd668
movq 0x18(%rsp), %rdi
callq 0xa34e
cmpl %r14d, %eax
jge 0xd668
movq 0x18(%rsp), %rsi
movq %r15, %rdi
callq 0x4c86
testb %al, %al
jne 0xd668
movq 0x18(%rsp), %rcx
movq 0x50(%rsp), %rdi
movq 0x58(%rsp), %r8
xorl %r9d, %r9d
movq %rcx, %rax
xorl %edx, %edx
divq %r8
movq (%rdi,%rdx,8), %rax
testq %rax, %rax
je 0xd5c9
movq %rdx, %rsi
movq (%rax), %r10
movq %rax, %r9
cmpq 0x8(%r10), %rcx
je 0xd5c9
movq %r10, %r11
movq (%r10), %r10
testq %r10, %r10
je 0xd5c6
movq 0x8(%r10), %rdi
xorl %r9d, %r9d
movq %rdi, %rax
xorl %edx, %edx
divq %r8
cmpq %rsi, %rdx
jne 0xd5c9
movq %r11, %r9
cmpq %rdi, %rcx
jne 0xd59d
jmp 0xd5c9
xorl %r9d, %r9d
testq %r9, %r9
je 0xd63d
cmpq $0x0, (%r9)
je 0xd63d
leaq 0x50(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0xc23e
movq (%rax), %rsi
movq %rbx, %rdi
callq 0x3a10
testb %al, %al
je 0xd668
leaq 0x50(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0xc23e
leaq 0x90(%rsp), %rdi
movq %rax, %rsi
callq 0xd858
leaq 0x50(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0xc280
movq 0x18(%rsp), %rax
leaq 0x20(%rsp), %rsi
movq %rax, (%rsi)
movq %rbx, 0x8(%rsi)
leaq 0x50(%rsp), %rdi
callq 0xc366
jmp 0xd653
leaq 0x20(%rsp), %rsi
movq %rcx, (%rsi)
movq %rbx, 0x8(%rsi)
leaq 0x50(%rsp), %rdi
callq 0xc366
leaq 0x90(%rsp), %rdi
leaq 0x88(%rsp), %rsi
callq 0xd9ea
movq (%r12), %r12
jmp 0xd50b
movq %r15, %rdi
callq 0x4d02
cmpl 0x44(%rsp), %eax
movq 0x30(%rsp), %r12
jl 0xd372
movq 0x9909(%rip), %rdi      # 0x16f98
callq 0x31c0
cmpl $0x0, 0x108(%r12)
jne 0xd7d6
callq 0x3380
movq %rax, %rbx
movq 0x9906(%rip), %r15      # 0x16fb8
leaq 0x409a(%rip), %rsi      # 0x11753
movl $0x4, %edx
movq %r15, %rdi
callq 0x32d0
movq (%r15), %rax
addq -0x18(%rax), %r15
movq %r15, %rdi
movl $0xa, %esi
callq 0x3270
movsbl %al, %esi
movq 0x98d4(%rip), %rdi      # 0x16fb8
callq 0x3040
movq %rax, %rdi
callq 0x31a0
movq %r12, %rdi
callq 0xa7e6
movq 0x98b8(%rip), %rdi      # 0x16fb8
leaq 0x4051(%rip), %rsi      # 0x11758
movl $0x19, %edx
callq 0x32d0
movsd 0xf8(%r12), %xmm0
movq 0x9896(%rip), %rdi      # 0x16fb8
callq 0x3420
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x3270
movsbl %al, %esi
movq %r14, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31a0
movq 0x9860(%rip), %rdi      # 0x16fb8
leaq 0x4013(%rip), %rsi      # 0x11772
movl $0x1d, %edx
callq 0x32d0
subq 0x48(%rsp), %rbx
movabsq $0x20c49ba5e353f7cf, %rcx # imm = 0x20C49BA5E353F7CF
movq %rbx, %rax
imulq %rcx
movq %rdx, %rsi
shrq $0x3f, %rsi
sarq $0x7, %rdx
addq %rdx, %rsi
movq 0x9825(%rip), %rdi      # 0x16fb8
callq 0x3260
movq %rax, %rbx
leaq 0x3fee(%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 0xe0(%rsp), %rdi
callq 0x52b6
movq 0xa0(%rsp), %rdi
callq 0xdb08
leaq 0x50(%rsp), %rdi
callq 0xc186
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xd829
jmp 0xd829
jmp 0xd829
jmp 0xd829
jmp 0xd816
movq %rax, %rbx
jmp 0xd839
jmp 0xd829
jmp 0xd829
jmp 0xd829
jmp 0xd829
jmp 0xd829
jmp 0xd829
jmp 0xd829
movq %rax, %rbx
leaq 0xe0(%rsp), %rdi
callq 0x52b6
movq 0xa0(%rsp), %rdi
callq 0xdb08
leaq 0x50(%rsp), %rdi
callq 0xc186
movq %rbx, %rdi
callq 0x34a0