// File: /workspace/github_cpp_makefile_star2_O2/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 $0x158, %rsp            # imm = 0x158
movsd %xmm0, 0x28(%rsp)
movq %rsi, %r14
movq %rdi, %rbx
cmpb $0x0, 0x100(%rsi)
jne 0x9704
movq (%r14), %rax
movq %r14, %rdi
callq *(%rax)
addq $0x10, %r14
leaq 0x70(%rsp), %rdi
movq %r14, %rsi
callq 0x46e4
leaq 0x30(%rsp), %rdi
movq %r14, %rsi
callq 0x465e
leaq 0x58(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x9a6c
leaq 0xa8(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0xc084
leaq 0x58(%rsp), %rdi
callq 0x5d78
leaq 0x80(%rsp), %rbp
movq (%rbp), %rbp
testq %rbp, %rbp
je 0x98a4
movq 0x8(%rbp), %r15
movq (%r15), %r12
movq 0x8(%r15), %r13
movq %r14, %rdi
movq %r12, %rsi
callq 0x4648
addq $0x10, %rax
xorpd %xmm0, %xmm0
movapd %xmm0, (%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x10(%rsp)
movq (%rax), %rax
testq %rax, %rax
je 0x97d6
movq 0x8(%rax), %rcx
cmpq %r15, %rcx
je 0x978f
movsd 0x10(%rcx), %xmm0
movapd %xmm0, %xmm1
mulsd %xmm0, %xmm1
movapd 0x10(%rsp), %xmm2
addsd %xmm1, %xmm2
movapd %xmm2, 0x10(%rsp)
shufpd $0x2, 0x5eaa(%rip), %xmm0 # xmm0 = xmm0[0],mem[1]
movapd (%rsp), %xmm1
addpd %xmm0, %xmm1
movapd %xmm1, (%rsp)
jmp 0x978f
movq %r14, %rdi
movq %r13, %rsi
callq 0x4648
addq $0x10, %rax
movq (%rax), %rax
testq %rax, %rax
je 0x982c
movq 0x8(%rax), %rcx
cmpq %r15, %rcx
je 0x97e5
movsd 0x10(%rcx), %xmm0
movapd %xmm0, %xmm1
mulsd %xmm0, %xmm1
movapd 0x10(%rsp), %xmm2
addsd %xmm1, %xmm2
movapd %xmm2, 0x10(%rsp)
shufpd $0x2, 0x5e54(%rip), %xmm0 # xmm0 = xmm0[0],mem[1]
movapd (%rsp), %xmm1
addpd %xmm0, %xmm1
movapd %xmm1, (%rsp)
jmp 0x97e5
movapd (%rsp), %xmm1
movapd %xmm1, %xmm2
unpckhpd %xmm1, %xmm2            # xmm2 = xmm2[1],xmm1[1]
divsd %xmm2, %xmm1
movapd 0x10(%rsp), %xmm0
divsd %xmm2, %xmm0
movapd %xmm1, (%rsp)
mulsd %xmm1, %xmm1
subsd %xmm1, %xmm0
ucomisd 0x5e04(%rip), %xmm0     # 0xf660
jb 0x9864
sqrtsd %xmm0, %xmm0
jmp 0x9869
callq 0x3240
movsd 0x10(%r15), %xmm1
subsd (%rsp), %xmm1
andpd 0x5e04(%rip), %xmm1     # 0xf680
mulsd 0x28(%rsp), %xmm0
ucomisd %xmm0, %xmm1
jae 0x9755
leaq 0xa8(%rsp), %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0xc2c4
jmp 0x9755
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 0x30(%rsp), %r13
movq 0x38(%rsp), %rbp
leaq 0xa8(%rsp), %r14
leaq 0x48(%rsp), %r15
cmpq %rbp, %r13
je 0x990e
movq (%r13), %r12
movq %r14, %rdi
movq %r12, %rsi
callq 0xc24a
movq %r12, 0x48(%rsp)
movq %rax, 0x50(%rsp)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x9c76
addq $0x8, %r13
jmp 0x98df
leaq 0xa8(%rsp), %rdi
callq 0xc19a
leaq 0x30(%rsp), %rdi
callq 0x5d78
leaq 0x70(%rsp), %rdi
callq 0x4cfe
movq %rbx, %rax
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r14
leaq 0x58(%rsp), %rdi
callq 0x5d78
jmp 0x9980
movq %rax, %r14
jmp 0x9980
movq %rax, %r14
jmp 0x998a
jmp 0x9970
jmp 0x9961
movq %rax, %r14
movq %rbx, %rdi
callq 0x7e78
jmp 0x9973
jmp 0x9970
movq %rax, %r14
leaq 0xa8(%rsp), %rdi
callq 0xc19a
leaq 0x30(%rsp), %rdi
callq 0x5d78
leaq 0x70(%rsp), %rdi
callq 0x4cfe
movq %r14, %rdi
callq 0x34b0