public bool IsDependent(DependenceGraphNode n1, DependenceGraphNode n2) { if (n1 == null || n2 == null || n1 == n2) { return(false); } Dictionary <DependenceGraphNode, bool> oldNodes = new Dictionary <DependenceGraphNode, bool>(); Queue <DependenceGraphNode> iterateQueue = new Queue <DependenceGraphNode>(); iterateQueue.Enqueue(n1); oldNodes[n1] = true; while (iterateQueue.Count > 0) { DependenceGraphNode n = iterateQueue.Dequeue(); foreach (DependenceGraphNode nn in n.NextNodes) { if (nn == n2) { iterateQueue.Clear(); oldNodes.Clear(); return(true); } if (!oldNodes.ContainsKey(nn)) { iterateQueue.Enqueue(nn); oldNodes[nn] = true; } } } return(false); }
public bool IsAccessed(DependenceGraphNode node) { if (accessedNodes.ContainsKey(node)) { return(accessedNodes[node]); } return(false); }
public DependenceGraphNode NewNode(string id) { if (nodes.ContainsKey(id)) { return(nodes[id]); } else { DependenceGraphNode node = new DependenceGraphNode(id); nodes[id] = node; return(node); } }
private DependenceGraphNode PopDeepFirstNode() { if (iterateStack.Count <= 0) { return(null); } DependenceGraphNode node = iterateStack.Peek(); while (node.NextNodes.Count > 0) { bool existNext = false; //每次只扩展一个后继,深度优先 for (int i = 0; i < node.NextNodes.Count; i++) { DependenceGraphNode n = node.NextNodes[i]; if (!accessedNodes.ContainsKey(n)) { iterateStack.Push(n); PrepareAccessNode(n); existNext = true; break; } else if (!accessedNodes[n])//依赖一个存于深度优先栈内的节点,表明有循环依赖 { cyclicDependences.Add(new KeyValuePair <string, string>(node.ID, n.ID)); } } if (!existNext) { break; } else { node = iterateStack.Peek(); } } node = iterateStack.Pop(); return(node); }
/// <summary> /// 设定一个结点为已访问结点,已访问结点是已经由迭代过程返回过的结点。 /// </summary> /// <param name="node"></param> private void AccessNode(DependenceGraphNode node) { accessedNodes[node] = true; }
/// <summary> /// 设定一个结点为待访问结点,同时从未访问集合中删除此结点。 /// </summary> /// <param name="node"></param> private void PrepareAccessNode(DependenceGraphNode node) { accessedNodes[node] = false; unaccessedNodes.Remove(node); }