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()); }
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; }
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; }
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); }
private static void PreVisitVertice(int v, DfsStats dfsStats) { dfsStats.Visited[v] = true; dfsStats.ComponentNum[v] = dfsStats.ComponentCount; dfsStats.PreVisit[v] = dfsStats.Clock++; }
private static void PostVisitVertice(int v, DfsStats dfsStats) { dfsStats.PostVisit[v] = dfsStats.Clock++; dfsStats.Linearization.AddFirst(v); }