// File: /workspace/github_cpp_makefile_star2_O3/sebastienmeyer2[P]mst-clustering/src/mst_prim.cpp
// Function: PrimAlgorithm::compute_mst()
void PrimAlgorithm::compute_mst() 
{
    /*!
     * @brief Computes the Minimum Spanning Tree of the initial graph using 
     * Prim'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. 
     * 
     */

    this->mst_weight = 0.0;
    std::cout << "\nComputing the MST using Prim'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();

    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);

    std::unordered_set<Node*> visited_nodes;

    Node* current_node = this->initial_graph->get_any_node();
    visited_nodes.insert(current_node);
    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 (visited_nodes.find(other_node) == visited_nodes.end()) {
            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);
            }
        }
    }

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

        // testing whether if the set is empty
        if (pq.empty()) {
            throw std::invalid_argument("No MST can be built !");
        }

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

        Node* p1 = new_edge->p1;
        Node* p2 = new_edge->p2;
        Node* new_node = (visited_nodes.find(p1) != visited_nodes.end()) ? p2 : p1;

        visited_nodes.insert(new_node);

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

        // we won't need the best edge for this node anymore
        min_edge.erase(new_node);
        pq.erase(new_edge);

        // 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 (visited_nodes.find(other_node) == visited_nodes.end()) {
                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::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 $0x128, %rsp            # imm = 0x128
