graph.cpp 7.84 KB
Newer Older
1
#include "main.h"
2 3
#include <cmath>
#include <map>
4
#include <queue>
5 6 7 8 9 10 11 12 13 14 15 16
#include <stack>

void erase(vector<int>& v, int value)
{
    for(int i=0;;i++)
        if(v[i] == value)
        {
            v[i] = v.back();
            v.pop_back();
            break;
        }
}
17

18
Graph::Graph(int size, int k, int maxPeers, mt19937& rng) :
19
    distrib(uniform_int_distribution<int>(0, size-1)),
20
    size(size), generator(rng), k(k), maxPeers(maxPeers)
21 22
{
    adjacency = new vector<int>[size];
23 24
    generated = new vector<int>[size];

25 26
    for(int i=0; i<size; i++)
    {
27
        int otherNode;
28
        unordered_set<int> alreadyConnected;
29 30 31 32
        alreadyConnected.insert(i);

        for(int j=0; j<k; j++)
        {
33 34
            while(alreadyConnected.count(otherNode = distrib(generator)) >= 1
                || otherNode > i && adjacency[otherNode].size() > maxPeers-k
35
                || adjacency[otherNode].size() > maxPeers)
36 37
            { }
            adjacency[i].push_back(otherNode);
38
            generated[i].push_back(otherNode);
39 40 41 42 43
            adjacency[otherNode].push_back(i);
        }
    }
}

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
int Graph::AddEdge(int from, unordered_set<int>& alreadyConnected)
{
    int otherNode;
    while(alreadyConnected.count(otherNode = distrib(generator)) >= 1
        || (adjacency[otherNode].size() > maxPeers ))
    { }
    adjacency[from].push_back(otherNode);
    generated[from].push_back(otherNode);
    adjacency[otherNode].push_back(from);
    return otherNode;
}

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

63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
void Graph::GetDistancesFrom(int node, int* distance)
{
    for(int j=0; j<size; j++)
        distance[j] = -1;
    distance[node] = 0;

    queue<int> remainingNodes;
    remainingNodes.push(node);

    while(!remainingNodes.empty())
    {
        int node = remainingNodes.front();
        remainingNodes.pop();

        for(int neighbor : adjacency[node])
            if(distance[neighbor] == -1)
            {
                distance[neighbor] = distance[node]+1;
                remainingNodes.push(neighbor);
            }
    }
}

86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
void Graph::GetRoutesFrom(int node, int* distance, float* routesCount)
{
    
    unordered_set<int> prevs[size];

    for(int i=0; i<size; i++)
    {
        distance[i] = -1;
        routesCount[i] = 1;
    }
    distance[node] = 0;

    queue<int> remainingNodes;
    remainingNodes.push(node);

    stack<int> order;

    // Get the order
    while(!remainingNodes.empty())
    {
        int node = remainingNodes.front();
        int d = distance[node];
        remainingNodes.pop();
        order.push(node);

        for(int neighbor : adjacency[node])
            if(distance[neighbor] == -1)
            {
                distance[neighbor] = d+1;
                prevs[neighbor].insert(node);
                remainingNodes.push(neighbor);
            }
            else if(distance[neighbor] == d+1)
                prevs[neighbor].insert(node);
    }

    // get the BC
    while(!order.empty())
    {
        int node = order.top();
        order.pop();
        float w = routesCount[node];
        for(int i : prevs[node])
            routesCount[i] += w;
    }

}

134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
int Graph::CountUnreachableFrom(int node)
{
    bool accessibility[size];
    for(int i=0; i<size; i++)
        accessibility[i] = false;
    accessibility[node] = true;
    int unAccessible = 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;
            }
        }

        unAccessible--;
        toVisit.pop();
    }
159

160 161 162
    return unAccessible;
}

163 164 165
// kill the last proportion*size machines of the graph
void Graph::KillMachines(float proportion)
{
166 167 168 169 170 171 172 173 174 175 176 177
    size = proportion*size;
    for(int i=0; i<size; i++)
    {
        auto it=adjacency[i].begin();
        while(it!=adjacency[i].end())
        {
            if(*it >= size)
                it = adjacency[i].erase(it);
            else
                it++;
        }
    }
178
}
179 180 181

