//Accepts the dependencies and generates a graph public List<CsNode<string, string>> graphAccept(List<Tuple<string,string>> graph, List<string> files) { List<CsNode<string, string>> nodes = new List<CsNode<string, string>>(); for(int i=0;i<files.Count;i++) { CsNode<string, string> node = new CsNode<string, string>(files[i]); node.name = Path.GetFileName(files[i]); nodes.Add(node); } int n = graph.Count; for(int i=0;i<n;i++) { int j = 0; for(j=0;j<nodes.Count;j++) { if (nodes[j].name == graph[i].Item1) break; } for(int k=0;k<nodes.Count;k++) { if (nodes[k].name == graph[i].Item2 && j != nodes.Count) nodes[j].addChild(nodes[k], " "); } } return nodes; }
//----< depth first search from specific node >------------------------ public void walk(CsNode<V, E> node) { // process this node gop.doNodeOp(node); node.visited = true; // visit children do { CsEdge<V, E> childEdge = node.getNextUnmarkedChild(); if (childEdge == null) { return; } else { gop.doEdgeOp(childEdge.edgeValue); walk(childEdge.targetNode); if (node.hasUnmarkedChild() || showBackTrack) { // popped back to predecessor node gop.doNodeOp(node); // more edges to visit so announce } // location and next edge } } while (true); }
//determines the strongly connectedcomponents present public List<string> Tarjan(List<CsNode<string, string>> m) { var index = 0; string sccComponents = null; List<string> sccNames = new List<string>(); var S = new Stack<CsNode<string, string>>(); void StrongConnect(CsNode<string, string> v) { v.Index = index; v.LowLink = index; index++; S.Push(v); CsNode<string, string> w = null; for (int i = 0; i < v.children.Count; i++) { w = v.children[i].targetNode; if (w.Index < 0) { StrongConnect(w); v.LowLink = Math.Min(v.LowLink, w.LowLink); } else if (S.Contains(w)) v.LowLink = Math.Min(v.LowLink, w.Index); } if (v.LowLink == v.Index) { do { w = S.Pop(); sccComponents += w.name + ","; } while (w != v); sccNames.Add(sccComponents); sccComponents = null; } } foreach (var v in m) if (v.Index < 0) StrongConnect(v); return sccNames; }
static void Main(string[] args) { Console.Write("\nDemonstating Test Stub for Strongly Connected Component"); Console.Write("\n =======================\n"); CsNode<string, string> node1 = new CsNode<string, string>("node1"); CsNode<string, string> node2 = new CsNode<string, string>("node2"); CsNode<string, string> node3 = new CsNode<string, string>("node3"); CsNode<string, string> node4 = new CsNode<string, string>("node4"); CsNode<string, string> node5 = new CsNode<string, string>("node5"); List<CsNode<string, string>> nodes = new List<CsNode<string, string>>(); node1.addChild(node2, "edge12"); node1.addChild(node3, "edge13"); node2.addChild(node3, "edge23"); node2.addChild(node4, "edge24"); node3.addChild(node1, "edge31"); node5.addChild(node1, "edge51"); node5.addChild(node4, "edge54"); CsGraph<string, string> graph = new CsGraph<string, string>("Fred"); graph.addNode(node1); graph.addNode(node2); graph.addNode(node3); graph.addNode(node4); graph.addNode(node5); nodes.Add(node1); nodes.Add(node2); nodes.Add(node3); nodes.Add(node4); nodes.Add(node5); StronglyConnectedComponent T = new StronglyConnectedComponent(); List<string> scc=T.Tarjan(nodes); for (int i = 0; i < scc.Count; i++) Console.WriteLine("{0}:{1}", i + 1, scc[i]); Console.Read(); }
override public bool doNodeOp(CsNode<string, string> node) { Console.Write("\n -- {0}", node.name); return true; }
//----< add vertex to graph adjacency list >--------------------------- public void addNode(CsNode<V, E> node) { adjList.Add(node); }
//----< graph.walk() calls this on every node >------------------------ virtual public bool doNodeOp(CsNode<V, E> node) { Console.Write("\n {0}", node.ToString()); return true; }
//----< add child vertex and its associated edge value to vertex >----- public void addChild(CsNode<V, E> childNode, E edgeVal) { children.Add(new CsEdge<V, E>(childNode, edgeVal)); }
public CsEdge(CsNode<V, E> node, E value) { targetNode = node; edgeValue = value; }