public BreadthFirstSearch(DiGraph g, int s)
        {
            nodes = new VertexNode[g.V];
            for (int i = 0; i < nodes.Length; i++)
            {
                nodes[i]       = new VertexNode(i);
                nodes[i].Color = VertexColor.White;
            }

            bfs(g, s);
        }
 private void dfs(DiGraph g, int v)
 {
     marked[v] = true;
     sc.Last().Add(v);
     foreach (int w in g.Adj(v))
     {
         if (!marked[w])
         {
             dfs(g, w);
         }
     }
 }
Exemple #3
0
        /// <summary>
        /// the reverse version
        /// </summary>
        /// <returns></returns>
        public DiGraph Reverse()
        {
            DiGraph graph = new DiGraph(v);

            for (int i = 0; i < adjs.Length; i++)
            {
                for (int j = 0; j < adjs[i].Count; j++)
                {
                    graph.AddEdge(adjs[i][j], i);
                }
            }
            return(graph);
        }
 public DepthFirstSearch(DiGraph g, int s)
 {
     nodes     = new VertexNode[g.V];
     preOrder  = new Queue <int>();
     postOrder = new Queue <int>();
     for (int i = 0; i < nodes.Length; i++)
     {
         nodes[i]       = new VertexNode(i);
         nodes[i].Color = VertexColor.White;
     }
     nodes[s].Distance = 0;
     dfs(g, s);
 }
Exemple #5
0
 private void dfs(DiGraph g, int s)
 {
     nodes[s].Color      = VertexColor.Gray;
     nodes[s].Discovered = ++time;
     foreach (int w in g.Adj(s))
     {
         if (nodes[w].Color == VertexColor.White)
         {
             nodes[w].Parent = nodes[s];
             dfs(g, w);
         }
     }
     nodes[s].Color    = VertexColor.Black;
     nodes[s].Finished = ++time;
     postOrder.Enqueue(s);
 }
        public StrongConnected(DiGraph g)
        {
            DepthFirstSearchs dfo = new DepthFirstSearchs(g);

            marked = new bool[g.V];
            sc     = new List <HashSet <int> >();
            g      = g.Reverse();
            foreach (int v in dfo.ReversePostOrder())
            {
                if (!marked[v])
                {
                    sc.Add(new HashSet <int>());
                    dfs(g, v);
                }
            }
        }
Exemple #7
0
 public TopologicalSort(DiGraph g)
 {
     postOrder = new Queue <int>();
     nodes     = new VertexNode[g.V];
     for (int i = 0; i < nodes.Length; i++)
     {
         nodes[i]       = new VertexNode(i);
         nodes[i].Color = VertexColor.White;
     }
     for (int i = 0; i < g.V; i++)
     {
         if (nodes[i].Color == VertexColor.White)
         {
             dfs(g, i);
         }
     }
 }
        public DepthFirstSearchs(DiGraph g)
        {
            preOrder  = new Queue <int>();
            postOrder = new Queue <int>();
            nodes     = new VertexNode[g.V];
            for (int i = 0; i < nodes.Length; i++)
            {
                nodes[i]       = new VertexNode(i);
                nodes[i].Color = VertexColor.White;
            }

            for (int i = 0; i < nodes.Length; i++)
            {
                if (nodes[i].Color == VertexColor.White)
                {
                    dfs(g, i);
                }
            }
        }
        private void bfs(DiGraph g, int s)
        {
            nodes[s].Parent   = null;
            nodes[s].Distance = 0;
            nodes[s].Color    = VertexColor.Gray;
            Queue <int> queue = new Queue <int>();

            queue.Enqueue(s);
            while (queue.Count != 0)
            {
                int u = queue.Dequeue();
                foreach (var v in g.Adj(u))
                {
                    if (nodes[v].Color == VertexColor.White)
                    {
                        nodes[v].Color    = VertexColor.Gray;
                        nodes[v].Distance = nodes[u].Distance + 1;
                        nodes[v].Parent   = nodes[u];
                        queue.Enqueue(v);
                    }
                }
                nodes[u].Color = VertexColor.Black;
            }
        }