public List<Graph> ConnectedComponents() { List<Graph> ret = new List<Graph>(); HashSet<int> visited = new HashSet<int>(); Queue<int> queue = new Queue<int>(); ForEachVertex((v) => { if (!visited.Contains(v)) { Graph g = new Graph(); queue.Enqueue(v); visited.Add(v); while (queue.Count != 0) { int u = queue.Dequeue(); g.storage[u] = storage[u]; ForEachNeighbor(u, (w) => { if (!visited.Contains(w)) { queue.Enqueue(w); visited.Add(w); } }); } ret.Add(g); } }); return ret; }
public static Graph GenerateErdosRenyi(int n, int m) { Random r = new Random(); Graph g = new Graph(); for (int i = 0; i < m; ++i) { g.AddEdge(r.Next(n), r.Next(n)); } return g; }
static void Test(string record) { Graph g = new Graph(); g.Load(record + ".txt"); Console.WriteLine("Splitting to connected components..."); var list = g.ConnectedComponents(); Console.WriteLine(list.Count); Console.WriteLine(list[0].NodeCount); var ST = list[0].SplitDecomposition(); //ST.Debug(false); var gPrime = ST.GenerateAccessabilityGraph(); Console.WriteLine(list[0].SameAs(gPrime)); }
//========================================================================================================== public Graph GenerateAccessabilityGraph() { var G = new Graph(); foreach (var kvp in LeafMapper) { var leaf = kvp.Value; var list = leaf.GetAccessableLeaves(); foreach (var l in list) { G.AddEdge(leaf.id, l.id); } } return G; }
public bool SameAs(Graph g) { if (g.storage.Count != storage.Count) return false; foreach (var t in storage) { HashSet<int> correspondingSet = null; if (!g.storage.TryGetValue(t.Key, out correspondingSet)) return false; if (t.Value.Count != correspondingSet.Count) return false; foreach (var n in t.Value) if (!correspondingSet.Contains(n)) return false; } return true; }