Skip to content

Instantly share code, notes, and snippets.

@bingyuanng
Created January 12, 2016 10:16
Show Gist options
  • Save bingyuanng/1bcb267238d46dd90f22 to your computer and use it in GitHub Desktop.
Save bingyuanng/1bcb267238d46dd90f22 to your computer and use it in GitHub Desktop.
#include <random>
using namespace std;
#define INF 999
struct Edge {
Edge(int to,int cost): to(to), cost(cost) {};
int to, cost;
};
vector<vector<int> > generateAdjMatrix(int vertex_count, int edge_count) {
vector<vector<int> > graph(vertex_count, vector<int>(vertex_count, INF));
uniform_int_distribution<int> distr(0, vertex_count - 1);
std::random_device rand_dev;
std::mt19937 generator(rand_dev());
for (int i = 0; i <= edge_count; i++) {
int u = distr(generator);
int v = distr(generator);
int weight = rand() % 10;
if (u != v && graph[v][u] == INF && graph[u][v] == INF) {
graph[u][v] = weight;
}else {
// cout << u << " " << v << endl;
i--;
}
}
for (int i = 0; i < vertex_count; i++) {
graph[i][i] = 0;
}
return graph;
}
vector<vector<Edge> > generateAdjList(vector<vector<int> > adjMatrix) {
long vertex_count = adjMatrix.size();
vector<vector<Edge> > graphList(vertex_count, vector<Edge>());
for (int i = 0; i < vertex_count; i++) {
for (int j = 0; j < vertex_count; j++) {
graphList[i].push_back(Edge(j, adjMatrix[i][j]));
}
}
return graphList;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment