// File: /workspace/github_cpp_makefile_star2_O2/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 $0x158, %rsp            # imm = 0x158
movq %rdi, %r14
andq $0x0, 0xf8(%rdi)
movq 0xd023(%rip), %rdi      # 0x16fb0
leaq 0x578c(%rip), %rsi      # 0xf720
callq 0x32a0
callq 0x33a0
movq %rax, %rbx
movq 0x8(%r14), %rdi
callq 0x465a
movl %eax, 0x4c(%rsp)
movq 0x8(%r14), %rsi
leaq 0x98(%rsp), %rdi
callq 0x465e
leaq 0x150(%rsp), %rax
movq %rax, -0x30(%rax)
pushq $0x1
popq %rcx
movq %rcx, -0x28(%rax)
leaq 0x70(%rsp), %rdx
andl $0x0, (%rdx)
andq $0x0, 0x8(%rdx)
movq %rdx, 0x10(%rdx)
movq %rdx, 0x18(%rdx)
andq $0x0, 0x20(%rdx)
xorpd %xmm0, %xmm0
movupd %xmm0, -0x20(%rax)
movl $0x3f800000, %edx       # imm = 0x3F800000
movl %edx, -0x10(%rax)
movupd %xmm0, -0x8(%rax)
leaq 0x118(%rsp), %rax
movq %rax, -0x30(%rax)
movq %rcx, -0x28(%rax)
movupd %xmm0, -0x20(%rax)
movl %edx, -0x10(%rax)
movupd %xmm0, -0x8(%rax)
movq %r14, 0x8(%rsp)
movq 0x8(%r14), %rdi
callq 0x46f6
leaq 0x40(%rsp), %rsi
movq %rax, (%rsi)
leaq 0xe8(%rsp), %rdi
callq 0x593a
movq 0x8(%rsp), %rax
leaq 0x10(%rax), %rdi
movq 0x40(%rsp), %rsi
movq %rdi, 0x50(%rsp)
callq 0x4556
movq %rbx, 0x58(%rsp)
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rdi
movq 0x40(%rsp), %rsi
callq 0x4648
leaq 0xb0(%rsp), %rdi
movq %rax, %rsi
callq 0x4efc
leaq 0xc0(%rsp), %rax
movq (%rax), %rbp
leaq 0xe8(%rsp), %rbx
leaq 0x10(%rsp), %r14
leaq 0x120(%rsp), %r15
leaq 0x68(%rsp), %r13
testq %rbp, %rbp
je 0xa17a
movq 0x8(%rbp), %r12
movq %r12, 0x38(%rsp)
movq 0x40(%rsp), %rsi
movq %r12, %rdi
callq 0x3838
movq %rax, 0x10(%rsp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x58be
testq %rax, %rax
jne 0xa171
movq %r15, %rdi
movq %r14, %rsi
callq 0xaa80
testq %rax, %rax
je 0xa148
movq %r15, %rdi
movq %r14, %rsi
callq 0xaafc
movq (%rax), %rsi
movq %r12, %rdi
callq 0x3850
testb %al, %al
je 0xa171
movq %r15, %rdi
movq %r14, %rsi
callq 0xaafc
movq %r13, %rdi
movq %rax, %rsi
callq 0xa4d8
movq %r15, %rdi
movq %r14, %rsi
callq 0xab1a
movq 0x10(%rsp), %rax
movq %rax, 0x18(%rsp)
movq %r12, 0x20(%rsp)
movq %r15, %rdi
leaq 0x18(%rsp), %rsi
callq 0xac1c
jmp 0xa164
movq 0x10(%rsp), %rax
movq %rax, 0x18(%rsp)
movq %r12, 0x20(%rsp)
movq %r15, %rdi
leaq 0x18(%rsp), %rsi
callq 0xac1c
movq %r13, %rdi
leaq 0x38(%rsp), %rsi
callq 0xa64e
movq (%rbp), %rbp
jmp 0xa0a7
leaq 0xe8(%rsp), %r12
leaq 0x120(%rsp), %r13
leaq 0x68(%rsp), %rbp
leaq 0x30(%rsp), %rbx
movq 0x50(%rsp), %rdi
callq 0x465a
cmpl 0x4c(%rsp), %eax
jge 0xa338
cmpq $0x0, 0x90(%rsp)
je 0xa42c
movq 0x80(%rsp), %rax
movq 0x20(%rax), %r15
movq %r15, 0x38(%rsp)
movq (%r15), %rax
movq %rax, 0x10(%rsp)
movq 0x8(%r15), %r14
movq %r12, %rdi
leaq 0x10(%rsp), %rsi
callq 0x58be
testq %rax, %rax
jne 0xa1eb
movq 0x10(%rsp), %r14
movq %r14, 0x28(%rsp)
movq %r12, %rdi
leaq 0x28(%rsp), %rsi
callq 0x593a
movq 0x50(%rsp), %rdi
movq %r15, %rsi
callq 0x4250
movsd 0x10(%r15), %xmm0
movq 0x8(%rsp), %rax
addsd 0xf8(%rax), %xmm0
movsd %xmm0, 0xf8(%rax)
movq %r13, %rdi
leaq 0x28(%rsp), %rsi
callq 0xab1a
movq %rbp, %rdi
leaq 0x38(%rsp), %rsi
callq 0xa4d8
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rdi
movq 0x28(%rsp), %rsi
callq 0x4648
leaq 0xb0(%rsp), %rdi
movq %rax, %rsi
callq 0xa78e
leaq 0xc0(%rsp), %r14
movq (%r14), %r14
testq %r14, %r14
je 0xa194
movq 0x8(%r14), %r15
movq %r15, 0x60(%rsp)
movq 0x28(%rsp), %rsi
movq %r15, %rdi
callq 0x3838
movq %rax, 0x30(%rsp)
movq %r12, %rdi
movq %rbx, %rsi
callq 0x58be
testq %rax, %rax
jne 0xa26a
movq %r13, %rdi
movq %rbx, %rsi
callq 0xaa80
testq %rax, %rax
je 0xa30a
movq %r13, %rdi
movq %rbx, %rsi
callq 0xaafc
movq (%rax), %rsi
movq %r15, %rdi
callq 0x3850
testb %al, %al
je 0xa26a
movq %r13, %rdi
movq %rbx, %rsi
callq 0xaafc
movq %rbp, %rdi
movq %rax, %rsi
callq 0xa4d8
movq %r13, %rdi
movq %rbx, %rsi
callq 0xab1a
movq 0x30(%rsp), %rax
movq %rax, 0x18(%rsp)
movq %r15, 0x20(%rsp)
movq %r13, %rdi
leaq 0x18(%rsp), %rsi
callq 0xac1c
jmp 0xa326
movq 0x30(%rsp), %rax
movq %rax, 0x18(%rsp)
movq %r15, 0x20(%rsp)
movq %r13, %rdi
leaq 0x18(%rsp), %rsi
callq 0xac1c
movq %rbp, %rdi
leaq 0x60(%rsp), %rsi
callq 0xa64e
jmp 0xa26a
callq 0x33a0
movq %rax, %rbx
movq 0xcc69(%rip), %rdi      # 0x16fb0
leaq 0x5415(%rip), %rsi      # 0xf763
callq 0x32a0
movq %rax, %rdi
callq 0x3130
movq 0x8(%rsp), %rdi
callq 0x944c
movq 0xcc44(%rip), %rdi      # 0x16fb0
leaq 0x53f5(%rip), %rsi      # 0xf768
callq 0x32a0
movq 0x8(%rsp), %rcx
movsd 0xf8(%rcx), %xmm0
movq %rax, %rdi
callq 0x3430
movq %rax, %rdi
callq 0x3130
movq 0xcc14(%rip), %rdi      # 0x16fb0
leaq 0x53df(%rip), %rsi      # 0xf782
callq 0x32a0
movq %rax, %rdi
subq 0x58(%rsp), %rbx
movl $0x3e8, %ecx            # imm = 0x3E8
movq %rbx, %rax
cqto
idivq %rcx
movq %rax, %rsi
callq 0x3290
leaq 0x53d4(%rip), %rsi      # 0xf7a0
movq %rax, %rdi
callq 0x32a0
movq %rax, %rdi
callq 0x3130
leaq 0xb0(%rsp), %rdi
callq 0x4cfe
leaq 0xe8(%rsp), %rdi
callq 0x4a48
leaq 0x68(%rsp), %rdi
callq 0xa754
leaq 0x120(%rsp), %rdi
callq 0xa9d8
leaq 0x98(%rsp), %rdi
callq 0x5d78
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x10
popq %rdi
callq 0x3140
movq %rax, %r14
leaq 0x530f(%rip), %rsi      # 0xf74d
movq %rax, %rdi
callq 0x34d0
movq 0xcb93(%rip), %rsi      # 0x16fe0
movq 0xcb7c(%rip), %rdx      # 0x16fd0
movq %r14, %rdi
callq 0x3490
jmp 0xa460
jmp 0xa48e
movq %rax, %rbx
jmp 0xa49e
jmp 0xa48e
jmp 0xa48e
jmp 0xa48e
jmp 0xa48e
jmp 0xa48e
movq %rax, %rbx
movq %r14, %rdi
callq 0x3200
jmp 0xa491
jmp 0xa48e
jmp 0xa48e
jmp 0xa48e
jmp 0xa48e
jmp 0xa48e
jmp 0xa48e
jmp 0xa48e
jmp 0xa48e
jmp 0xa48e
movq %rax, %rbx
leaq 0xb0(%rsp), %rdi
callq 0x4cfe
leaq 0xe8(%rsp), %rdi
callq 0x4a48
leaq 0x68(%rsp), %rdi
callq 0xa754
leaq 0x120(%rsp), %rdi
callq 0xa9d8
leaq 0x98(%rsp), %rdi
callq 0x5d78
movq %rbx, %rdi
callq 0x34b0
nop