// File: /workspace/github_cpp_makefile_star2_O1/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 $0x138, %rsp            # imm = 0x138
movq %rdi, %r14
xorl %ebx, %ebx
movq %rbx, 0xf8(%rdi)
movq 0xbc02(%rip), %rdi      # 0x16fb8
leaq 0x6353(%rip), %rsi      # 0x11710
movl $0x2c, %edx
callq 0x32d0
callq 0x3380
movq %rax, 0x70(%rsp)
movq 0x8(%r14), %rdi
callq 0x4d02
movl %eax, %ebp
movq 0x8(%r14), %rsi
leaq 0xe8(%rsp), %rdi
callq 0x4d06
leaq 0x68(%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 0xc0(%rsp), %rax
movl $0x0, (%rax)
movq %rbx, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
movq %rbx, 0x20(%rax)
leaq 0x130(%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 0x4e4a
leaq 0x30(%rsp), %rsi
movq %rax, (%rsi)
leaq 0x100(%rsp), %r15
leaq 0x80(%rsp), %rdx
movq %r15, (%rdx)
movq %r15, %rdi
callq 0x5d94
leaq 0x10(%r14), %r13
movq 0x30(%rsp), %rsi
movq %r13, %rdi
callq 0x4ba4
movq 0x8(%r14), %rdi
movq 0x30(%rsp), %rsi
callq 0x4cf0
xorl %ecx, %ecx
movq %rcx, 0x80(%rsp)
movq 0x8(%rax), %rdx
movq %rdx, 0x88(%rsp)
movq %rcx, 0x90(%rsp)
movq 0x18(%rax), %rdx
movq %rdx, 0x98(%rsp)
movupd 0x20(%rax), %xmm0
movupd %xmm0, 0xa0(%rsp)
movq %rcx, 0xb0(%rsp)
leaq 0x18(%rsp), %rdx
leaq 0x80(%rsp), %rdi
movq %rdi, (%rdx)
movq %rax, %rsi
callq 0x543a
movq 0x90(%rsp), %rbx
testq %rbx, %rbx
je 0xb948
movq 0x8(%rbx), %r12
movq %r12, 0x28(%rsp)
movq 0x30(%rsp), %rsi
movq %r12, %rdi
callq 0x39f8
movq %rax, %rcx
movq %rax, 0x8(%rsp)
movq 0x100(%rsp), %rdi
movq 0x108(%rsp), %r8
xorl %edx, %edx
divq %r8
movq %rdx, %rsi
movq (%rdi,%rdx,8), %rdx
movl $0x0, %eax
testq %rdx, %rdx
je 0xb581
movq (%rdx), %r9
movq %rdx, %rax
cmpq 0x8(%r9), %rcx
je 0xb581
movq %r9, %r10
movq (%r9), %r9
testq %r9, %r9
je 0xb57f
movq 0x8(%r9), %rdi
movq %rdi, %rax
xorl %edx, %edx
divq %r8
movl $0x0, %eax
cmpq %rsi, %rdx
jne 0xb581
movq %r10, %rax
cmpq %rdi, %rcx
jne 0xb554
jmp 0xb581
xorl %eax, %eax
testq %rax, %rax
je 0xb590
cmpq $0x0, (%rax)
jne 0xb68e
movq 0x38(%rsp), %rdi
movq 0x40(%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 0xb5ec
movq (%rdx), %r9
movq %rdx, %rax
cmpq 0x8(%r9), %rcx
je 0xb5ec
movq %r9, %r10
movq (%r9), %r9
testq %r9, %r9
je 0xb5ea
movq 0x8(%r9), %rdi
movq %rdi, %rax
xorl %edx, %edx
divq %r8
movl $0x0, %eax
cmpq %rsi, %rdx
jne 0xb5ec
movq %r10, %rax
cmpq %rdi, %rcx
jne 0xb5bf
jmp 0xb5ec
xorl %eax, %eax
testq %rax, %rax
je 0xb663
cmpq $0x0, (%rax)
je 0xb663
leaq 0x38(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0xc23e
movq (%rax), %rsi
movq %r12, %rdi
callq 0x3a10
testb %al, %al
je 0xb68e
leaq 0x38(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0xc23e
leaq 0xb8(%rsp), %rdi
movq %rax, %rsi
callq 0xbb9e
leaq 0x38(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0xc280
movq 0x8(%rsp), %rax
movq %rax, 0x18(%rsp)
movq %r12, 0x20(%rsp)
leaq 0x38(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0xc366
jmp 0xb67c
movq %rcx, 0x18(%rsp)
movq %r12, 0x20(%rsp)
leaq 0x38(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0xc366
leaq 0xb8(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0xbd30
movq (%rbx), %rbx
jmp 0xb4fb
cmpq $0x0, 0xe0(%rsp)
je 0xbaea
movq 0xd0(%rsp), %rax
movq 0x20(%rax), %rbx
movq %rbx, 0x28(%rsp)
movq (%rbx), %rcx
movq 0x8(%rbx), %r8
movq 0x100(%rsp), %rdi
movq 0x108(%rsp), %r9
xorl %r10d, %r10d
movq %rcx, %rax
xorl %edx, %edx
divq %r9
movq (%rdi,%rdx,8), %rax
testq %rax, %rax
je 0xb71c
movq %rdx, %rsi
movq (%rax), %r11
movq %rax, %r10
cmpq 0x8(%r11), %rcx
je 0xb71c
movq %r11, %r12
movq (%r11), %r11
testq %r11, %r11
je 0xb719
movq 0x8(%r11), %rdi
xorl %r10d, %r10d
movq %rdi, %rax
xorl %edx, %edx
divq %r9
cmpq %rsi, %rdx
jne 0xb71c
movq %r12, %r10
cmpq %rdi, %rcx
jne 0xb6f0
jmp 0xb71c
xorl %r10d, %r10d
testq %r10, %r10
je 0xb729
cmpq $0x0, (%r10)
cmovneq %r8, %rcx
leaq 0x8(%rsp), %rsi
movq %rcx, (%rsi)
leaq 0x18(%rsp), %rdx
movq %r15, (%rdx)
movq %r15, %rdi
callq 0x5d94
movq %r13, %rdi
movq %rbx, %rsi
callq 0x47c2
movsd 0x10(%rbx), %xmm0
addsd 0xf8(%r14), %xmm0
movsd %xmm0, 0xf8(%r14)
leaq 0x38(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0xc280
leaq 0xb8(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0xbb9e
movq 0x8(%r14), %rdi
movq 0x8(%rsp), %rsi
callq 0x4cf0
leaq 0x80(%rsp), %rcx
cmpq %rcx, %rax
je 0xb7af
leaq 0x80(%rsp), %rdi
movq %rax, %rsi
callq 0xbe84
movq 0x90(%rsp), %r12
testq %r12, %r12
je 0xb948
movq 0x8(%r12), %rbx
movq %rbx, 0x78(%rsp)
movq 0x8(%rsp), %rsi
movq %rbx, %rdi
callq 0x39f8
movq %rax, %rcx
movq %rax, 0x10(%rsp)
movq 0x100(%rsp), %rdi
movq 0x108(%rsp), %r8
xorl %r9d, %r9d
xorl %edx, %edx
divq %r8
movq (%rdi,%rdx,8), %rax
testq %rax, %rax
je 0xb83b
movq %rdx, %rsi
movq (%rax), %r10
movq %rax, %r9
cmpq 0x8(%r10), %rcx
je 0xb83b
movq %r10, %r11
movq (%r10), %r10
testq %r10, %r10
je 0xb838
movq 0x8(%r10), %rdi
xorl %r9d, %r9d
movq %rdi, %rax
xorl %edx, %edx
divq %r8
cmpq %rsi, %rdx
jne 0xb83b
movq %r11, %r9
cmpq %rdi, %rcx
jne 0xb80f
jmp 0xb83b
xorl %r9d, %r9d
testq %r9, %r9
je 0xb84a
cmpq $0x0, (%r9)
jne 0xb93f
movq 0x38(%rsp), %rdi
movq 0x40(%rsp), %r8
xorl %r9d, %r9d
movq %rcx, %rax
xorl %edx, %edx
divq %r8
movq (%rdi,%rdx,8), %rax
testq %rax, %rax
je 0xb8a3
movq %rdx, %rsi
movq (%rax), %r10
movq %rax, %r9
cmpq 0x8(%r10), %rcx
je 0xb8a3
movq %r10, %r11
movq (%r10), %r10
testq %r10, %r10
je 0xb8a0
movq 0x8(%r10), %rdi
xorl %r9d, %r9d
movq %rdi, %rax
xorl %edx, %edx
divq %r8
cmpq %rsi, %rdx
jne 0xb8a3
movq %r11, %r9
cmpq %rdi, %rcx
jne 0xb877
jmp 0xb8a3
xorl %r9d, %r9d
testq %r9, %r9
je 0xb917
cmpq $0x0, (%r9)
je 0xb917
leaq 0x38(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0xc23e
movq (%rax), %rsi
movq %rbx, %rdi
callq 0x3a10
testb %al, %al
je 0xb93f
leaq 0x38(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0xc23e
leaq 0xb8(%rsp), %rdi
movq %rax, %rsi
callq 0xbb9e
leaq 0x38(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0xc280
movq 0x10(%rsp), %rax
leaq 0x18(%rsp), %rsi
movq %rax, (%rsi)
movq %rbx, 0x8(%rsi)
leaq 0x38(%rsp), %rdi
callq 0xc366
jmp 0xb92d
leaq 0x18(%rsp), %rsi
movq %rcx, (%rsi)
movq %rbx, 0x8(%rsi)
leaq 0x38(%rsp), %rdi
callq 0xc366
leaq 0xb8(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0xbd30
movq (%r12), %r12
jmp 0xb7b7
movq %r13, %rdi
callq 0x4d02
cmpl %ebp, %eax
jl 0xb696
callq 0x3380
movq %rax, %rbx
movq 0xb651(%rip), %r15      # 0x16fb8
leaq 0x5de5(%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 0xb61f(%rip), %rdi      # 0x16fb8
callq 0x3040
movq %rax, %rdi
callq 0x31a0
movq %r14, %rdi
callq 0xa7e6
movq 0xb603(%rip), %rdi      # 0x16fb8
leaq 0x5d9c(%rip), %rsi      # 0x11758
movl $0x19, %edx
callq 0x32d0
movsd 0xf8(%r14), %xmm0
movq 0xb5e2(%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 0xb5ac(%rip), %rdi      # 0x16fb8
leaq 0x5d5f(%rip), %rsi      # 0x11772
movl $0x1d, %edx
callq 0x32d0
subq 0x70(%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 0xb571(%rip), %rdi      # 0x16fb8
callq 0x3260
movq %rax, %rbx
leaq 0x5d3a(%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 0x80(%rsp), %rdi
callq 0x52b6
leaq 0x100(%rsp), %rdi
callq 0x50cc
movq 0xc8(%rsp), %rdi
callq 0xbe4e
leaq 0x38(%rsp), %rdi
callq 0xc186
movq 0xe8(%rsp), %rdi
testq %rdi, %rdi
je 0xbad8
movq 0xf8(%rsp), %rsi
subq %rdi, %rsi
callq 0x3290
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x10, %edi
callq 0x3100
movq %rax, %r14
leaq 0x5c3f(%rip), %rsi      # 0x1173d
movq %rax, %rdi
callq 0x34d0
movq 0xb4d3(%rip), %rsi      # 0x16fe0
movq 0xb4bc(%rip), %rdx      # 0x16fd0
movq %r14, %rdi
callq 0x3480
jmp 0xbb20
jmp 0xbb44
movq %rax, %rbx
jmp 0xbb54
jmp 0xbb44
jmp 0xbb44
jmp 0xbb44
jmp 0xbb44
movq %rax, %rbx
movq %r14, %rdi
callq 0x31d0
jmp 0xbb47
jmp 0xbb44
jmp 0xbb44
jmp 0xbb44
jmp 0xbb44
jmp 0xbb44
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
callq 0x52b6
leaq 0x100(%rsp), %rdi
callq 0x50cc
movq 0xc8(%rsp), %rdi
callq 0xbe4e
leaq 0x38(%rsp), %rdi
callq 0xc186
movq 0xe8(%rsp), %rdi
testq %rdi, %rdi
je 0xbb95
movq 0xf8(%rsp), %rsi
subq %rdi, %rsi
callq 0x3290
movq %rbx, %rdi
callq 0x34a0
nop