public void GraphWithSelfEdges(
            [UsingLinear(2, 5)] int i,
            [UsingLinear(0, 25)] int j
            )
        {
            if (i == 0 && j == 0)
            {
                return;
            }

            Random rnd = new Random();

            var next = new IntFactory();

            g = new AdjacencyGraph <int, Edge <int> >(true);
            RandomGraphFactory.Create <int, Edge <int> >(g,
                                                         next.Next,
                                                         (s, t) => new Edge <int>(s, t),
                                                         rnd, i, j, true);

            foreach (var sv in g.Vertices)
            {
                this.sourceVertex = sv;
                RunBfs();
            }
        }
Beispiel #2
0
        public void GraphWithSelfEdges(
            [UsingLinear(2, 9)] int i,
            [UsingLinear(0, 10)] int j
            )
        {
            if (i == 0 && j == 0)
            {
                return;
            }

            Random rnd = new Random();

            g = new AdjacencyGraph <int, Edge <int> >(true);
            RandomGraphFactory.Create <int, Edge <int> >(g,
                                                         new IntVertexFactory(),
                                                         FactoryCompiler.GetEdgeFactory <int, Edge <int> >(),
                                                         rnd, i, j, true);

            algo = new BreadthFirstSearchAlgorithm <int, Edge <int> >(g);
            try
            {
                algo.InitializeVertex += new VertexEventHandler <int>(this.InitializeVertex);
                algo.DiscoverVertex   += new VertexEventHandler <int>(this.DiscoverVertex);
                algo.ExamineEdge      += new EdgeEventHandler <int, Edge <int> >(this.ExamineEdge);
                algo.ExamineVertex    += new VertexEventHandler <int>(this.ExamineVertex);
                algo.TreeEdge         += new EdgeEventHandler <int, Edge <int> >(this.TreeEdge);
                algo.NonTreeEdge      += new EdgeEventHandler <int, Edge <int> >(this.NonTreeEdge);
                algo.GrayTarget       += new EdgeEventHandler <int, Edge <int> >(this.GrayTarget);
                algo.BlackTarget      += new EdgeEventHandler <int, Edge <int> >(this.BlackTarget);
                algo.FinishVertex     += new VertexEventHandler <int>(this.FinishVertex);

                parents.Clear();
                distances.Clear();
                currentDistance = 0;
                sourceVertex    = RandomGraphFactory.GetVertex(g, rnd);

                foreach (int v in g.Vertices)
                {
                    distances[v] = int.MaxValue;
                    parents[v]   = v;
                }
                distances[sourceVertex] = 0;
                algo.Compute(sourceVertex);

                CheckBfs();
            }
            finally
            {
                algo.InitializeVertex -= new VertexEventHandler <int>(this.InitializeVertex);
                algo.DiscoverVertex   -= new VertexEventHandler <int>(this.DiscoverVertex);
                algo.ExamineEdge      -= new EdgeEventHandler <int, Edge <int> >(this.ExamineEdge);
                algo.ExamineVertex    -= new VertexEventHandler <int>(this.ExamineVertex);
                algo.TreeEdge         -= new EdgeEventHandler <int, Edge <int> >(this.TreeEdge);
                algo.NonTreeEdge      -= new EdgeEventHandler <int, Edge <int> >(this.NonTreeEdge);
                algo.GrayTarget       -= new EdgeEventHandler <int, Edge <int> >(this.GrayTarget);
                algo.BlackTarget      -= new EdgeEventHandler <int, Edge <int> >(this.BlackTarget);
                algo.FinishVertex     -= new VertexEventHandler <int>(this.FinishVertex);
            }
        }
        public static BidirectionalGraph <int, IEdge <int> > QuickGraphRandomGraph(int vertexCount = 200, int edgeCount = 400)
        {
            var graph = new BidirectionalGraph <int, IEdge <int> >();

            int currentVertexIndex = 0;

            RandomGraphFactory.Create(
                g: graph,
                vertexFactory: () => currentVertexIndex++,
                edgeFactory: (v1, v2) => new Edge <int>(v1, v2),
                rnd: new Random(),
                vertexCount: vertexCount,
                edgeCount: edgeCount,
                selfEdges: false);

            return(graph);
        }
        public void GraphWithSelfEdgesBig()
        {
            TestConsole.WriteLine("processors: {0}", TaskManager.Current.Policy.IdealProcessors);
            Random rnd = new Random();

            g = new AdjacencyGraph <int, Edge <int> >(true);
            var next = new IntFactory();

            RandomGraphFactory.Create <int, Edge <int> >(g,
                                                         next.Next,
                                                         (s, t) => new Edge <int>(s, t),
                                                         rnd, 5000, 20000, false);

            var sv = g.Vertices.FirstOrDefault();

            this.sourceVertex = sv;
            RunBfs();
        }
