graph.cpp 5.59 KB
Newer Older
1 2
#include "main.h"

3 4
Graph::Graph(int size, int k, int maxPeers, mt19937& rng,  const Latency& latency) :
	generator(mt19937(rng())), size(size), k(k), maxPeers(maxPeers), latency(latency),
5 6 7 8 9 10 11 12 13
	distrib(uniform_int_distribution<int>(0, size-1))
{
	adjacency = new unordered_set<int>[size];
	generated = new unordered_set<int>[size];

	for(int i=0; i<size; i++)
		SaturateNode(i);
}

Ulysse Beaugnon's avatar
Ulysse Beaugnon committed
14 15 16 17 18 19 20 21 22 23 24 25 26 27
Graph::Graph(const Graph& g) :
	generator(g.generator), size(g.size), k(g.k), maxPeers(g.maxPeers), latency(latency),
	distrib(g.distrib)
{
	adjacency = new unordered_set<int>[size];
	generated = new unordered_set<int>[size];

	for(int i=0; i<size; i++)
	{
		adjacency[i] = unordered_set<int>(g.adjacency[i]);
		generated[i] = unordered_set<int>(g.generated[i]);
	}
}

28 29 30 31 32 33 34 35 36 37 38
void Graph::SaturateNode(int node)
{
	while(generated[node].size() < k && AddEdge(node)) { }
}

bool Graph::AddEdge(int from)
{
	int to;
	for(int i=0; i<50; i++)
	{
		to = distrib(generator);
39 40 41

		if(	to != from
			&& latency.values[from][to] > 0
42
			&& adjacency[from].count(to) == 0
Ulysse Beaugnon's avatar
Ulysse Beaugnon committed
43
			&& adjacency[to].size() + k < maxPeers + generated[to].size())
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
		{
			generated[from].insert(to);
			adjacency[from].insert(to);
			adjacency[to].insert(from);
			return true;
		}
	}

	//cout << "Warning : a link could not be established from " << from << endl;
	return false;
}

void Graph::RemoveEdge(int from, int to)
{
	generated[from].erase(to);
	adjacency[from].erase(to);
	adjacency[to].erase(from);
}

void Graph::GetRoutesFrom(int from,  int* nRoutes, int* prevs, int* distances)
{
	// init vars
    stack<int> order;

    for(int i=0; i<size; i++)
    {
        distances[i] = -1;
        nRoutes[i] = 1;
    }
    distances[from] = 0;

    priority_queue<pair<int, int>> remainingNodes;
    remainingNodes.push(pair<int, int>(-0, from));

    // Get the order
    while(!remainingNodes.empty())
    {
    	pair<int, int> p = remainingNodes.top();
        int node = p.second;
        int d = -p.first;
        remainingNodes.pop();

        if(d == distances[node])
        {
	        order.push(node);
	        for(int neighbor : adjacency[node])
	        {
	        	int neighborDist = d + latency.values[neighbor][node];

	            if(distances[neighbor] == -1 || distances[neighbor] > neighborDist)
	            {
	                distances[neighbor] = neighborDist;
	                prevs[neighbor] = node;
	                remainingNodes.push(pair<int, int>(-neighborDist, neighbor));
	            }
	        }
	    }
    }

    // get the BC
Ulysse Beaugnon's avatar
Ulysse Beaugnon committed
104
    // The error is here
105 106 107 108
    while(!order.empty())
    {
        int node = order.top();
        order.pop();
Ulysse Beaugnon's avatar
Ulysse Beaugnon committed
109
        if(distances[node] != -1 && node != from)
110 111 112 113 114
        	nRoutes[prevs[node]] += nRoutes[node];
    }
}


Ulysse Beaugnon's avatar
Ulysse Beaugnon committed
115 116
int Graph::UpdateLowRoutes(double& avgDistance, double unreachable, double* arityDistrib,
		double* bcArity, int nRefresh, int round)
