コード例 #1
0
ファイル: DemucronLevels.cs プロジェクト: madmazoku/OTUS-Algo
        void Build()
        {
            if (data != null)
            {
                return;
            }

            AdjancenceArray <T> adjancenceArray = new AdjancenceArray <T>(graph);

            int[] nodeStocks     = new int[graph.NodesCount];
            int   usedNodesCount = 0;

            bool[] usedNodesFlag = new bool[graph.NodesCount];

            for (int node = 0; node < graph.NodesCount; ++node)
            {
                for (int adjancentNode = 0; adjancentNode < graph.NodesCount; ++adjancentNode)
                {
                    if (adjancenceArray.HasEdge(node, adjancentNode))
                    {
                        ++nodeStocks[adjancentNode];
                    }
                }
            }

            NodeStack <NodeStack <int> > skewStack = new NodeStack <NodeStack <int> >();

            while (usedNodesCount < usedNodesFlag.Length)
            {
                skewStack.Push(new NodeStack <int>());

                for (int node = 0; node < graph.NodesCount; ++node)
                {
                    if (!usedNodesFlag[node] && nodeStocks[node] == 0)
                    {
                        skewStack.Top.Push(node);
                    }
                }
                if (skewStack.Top.size == 0)
                {
                    throw new Exception("Cycles found in graph");
                }

                usedNodesCount += skewStack.Top.size;
                for (Node <int> node = skewStack.Top.top; node != null; node = node.next)
                {
                    usedNodesFlag[node.value] = true;
                    for (int adjancentNode = 0; adjancentNode < graph.NodesCount; ++adjancentNode)
                    {
                        if (!usedNodesFlag[adjancentNode] && adjancenceArray.Data[node.value, adjancentNode] != null)
                        {
                            --nodeStocks[adjancentNode];
                        }
                    }
                }
            }

            data = Util.SkewListToArray(skewStack);
        }
コード例 #2
0
ファイル: Graph.cs プロジェクト: madmazoku/OTUS-Algo
        public int[] TarjanRecursive()
        {
            NodeColors[] nodeColors = new NodeColors[Data.NodesCount];

            NodeStack <int> stack = new NodeStack <int>();

            if (!TarjanRecursiveStart(stack, nodeColors))
            {
                throw new Exception("Invalid data for Tarjan");
            }

            return(Util.ListToArray <int>(stack));
        }
コード例 #3
0
ファイル: Graph.cs プロジェクト: madmazoku/OTUS-Algo
 public bool TarjanRecursiveStart(NodeStack <int> stack, NodeColors[] nodeColors)
 {
     for (int node = 0; node < Data.NodesCount; ++node)
     {
         if (nodeColors[node] == NodeColors.White)
         {
             if (!TarjanRecursiveDSF(node, stack, nodeColors))
             {
                 return(false);
             }
         }
     }
     return(true);
 }
コード例 #4
0
        public ArticulationNodes(AdjancenceVector <T> graph)
        {
            this.graph = graph;

            order = 0;
            pre   = new int[graph.NodesCount];
            low   = new int[graph.NodesCount];

            Array.Fill(pre, -1);

            stack = new NodeStack <int>();

            data = null;
        }
コード例 #5
0
        public TarjanStrongConnected(AdjancenceVector <T> graph)
        {
            this.graph = graph;

            order = 0;
            pre   = new int[graph.NodesCount];
            low   = new int[graph.NodesCount];
            stack = new NodeStack <int>();

            Array.Fill(pre, -1);

            skewStackQueue = new NodeStack <NodeQueue <int> >();

            data = null;
        }
コード例 #6
0
ファイル: Graph.cs プロジェクト: madmazoku/OTUS-Algo
 public bool TarjanRecursiveDSF(int node, NodeStack <int> stack, NodeColors[] nodeColors)
 {
     nodeColors[node] = NodeColors.Gray;
     (int, T)[] adjancentNodes = Data.Data[node];