Exemple #1
0
 public void UndirectedGraph32_Explore_Test()
 {
     var g = GraphFactory.CreateUndirectedGraph32();
     var stats = new DfsStats(g.V);
     Dfs.Explore(g, 0, stats);     //explore Vertice A
     Debug.WriteLine(stats.ToString());
 }
Exemple #2
0
        public static DfsStats DepthFirstSearch(GraphBase g)
        {
            var ds = new DfsStats(g.V);

            for (var i = 0; i < g.V; i++)
                ds.Visited[i] = false;

            for (var i = 0; i < g.V; i++)
                if (!ds.Visited[i])
                {
                    ds.ComponentCount++;
                    Explore(g, i, ds);
                }

            return ds;
        }
Exemple #3
0
        public static DfsStats StrongConnectedComponentAlgorithm(DirectedGraph g)
        {
            g.ReverseGraph();//reverse G
            var linearization = GetLinearization(g);

            var ds = new DfsStats(g.V) { Clock = 1, ComponentCount = 0 };

            g.ReverseGraph();//reverse G back
            foreach (var v in linearization)
            {
                if (!ds.Visited[v])
                {
                    ds.ComponentCount++;
                    Explore(g, v, ds);
                }
            }

            return ds;
        }
Exemple #4
0
        public static void Explore(GraphBase g, int v, DfsStats dfsStats)
        {
            PreVisitVertice(v, dfsStats);

            foreach (var e in g.Adjacent(v))
            {
                if (!dfsStats.Visited[e.V2])
                {
                    Explore(g, e.V2, dfsStats);
                }
                //else if (e < v)
                //only directed graph has 'back edge'
                else if (g is DirectedGraph && e.V2 < v)//e < v is not right
                {
                    dfsStats.BackEdges.Add(new Edge { V1 = v, V2 = e.V2 });
                }
            }

            PostVisitVertice(v, dfsStats);
        }
Exemple #5
0
 private static void PreVisitVertice(int v, DfsStats dfsStats)
 {
     dfsStats.Visited[v] = true;
     dfsStats.ComponentNum[v] = dfsStats.ComponentCount;
     dfsStats.PreVisit[v] = dfsStats.Clock++;
 }
Exemple #6
0
 private static void PostVisitVertice(int v, DfsStats dfsStats)
 {
     dfsStats.PostVisit[v] = dfsStats.Clock++;
     dfsStats.Linearization.AddFirst(v);
 }