Skip to content

Commit

Permalink
initWithEdges for randomStretched
Browse files Browse the repository at this point in the history
  • Loading branch information
ifsmirnov committed Dec 24, 2017
1 parent 7b9f99f commit 670bd6a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 26 deletions.
25 changes: 12 additions & 13 deletions impl/graph_inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class GraphRandom {
checkLargeParameter(n * n);
return BuilderProxy(Traits(n), [](Traits t) {
Graph g;
g.setN(t.n);
if (t.directed) {
g.directed_ = true;
}
Expand Down Expand Up @@ -200,10 +201,6 @@ class GraphRandom {
Array parents = tree.parents(0);
parents[0] = 0;

Graph graph(t.n);
if (t.directed) {
graph.directed_ = true;
}

auto treeEdges = tree.edges();
if (t.directed && !t.acyclic) {
Expand All @@ -214,11 +211,10 @@ class GraphRandom {
}
}

for (const auto& edge: treeEdges) {
graph.addEdge(edge.first, edge.second);
}
Arrayp edges = treeEdges;
edges.reserve(t.m);

std::set<std::pair<int, int>> usedEdges(
std::unordered_set<std::pair<int, int>> usedEdges(
treeEdges.begin(), treeEdges.end());

auto edgeIsGood = [&usedEdges, t](std::pair<int, int> edge) {
Expand All @@ -233,8 +229,7 @@ class GraphRandom {
return true;
};

// TODO: add initWithEdges here and to other generators
while (graph.m() != t.m) {
while (static_cast<int>(edges.size()) != t.m) {
int u = rnd.next(t.n);
int up = rnd.next(0, spread);
int v = u;
Expand All @@ -256,11 +251,15 @@ class GraphRandom {
std::swap(u, v);
}

graph.addEdge(v, u);
usedEdges.emplace(v, u);
edges.emplace_back(v, u);
}

Graph graph;
if (t.directed) {
graph.directed_ = true;
}

graph.normalizeEdges();
graph.initWithEdges(t.n, edges);
return graph;
}

Expand Down
25 changes: 12 additions & 13 deletions jngen.h
Original file line number Diff line number Diff line change
Expand Up @@ -6405,6 +6405,7 @@ class GraphRandom {
checkLargeParameter(n * n);
return BuilderProxy(Traits(n), [](Traits t) {
Graph g;
g.setN(t.n);
if (t.directed) {
g.directed_ = true;
}
Expand Down Expand Up @@ -6559,10 +6560,6 @@ class GraphRandom {
Array parents = tree.parents(0);
parents[0] = 0;

Graph graph(t.n);
if (t.directed) {
graph.directed_ = true;
}

auto treeEdges = tree.edges();
if (t.directed && !t.acyclic) {
Expand All @@ -6573,11 +6570,10 @@ class GraphRandom {
}
}

for (const auto& edge: treeEdges) {
graph.addEdge(edge.first, edge.second);
}
Arrayp edges = treeEdges;
edges.reserve(t.m);

std::set<std::pair<int, int>> usedEdges(
std::unordered_set<std::pair<int, int>> usedEdges(
treeEdges.begin(), treeEdges.end());

auto edgeIsGood = [&usedEdges, t](std::pair<int, int> edge) {
Expand All @@ -6592,8 +6588,7 @@ class GraphRandom {
return true;
};

// TODO: add initWithEdges here and to other generators
while (graph.m() != t.m) {
while (static_cast<int>(edges.size()) != t.m) {
int u = rnd.next(t.n);
int up = rnd.next(0, spread);
int v = u;
Expand All @@ -6615,11 +6610,15 @@ class GraphRandom {
std::swap(u, v);
}

graph.addEdge(v, u);
usedEdges.emplace(v, u);
edges.emplace_back(v, u);
}

Graph graph;
if (t.directed) {
graph.directed_ = true;
}

graph.normalizeEdges();
graph.initWithEdges(t.n, edges);
return graph;
}

Expand Down

0 comments on commit 670bd6a

Please sign in to comment.