// File: /workspace/github_cpp_makefile_star2_O3/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, 0x40(%rsp)
movq %rsi, %r14
movq %rdi, %rbx
cmpb $0x0, 0x100(%rsi)
jne 0xad0c
movq (%r14), %rax
movq %r14, %rdi
callq *(%rax)
addq $0x10, %r14
leaq 0x70(%rsp), %rdi
movq %r14, %rsi
callq 0x4dc6
leaq 0x28(%rsp), %rdi
movq %r14, %rsi
callq 0x4ccc
leaq 0x48(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0xb0c4
leaq 0xa8(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0xdeac
movq 0x48(%rsp), %rdi
testq %rdi, %rdi
je 0xad62
movq 0x58(%rsp), %rsi
subq %rdi, %rsi
callq 0x3290
movq 0x80(%rsp), %rbp
testq %rbp, %rbp
je 0xaec9
movq 0x8(%rbp), %r15
movq (%r15), %r12
movq 0x8(%r15), %r13
movq %r14, %rdi
movq %r12, %rsi
callq 0x4ca2
movq 0x10(%rax), %rax
xorpd %xmm0, %xmm0
testq %rax, %rax
je 0xadec
movapd %xmm0, (%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x10(%rsp)
movq 0x8(%rax), %rcx
cmpq %r15, %rcx
je 0xade2
movsd 0x10(%rcx), %xmm0
movapd %xmm0, %xmm1
mulsd %xmm0, %xmm1
movapd 0x10(%rsp), %xmm2
addsd %xmm1, %xmm2
movapd %xmm2, 0x10(%rsp)
shufpd $0x2, 0x68cc(%rip), %xmm0 # xmm0 = xmm0[0],mem[1]
movapd (%rsp), %xmm1
addpd %xmm0, %xmm1
movapd %xmm1, (%rsp)
movq (%rax), %rax
testq %rax, %rax
jne 0xada5
jmp 0xadfb
movapd %xmm0, (%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x10(%rsp)
movq %r14, %rdi
movq %r13, %rsi
callq 0x4ca2
movq 0x10(%rax), %rax
testq %rax, %rax
je 0xae51
movq 0x8(%rax), %rcx
cmpq %r15, %rcx
je 0xae4c
movsd 0x10(%rcx), %xmm0
movapd %xmm0, %xmm1
mulsd %xmm0, %xmm1
movapd 0x10(%rsp), %xmm2
addsd %xmm1, %xmm2
movapd %xmm2, 0x10(%rsp)
shufpd $0x2, 0x6862(%rip), %xmm0 # xmm0 = xmm0[0],mem[1]
movapd (%rsp), %xmm1
addpd %xmm0, %xmm1
movapd %xmm1, (%rsp)
movq (%rax), %rax
jmp 0xae0a
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 0x680f(%rip), %xmm0     # 0x11690
jb 0xae89
sqrtsd %xmm0, %xmm0
jmp 0xae8e
callq 0x3210
movsd 0x10(%r15), %xmm1
subsd (%rsp), %xmm1
andpd 0x680f(%rip), %xmm1     # 0x116b0
mulsd 0x40(%rsp), %xmm0
ucomisd %xmm0, %xmm1
jae 0xaec0
leaq 0xa8(%rsp), %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0xe15c
movq (%rbp), %rbp
jmp 0xad6a
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 0x28(%rsp), %r13
movq 0x30(%rsp), %rbp
cmpq %rbp, %r13
je 0xaf36
leaq 0xa8(%rsp), %r14
leaq 0x60(%rsp), %r15
movq (%r13), %r12
movq %r14, %rdi
movq %r12, %rsi
callq 0xe0e0
movq %r12, 0x60(%rsp)
movq %rax, 0x68(%rsp)
movq %rbx, %rdi
movq %r15, %rsi
callq 0xb280
addq $0x8, %r13
cmpq %rbp, %r13
jne 0xaf09
leaq 0xa8(%rsp), %rdi
callq 0xdfce
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0xaf5a
movq 0x38(%rsp), %rsi
subq %rdi, %rsi
callq 0x3290
leaq 0x70(%rsp), %rdi
callq 0x529c
movq %rbx, %rax
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r14
movq 0x48(%rsp), %rdi
testq %rdi, %rdi
je 0xafc2
movq 0x58(%rsp), %rsi
subq %rdi, %rsi
callq 0x3290
jmp 0xafc2
movq %rax, %r14
jmp 0xafc2
movq %rax, %r14
jmp 0xafd9
jmp 0xafb2
jmp 0xafa3
movq %rax, %r14
movq %rbx, %rdi
callq 0x90a2
jmp 0xafb5
jmp 0xafb2
movq %rax, %r14
leaq 0xa8(%rsp), %rdi
callq 0xdfce
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0xafd9
movq 0x38(%rsp), %rsi
subq %rdi, %rsi
callq 0x3290
leaq 0x70(%rsp), %rdi
callq 0x529c
movq %r14, %rdi
callq 0x34a0