// File: /workspace/github_cpp_makefile_star2_O1/sebastienmeyer2[P]mst-clustering/src/mst_algorithm.cpp
// Function: MSTAlgorithm::compute_clustering(double)
std::unordered_map<Node*, Node*> MSTAlgorithm::compute_clustering(double cutoff)
{
    /*!
     * @brief Creates a mapping between nodes of the MST (and initial) graph 
     * after it is built, and their representatives where we have eliminated 
     * the edges whose weights have a standard deviation larger than cutoff 
     * times the average weights of the other edges connected to its nodes
     * 
     * @param cutoff The scale to eliminate edges
     * 
     * @return A mapping between nodes of the graph(s) and their representatives 
     * after removing inconsistent edges
     * 
     */

    if (!this->treated) {
        this->compute_mst();
    }

    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 union-find ds and initializes the edges
    std::unordered_set<Edge*> mst_edges = this->mst_graph.get_edges();

    std::vector<Node*> nodes = this->mst_graph.get_nodes();
    UnionFind uf = UnionFind(nodes);

    // passes through the MST edges and keeps the consistent edges
    
    for (Edge* e : mst_edges) {
        bool add_edge = true;

        Node* p1 = e->p1;
        Node* p2 = e->p2;

        double avg_weight = 0.0;
        double std_dev = 0.0;
        double cnt = 0.0;

        double edge_std_dev = 0.0;

        // computes mean weight and std deviation for edges on both sides
        for (Edge* e1 : this->mst_graph.connected_edges(p1)) {
            if (e1 != e) {
                double edge_weight = e1->weight;
                avg_weight += edge_weight;
                std_dev += edge_weight*edge_weight;
                cnt += 1;
            }
        }

        for (Edge* e2 : this->mst_graph.connected_edges(p2)) {
            if (e2 != e) {
                double edge_weight = e2->weight;
                avg_weight += edge_weight;
                std_dev += edge_weight*edge_weight;
                cnt += 1;
            }
        }

        avg_weight /= cnt;
        std_dev = std::sqrt((std_dev/cnt) - avg_weight*avg_weight);

        // discards the considered edge if its std deviation is too large 
        edge_std_dev = std::abs(e->weight - avg_weight);
        if (edge_std_dev >= cutoff*std_dev) {
            add_edge = false;
        }

        // adds the edge if it is consistent
        if (add_edge == true) {
            uf.Union(p1, p2);
        }
    }

    // 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
movsd %xmm0, 0x30(%rsp)
movq %rsi, %r14
movq %rdi, %rbx
cmpb $0x0, 0x100(%rsi)
jne 0xab46
movq (%r14), %rax
movq %r14, %rdi
callq *(%rax)
addq $0x10, %r14
leaq 0x60(%rsp), %rdi
movq %r14, %rsi
callq 0x4e00
leaq 0x18(%rsp), %rdi
movq %r14, %rsi
callq 0x4d06
leaq 0x38(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0xaf1a
leaq 0x98(%rsp), %rdi
leaq 0x38(%rsp), %rsi
callq 0xdb40
movq 0x38(%rsp), %rdi
testq %rdi, %rdi
je 0xab9c
movq 0x48(%rsp), %rsi
subq %rdi, %rsi
callq 0x3290
movq 0x70(%rsp), %rbp
testq %rbp, %rbp
je 0xad17
movq 0x8(%rbp), %r15
movq (%r15), %r12
movq 0x8(%r15), %r13
movq %r14, %rdi
movq %r12, %rsi
callq 0x4cf0
movq 0x10(%rax), %rax
xorpd %xmm0, %xmm0
testq %rax, %rax
je 0xac2c
movsd %xmm0, (%rsp)
movsd %xmm0, 0x10(%rsp)
movsd %xmm0, 0x8(%rsp)
movq 0x8(%rax), %rcx
cmpq %r15, %rcx
je 0xac22
movsd 0x10(%rcx), %xmm0
movsd (%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, (%rsp)
mulsd %xmm0, %xmm0
movsd 0x10(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x10(%rsp)
movsd 0x8(%rsp), %xmm0
addsd 0x647c(%rip), %xmm0     # 0x11098
movsd %xmm0, 0x8(%rsp)
movq (%rax), %rax
testq %rax, %rax
jne 0xabde
jmp 0xac3d
movsd %xmm0, 0x8(%rsp)
movsd %xmm0, 0x10(%rsp)
movsd %xmm0, (%rsp)
movq %r14, %rdi
movq %r13, %rsi
callq 0x4cf0
movq 0x10(%rax), %rax
testq %rax, %rax
je 0xac9a
movq 0x8(%rax), %rcx
cmpq %r15, %rcx
je 0xac95
movsd 0x10(%rcx), %xmm0
movsd (%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, (%rsp)
mulsd %xmm0, %xmm0
movsd 0x10(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x10(%rsp)
movsd 0x8(%rsp), %xmm0
addsd 0x6409(%rip), %xmm0     # 0x11098
movsd %xmm0, 0x8(%rsp)
movq (%rax), %rax
jmp 0xac4c
movsd (%rsp), %xmm0
movsd 0x8(%rsp), %xmm2
divsd %xmm2, %xmm0
movsd 0x10(%rsp), %xmm1
divsd %xmm2, %xmm1
movsd %xmm0, (%rsp)
mulsd %xmm0, %xmm0
subsd %xmm0, %xmm1
ucomisd 0x6998(%rip), %xmm1     # 0x11660
jb 0xacd3
xorps %xmm0, %xmm0
sqrtsd %xmm1, %xmm0
jmp 0xacdc
movapd %xmm1, %xmm0
callq 0x3210
movsd 0x10(%r15), %xmm1
subsd (%rsp), %xmm1
andpd 0x6981(%rip), %xmm1     # 0x11670
mulsd 0x30(%rsp), %xmm0
ucomisd %xmm0, %xmm1
jae 0xad0e
leaq 0x98(%rsp), %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0xde5a
movq (%rbp), %rbp
jmp 0xaba1
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 0x18(%rsp), %r13
movq 0x20(%rsp), %rbp
cmpq %rbp, %r13
je 0xad84
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 0xad57
leaq 0x98(%rsp), %rdi
callq 0xdc62
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0xada8
movq 0x28(%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
movq %rax, %r14
movq 0x38(%rsp), %rdi
testq %rdi, %rdi
je 0xae10
movq 0x48(%rsp), %rsi
subq %rdi, %rsi
callq 0x3290
jmp 0xae10
movq %rax, %r14
jmp 0xae10
movq %rax, %r14
jmp 0xae27
jmp 0xae00
jmp 0xadf1
movq %rax, %r14
movq %rbx, %rdi
callq 0x91c8
jmp 0xae03
jmp 0xae00
movq %rax, %r14
leaq 0x98(%rsp), %rdi
callq 0xdc62
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0xae27
movq 0x28(%rsp), %rsi
subq %rdi, %rsi
callq 0x3290
leaq 0x60(%rsp), %rdi
callq 0x52b6
movq %r14, %rdi
callq 0x34a0