Exemplo n.º 1
0
        public static bool HasCircle(INewGraph graph)
        {
            var queue   = new Queue <IVertex>();
            var visited = new HashSet <IVertex>();
            var parent  = new Dictionary <IVertex, IVertex>();

            queue.Enqueue(graph.First());
            while (queue.Count > 0)
            {
                var node = queue.Dequeue();
                visited.Add(node);
                foreach (var next in graph.GetNeighborVertices(node))
                {
                    if (visited.Contains(next))
                    {
                        continue;
                    }
                    queue.Enqueue(next);

                    var px = Find(parent, node);
                    var py = Find(parent, next);
                    if (px.Equals(py))
                    {
                        return(true);
                    }

                    Union(parent, node, next);
                }
            }
            return(false);
        }
Exemplo n.º 2
0
        public static INewGraph Generate(INewGraph graph, IVertex first = null)
        {
            var ret     = graph.Factory.CreateGraph();
            var queue   = new Queue <IVertex>();
            var visited = new HashSet <IVertex>();

            queue.Enqueue(first == null ? graph.First() : first);

            while (queue.Count > 0)
            {
                var node = queue.Dequeue();
                visited.Add(node);

                var v1 = ret.AddVertex(node.Clone() as IVertex);

                foreach (var next in graph.GetNeighborVertices(node))
                {
                    if (visited.Contains(next))
                    {
                        continue;
                    }

                    visited.Add(next);
                    queue.Enqueue(next);

                    var v2 = ret.AddVertex(next.Clone() as IVertex);

                    ret.AddEdge(v1, v2);
                }
            }
            return(ret);
        }