-
Notifications
You must be signed in to change notification settings - Fork 1
/
RandomGraphGenerator.cpp
148 lines (110 loc) · 4.08 KB
/
RandomGraphGenerator.cpp
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
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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: RandomGraphGenerator.cpp
* Author: Wooyoung
*
* Created on November 2, 2017, 1:23 PM
*/
#include "RandomGraphGenerator.h"
RandomGraphGenerator::RandomGraphGenerator() {
}
RandomGraphGenerator::~RandomGraphGenerator() {
}
int nextInt2(int min, int max){
return rand() % (max - min + 1) + min;
}
Graph RandomGraphGenerator:: generate(Graph& inputGraph){
vector<int> degreeSeq = getDegreeSequenceVector(inputGraph);
bool directed = inputGraph.isDirected();
Graph randomGraph(directed);
// generate randomized list of vertices
// the vertexList is a set where each node is represented by a number
// of elements equal to that vertex's degree
vector <int> vertexList;
for (int vertex = 0; vertex<inputGraph.getSize();++vertex){
randomGraph.addVertex();
for (int degree=0; degree<degreeSeq[vertex];++degree)
vertexList.push_back(vertex);
}
random_shuffle(vertexList.begin(), vertexList.end());
// create edges
int u,v;
for (int c=0; vertexList.size()>1;++c){
u = nextInt2(0, vertexList.size()-1);
// make sure v != u
v = nextInt2(0, vertexList.size()-1);
while (v==u){
v = nextInt2(0, vertexList.size()-1);
}
//swap if u>v
if (u>v){
int temp = u;
u=v;
v=temp;
}
int edgeVertexV = vertexList[v];
int edgeVertexU = vertexList[u];
vertexList.erase(vertexList.begin()+ v );
vertexList.erase(vertexList.begin() +u);
if (edgeVertexV == edgeVertexU) continue; // avoid self-edge
randomGraph.addEdge(edgeVertexU, edgeVertexV);
}
return randomGraph;
}
Graph RandomGraphGenerator::generate(Graph& inputGraph, vector <int> probs){
vector<int> degreeSeq = getDegreeSequenceVector(inputGraph);
bool directed = inputGraph.isDirected();
Graph randomGraph(directed);
// generate randomized list of vertices
// the vertexList is a set where each node is represented by a number
// of elements equal to that vertex's degree
vector <int> vertexList;
for (int vertex = 0; vertex<inputGraph.getSize();++vertex){
randomGraph.addVertex();
for (int degree=0; degree<degreeSeq[vertex];++degree)
vertexList.push_back(vertex);
}
random_shuffle(vertexList.begin(), vertexList.end());
// create edges
int u,v;
vector<int>::iterator it=probs.begin();
for (int c=0; vertexList.size()>1;++c){
u = *(it++);
// make sure v != u
v=*(it++);
while (v==u){
v=*(it++);
}
//swap if u>v
if (u>v){
int temp = u;
u=v;
v=temp;
}
int edgeVertexV = vertexList[v];
int edgeVertexU = vertexList[u];
vertexList.erase(vertexList.begin()+ v);
vertexList.erase(vertexList.begin() +u);
if (edgeVertexV == edgeVertexU) continue; // avoid self-edge
randomGraph.addEdge(edgeVertexU, edgeVertexV);
}
return randomGraph;
}
/**
* Generates a degree sequence vector for a given Graph
* @param inputGraph the Graph from which to derive the degree sequence
* vector
* @return a List representing the degree sequence vector
*/
vector<int> RandomGraphGenerator::getDegreeSequenceVector(Graph& inputGraph) {
vector <int> degreeSequenceVector;
for (int currentVertex = 0; currentVertex < inputGraph.getSize(); ++currentVertex) {
int degree = inputGraph.getAdjacencyList(currentVertex).size();
degreeSequenceVector.push_back(degree);
}
return degreeSequenceVector;
}