Beispiel #5
0
        public void Create_Undirected_Throws()
        {
            var graph  = new UndirectedGraph <int, Edge <int> >();
            var random = new Random();

            // ReSharper disable AssignNullToNotNullAttribute
            Assert.Throws <ArgumentNullException>(
                () => RandomGraphFactory.Create(
                    (IMutableUndirectedGraph <int, Edge <int> >)null,
                    () => 1,
                    (source, target) => new Edge <int>(source, target),
                    random,
                    1, 1, false));
            Assert.Throws <ArgumentNullException>(
                () => RandomGraphFactory.Create(
                    graph,
                    null,
                    (source, target) => new Edge <int>(source, target),
                    random,
                    1, 1, false));
            Assert.Throws <ArgumentNullException>(
                () => RandomGraphFactory.Create(
                    graph,
                    () => 1,
                    null,
                    random,
                    1, 1, false));
            Assert.Throws <ArgumentNullException>(
                () => RandomGraphFactory.Create(
                    graph,
                    () => 1,
                    (source, target) => new Edge <int>(source, target),
                    null,
                    1, 1, false));
            Assert.Throws <ArgumentNullException>(
                () => RandomGraphFactory.Create(
                    (IMutableUndirectedGraph <int, Edge <int> >)null,
                    null,
                    (source, target) => new Edge <int>(source, target),
                    random,
                    1, 1, false));
            Assert.Throws <ArgumentNullException>(
                () => RandomGraphFactory.Create(
                    (IMutableUndirectedGraph <int, Edge <int> >)null,
                    () => 1,
                    null,
                    random,
                    1, 1, false));
            Assert.Throws <ArgumentNullException>(
                () => RandomGraphFactory.Create(
                    (IMutableUndirectedGraph <int, Edge <int> >)null,
                    () => 1,
                    (source, target) => new Edge <int>(source, target),
                    null,
                    1, 1, false));
            Assert.Throws <ArgumentNullException>(
                () => RandomGraphFactory.Create(
                    graph,
                    null,
                    null,
                    random,
                    1, 1, false));
            Assert.Throws <ArgumentNullException>(
                () => RandomGraphFactory.Create(
                    graph,
                    null,
                    (source, target) => new Edge <int>(source, target),
                    null,
                    1, 1, false));
            Assert.Throws <ArgumentNullException>(
                () => RandomGraphFactory.Create(
                    graph,
                    () => 1,
                    null,
                    null,
                    1, 1, false));
            Assert.Throws <ArgumentNullException>(
                () => RandomGraphFactory.Create(
                    graph,
                    null,
                    null,
                    null,
                    1, 1, false));
            Assert.Throws <ArgumentNullException>(
                () => RandomGraphFactory.Create(
                    (IMutableUndirectedGraph <int, Edge <int> >)null,
                    () => 1,
                    null,
                    null,
                    1, 1, false));
            Assert.Throws <ArgumentNullException>(
                () => RandomGraphFactory.Create(
                    (IMutableUndirectedGraph <int, Edge <int> >)null,
                    null,
                    (source, target) => new Edge <int>(source, target),
                    null,
                    1, 1, false));
            Assert.Throws <ArgumentNullException>(
                () => RandomGraphFactory.Create(
                    (IMutableUndirectedGraph <int, Edge <int> >)null,
                    null,
                    null,
                    random,
                    1, 1, false));
            Assert.Throws <ArgumentNullException>(
                () => RandomGraphFactory.Create(
                    (IMutableUndirectedGraph <int, Edge <int> >)null,
                    null,
                    null,
                    null,
                    1, 1, false));
            // ReSharper restore AssignNullToNotNullAttribute
            Assert.Throws <ArgumentOutOfRangeException>(
                () => RandomGraphFactory.Create(
                    graph,
                    () => 1,
                    (source, target) => new Edge <int>(source, target),
                    random,
                    -1, 1, false));
            Assert.Throws <ArgumentOutOfRangeException>(
                () => RandomGraphFactory.Create(
                    graph,
                    () => 1,
                    (source, target) => new Edge <int>(source, target),
                    random,
                    0, 1, false));
            Assert.Throws <ArgumentOutOfRangeException>(
                () => RandomGraphFactory.Create(
                    graph,
                    () => 1,
                    (source, target) => new Edge <int>(source, target),
                    random,
                    1, -1, false));
            Assert.Throws <ArgumentOutOfRangeException>(
                () => RandomGraphFactory.Create(
                    graph,
                    () => 1,
                    (source, target) => new Edge <int>(source, target),
                    random,
                    1, 0, false));
            Assert.Throws <ArgumentOutOfRangeException>(
                () => RandomGraphFactory.Create(
                    graph,
                    () => 1,
                    (source, target) => new Edge <int>(source, target),
                    random,
                    0, 0, false));
            Assert.Throws <ArgumentOutOfRangeException>(
                () => RandomGraphFactory.Create(
                    graph,
                    () => 1,
                    (source, target) => new Edge <int>(source, target),
                    random,
                    -1, -1, false));
        }
