-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkingman.hpp
52 lines (49 loc) · 1.52 KB
/
kingman.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#define _USE_MATH_DEFINES
#include <iostream>
#include <list>
#include <cstdlib>
#include <math.h>
//#include </home/dak33/boost_1_47_0/boost/lambda/lambda.hpp>
#include <boost/math/special_functions/gamma.hpp>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/uniform_real.hpp>
#include <boost/random/uniform_int_distribution.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/numeric/ublas/vector.hpp>
#include "TreeNode.hpp"
#include <string>
#include <limits>
#include <algorithm>
using namespace std;
template <typename T>
T ListGet(list<T> l, int i)
{
int counter=0;
for (typename list<T>::iterator it=l.begin(); it!=l.end(); it++)
if (counter==i)
return *it;
else
counter++;
throw 1;
}
// build a tree from the coalescent, return the root
TreeNode* KingmansCoalescent(int numLeaves, Settings &settings)
{
list<TreeNode*> current;
for (int i=0; i<numLeaves; i++)
current.push_front(new TreeNode(0,true));
for (int i=0; i<numLeaves-1; i++)
{
TreeNode *t = new TreeNode(-(i+1),false);
int l = settings.iRand(0,numLeaves-i-1);
TreeNode* l_node = ListGet<TreeNode*>(current,l);
current.remove(l_node);
int j = settings.iRand(0,numLeaves-i-2);
TreeNode* j_node = ListGet<TreeNode*>(current,j);
current.remove(j_node);
t->children.push_back(l_node);
t->children.push_back(j_node);
current.push_back(t);
}
return *current.begin(); // should be the only element!
}