Ejemplo n.º 1
0
 public DirectedDFS(Digraph G, int s)
 {
     marked = new bool[G.V];
     edgeTo = new int[G.V];
     this.s = s;
     DFS(G, s);
 }
Ejemplo n.º 2
0
 public DirectedBFS(Digraph G, IEnumerable<int> sources)
 {
     marked = new bool[G.V];
     edgeTo = new int[G.V];
     this.s = new HashSet<int>(sources);
     BFS(G, s);
 }
Ejemplo n.º 3
0
 private void DFS(Digraph G, int v)
 {
     marked[v] = true;
     id[v] = count;
     foreach (var w in G.Adj(v))
         if (!marked[w])
             DFS(G, w);
 }
Ejemplo n.º 4
0
 public DirectedBFS(Digraph G, int source)
 {
     marked = new bool[G.V];
     edgeTo = new int[G.V];
     this.s = new HashSet<int>();
     s.Add(source);
     BFS(G, s);
 }
Ejemplo n.º 5
0
 public DepthFirstOrder(Digraph G)
 {
     reversePost = new Stack<int>();
     marked = new bool[G.V];
     for (int v = 0; v < G.V; v++)
         if (!marked[v])
             DFS(G, v);
 }
Ejemplo n.º 6
0
 private void DFS(Digraph G, int v)
 {
     marked[v] = true;
     foreach (var w in G.Adj(v))
     {
         if (!marked[w]) DFS(G,w);
     }
     reversePost.Push(v);
 }
Ejemplo n.º 7
0
 private void DFS(Digraph G, int v)
 {
     marked[v] = true;
     foreach (var w in G.Adj(v))
     {
         if (!marked[w])
         {
             DFS(G, w);
             edgeTo[w] = v;
         }
     }
 }
Ejemplo n.º 8
0
 public SCC_KosarajuSharir(Digraph G)
 {
     marked = new bool[G.V];
     id = new int[G.V];
     DepthFirstOrder dfs = new DepthFirstOrder(G.Reverse());
     foreach (int v in dfs.GetReversePost())
     {
         if (!marked[v])
         {
             DFS(G, v);
             count++;
         }
     }
 }
Ejemplo n.º 9
0
        private void BFS(Digraph G, IEnumerable<int> sources)
        {
            Queue<int> q = new Queue<int>();
            foreach (int source in sources)
            {
                q.Enqueue(source);
            }

            while (!(q.Count == 0))
            {
                int v = q.Dequeue();
                foreach (int w in G.Adj(v))
                {
                    if (!marked[w])
                    {
                        q.Enqueue(w);
                        marked[w] = true;
                        edgeTo[w] = v;
                    }
                }
            }
        }
Ejemplo n.º 10
0
        public static void ClassInit(TestContext context)
        {
            basicGraph = new Graph(9);
            basicGraph.AddEdge(0, 1);
            basicGraph.AddEdge(0, 2);
            basicGraph.AddEdge(0, 5);
            basicGraph.AddEdge(0, 6);
            basicGraph.AddEdge(2, 7);
            basicGraph.AddEdge(6, 4);
            basicGraph.AddEdge(4, 3);
            basicGraph.AddEdge(4, 5);

            baseicDigraph = new Digraph(7);
            baseicDigraph.AddEdge(5, 0);
            baseicDigraph.AddEdge(2, 4);
            baseicDigraph.AddEdge(3, 2);
            baseicDigraph.AddEdge(1, 2);
            baseicDigraph.AddEdge(0, 1);
            baseicDigraph.AddEdge(4, 3);
            baseicDigraph.AddEdge(3, 5);
            baseicDigraph.AddEdge(0, 2);
            baseicDigraph.AddEdge(6, 5);
        }
Ejemplo n.º 11
0
 public Digraph Reverse()
 {
     var rev = new Digraph(this._V);
     for (int v = 0; v < _V; v++)
     {
         foreach (int w in adj[v])
         {
             rev.AddEdge(w, v);
         }
     }
     return rev;
 }
Ejemplo n.º 12
0
 public void Digraph_API_Test()
 {
     Digraph G = new Digraph(3);
     G.AddEdge(0, 1);
     G.AddEdge(0, 2);
     List<int> e = new List<int>(G.Adj(0));
     Assert.AreEqual(1, e[0]);
 }
Ejemplo n.º 13
0
 public void DepthFirstOrder_Test()
 {
     var G = new Digraph(4);
     G.AddEdge(3, 2);
     G.AddEdge(3, 1);
     G.AddEdge(1, 0);
     G.AddEdge(2, 0);
     var DFO = new DepthFirstOrder(G);
     var actual = new List<int>( DFO.GetReversePost());
     Assert.AreEqual(3, actual[0]);
     Assert.AreEqual(0, actual[3]);
 }