static void Main(string[] args) { Digraph d = new Digraph(10); Console.WriteLine(d.toString()); d.addEdge(new Edge(0, 1, 0.4)); d.addEdge(new Edge(0, 2, 0.2)); d.addEdge(new Edge(0, 3, 0.5)); d.addEdge(new Edge(3, 1, 0.1)); Console.WriteLine(d.toString()); Console.ReadKey(); }
public Order(Digraph g) { _g = g; _marked = new bool[g.Vertexes.Count]; _pre = new Queue <int>(); _reversePre = new Stack <int>(); _post = new Queue <int>(); _reversePost = new Stack <int>(); foreach (int v in _g.Vertexes) { if (!_marked[v]) { Dfs(v); } } }
public DfsSCC(Digraph g) : base(g) { _marked = new bool[g.Vertexes.Count]; _id = new int[g.Vertexes.Count]; Digraph gReverse = g.Reverse(); Order order = new Order(gReverse); foreach (int v in order.ReversePost()) { if (!_marked[v]) { Dfs(g, v); _count++; } } }
public void Run() { Digraph g = BuildGraph(); Topological topological = GetTopological(g); var order = topological.Order(); if (order != null) { string orderStr = string.Join(",", order); Console.WriteLine($"Topological order is {orderStr}"); } else { Console.WriteLine("Not a dag"); } }
public DfsCycle(Digraph g) : base(g) { _g = g; int n = g.Vertexes.Count; _marked = new bool[n]; _onStack = new bool[n]; _pathTo = new int[n]; foreach (var v in _g.Vertexes) { if (!_marked[v] && !_hasCycle) { Dfs(v); } } }
/** * Determines whether the digraph {@code G} has a topological order and, if so, * finds such a topological order. * @param G the digraph */ public DigraphTopological2(Digraph G) { V = G.GetVertices(); DirectedCycle finder = new DirectedCycle(G); if (!finder.IsCycle()) { DigraphTopological d = new DigraphTopological(G); Order = d.ReversePost; rank = new int[G.GetVertices()]; int i = 0; foreach (int v in Order) { rank[v] = i++; } } }
private Digraph BuildGraph() { List <int> vertexes = new List <int>() { 0, 1, 2, 3, 4, 5, 6 }; Digraph g = new Digraph(vertexes); g.AddEdge(0, 5); g.AddEdge(0, 1); g.AddEdge(0, 6); g.AddEdge(5, 4); g.AddEdge(6, 4); g.AddEdge(2, 0); g.AddEdge(2, 3); g.AddEdge(3, 5); return(g); }
public void Run() { Digraph g = BuildGraph(); int s = 0; int t = 3; Path p = GetPath(g, s); bool hasPath = p.HasPathTo(t); if (hasPath) { var path = p.PathTo(3); Console.WriteLine($"The path from {s} to {t} is {string.Join("->", path)}"); } else { Console.WriteLine($"No path from {s} to {t}"); } }
private void Bfs(Digraph g, int s) { Queue <int> queue = new Queue <int>(); queue.Enqueue(s); while (queue.Count > 0) { int v = queue.Dequeue(); _marked[v] = true; _id[v] = _count; foreach (int w in g.Adj(v)) { if (!_marked[w]) { queue.Enqueue(w); } } } }
private Digraph BuildGraph(bool hasCycle) { List <int> vertexes = new List <int>() { 0, 1, 2, 3 }; Digraph g = new Digraph(vertexes); g.AddEdge(0, 1); g.AddEdge(1, 2); g.AddEdge(2, 3); if (hasCycle) { g.AddEdge(3, 0); } else { g.AddEdge(0, 3); } return(g); }
public void Run() { Digraph g = BuildGraph(true); Cycle cycle = GetCycle(g); bool hasCycle = cycle.HasCycle(); if (hasCycle) { var cycles = cycle.GetCycle(); string cycleStr = ""; if (cycles != null) { cycleStr = string.Join(",", cycles); } Console.WriteLine($"Has cycle, the cycle is : {cycleStr}"); } else { Console.WriteLine("Hasn't cycle"); } g = BuildGraph(false); cycle = GetCycle(g); hasCycle = cycle.HasCycle(); if (hasCycle) { var cycles = cycle.GetCycle(); string cycleStr = ""; if (cycles != null) { cycleStr = string.Join(",", cycles); } Console.WriteLine($"Has cycle, the cycle is : {cycleStr}"); } else { Console.WriteLine("Hasn't cycle"); } }
// BFS from single source private void Bfs(Digraph G, int s) { Queue <int> q = new Queue <int>(); marked[s] = true; distTo[s] = 0; q.Enqueue(s); while (q.Count != 0) { int v = q.Dequeue(); foreach (int w in G.GetAdj(v)) { if (!marked[w]) { edgeTo[w] = v; distTo[w] = distTo[v] + 1; marked[w] = true; q.Enqueue(w); } } } }
/** * Initializes a new digraph that is a deep copy of the specified digraph. * * @param G the digraph to copy */ public Digraph(Digraph G) { this.V = (G.GetVertices()); this.E = G.GetEdges(); for (int v = 0; v < V; v++) { this.indegree[v] = G.GetInDegree(v); } for (int v = 0; v < G.GetVertices(); v++) { // reverse so that adjacency list is in same order as original Stack <int> reverse = new Stack <int>(); foreach (int w in G.adj[v]) { reverse.Push(w); } foreach (int w in reverse) { adj[v].Push(w); } } }
public BfsCycle(Digraph g) : base(g) { int n = g.Vertexes.Count; int[] inDegrees = new int[n]; foreach (var v in g.Vertexes) { foreach (var w in g.Adj(v)) { inDegrees[w]++; } } Queue <int> queue = new Queue <int>(); int count = 0; foreach (var v in g.Vertexes) { if (inDegrees[v] == 0) { queue.Enqueue(v); count++; } } while (queue.Count > 0) { int v = queue.Dequeue(); foreach (var w in g.Adj(v)) { if (--inDegrees[w] == 0) { queue.Enqueue(w); count++; } } } _hasCycle = count != g.Vertexes.Count; }
public DfsTopological(Digraph g) : base(g) { _cycle = new DfsCycle(g); _order = new Order(g); }
public Topological(Digraph g) { }
public Path(Digraph g, int s) { }
private Path GetPath(Digraph g, int s) { // return new DfsPath(g, s); return(new BfsPath(g, s)); }
public BfsTopological(Digraph g) : base(g) { _g = g; _cycle = new BfsCycle(g); }
private SCC GetSCC(Digraph g) { // return new DfsSCC(g); return(new BfsSCC(g)); }
public Cycle(Digraph g) { }
private Cycle GetCycle(Digraph g) { // return new DfsCycle(g); return(new BfsCycle(g)); }
public SCC(Digraph g) { }
private int Count; // number of vertices reachable from source(s) // --- constructor, from 1 source public DirectedDFS(Digraph G, int s) { marked = new Boolean[G.GetVertices()]; ValidateVertex(s); Dfs(G, s); }
static void Main(string[] args) { Digraph G = new Digraph("tinyDG.txt"); //how much vertices? Console.WriteLine("Vertices: " + G.GetVertices()); //how much edges? Console.WriteLine("Edges: " + G.GetEdges()); // indegree Console.WriteLine("InDegree for 0 vertice: " + G.GetInDegree(0)); // outdegree Console.WriteLine("OutDegree for 0 vertice: " + G.GetOutDegree(0)); // is v reachible from s ? Console.WriteLine("is 1 reachible from 0: " + G.IsReachible(G, 0, 1)); // True Console.WriteLine("is 6 reachible from 0: " + G.IsReachible(G, 0, 1)); // False // isEdge Console.WriteLine("Is Edge ? 0,12 " + G.IsEdge(0, 12)); Console.WriteLine("Is Edge ? 0,5 " + G.IsEdge(0, 5)); int s = 0; // start vertex int f = 2; // finish vertex // --- DFS Console.WriteLine(" --- DFS..."); // Has path to ? Console.WriteLine("Has path from " + s + " to " + f + " vertex, DFS? " + G.IsPathToDFS(G, s, f)); // This is the path, DFS Console.WriteLine("This is the path, DFS?"); Stack <int> path = new Stack <int>(); G.GetPathToDFS(G, s, f, ref path); if (path.Count != 0) { foreach (int i in path) { Console.WriteLine(" > " + i.ToString()); } } else { Console.WriteLine("no path..."); } // --- BFS Console.WriteLine("--- BFS..."); // Haspath to Console.WriteLine("Has path from " + s + " to " + f + " vertex, DFS? " + G.IsPathToBFS(G, s, f)); // --- shortest distance path.Clear(); if (G.IsPathToBFS(G, s, f)) { Console.WriteLine("Shortest distance: " + G.GetShortestDistToBFS(G, s, f)); } // --- Console.WriteLine("This is the path, DFS?"); G.GetShortestPathToBFS(G, s, f, ref path); if (path.Count != 0) { foreach (int i in path) { Console.WriteLine(" > " + i.ToString()); } } else { Console.WriteLine("no path..."); } //--- Cycles bool isCycle = G.IsCycle(G); Console.WriteLine("Is Cycle? " + isCycle); if (isCycle) { Stack <int> cycle = new Stack <int>(); cycle = G.GetCycle(G); foreach (int v in cycle) { Console.WriteLine(v); } } // -- Kosaraju, strongly connected components Console.WriteLine("Kosaraju IsStronglyConnected 0 and 2 "); Console.WriteLine(G.IsStronglyConnected(G, 0, 2)); Console.WriteLine("Kosaraju IsStronglyConnected 0 and 1 "); Console.WriteLine(G.IsStronglyConnected(G, 0, 1)); // --- Topological Queue <int> preOrder = G.GetPreOrder(G); Console.WriteLine(" ---preOrder"); while (preOrder.Count != 0) { Console.WriteLine(preOrder.Dequeue()); } Queue <int> postOrder = G.GetPostOrder(G); Console.WriteLine(" ---postOrder"); while (postOrder.Count != 0) { Console.WriteLine(postOrder.Dequeue()); } /* * Stack<int> reversePost = G.GetReversePostOrder(G); * Console.WriteLine(" ---reversePostOrder"); * while (reversePost.Count != 0) * { * Console.WriteLine(reversePost.Pop()); * } */ // G = new Digraph("tinyDG_noCycles.txt"); // <<< setting new Graph Console.WriteLine(" --- Topological reversePostOrder, changing to tinyDG_noCycles.txt"); isCycle = G.IsCycle(G); if (!isCycle) { Stack <int> TopologicalOrder = G.GetTopologicalOrder(G); while (TopologicalOrder.Count != 0) { Console.WriteLine(TopologicalOrder.Pop()); } } else { if (isCycle) { Console.WriteLine("Topological order impossible, there is cycle there... "); } } // --- just describing the orGraph... toString() Console.WriteLine(); Console.WriteLine("---ToString(): " + G.ToString()); Console.ReadLine(); }
public TransitiveClosure(Digraph g) { _searchs = new Search[g.Vertexes.Count]; }
private Topological GetTopological(Digraph g) { // return new DfsTopological(g); return(new BfsTopological(g)); }