Beispiel #6
0
        public void Create_Undirected()
        {
            var graph = new UndirectedGraph <int, EquatableEdge <int> >();

            // With self edge
            int v = 0;

            RandomGraphFactory.Create(
                graph,
                () => ++ v,
                (source, target) => new EquatableEdge <int>(source, target),
                new Random(123456),
                5,
                10,
                true);
            AssertHasVertices(graph, new[] { 1, 2, 3, 4, 5 });
            AssertHasEdges(
                graph,
                new[]
            {
                new EquatableEdge <int>(1, 2),
                new EquatableEdge <int>(1, 2),
                new EquatableEdge <int>(1, 2),
                new EquatableEdge <int>(2, 1),
                new EquatableEdge <int>(3, 5),
                new EquatableEdge <int>(3, 1),
                new EquatableEdge <int>(4, 5),
                new EquatableEdge <int>(4, 4),
                new EquatableEdge <int>(4, 1),
                new EquatableEdge <int>(5, 3)
            });

            // Without self edge
            graph.Clear();
            v = 0;
            RandomGraphFactory.Create(
                graph,
                () => ++ v,
                (source, target) => new EquatableEdge <int>(source, target),
                new Random(123456),
                5,
                10,
                false);
            AssertHasVertices(graph, new[] { 1, 2, 3, 4, 5 });
            AssertHasEdges(
                graph,
                new[]
            {
                new EquatableEdge <int>(1, 2),
                new EquatableEdge <int>(1, 2),
                new EquatableEdge <int>(1, 2),
                new EquatableEdge <int>(2, 1),
                new EquatableEdge <int>(3, 5),
                new EquatableEdge <int>(3, 1),
                new EquatableEdge <int>(3, 1),
                new EquatableEdge <int>(4, 5),
                new EquatableEdge <int>(4, 1),
                new EquatableEdge <int>(5, 3)
            });

            // Different seed change generated graph
            graph.Clear();
            v = 0;
            RandomGraphFactory.Create(
                graph,
                () => ++ v,
                (source, target) => new EquatableEdge <int>(source, target),
                new Random(456789),
                5,
                10,
                true);
            AssertHasVertices(graph, new[] { 1, 2, 3, 4, 5 });
            AssertHasEdges(
                graph,
                new[]
            {
                new EquatableEdge <int>(1, 2),
                new EquatableEdge <int>(2, 2),
                new EquatableEdge <int>(2, 5),
                new EquatableEdge <int>(3, 4),
                new EquatableEdge <int>(3, 2),
                new EquatableEdge <int>(4, 5),
                new EquatableEdge <int>(4, 2),
                new EquatableEdge <int>(4, 2),
                new EquatableEdge <int>(5, 2),
                new EquatableEdge <int>(5, 3)
            });

            // On non empty graph, keep existing stuff
            graph.Clear();
            graph.AddVerticesAndEdge(new EquatableEdge <int>(6, 7));
            v = 0;
            RandomGraphFactory.Create(
                graph,
                () => ++ v,
                (source, target) => new EquatableEdge <int>(source, target),
                new Random(123456),
                5,
                10,
                true);
            AssertHasVertices(graph, new[] { 1, 2, 3, 4, 5, 6, 7 });
            AssertHasEdges(
                graph,
                new[]
            {
                new EquatableEdge <int>(6, 7),
                new EquatableEdge <int>(1, 2),
                new EquatableEdge <int>(1, 2),
                new EquatableEdge <int>(1, 2),
                new EquatableEdge <int>(2, 1),
                new EquatableEdge <int>(3, 5),
                new EquatableEdge <int>(3, 1),
                new EquatableEdge <int>(4, 5),
                new EquatableEdge <int>(4, 4),
                new EquatableEdge <int>(4, 1),
                new EquatableEdge <int>(5, 3)
            });
        }
        public void Create_Throws()
        {
            var graph  = new AdjacencyGraph <int, Edge <int> >();
            var random = new QuikGraph.Utils.CryptoRandom();

            // ReSharper disable AssignNullToNotNullAttribute
            Assert.Throws <ArgumentNullException>(
                () => RandomGraphFactory.Create(
                    (IMutableVertexAndEdgeListGraph <int, Edge <int> >)null,
                    () => 1,
                    (source, target) => new Edge <int>(source, target),
                    random,
                    1, 1, false));
            Assert.Throws <ArgumentNullException>(
                () => RandomGraphFactory.Create(
                    graph,
                    null,
                    (source, target) => new Edge <int>(source, target),
                    random,
                    1, 1, false));
            Assert.Throws <ArgumentNullException>(
                () => RandomGraphFactory.Create(
                    graph,
                    () => 1,
                    null,
                    random,
                    1, 1, false));
            Assert.Throws <ArgumentNullException>(
                () => RandomGraphFactory.Create(
                    graph,
                    () => 1,
                    (source, target) => new Edge <int>(source, target),
                    null,
                    1, 1, false));
            Assert.Throws <ArgumentNullException>(
                () => RandomGraphFactory.Create(
                    (IMutableVertexAndEdgeListGraph <int, Edge <int> >)null,
                    null,
                    (source, target) => new Edge <int>(source, target),
                    random,
                    1, 1, false));
            Assert.Throws <ArgumentNullException>(
                () => RandomGraphFactory.Create(
                    (IMutableVertexAndEdgeListGraph <int, Edge <int> >)null,
                    () => 1,
                    null,
                    random,
                    1, 1, false));
            Assert.Throws <ArgumentNullException>(
                () => RandomGraphFactory.Create(
                    (IMutableVertexAndEdgeListGraph <int, Edge <int> >)null,
                    () => 1,
                    (source, target) => new Edge <int>(source, target),
                    null,
                    1, 1, false));
            Assert.Throws <ArgumentNullException>(
                () => RandomGraphFactory.Create(
                    graph,
                    null,
                    null,
                    random,
                    1, 1, false));
            Assert.Throws <ArgumentNullException>(
                () => RandomGraphFactory.Create(
                    graph,
                    null,
                    (source, target) => new Edge <int>(source, target),
                    null,
                    1, 1, false));
            Assert.Throws <ArgumentNullException>(
                () => RandomGraphFactory.Create(
                    graph,
                    () => 1,
                    null,
                    null,
                    1, 1, false));
            Assert.Throws <ArgumentNullException>(
                () => RandomGraphFactory.Create(
                    graph,
                    null,
                    null,
                    null,
                    1, 1, false));
            Assert.Throws <ArgumentNullException>(
                () => RandomGraphFactory.Create(
                    (IMutableVertexAndEdgeListGraph <int, Edge <int> >)null,
                    () => 1,
                    null,
                    null,
                    1, 1, false));
            Assert.Throws <ArgumentNullException>(
                () => RandomGraphFactory.Create(
                    (IMutableVertexAndEdgeListGraph <int, Edge <int> >)null,
                    null,
                    (source, target) => new Edge <int>(source, target),
                    null,
                    1, 1, false));
            Assert.Throws <ArgumentNullException>(
                () => RandomGraphFactory.Create(
                    (IMutableVertexAndEdgeListGraph <int, Edge <int> >)null,
                    null,
                    null,
                    random,
                    1, 1, false));
            Assert.Throws <ArgumentNullException>(
                () => RandomGraphFactory.Create(
                    (IMutableVertexAndEdgeListGraph <int, Edge <int> >)null,
                    null,
                    null,
                    null,
                    1, 1, false));
            // ReSharper restore AssignNullToNotNullAttribute
            Assert.Throws <ArgumentOutOfRangeException>(
                () => RandomGraphFactory.Create(
                    graph,
                    () => 1,
                    (source, target) => new Edge <int>(source, target),
                    random,
                    -1, 1, false));
            Assert.Throws <ArgumentOutOfRangeException>(
                () => RandomGraphFactory.Create(
                    graph,
                    () => 1,
                    (source, target) => new Edge <int>(source, target),
                    random,
                    0, 1, false));
            Assert.Throws <ArgumentOutOfRangeException>(
                () => RandomGraphFactory.Create(
                    graph,
                    () => 1,
                    (source, target) => new Edge <int>(source, target),
                    random,
                    1, -1, false));
            Assert.Throws <ArgumentOutOfRangeException>(
                () => RandomGraphFactory.Create(
                    graph,
                    () => 1,
                    (source, target) => new Edge <int>(source, target),
                    random,
                    1, 0, false));
            Assert.Throws <ArgumentOutOfRangeException>(
                () => RandomGraphFactory.Create(
                    graph,
                    () => 1,
                    (source, target) => new Edge <int>(source, target),
                    random,
                    0, 0, false));
            Assert.Throws <ArgumentOutOfRangeException>(
                () => RandomGraphFactory.Create(
                    graph,
                    () => 1,
                    (source, target) => new Edge <int>(source, target),
                    random,
                    -1, -1, false));
        }