/// <summary> /// Adds edgeCount number of edges to network. AllowMultiEdges and AllowSelfLoops will affect /// how edges are added. /// </summary> public static void CreateEdges(INetworkAdjList network, int edgeCount) { Random rand = new Random(DateTime.Now.Millisecond); int srcNodeIndex = -1; int destNodeIndex = -1; INode srcNode = null; INode destNode = null; for (int i = 0; i < edgeCount; i++) { srcNodeIndex = rand.Next(network.NodeCount); destNodeIndex = rand.Next(network.NodeCount); srcNode = network.Nodes[srcNodeIndex]; destNode = network.Nodes[destNodeIndex]; if (!AllowMultiEdges && AllowSelfLoops) { if (network.IsEdge(srcNode, destNode)) continue; else network.CreateEdge(srcNode, destNode); } else if (AllowMultiEdges && !AllowSelfLoops) { if (srcNode == destNode) continue; else network.CreateEdge(srcNode, destNode); } else network.CreateEdge(srcNode, destNode); } }
public void GarunteeEdgeCount(int edgeCount, INetworkAdjList network) { Random rand = new Random(); while (network.EdgeCount < edgeCount) { int index0 = rand.Next(network.EdgeCount); int index1 = rand.Next(network.EdgeCount); INode node0 = network.Nodes[index0]; INode node1 = network.Nodes[index1]; network.CreateEdge(node0, node1); } }
/// <summary> /// Ensures a minimum number of self loops exist in the network. /// </summary> public static void EnsureMinSelfLoopCount(INetworkAdjList network, int n) { int ctr = 0; foreach (IEdge edge in network.EdgeEnumerator) { if (edge.IsSelfLoop) ctr++; } // if that many self loops exist, return if (ctr>=n) return; // else create the needed number of self loops int needed = n; if (n<ctr) needed = ctr-n; ctr=0; // re-use ctr variable do { foreach (INode node in network.NodeEnumerator) { network.CreateEdge(node, node); ctr++; if (ctr>=needed) break; } } while (needed>ctr); }