Exemplo n.º 1
0
        private static void Main()
        {
            SimpleIncidenceGraph.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);
                }
            }

            SimpleIncidenceGraph 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;

            EnumerableDfs <SimpleIncidenceGraph, int, Endpoints, ArraySegment <Endpoints> .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();
Exemplo n.º 2
0
        internal void EnumerateVertices(GraphParameter <Graph> p)
        {
            SimpleIncidenceGraph graph = p.Graph;

            // Arrange

            int source = graph.VertexCount >> 1;

            byte[] setBackingStore = ArrayPool <byte> .Shared.Rent(Math.Max(graph.VertexCount, source + 1));

            Array.Clear(setBackingStore, 0, setBackingStore.Length);
            IndexedSet exploredSet = new(setBackingStore);

            // Act

            IEnumerator <int> basicSteps      = Dfs.EnumerateVertices(graph, source, graph.VertexCount) !;
            IEnumerator <int> enumerableSteps = EnumerableDfs.EnumerateVertices(graph, source, exploredSet) !;

            // Assert

            while (true)
            {
                bool expectedHasCurrent = enumerableSteps.MoveNext();
                bool actualHasCurrent   = basicSteps.MoveNext();

                Assert.Equal(expectedHasCurrent, actualHasCurrent);

                if (!expectedHasCurrent || !actualHasCurrent)
                {
                    break;
                }

                int expected = enumerableSteps.Current;
                int actual   = basicSteps.Current;

                if (expected != actual)
                {
                    Assert.Equal(expected, actual);
                    break;
                }
            }
        }
Exemplo n.º 3
0
        private static void Main()
        {
            SimpleIncidenceGraph.Builder builder = new();
            builder.Add(2, 0);
            builder.Add(4, 3);
            builder.Add(0, 4);
            builder.Add(3, 2);
            builder.Add(4, 4);
            builder.Add(0, 2);
            builder.Add(2, 4);
            SimpleIncidenceGraph graph = builder.ToGraph();

            EnumerableBfs <SimpleIncidenceGraph, Endpoints, ArraySegment <Endpoints> .Enumerator> bfs;

            IEnumerator <Endpoints> edges = bfs.EnumerateEdges(graph, source: 3, vertexCount: graph.VertexCount);

            while (edges.MoveNext())
            {
                Console.WriteLine(edges.Current);
            }
        }