int Graph::GetMinCut()
{
182
    int nIter = log(size);
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 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 233 234 235 236 237 238 239
    int minCut = -1;
    for(int i=0; i<nIter; i++)
    {
        MinCutGraph graph = MinCutGraph(adjacency, size);
        int minCutCandidate = GetMinCut(graph);
        if(minCut == -1 || minCut > minCutCandidate)
            minCut = minCutCandidate;
    }

    return minCut;
}

int Graph::GetMinCut(MinCutGraph& copy1)
{
    int n=copy1.nodes.size();

    if(n==2)
        return copy1.edges.size();

    MinCutGraph copy2(copy1);

    int nMerge = min(n-2.0, n/1.414);
    copy1.Merge(nMerge, generator);
    copy2.Merge(nMerge, generator);

    return min(GetMinCut(copy1), GetMinCut(copy2));
}

MinCutGraph::MinCutGraph(vector<int>* adjacency, int n)
{
    nodes.resize(n);
    int nextEdgeId = 0;

    for(int i=0; i<n; i++)
        for(int j : adjacency[i])
            if(j > i)
            {
                nodes[i].v.insert(nextEdgeId);
                nodes[j].v.insert(nextEdgeId);
                edges.push_back(nullable<pair<int, int>>(pair<int, int>(i, j)));
                nextEdgeId++;
            }
}

void MinCutGraph::Merge(int nMerge, mt19937& rng)
{
    uniform_int_distribution<int> distrib(0, edges.size()-1);

    while(nMerge > 0)
    {
        // Choose an edge
        int eId = distrib(rng);
        if(edges[eId].null)
            continue;

        int n1 = edges[eId].v.first;
        int n2 = edges[eId].v.second;
240

241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
        // anilate n2
        nodes[n2].null = true;

        // redirect all edges from n2
        for(int i : nodes[n2].v)
        {
            if(edges[i].v.first == n1 || edges[i].v.second == n1)
            {
                nodes[n1].v.erase(i);
                edges[i].null = true;
            }

            else
            {
                nodes[n1].v.insert(i);

                if(edges[i].v.first == n2)
                    edges[i].v.first = n1;
                else
                    edges[i].v.second = n1;
            }
        }

        nMerge--;
    }

    RenumNodes();
    RenumEdges();
}

void MinCutGraph::Check()
{
    cout << "CHECKING ... "; cout.flush();
    for(int i=0; i<edges.size(); i++)
    {
        if(!edges[i].null)
        {
            auto e = edges[i].v;

            if(e.first >= nodes.size())
                cout << "Bad first" << endl; cout.flush();
            if(e.second >= nodes.size())
                cout << "Bad second" << endl; cout.flush();
            if(nodes[e.first].v.count(i) == 0)
                cout << "Bad first node" << endl; cout.flush();
            if(nodes[e.second].v.count(i) == 0)
                cout << "Bad second node" << endl; cout.flush();
        }
    }

    for(int i=0; i<nodes.size(); i++)
        if(!nodes[i].null)
            for(int e : nodes[i].v)
                if(edges[e].v.first != i && edges[e].v.second != i)
                    cout << "Bad edge" << endl; cout.flush();

    cout << "DONE" << endl; cout.flush();
}

void MinCutGraph::RenumEdges()
{
    int nextId = 0;
    for(int i=0; i<edges.size(); i++)
        if(!edges[i].null)
        {
            edges[nextId] = edges[i];
            nodes[edges[nextId].v.first].v.erase(i);
            nodes[edges[nextId].v.first].v.insert(nextId);
            nodes[edges[nextId].v.second].v.erase(i);
            nodes[edges[nextId].v.second].v.insert(nextId);
            nextId++;
        }
    edges.resize(nextId);
}

void MinCutGraph::RenumNodes()
{
    int nextId = 0;
    for(int i=0; i<nodes.size(); i++)
        if(!nodes[i].null)
        {
            nodes[nextId] = nodes[i];
            for(int j : nodes[nextId].v)
            {
                if(edges[j].v.first == i)
                    edges[j].v.first = nextId;
                else
                    edges[j].v.second = nextId;
            }
            nextId++;
        }
    nodes.resize(nextId);
}