private void bfs(Diagraph g, IEnumerable <int> sources) { Queue <int> q = new Queue <int>(); for (int v = 0; v < g.V; v++) { distTo[v] = Int32.MaxValue; } foreach (int s in sources) { distTo[s] = 0; marked[s] = true; q.Enqueue(s); } while (q.Count != 0) { int v = q.Dequeue(); foreach (var i in g.adj(v)) { if (!marked[i]) { edgeTo[i] = v; distTo[i] = distTo[v] + 1; marked[i] = true; q.Enqueue(i); } } } }
public void dfs(Diagraph g, int v) { marked[v] = true; onStack[v] = true; foreach (int w in g.adj(v)) { if (cycle != null) { return; } if (!marked[w]) { edgeTo[w] = v; dfs(g, w); } else if (onStack[w]) { //we have cycle cycle = new Stack <int>(); int i = v; for (; i != w; i = edgeTo[i]) { cycle.Push(i); } cycle.Push(w); cycle.Push(v); return; } } onStack[v] = false; }
public int[] distTo; // to keep level /// <summary> /// /// </summary> /// <param name="g"></param> /// <param name="s">Source</param> public DirectedBFS(Diagraph g, int s) { marked = new bool[g.V]; edgeTo = new int[g.V]; distTo = new int[g.V]; bfs(g, s); }
public DirectedBFS(Diagraph g, IEnumerable <int> sources) { marked = new bool[g.V]; edgeTo = new int[g.V]; distTo = new int[g.V]; bfs(g, sources); }
/// <summary> /// /// </summary> /// <param name="g"></param> /// <param name="s">Source</param> public DirectedDFS(Diagraph g, int s) { marked = new bool[g.V]; edgeTo = new int[g.V]; marked[s] = true; edgeTo[s] = s; dfs(g, s); }
public static void InternalMain() { Diagraph g = new Diagraph(@"Graph/testdata/tinyDG.txt"); Console.Write(g.toString()); //Graph g1 = g.Clone(); //Console.Write(g1.toString()); }
public static void InternalMain() { Diagraph g = new Diagraph(@"Graph/testdata/TinyDAG.txt"); Console.Write(g.toString()); Topological t = new Topological(g); Console.WriteLine(Graph.PrintFormat(t.GetOrder())); }
public static void InternalMain() { Diagraph g = new Diagraph(@"Graph/testdata/tinyDAG.txt"); Console.Write(g.toString()); KosarajuSharirSCC cc = new KosarajuSharirSCC(g); Console.WriteLine("Number of connected components" + cc.count()); Console.WriteLine(cc.connected(3, 7)); }
public static void InternalMain() { Diagraph g = new Diagraph(@"Graph/testdata/tinyDG.txt"); Console.Write(g.toString()); DirectedCycle dc = new DirectedCycle(g); Console.WriteLine(dc.hasCycle()); Console.WriteLine(Graph.PrintFormat(dc.Cycle())); }
public void dfs(Diagraph g, int v) { marked[v] = true; foreach (int w in g.adj(v)) { if (!marked[w]) { edgeTo[w] = v; dfs(g, w); } } }
public void dfs(Diagraph g, int v) { marked[v] = true; connectedId[v] = connectedComponentCounter; foreach (int w in g.adj(v)) { if (!marked[w]) { dfs(g, w); } } }
public static void InternalMain() { Diagraph g = new Diagraph(@"Graph/testdata/TinyDG.txt"); Console.Write(g.toString()); DirectedBFS bfsPths = new DirectedBFS(g, 6); Console.WriteLine(bfsPths.hasPathTo(3)); Console.WriteLine(bfsPths.hasPathTo(1)); Console.WriteLine(bfsPths.hasPathTo(7)); Console.WriteLine(Graph.PrintFormat(bfsPths.pathTo(3))); }
public DirectedDFS(Diagraph g, IEnumerable <int> sources) { marked = new bool[g.V]; edgeTo = new int[g.V]; foreach (var i in sources) { if (!marked[i]) { dfs(g, i); } } }
public DirectedCycle(Diagraph g) { marked = new bool[g.V]; edgeTo = new int[g.V]; onStack = new bool[g.V]; for (int i = 0; i < g.V; i++) { if (!marked[i] && cycle == null) { dfs(g, i); } } }
public KosarajuSharirSCC(Diagraph g) { marked = new bool[g.V]; connectedId = new int[g.V]; DepthFirstOrder depthFirstOrder = new DepthFirstOrder(g); foreach (int i in depthFirstOrder.reversePost()) { if (!marked[i]) { dfs(g, i); connectedComponentCounter++; } } }
// run DFS in digraph G from vertex v and compute preorder/postorder private void dfs(Diagraph G, int v) { marked[v] = true; preArr[v] = preCounter++; preorder.Enqueue(v); foreach (int w in G.adj(v)) { if (!marked[w]) { dfs(G, w); } } postorder.Enqueue(v); postArr[v] = postCounter++; }
private int postCounter; // counter for postorder numbering /** * Determines a depth-first order for the digraph {@code G}. * @param G the digraph */ public DepthFirstOrder(Diagraph G) { preArr = new int[G.V]; postArr = new int[G.V]; postorder = new Queue <int>(); preorder = new Queue <int>(); marked = new bool[G.V]; for (int v = 0; v < G.V; v++) { if (!marked[v]) { dfs(G, v); } } }
public Topological(Diagraph G) { DirectedCycle finder = new DirectedCycle(G); if (!finder.hasCycle()) { DepthFirstOrder dfs = new DepthFirstOrder(G); order = dfs.reversePost(); rankArr = new int[G.V]; int i = 0; foreach (int v in order) { rankArr[v] = i++; } } }
public Diagraph reverse() { Diagraph reverseDiagraph = new Diagraph(this.V); int index = 0; foreach (var list in bag) { foreach (int i in list) { reverseDiagraph.addEdge(i, index); } index++; } return(reverseDiagraph); }