private void StrongConnect(Vertex <T> v) { v.Index = index; v.LowLink = index; index++; stack.Push(v); foreach (Vertex <T> w in v.Dependencies) { if (w.Index < 0) { StrongConnect(w); v.LowLink = System.Math.Min(v.LowLink, w.LowLink); } else if (stack.Contains(w)) { v.LowLink = System.Math.Min(v.LowLink, w.Index); } } if (v.LowLink == v.Index) { var scc = new StronglyConnectedComponent <T>(); Vertex <T> w; do { w = stack.Pop(); scc.Add(w); } while (v != w); stronglyConnectedComponents.Add(scc); } }
private void StrongConnect(Vertex <T> v) { v.Index = _index; v.LowLink = _index; _index++; _stack.Push(v); foreach (var w1 in v.Dependencies) { if (w1.Index < 0) { StrongConnect(w1); v.LowLink = Math.Min(v.LowLink, w1.LowLink); } else if (_stack.Contains(w1)) { v.LowLink = Math.Min(v.LowLink, w1.Index); } } if (v.LowLink != v.Index) { return; } var scc = new StronglyConnectedComponent <T>(); Vertex <T> w2; do { w2 = _stack.Pop(); scc.Add(w2); } while (w2 != v); _stronglyConnectedComponents.Add(scc); }
private static void StrongConnect(IVertex vertex, IGraph graph, Stack <IVertex> vertexStack, List <StronglyConnectedComponent> strongConnectedComponents, ref uint index) { var algorithmData = (TarjanStrongConnectedComponentAlgorithmData)vertex.AlgorithmData; algorithmData.Index = index; algorithmData.LowLink = index; index++; vertexStack.Push(vertex); algorithmData.IsOnStack = true; foreach (var adjacentVertex in GetAdjacentVertices(graph, vertex)) { var adjacentAlgorithmData = (TarjanStrongConnectedComponentAlgorithmData)adjacentVertex.AlgorithmData; if (!adjacentAlgorithmData.Index.HasValue) { StrongConnect(adjacentVertex, graph, vertexStack, strongConnectedComponents, ref index); algorithmData.LowLink = Math.Min(algorithmData.LowLink.Value, adjacentAlgorithmData.LowLink.Value); } else if (adjacentAlgorithmData.IsOnStack) { algorithmData.LowLink = Math.Min(algorithmData.LowLink.Value, adjacentAlgorithmData.Index.Value); } } if (algorithmData.LowLink == algorithmData.Index) { IVertex stackVertex; var strongConnectedComponent = new StronglyConnectedComponent(); do { stackVertex = vertexStack.Pop(); var stackVertexAlgorithmData = (TarjanStrongConnectedComponentAlgorithmData)stackVertex.AlgorithmData; stackVertexAlgorithmData.IsOnStack = false; strongConnectedComponent.Add(stackVertex); } while (!stackVertex.Equals(vertex)); strongConnectedComponents.Add(strongConnectedComponent); } }