Created
September 9, 2014 00:47
-
-
Save ngopal/94099d0168bb1bd387f1 to your computer and use it in GitHub Desktop.
A JS implementation of creating a Erdos-Renyi (random) network. Planning to add others.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getRandomInt(min, max) { | |
return Math.floor(Math.random() * (max - min + 1)) + min; | |
} | |
function erdosRenyi(nodesNum,directedness,prob) { | |
// create nodes list | |
var nodes = [] | |
for (var i = 0; i < nodesNum; i++) { | |
nodes.push(i); | |
} | |
// init edges | |
var edges = []; | |
for (var i = 0; i < nodes.length; i++) { | |
for (var j = i+1; j < nodes.length; j++) { | |
var pchance = Math.random() | |
if (pchance > prob) { | |
if (directedness === 0) { | |
edges.push({"source":i,"target":j,"directedness":0}); | |
} | |
else { | |
var dchance = Math.random(); | |
if (dchance > prob) { | |
edges.push({"source":i,"target":j,"directedness":1}) | |
edges.push({"source":j,"target":i,"directedness":1}) | |
} | |
else { | |
edges.push({"source":i,"target":j,"directedness":1}) | |
// need to fix - can't be i or j! | |
var rando = getRandomInt(0,nodes.length); | |
edges.push({"source":getRandomInt(0,nodes.length),"target":i,"directedness":1}) | |
} | |
} | |
} | |
} | |
} | |
return {"nodes":nodes.map(function(m) { return {"name":m.toString()};}), "edges":edges}; | |
} | |
erdosRenyi(40,1,0.4) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment