Skip to content

Instantly share code, notes, and snippets.

@schani
Created July 12, 2010 19:48
Show Gist options
  • Select an option

  • Save schani/472970 to your computer and use it in GitHub Desktop.

Select an option

Save schani/472970 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
public class Node {
Node n1, n2, n3, n4;
public void connect (Node _n1, Node _n2, Node _n3, Node _n4) {
n1 = _n1;
n2 = _n2;
n3 = _n3;
n4 = _n4;
}
public int countNodes () {
Queue<Node> q = new Queue<Node> ();
HashSet<Node> s = new HashSet<Node> ();
/*
q.Enqueue (this);
while (q.Count > 0) {
Node n = q.Dequeue ();
if (s.Contains (n))
continue;
s.Add (n);
q.Enqueue (n.n1);
q.Enqueue (n.n2);
q.Enqueue (n.n3);
q.Enqueue (n.n4);
}
*/
return s.Count;
}
public static Node randomGraph (int size, Random r) {
Node[] nodes = new Node [size];
for (int i = 0; i < size; ++i)
nodes [i] = new Node ();
for (int i = 0; i < size; ++i)
nodes [i].connect (nodes [r.Next (size)],
nodes [r.Next (size)],
nodes [r.Next (size)],
nodes [r.Next (size)]);
return nodes [0];
}
public static int Main () {
Random r = new Random (31415);
Node g = randomGraph (4000000, r);
Console.WriteLine ("long graph constructed");
for (int i = 0; i < 30; ++i)
randomGraph (500000, r);
Console.WriteLine ("done");
Console.WriteLine ("nodes: " + g.countNodes ());
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment