Skip to content

Instantly share code, notes, and snippets.

@MartinThoma
Created June 17, 2012 10:04
Show Gist options
  • Save MartinThoma/2944092 to your computer and use it in GitHub Desktop.
Save MartinThoma/2944092 to your computer and use it in GitHub Desktop.
Create a random graph
#include <iostream>
#include <vector>
#include <iterator>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#define DIRECTED true
#define MULTIPLE_EDGES false
#define MAX_NODES 100
#define MAX_EDGES 100000
#define CONNECTED true
#define MAX_CAPACITY 1000
#define LOOPS_ALLOWED false
struct Edge{
Edge(){}
Edge(int from, int to):from(from),to(to){uniqueEdgeGen();}
int from,to;
unsigned long long uniqueEdge;
void uniqueEdgeGen() {
int a, b;
if (!DIRECTED) {
int tmp = std::max(from, to);
a = std::min(from, to);
b = tmp;
} else {
a = from;
b = to;
}
uniqueEdge = a + b * MAX_NODES;
}
};
using namespace std;
bool operator<(const Edge& left, const Edge& right)
{
return left.uniqueEdge < right.uniqueEdge;
}
int main(){
/* initialize random seed: */
srand ( time(NULL) );
/* generate basic graph values */
unsigned int nodes = rand() % MAX_NODES;
unsigned int edges = rand() % MAX_EDGES;
while (!MULTIPLE_EDGES && (edges > (nodes*nodes + nodes) / 2)) {
edges = rand() % MAX_EDGES;
}
cout << nodes << " " << edges << endl;
if (CONNECTED && DIRECTED) {
if(!MULTIPLE_EDGES) {
set<Edge> edgeList;
set<int> visitedSet;
vector<int> visitedVector;
visitedSet.insert(0);
visitedVector.push_back(0);
while(edgeList.size() < edges) {
int from = visitedVector[rand() % visitedVector.size()];
int to = rand() % nodes;
while (!LOOPS_ALLOWED && from == to) {
to = rand() % nodes;
}
Edge e = Edge(from, to);
bool is_in = visitedSet.find(to) != visitedSet.end();
if(!is_in) {
visitedVector.push_back(to);
visitedSet.insert(to);
}
edgeList.insert(e);
}
/* print them */
set<Edge>::iterator myIt;
for(myIt=edgeList.begin(); myIt != edgeList.end(); myIt++){
cout << (*myIt).to << " " << (*myIt).from;
if (MAX_CAPACITY > 0) {
cout << " " << rand() % MAX_CAPACITY;
}
cout << endl;
}
}
} else if (DIRECTED) {
/* generate edges */
if(MULTIPLE_EDGES) {
vector<Edge> edgeList;
for (unsigned int j = 0; j < edges; j++) {
int from = rand() % nodes;
int to = rand() % nodes;
edgeList.push_back(Edge(from, to));
}
/* print them */
vector<Edge>::iterator myIt;
for(myIt=edgeList.begin(); myIt != edgeList.end(); myIt++){
cout << (*myIt).from << " " << (*myIt).to;
if (MAX_CAPACITY > 0) {
cout << " " << rand() % MAX_CAPACITY;
}
cout << endl;
}
} else {
set<Edge> edgeList;
while(edgeList.size() < edges) {
int from = rand() % nodes;
int to = rand() % nodes;
Edge e = Edge(from, to);
edgeList.insert(e);
}
/* print them */
set<Edge>::iterator myIt;
for(myIt=edgeList.begin(); myIt != edgeList.end(); myIt++){
cout << (*myIt).from << " " << (*myIt).to;
if (MAX_CAPACITY > 0) {
cout << " " << rand() % MAX_CAPACITY;
}
cout << endl;
}
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment