Esempio n. 1
0
        public void BreadthFirstSearchThrowsExceptionIfIndexIsGreaterOrEqualToSize(Type generic_type_def)
        {
            Type         constructed_graph_type = ConstructGraphType <int>(generic_type_def);
            IGraph <int> graph = InstantiateGraph <int>(constructed_graph_type);

            graph.AddVertex(5);
            Assert.Throws <ArgumentException>(() => graph.BreadthFirstSearch(1).GetEnumerator().MoveNext());
        }
Esempio n. 2
0
        /// <summary>
        /// Получение компонент связности для неориентированного графа
        /// </summary>
        /// <param name="graph">Граф</param>
        /// <returns>список компонент</returns>
        public static IEnumerable <ConnectiveComponent> GetConnectiveComponents(this IGraph graph)
        {
            if (graph.DirectionType == EdgeDirectionType.Directed)
            {
                throw new InvalidOperationException("Current graph is directed");
            }

            var visited = new HashSet <int>();

            foreach (var vertex in graph.Vertices)
            {
                if (visited.Contains(vertex.Id))
                {
                    continue;
                }
                var vertices = graph.BreadthFirstSearch(vertex).ToList();
                var cluster  = new Collection <Vertex>();
                foreach (var found in vertices)
                {
                    if (visited.Contains(found.Id))
                    {
                        continue;
                    }

                    cluster.Add(found);
                    visited.Add(found.Id);
                }
                if (cluster.Any())
                {
                    yield return new ConnectiveComponent {
                               Vertices = cluster
                    }
                }
                ;
            }
        }
Esempio n. 3
0
        private static void DeepthFirstSearch(IGraph graph)
        {
            BuildGraph1(graph);

            Assert.ThrowsException <ArgumentException>(() => graph.DeepthFirstSearch(new Vertex {
                Id = -1
            }).ToList(), "unknown vertex");

            var pathFrom1 = graph.DeepthFirstSearch(new Vertex {
                Id = 1
            }).ToList();

            Assert.IsTrue(pathFrom1.Select(x => x.Id).Distinct().Count() == pathFrom1.Count, "unique vertex ids from 1");
            Assert.AreEqual(graph.Vertices.Count(), pathFrom1.Count, "all vertices found from 1");

            var i = 0;

            Assert.AreEqual(1, pathFrom1[i].Id, $"path elem number {i++} from 1");
            Assert.AreEqual(2, pathFrom1[i].Id, $"path elem number {i++} from 1");
            Assert.AreEqual(6, pathFrom1[i].Id, $"path elem number {i++} from 1");
            Assert.AreEqual(3, pathFrom1[i].Id, $"path elem number {i++} from 1");
            Assert.AreEqual(7, pathFrom1[i].Id, $"path elem number {i++} from 1");
            Assert.AreEqual(8, pathFrom1[i].Id, $"path elem number {i++} from 1");
            Assert.AreEqual(5, pathFrom1[i].Id, $"path elem number {i++} from 1");
            Assert.AreEqual(9, pathFrom1[i].Id, $"path elem number {i++} from 1");
            Assert.AreEqual(10, pathFrom1[i].Id, $"path elem number {i++} from 1");
            Assert.AreEqual(11, pathFrom1[i].Id, $"path elem number {i++} from 1");
            Assert.AreEqual(4, pathFrom1[i].Id, $"path elem number {i++} from 1");


            var pathFrom5 = graph.DeepthFirstSearch(new Vertex {
                Id = 5
            }).ToList();

            Assert.IsTrue(pathFrom5.Select(x => x.Id).Distinct().Count() == pathFrom5.Count, "unique vertex ids from 5");
            Assert.AreEqual(graph.Vertices.Count(), pathFrom5.Count, "all vertices found from 5");

            i = 0;
            Assert.AreEqual(5, pathFrom5[i].Id, $"path elem number {i++} from 5");
            Assert.AreEqual(1, pathFrom5[i].Id, $"path elem number {i++} from 5");
            Assert.AreEqual(2, pathFrom5[i].Id, $"path elem number {i++} from 5");
            Assert.AreEqual(6, pathFrom5[i].Id, $"path elem number {i++} from 5");
            Assert.AreEqual(3, pathFrom5[i].Id, $"path elem number {i++} from 5");
            Assert.AreEqual(7, pathFrom5[i].Id, $"path elem number {i++} from 5");
            Assert.AreEqual(8, pathFrom5[i].Id, $"path elem number {i++} from 5");
            Assert.AreEqual(10, pathFrom5[i].Id, $"path elem number {i++} from 5");
            Assert.AreEqual(11, pathFrom5[i].Id, $"path elem number {i++} from 5");
            Assert.AreEqual(9, pathFrom5[i].Id, $"path elem number {i++} from 5");
            Assert.AreEqual(4, pathFrom5[i].Id, $"path elem number {i++} from 5");

            var pathFrom10 = graph.BreadthFirstSearch(new Vertex {
                Id = 10
            }).ToList();

            Assert.IsTrue(pathFrom10.Select(x => x.Id).Distinct().Count() == pathFrom10.Count, "unique vertex ids from 10");
            Assert.AreEqual(graph.Vertices.Count(), pathFrom10.Count, "all vertices found from 10");

            var pathFrom11 = graph.BreadthFirstSearch(new Vertex {
                Id = 11
            }).ToList();

            Assert.AreEqual(1, pathFrom11.Count, "one vertex found from 11");
        }