//----< 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); }
//----< add child vertex and its associated edge value to vertex >----- public void addChild(CsNode <V, E> childNode, E edgeVal) { if (!containChild(childNode)) { children.Add(new CsEdge <V, E>(childNode, edgeVal)); } }
private static void Main(string[] args) { Console.Write("\n Testing CsGraph class"); Console.Write("\n ======================="); var node1 = new CsNode <string, string>("node1"); var node2 = new CsNode <string, string>("node2"); var node3 = new CsNode <string, string>("node3"); var node4 = new CsNode <string, string>("node4"); var node5 = new CsNode <string, string>("node5"); 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"); var graph = new CsGraph <string, string>("Fred"); graph.addNode(node1); graph.addNode(node2); graph.addNode(node3); graph.addNode(node4); graph.addNode(node5); graph.showDependencies(); graph.startNode = node1; Console.Write("\n\n starting walk at {0}", graph.startNode.name); Console.Write("\n not showing backtracks"); graph.walk(); graph.startNode = node2; Console.Write("\n\n starting walk at {0}", graph.startNode.name); graph.showBackTrack = true; Console.Write("\n show backtracks"); graph.setOperation(new demoOperation()); graph.walk(); Console.Write("\n\n Strong Components:"); graph.strongComponents(); foreach (var item in graph.strongComp) { Console.Write("\n component {0}", item.Key); Console.Write("\n "); foreach (var elem in item.Value) { Console.Write("{0} ", elem.name); } } Console.Write("\n\n"); }
//----< Does current node contain specified node? >----- public bool containChild(CsNode <V, E> childNode) { foreach (CsEdge <V, E> edge in children) { if (edge.targetNode == childNode) { return(true); } } return(false); }
static void Main(string[] args) { Console.Write("\n Testing CsGraph class"); Console.Write("\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"); 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); graph.showDependencies(); graph.startNode = node1; Console.Write("\n\n starting walk at {0}", graph.startNode.name); Console.Write("\n not showing backtracks"); graph.walk(); graph.startNode = node2; Console.Write("\n\n starting walk at {0}", graph.startNode.name); graph.showBackTrack = true; Console.Write("\n show backtracks"); graph.setOperation(new demoOperation()); graph.walk(); Console.Write("\n\n"); }
private void strongConnect(CsNode <V, E> node) { node.index = index; node.lowlink = index; ++index; S.Push(node); node.onStack = true; CsNode <V, E> child = null; foreach (var edge in node.children) { child = edge.targetNode; if (child.index == -1) { strongConnect(child); node.lowlink = Math.Min(node.lowlink, child.lowlink); } else if (child.onStack) { node.lowlink = Math.Min(node.lowlink, child.index); } } if (node.lowlink == node.index) { var compNodes = new List <CsNode <V, E> >(); strongComp.Add(strongCompId, compNodes); do { child = S.Pop(); child.onStack = false; strongComp[strongCompId].Add(child); } while (child != node); ++strongCompId; } }
public void Walk(CsNode <V, E> node) { graphOp.DoNodeOperation(node); node.visited = true; do { CsEdge <V, E> childEdge = node.GetNextUnmarkedChild(); if (childEdge == null) { return; } else { graphOp.DoEdgeOperation(childEdge.edgeValue); Walk(childEdge.targetNode); if (node.HasUnmarkedChild() || showBackTrack) { graphOp.DoNodeOperation(node); } } } while (true); }
//----< 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 { var childEdge = node.getNextUnmarkedChild(); if (childEdge == null) { return; } gop.doEdgeOp(childEdge.edgeValue); walk(childEdge.targetNode); if (node.hasUnmarkedChild() || showBackTrack) { gop.doNodeOp(node); // more edges to visit so announce } } while (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 override bool doNodeOp(CsNode <string, string> node) { Console.Write("\n -- {0}", node.name); return(true); }
public CsEdge(CsNode <V, E> node, E value) { targetNode = node; edgeValue = value; }
//----< add vertex to graph adjacency list >--------------------------- public void addNode(CsNode <V, E> node) { adjList.Add(node); }
//----< graph.walk() calls this on every node >------------------------ public virtual bool doNodeOp(CsNode <V, E> node) { Console.Write("\n {0}", node); return(true); }
//----< graph.walk() calls this on every node >------------------------ virtual public bool doNodeOp(CsNode <V, E> node) { Console.Write("\n {0}", node.ToString()); return(true); }
override public bool DoNodeOperation(CsNode <string, string> node) { Console.Write("\n -- {0}", node.name); return(true); }
public void AddNode(CsNode <V, E> node) { adjacencyList.Add(node); }