movq %rdi, %r14
xorl %ebx, %ebx
movq %rbx, 0xf8(%rdi)
movq 0xba66(%rip), %rdi      # 0x16fb8
leaq 0x61f7(%rip), %rsi      # 0x11750
movl $0x2c, %edx
callq 0x32d0
callq 0x3380
movq %rax, 0x68(%rsp)
movq 0x8(%r14), %rdi
callq 0x4cc8
movl %eax, %ebp
movq 0x8(%r14), %rsi
leaq 0xd8(%rsp), %rdi
callq 0x4ccc
leaq 0x60(%rsp), %rax
movq %rax, -0x30(%rax)
movl $0x1, %ecx
movq %rcx, -0x28(%rax)
xorpd %xmm0, %xmm0
movupd %xmm0, -0x20(%rax)
movl $0x3f800000, %edx       # imm = 0x3F800000
movl %edx, -0x10(%rax)
movupd %xmm0, -0x8(%rax)
leaq 0xb0(%rsp), %rax
movl $0x0, (%rax)
movq %rbx, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
movq %rbx, 0x20(%rax)
leaq 0x120(%rsp), %rax
movq %rax, -0x30(%rax)
movq %rcx, -0x28(%rax)
movupd %xmm0, -0x20(%rax)
movl %edx, -0x10(%rax)
movupd %xmm0, -0x8(%rax)
movq 0x8(%r14), %rdi
callq 0x4e10
leaq 0x28(%rsp), %rsi
movq %rax, (%rsi)
leaq 0xf0(%rsp), %r15
leaq 0x70(%rsp), %rdx
movq %r15, (%rdx)
movq %r15, %rdi
callq 0x5ccc
leaq 0x10(%r14), %r13
movq 0x28(%rsp), %rsi
movq %r13, %rdi
callq 0x4ba0
movq 0x8(%r14), %rdi
movq 0x28(%rsp), %rsi
callq 0x4ca2
xorl %ecx, %ecx
movq %rcx, 0x70(%rsp)
movq 0x8(%rax), %rdx
movq %rdx, 0x78(%rsp)
movq %rcx, 0x80(%rsp)
movq 0x18(%rax), %rdx
movq %rdx, 0x88(%rsp)
movupd 0x20(%rax), %xmm0
movupd %xmm0, 0x90(%rsp)
movq %rcx, 0xa0(%rsp)
leaq 0x10(%rsp), %rdx
leaq 0x70(%rsp), %rdi
movq %rdi, (%rdx)
movq %rax, %rsi
callq 0x541c
movq 0x80(%rsp), %r12
testq %r12, %r12
je 0xbb3a
movq 0x8(%r12), %rbx
movq %rbx, 0x8(%rsp)
movq 0x28(%rsp), %rsi
movq %rbx, %rdi
callq 0x39f8
movq %rax, %rcx
movq %rax, (%rsp)
movq 0xf0(%rsp), %rdi
movq 0xf8(%rsp), %r8
xorl %edx, %edx
divq %r8
movq (%rdi,%rdx,8), %rax
testq %rax, %rax
je 0xb703
movq %rdx, %rsi
movq (%rax), %r9
cmpq 0x8(%r9), %rcx
je 0xb856
movq (%r9), %r9
testq %r9, %r9
je 0xb703
movq 0x8(%r9), %rdi
movq %rdi, %rax
xorl %edx, %edx
divq %r8
cmpq %rsi, %rdx
jne 0xb703
cmpq %rdi, %rcx
jne 0xb6e0
jmp 0xb856
movq 0x30(%rsp), %rdi
movq 0x38(%rsp), %r8
movq %rcx, %rax
xorl %edx, %edx
divq %r8
movq (%rdi,%rdx,8), %rax
testq %rax, %rax
je 0xb82b
movq %rdx, %rsi
movq (%rax), %r9
movq 0x8(%r9), %r10
movq %r9, %r11
cmpq %r10, %rcx
je 0xb785
movq (%r11), %r11
testq %r11, %r11
je 0xb82b
movq 0x8(%r11), %rdi
movq %rdi, %rax
xorl %edx, %edx
divq %r8
cmpq %rsi, %rdx
jne 0xb82b
cmpq %rdi, %rcx
jne 0xb734
cmpq %r10, %rcx
je 0xb785
movq (%r9), %r9
testq %r9, %r9
je 0xbce5
movq 0x8(%r9), %rdi
movq %rdi, %rax
xorl %edx, %edx
divq %r8
cmpq %rsi, %rdx
jne 0xbce5
cmpq %rdi, %rcx
jne 0xb75f
movq 0x10(%r9), %rsi
movq %rbx, %rdi
callq 0x3a10
testb %al, %al
je 0xb856
movq (%rsp), %rcx
movq 0x30(%rsp), %rdi
movq 0x38(%rsp), %r8
movq %rcx, %rax
xorl %edx, %edx
divq %r8
movq (%rdi,%rdx,8), %rax
testq %rax, %rax
je 0xbce5
movq %rdx, %rsi
movq (%rax), %r9
cmpq 0x8(%r9), %rcx
je 0xb7ee
movq (%r9), %r9
testq %r9, %r9
je 0xbce5
movq 0x8(%r9), %rdi
movq %rdi, %rax
xorl %edx, %edx
divq %r8
cmpq %rsi, %rdx
jne 0xbce5
cmpq %rdi, %rcx
jne 0xb7c8
movq 0x10(%r9), %rsi
leaq 0xa8(%rsp), %rdi
callq 0xbda6
leaq 0x30(%rsp), %rdi
movq %rsp, %rsi
callq 0xc400
movq (%rsp), %rax
movq %rax, 0x10(%rsp)
movq %rbx, 0x18(%rsp)
leaq 0x30(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0xc4f0
jmp 0xb844
movq %rcx, 0x10(%rsp)
movq %rbx, 0x18(%rsp)
leaq 0x30(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0xc4f0
leaq 0xa8(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0xbf3a
movq (%r12), %r12
jmp 0xb68b
cmpq $0x0, 0xd0(%rsp)
je 0xbcf1
movq 0xc0(%rsp), %rax
movq 0x20(%rax), %rbx
movq (%rbx), %rcx
movq 0x8(%rbx), %r8
movq 0xf0(%rsp), %rdi
movq 0xf8(%rsp), %r9
xorl %r10d, %r10d
movq %rcx, %rax
xorl %edx, %edx
divq %r9
movq (%rdi,%rdx,8), %rax
testq %rax, %rax
je 0xb8dd
movq %rdx, %rsi
movq (%rax), %r11
movq %r11, %r10
cmpq 0x8(%r11), %rcx
je 0xb8dd
movq (%r11), %r11
testq %r11, %r11
je 0xb8da
movq 0x8(%r11), %rdi
xorl %r10d, %r10d
movq %rdi, %rax
xorl %edx, %edx
divq %r9
cmpq %rsi, %rdx
jne 0xb8dd
movq %r11, %r10
cmpq %rdi, %rcx
jne 0xb8b4
jmp 0xb8dd
xorl %r10d, %r10d
testq %r10, %r10
cmoveq %rcx, %r8
leaq 0x8(%rsp), %rsi
movq %r8, (%rsi)
leaq 0x10(%rsp), %rdx
movq %r15, (%rdx)
movq %r15, %rdi
callq 0x5ccc
movq %r13, %rdi
movq %rbx, %rsi
callq 0x477c
movsd 0x10(%rbx), %xmm0
addsd 0xf8(%r14), %xmm0
movsd %xmm0, 0xf8(%r14)
leaq 0x30(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0xc400
leaq 0xa8(%rsp), %rdi
movq %rbx, %rsi
callq 0xbda6
movq 0x8(%r14), %rdi
movq 0x8(%rsp), %rsi
callq 0x4ca2
leaq 0x70(%rsp), %rcx
cmpq %rcx, %rax
je 0xb962
leaq 0x70(%rsp), %rdi
movq %rax, %rsi
callq 0xc070
movq 0x80(%rsp), %r12
testq %r12, %r12
je 0xbb3a
movq 0x8(%r12), %rbx
movq %rbx, (%rsp)
movq 0x8(%rsp), %rsi
movq %rbx, %rdi
callq 0x39f8
movq %rax, %rcx
movq %rax, 0x20(%rsp)
movq 0xf0(%rsp), %rdi
movq 0xf8(%rsp), %r8
xorl %edx, %edx
divq %r8
movq (%rdi,%rdx,8), %rax
testq %rax, %rax
je 0xb9e2
movq %rdx, %rsi
movq (%rax), %r9
cmpq 0x8(%r9), %rcx
je 0xbb31
movq (%r9), %r9
testq %r9, %r9
je 0xb9e2
movq 0x8(%r9), %rdi
movq %rdi, %rax
xorl %edx, %edx
divq %r8
cmpq %rsi, %rdx
jne 0xb9e2
cmpq %rdi, %rcx
jne 0xb9bf
jmp 0xbb31
movq 0x30(%rsp), %rdi
movq 0x38(%rsp), %r8
movq %rcx, %rax
xorl %edx, %edx
divq %r8
movq (%rdi,%rdx,8), %rax
testq %rax, %rax
je 0xbb0b
movq %rdx, %rsi
movq (%rax), %r9
movq 0x8(%r9), %r10
movq %r9, %r11
cmpq %r10, %rcx
je 0xba64
movq (%r11), %r11
testq %r11, %r11
je 0xbb0b
movq 0x8(%r11), %rdi
movq %rdi, %rax
xorl %edx, %edx
divq %r8
cmpq %rsi, %rdx
jne 0xbb0b
cmpq %rdi, %rcx
jne 0xba13
cmpq %r10, %rcx
je 0xba64
movq (%r9), %r9
testq %r9, %r9
je 0xbcd9
movq 0x8(%r9), %rdi
movq %rdi, %rax
xorl %edx, %edx
divq %r8
cmpq %rsi, %rdx
jne 0xbcd9
cmpq %rdi, %rcx
jne 0xba3e
movq 0x10(%r9), %rsi
movq %rbx, %rdi
callq 0x3a10
testb %al, %al
je 0xbb31
movq 0x20(%rsp), %rcx
movq 0x30(%rsp), %rdi
movq 0x38(%rsp), %r8
movq %rcx, %rax
xorl %edx, %edx
divq %r8
movq (%rdi,%rdx,8), %rax
testq %rax, %rax
je 0xbcd9
movq %rdx, %rsi
movq (%rax), %r9
cmpq 0x8(%r9), %rcx
je 0xbace
movq (%r9), %r9
testq %r9, %r9
je 0xbcd9
movq 0x8(%r9), %rdi
movq %rdi, %rax
xorl %edx, %edx
divq %r8
cmpq %rsi, %rdx
jne 0xbcd9
cmpq %rdi, %rcx
jne 0xbaa8
movq 0x10(%r9), %rsi
leaq 0xa8(%rsp), %rdi
callq 0xbda6
leaq 0x30(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0xc400
movq 0x20(%rsp), %rax
leaq 0x10(%rsp), %rsi
movq %rax, (%rsi)
movq %rbx, 0x8(%rsi)
leaq 0x30(%rsp), %rdi
callq 0xc4f0
jmp 0xbb21
leaq 0x10(%rsp), %rsi
movq %rcx, (%rsi)
movq %rbx, 0x8(%rsi)
leaq 0x30(%rsp), %rdi
callq 0xc4f0
leaq 0xa8(%rsp), %rdi
movq %rsp, %rsi
callq 0xbf3a
movq (%r12), %r12
jmp 0xb96a
movq %r13, %rdi
callq 0x4cc8
cmpl %ebp, %eax
jl 0xb85f
callq 0x3380
movq %rax, %rbx
movq 0xb45f(%rip), %r15      # 0x16fb8
leaq 0x5c33(%rip), %rsi      # 0x11793
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 0xb42d(%rip), %rdi      # 0x16fb8
callq 0x3040
movq %rax, %rdi
callq 0x31a0
movq %r14, %rdi
callq 0xa9a6
movq 0xb411(%rip), %rdi      # 0x16fb8
leaq 0x5bea(%rip), %rsi      # 0x11798
movl $0x19, %edx
callq 0x32d0
movsd 0xf8(%r14), %xmm0
movq 0xb3f0(%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 0xb3ba(%rip), %rdi      # 0x16fb8
leaq 0x5bad(%rip), %rsi      # 0x117b2
movl $0x1d, %edx
callq 0x32d0
subq 0x68(%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 0xb37f(%rip), %rdi      # 0x16fb8
callq 0x3260
movq %rax, %rbx
leaq 0x5b88(%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 0x70(%rsp), %rdi
callq 0x529c
leaq 0xf0(%rsp), %rdi
callq 0x50b2
movq 0xb8(%rsp), %rdi
callq 0xc03a
leaq 0x30(%rsp), %rdi
callq 0xc390
movq 0xd8(%rsp), %rdi
testq %rdi, %rdi
je 0xbcc7
movq 0xe8(%rsp), %rsi
subq %rdi, %rsi
callq 0x3290
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x54aa(%rip), %rdi      # 0x1118a
callq 0x33e0
leaq 0x549e(%rip), %rdi      # 0x1118a
callq 0x33e0
movl $0x10, %edi
callq 0x3100
movq %rax, %r14
leaq 0x5a78(%rip), %rsi      # 0x1177d
movq %rax, %rdi
callq 0x34d0
movq 0xb2cc(%rip), %rsi      # 0x16fe0
movq 0xb2b5(%rip), %rdx      # 0x16fd0
movq %r14, %rdi
callq 0x3480
jmp 0xbd27
jmp 0xbd4f
movq %rax, %rbx
jmp 0xbd5c
jmp 0xbd4f
jmp 0xbd4f
jmp 0xbd4f
jmp 0xbd4f
movq %rax, %rbx
movq %r14, %rdi
callq 0x31d0
jmp 0xbd52
jmp 0xbd4f
jmp 0xbd4f
jmp 0xbd4f
jmp 0xbd4f
jmp 0xbd4f
jmp 0xbd4f
jmp 0xbd4f
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x529c
leaq 0xf0(%rsp), %rdi
callq 0x50b2
movq 0xb8(%rsp), %rdi
callq 0xc03a
leaq 0x30(%rsp), %rdi
callq 0xc390
movq 0xd8(%rsp), %rdi
testq %rdi, %rdi
je 0xbd9d
movq 0xe8(%rsp), %rsi
subq %rdi, %rsi
callq 0x3290
movq %rbx, %rdi
callq 0x34a0
nop