Пример #1
0
        static List <int> GetAllLoops(Digraph g)
        {
            List <int> vertices = g.GetAllVertices();
            List <int> loops    = new List <int>();

            foreach (var vertex in vertices)
            {
                bool isLoop = IsLoop(g, vertex);

                if (isLoop)
                {
                    loops.Add(vertex);
                }
            }

            return(loops);
        }
Пример #2
0
        public Digraph(Digraph g) : this(g.V)
        {
            this.E = g.E;

            for (int v = 0; v < g.V; v++)
            {
                Stack <int> reverse = new Stack <int>();

                foreach (int adjacentVertex in g.adjacencyLists[v])
                {
                    reverse.Push(adjacentVertex);
                }

                foreach (int vertex in reverse)
                {
                    this.adjacencyLists[v].AddLast(vertex);
                }
            }
        }
Пример #3
0
        static void Main(string[] args)
        {
            Digraph g = new Digraph(10);

            g.AddEdge(0, 1);
            g.AddEdge(0, 7);
            g.AddEdge(7, 7);
            g.AddEdge(2, 0);
            g.AddEdge(2, 3);
            g.AddEdge(2, 4);
            g.AddEdge(2, 6);
            g.AddEdge(6, 3);
            g.AddEdge(3, 3);
            g.AddEdge(5, 5);
            g.AddEdge(4, 4);

            List <int> loops = GetAllLoops(g);

            Console.WriteLine("Loop vertices: " + string.Join(", ", loops));
        }
Пример #4
0
 static bool IsLoop(Digraph g, int v)
 {
     // If the adjacency list of vertex v contains itself, this vertex forms a loop.
     return(g.GetAdjacentVertices(v).Contains(v));
 }