c# - Randomly Connected Graph Generator -
i'm trying generate simple graph randomly connecting n nodes. i'm looking efficient algorithm so, this:
- input:
nnumber of nodesenumber of edgesn-1n(n-1)/2
- output: simple connected graph n vertices , e edges
create array of n nodes. fisher-yates shuffle array. scan array , create edge each pair of nodes adjacent in array. result connected graph n-1 edges.
if number of additional edges small, can randomly add edges till have enough. otherwise, create array of unused edges, fisher-yates shuffle array, , take first (e - (n-1)) edges array.
Comments
Post a Comment