117
{
Ulysse Beaugnon's avatar
Ulysse Beaugnon committed
118
	int nUpdated = 0;
119
	routesResult results[size];
Ulysse Beaugnon's avatar
Ulysse Beaugnon committed
120 121 122 123 124 125 126 127 128 129 130 131
	double bc[size];
	for(int i=0; i<size; i++)
		bc[i] = 0;

	avgDistance = 0;
	double avgDistanceWeight = 0;
	unreachable = 0;
	for(int i=0; i<=maxPeers; i++)
	{
		bcArity[i] = 0;
		arityDistrib[i] = 0;
	}
132 133 134 135 136 137

	for(int i=0; i<size; i++)
	{
    	// Compute the routes
        int nRoutes[size], prevs[size], distances[size];
        GetRoutesFrom(i, nRoutes, prevs, distances);
Ulysse Beaugnon's avatar
Ulysse Beaugnon committed
138 139
        for(int j=0; j<size; j++)
        	bc[j] += nRoutes[j];
140 141 142

        // Get the values
        routesResult r;
Ulysse Beaugnon's avatar
Ulysse Beaugnon committed
143 144 145 146 147 148 149 150 151 152 153
        
        for(int k=0; k<nRefresh; k++)
        {
        	int mini = -1;
			for(int j : generated[i])
				if(mini == -1 || nRoutes[mini] > nRoutes[j])
					mini = j;

			if(mini != -1)
				r.toDelete.push(mini);
		}
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170

		r.arity = adjacency[i].size();

		r.avgDistance = 0;
		r.unreachable = 0;
		for(int j=0; j<size; j++)
		{
			if(distances[i] >= 0)
				r.avgDistance += distances[j];
			else
				r.unreachable++;
		}

		r.avgDistance /= (double)(size - r.unreachable);

		results[i] = r;
	}
Ulysse Beaugnon's avatar
Ulysse Beaugnon committed
171
	
172 173 174
	for(int i = 0; i<size; i++)
	{
		routesResult r = results[i];
Ulysse Beaugnon's avatar
Ulysse Beaugnon committed
175 176 177 178 179 180 181 182 183
		if((adjacency[i].size() > 16 &&  adjacency[i].size() < 26) || round % 4 == 0)
		{
			nUpdated++;
			while(!r.toDelete.empty())
			{
				RemoveEdge(i, r.toDelete.top());
				r.toDelete.pop();
			}
		}
184 185 186 187 188 189 190

		SaturateNode(i);

		avgDistance += r.avgDistance*(size-r.unreachable);
		avgDistanceWeight += size-r.unreachable;
		unreachable += r.unreachable;
		arityDistrib[adjacency[i].size()]++;
Ulysse Beaugnon's avatar
Ulysse Beaugnon committed
191
		bcArity[adjacency[i].size()] += bc[i] - 2*size + 1;
192 193 194 195 196
	}

	avgDistance /= avgDistanceWeight;

	for(int i=0; i<=maxPeers; i++)
Ulysse Beaugnon's avatar
Ulysse Beaugnon committed
197 198
	{
		bcArity[i] = arityDistrib[i]>0 ? bcArity[i] / arityDistrib[i]:0;
199
		arityDistrib[i] /= size;
Ulysse Beaugnon's avatar
Ulysse Beaugnon committed
200 201 202
	}

	return nUpdated;
Ulysse Beaugnon's avatar
Ulysse Beaugnon committed
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
}

int Graph::CountUnreachableFrom(int node)
{
    bool accessibility[size];
    for(int i=0; i<size; i++)
        accessibility[i] = false;
    accessibility[node] = true;
    int unreachable = size;

    queue<int> toVisit;
    toVisit.push(node);
    while(!toVisit.empty())
    {
        int n = toVisit.front();
        for(int i : adjacency[n])
        {
            if(!accessibility[i])
            {
                toVisit.push(i);
                accessibility[i] = true;
            }
        }

        unreachable--;
        toVisit.pop();
    }

    return unreachable;
}
233

Ulysse Beaugnon's avatar
Ulysse Beaugnon committed
234 235 236 237 238 239
double Graph::GetUnAvalaibility()
{
	double moy = 0;
	for(int i=0; i<size; i++)
		moy += CountUnreachableFrom(i);
	return moy / (size*size);
240
}
Ulysse Beaugnon's avatar
Ulysse Beaugnon committed
241 242 243 244

void Graph::KillMachines(float proportion)
{
    size = proportion*size;
245 246
	distrib = uniform_int_distribution<int>(0, size - 1);

Ulysse Beaugnon's avatar
Ulysse Beaugnon committed
247 248 249 250 251 252 253 254 255 256 257 258 259
    for(int i=0; i<size; i++)
    {
    	vector<int> toBeRemoved;
    	for(int j : adjacency[i])
    		if(j >= size)
    			toBeRemoved.push_back(j);

        for(int j : toBeRemoved)
        {
        	generated[i].erase(j);
        	adjacency[i].erase(j);
        }
    }
260
}