public StrongConnectedComponent(Digragh g)
        {
            this.g = g;
            visited = new bool[g.NumV()];
            id = new int[g.NumV()];
            count = 0;
            var order = new OrderDigragh(g.Reverse()).ReversePostOrder();

            foreach (var v in order)
            {
                if (!visited[v])
                {
                    count++;
                    Dfs(v);
                }
            }
        }
Пример #2
0
        public CycleDFS(Digragh g)
        {
            this.g = g;
            var numV = g.NumV();
            visited = new bool[numV];
            path = new int[numV];
            onStack = new bool[numV];
            hasCycle = false;

            for (var v = 0; v < numV; v++)
            {
                if(!visited[v] && !hasCycle)
                {
                    DFSHelper(v);
                }
            }
        }
Пример #3
0
        public TopoBFS(Digragh g)
        {
            this.g = g;
            degree = new int[g.NumV()];
            order = new List<int>();
            var queue = new Queue<int>();

            for(var v= 0;v<g.NumV();v++)
            {
                degree[v] = g.InDegree(v);
                if(degree[v]==0)
                {
                    queue.Enqueue(v);
                }
            }

            while(queue.Count!=0)
            {
                var n = queue.Count;

                for(var i = 0;i<n;i++)
                {
                    var v = queue.Dequeue();
                    order.Add(v);

                    foreach(var w in g.EdgesFrom(v))
                    {
                        degree[w]--;
                        if(degree[w]==0)
                        {
                            queue.Enqueue(w);
                        }
                    }
                }
            }
        }
Пример #4
0
 public TopologySort(Digragh g)
 {
     dfs = new OrderDigragh(g);
 }
Пример #5
0
        public OrderDigragh(Digragh g)
        {
            this.g = g;
            var numV = g.NumV();
            visited = new bool[numV];
            reversePostOrder = new int[numV];
            postOrder = new int[numV];
            preOrder = new int[numV];
            post = new Stack<int>();
            pre = new Queue<int>();

            for (var v = 0; v < g.NumV(); v++)
            {
                if(!visited[v])
                {
                    DFSHelper(v);
                }
            }

            var i = 0;
            var j = numV - 1;

            while (post.Count != 0)
            {
                var tmp1 = post.Pop();
                var tmp2 = pre.Dequeue();

                reversePostOrder[i++] = tmp1;
                postOrder[j--] = tmp1;
                preOrder[i++] = tmp2;
            }
        }
Пример #6
0
 public DigraghCycle(Digragh g)
 {
     dfs = new CycleDFS(g);
 }
Пример #7
0
        public Digragh Reverse()
        {
            var reverse = new Digragh(this.V);

            for(var v = 0;v< V;v++)
            {
                foreach(var w in EdgesFrom(v))
                {
                    reverse.AddEdge(w, v);
                }
            }
            return reverse;
        }
Пример #8
0
            public DigraghDFS(Digragh g, int v)
            {
                visited = new int[g.NumV()];
                path = new int[g.NumV()];
                this.g = g;
                count = 1;

                path[v] = v;
                DFSHelper(v);
            }
Пример #9
0
 public SingleSource(Digragh g, int v)
 {
     dfs = new DigraghDFS(g, v);
     this.v = v;
 }
Пример #10
0
 public DoubleSource(Digragh g)
 {
     dfs = new DigraghDFS(g);
 }
Пример #11
0
            public DigraghDFS(Digragh g)
            {
                visited = new int[g.NumV()];
                this.g = g;

                for(var v = 0;v<g.NumV();v++)
                {
                    if(visited[v]==0)
                    {
                        path[v] = v;
                        DFSHelper(v);
                    }
                    count++;
                }
            }