Example #1
0
        public void HasConnection()
        {
            // Arrange
            var myGraph = new MatrixGraph(GraphTypes.Directed, 15);
            {
                myGraph.AddEdge(0, 1);
                myGraph.AddEdge(0, 2);
                myGraph.AddEdge(1, 9);
                myGraph.AddEdge(1, 2);
                myGraph.AddEdge(1, 3);
                myGraph.AddEdge(1, 11);
                myGraph.AddEdge(11, 9);
                myGraph.AddEdge(2, 1);
                myGraph.AddEdge(3, 4);
                myGraph.AddEdge(3, 8);
                myGraph.AddEdge(4, 10);
                myGraph.AddEdge(4, 3);
                myGraph.AddEdge(4, 5);
                myGraph.AddEdge(10, 7);
                myGraph.AddEdge(10, 4);
                myGraph.AddEdge(7, 6);
                myGraph.AddEdge(6, 7);
                myGraph.AddEdge(5, 6);
                myGraph.AddEdge(5, 2);
                myGraph.AddEdge(8, 13);
                myGraph.AddEdge(13, 14);
            }

            var graphMatrix = myGraph.GetGraphRepresentation();

            // Practice
            bool HasRoute(int node1, int node2)
            {
                var keepGoing    = true;
                var vertices     = graphMatrix.GetLength(0);
                var seachVisited = new int[vertices];

                var bfsOrder1 = new Queue <int>(vertices);

                bfsOrder1.Enqueue(node1);

                var bfsOrder2 = new Queue <int>(vertices);

                bfsOrder2.Enqueue(node2);

                bool HasVisited(int node, int nodeIndex) =>
                (seachVisited[node] & (1 << nodeIndex)) != 0;

                while (keepGoing)
                {
                    keepGoing = bfsOrder1.TryDequeue(out int node);
                    if (keepGoing)
                    {
                        for (byte i = 0; i < vertices; i++)
                        {
                            if (graphMatrix[node, i] == 1 && !HasVisited(i, 1))
                            {
                                bfsOrder1.Enqueue(i);
                                seachVisited[i] = seachVisited[i] | 1 << 1;
                                if (HasVisited(i, 2))
                                {
                                    return(true);
                                }
                            }
                        }
                    }

                    keepGoing = bfsOrder2.TryDequeue(out node);
                    if (keepGoing)
                    {
                        for (byte i = 0; i < vertices; i++)
                        {
                            if (graphMatrix[node, i] == 1 && !HasVisited(i, 2))
                            {
                                bfsOrder2.Enqueue(i);
                                seachVisited[i] = seachVisited[i] | 1 << 2;
                                if (HasVisited(i, 1))
                                {
                                    return(true);
                                }
                            }
                        }
                    }
                }

                return(false);
            }

            Assert.True(HasRoute(2, 10));
            Assert.False(HasRoute(11, 8));
        }