コード例 #1
0
        private void TraverseNetworkFlow()
        {
            // 清理工作:将所有节点和流都关闭
            foreach (var node in activeNodeList)
            {
                if (!(node is StartNode))
                {
                    node.Clear();
                }
            }
            foreach (var flow in activeFlowList)
            {
                flow.Clear();
            }
            visitedFlowList.Clear();

            // 从起点开始,查找周边的通路
            for (int i = 0; i < NetworkNode.NeighborNum; i++)
            {
                var flow = startNode.GetFlow(i);
                if (flow != null && startNode.IsReachableTo(flow.Direction))
                {
                    flowQueue.Enqueue(flow);
                    Log(string.Format("Enqueue: {0}", flow));
                }
            }

            // 开始遍历网络图
            while (flowQueue.Count > 0)
            {
                NetworkFlow flow     = flowQueue.Dequeue();
                NetworkNode node     = flow.FirstNode;
                NetworkNode neighbor = flow.SecondNode;

                visitedFlowList.Add(flow);
                Log(string.Format("Visit {0}", flow));

                // 激活流和输入状态
                NetworkNode.Direction direction = node.GetNeighborDirection(neighbor);
                flow.Activate(node, neighbor);
                neighbor.SetInput(NetworkNode.GetOppositeDirection(direction), true);

                bool nextFlowFound = false;
                for (int i = 0; i < NetworkNode.NeighborNum; i++)
                {
                    var nextFlow = neighbor.GetFlow(i);
                    if (nextFlow == null ||
                        visitedFlowList.Contains(nextFlow) ||
                        flowQueue.Contains(nextFlow) ||
                        // 第一趟查找,不包含那些已经走过一遍的通路
                        IsVisitedFlowExist(nextFlow.SecondNode, nextFlow.FirstNode))
                    {
                        continue;
                    }
                    if (neighbor.IsReachableTo(nextFlow.Direction))
                    {
                        Log(string.Format("Enqueue: {0}", nextFlow));
                        flowQueue.Enqueue(nextFlow);
                        nextFlowFound = true;
                    }
                }
                // 第二趟查找,可能包含反向的通路
                if (!nextFlowFound)
                {
                    for (int i = 0; i < NetworkNode.NeighborNum; i++)
                    {
                        var nextFlow = neighbor.GetFlow(i);
                        if (nextFlow == null ||
                            visitedFlowList.Contains(nextFlow) ||
                            flowQueue.Contains(nextFlow))
                        {
                            continue;
                        }
                        if (neighbor.IsReachableTo(nextFlow.Direction))
                        {
                            Log(string.Format("Enqueue2: {0}", nextFlow));
                            flowQueue.Enqueue(nextFlow);
                        }
                    }
                }
            }
        }
コード例 #2
0
        private void TraverseNetwork()
        {
            foreach (var node in activeNodeList)
            {
                if (!(node is StartNode))
                {
                    node.Clear();
                }
            }
            foreach (var flow in activeFlowList)
            {
                flow.Clear();
            }

            visitedNode.Clear();
            visitedFlow.Clear();
            // 广度优先遍历
            VisitNode(startNode);
            while (queue.Count > 0)
            {
                NetworkNode node = queue.Dequeue();
                // 不允许从结束节点访问邻居节点
                if (node is EndNode)
                {
                    continue;
                }

                for (int i = 0; i < NetworkNode.NeighborNum; i++)
                {
                    var direction = (NetworkNode.Direction)i;
                    var neighbor  = node.GetNeighbor(direction);

                    if (neighbor == null)
                    {
                        continue;
                    }
                    var flow = node.GetFlow(direction);
                    // 特例:结束(带锁)节点是可以被重复访问的
                    if (visitedNode.ContainsKey(neighbor) &&
                        visitedFlow.ContainsKey(flow) && visitedFlow[flow] &&
                        !(neighbor is EndNode))
                    {
                        continue;
                    }

                    // 激活或反激活邻居节点的输入
                    if (node.IsReachableTo(direction))
                    {
                        flow.Activate(node, neighbor);
                        neighbor.SetInput(NetworkNode.GetOppositeDirection(direction), true);
                    }
                    else
                    {
                        flow.Deactivate(node, neighbor);
                        neighbor.SetInput(NetworkNode.GetOppositeDirection(direction), false);
                    }
                    VisitNode(neighbor);
                    VisitFlow(flow);
                }
            }
        }