private void EnumerateEdgesCore(Graph graph, bool multipleSource) { // Arrange byte[] mapBackingStore = ArrayPool <byte> .Shared.Rent(Math.Max(graph.VertexCount, 1)); Array.Clear(mapBackingStore, 0, mapBackingStore.Length); IndexedColorDictionary eagerColorMap = new(mapBackingStore); byte[] setBackingStore = ArrayPool <byte> .Shared.Rent(Math.Max(graph.VertexCount, 1)); Array.Clear(setBackingStore, 0, setBackingStore.Length); IndexedSet set = new(setBackingStore); using Rist <int> eagerSteps = new(graph.VertexCount); using Rist <int> enumerableSteps = new(graph.VertexCount); DfsHandler <Graph, int, int> dfsHandler = CreateDfsHandler(eagerSteps); // Act if (multipleSource) { if (graph.VertexCount < 3) { return; } int sourceCount = graph.VertexCount / 3; IndexEnumerator sources = new(sourceCount); EagerDfs.Traverse(graph, sources, eagerColorMap, dfsHandler); using IEnumerator <int> edges = EnumerableDfs.EnumerateEdges(graph, sources, set); enumerableSteps.AddEnumerator(edges); } else { int source = graph.VertexCount >> 1; EagerDfs.Traverse(graph, source, eagerColorMap, dfsHandler); using IEnumerator <int> edges = EnumerableDfs.EnumerateEdges(graph, source, set); enumerableSteps.AddEnumerator(edges); } // Assert int eagerStepCount = eagerSteps.Count; int enumerableStepCount = enumerableSteps.Count; Assert.Equal(eagerStepCount, enumerableStepCount); int count = eagerStepCount; for (int i = 0; i < count; ++i) { int eagerStep = eagerSteps[i]; int enumerableStep = enumerableSteps[i]; if (eagerStep == enumerableStep) { continue; } Assert.Equal(eagerStep, enumerableStep); } // Cleanup ArrayPool <byte> .Shared.Return(mapBackingStore); ArrayPool <byte> .Shared.Return(setBackingStore); }
private static void Main() { IndexedIncidenceGraph.Builder builder = new(0, 31); using (TextReader textReader = IndexedGraphs.GetTextReader("09")) { IEnumerable <Endpoints> edges = IndexedEdgeListParser.ParseEdges(textReader); foreach (Endpoints edge in edges) { builder.Add(edge.Tail, edge.Head); } } IndexedIncidenceGraph graph = builder.ToGraph(); Console.Write($"{nameof(graph.VertexCount)}: {graph.VertexCount.ToString(F)}"); Console.WriteLine($", {nameof(graph.EdgeCount)}: {graph.EdgeCount.ToString(F)}"); TextWriter w = Console.Out; EagerDfs <IndexedIncidenceGraph, int, int, ArraySegment <int> .Enumerator> dfs = default; w.WriteLine($"digraph \"{dfs.GetType().Name}\" {{"); w.WriteLine(" node [shape=circle style=dashed fontname=\"Times-Italic\"]"); // Enumerate vertices. for (int v = 0; v < graph.VertexCount; ++v) { w.Write(v == 0 ? " " : " "); w.Write(V(v)); } w.WriteLine(); IndexEnumerator sources = new(2); byte[] backingStore = ArrayPool <byte> .Shared.Rent(graph.VertexCount); Array.Clear(backingStore, 0, backingStore.Length); IndexedColorDictionary colorMap = new(backingStore); HashSet <int> examinedEdges = new(graph.EdgeCount); DfsHandler <IndexedIncidenceGraph, int, int> handler = CreateHandler(w, examinedEdges); dfs.Traverse(graph, sources, colorMap, handler); ArrayPool <byte> .Shared.Return(backingStore); // Enumerate sources. w.WriteLine(); sources.Reset(); while (sources.MoveNext()) { int v = sources.Current; w.WriteLine($" {V(v)} [style=filled]"); } // Enumerate rest of edges. w.WriteLine(); for (int v = 0; v < graph.VertexCount; ++v) { ArraySegment <int> .Enumerator outEdges = graph.EnumerateOutEdges(v); while (outEdges.MoveNext()) { int e = outEdges.Current; if (examinedEdges.Contains(e)) { continue; } w.WriteLine($" {E(graph, e)} [label={e} style=dotted]"); } } w.WriteLine("}"); }