public static void PrintDot(Graph graph, TextWriter writer) { GraphTraversal traversal = new BFS(graph); writer.WriteLine("{0} {1} {{", graph.IsDirected ? "digraph" : "graph", graph.GetHashCode()); for (int i = 0; i < graph.V; i++) { traversal.Traverse(i, (G, V, E) => { writer.WriteLine("{0} {1} {2};", V, G.IsDirected ? "->" : "--", E.Y); }, (G, V) => { writer.WriteLine("{0};", V); }); } writer.WriteLine("}"); }
public ConnectedComponents(Graph G) { this.G = G; }
public GraphTraversal(Graph G) { this.G = G; VertexState = new State[G.V]; Parent = new int[G.V]; for (int i = 0; i < G.V; i++) VertexState[i] = State.Undiscovered; }
public static Graph Read(TextReader reader) { Graph G = new Graph(); string line = reader.ReadLine(); for (int i = 0; i < int.Parse(line); i++) G.CreateVertex(i); while ((line = reader.ReadLine()) != null) { string[] tokens = line.Split(','); G.InsertEdge(int.Parse(tokens[0]) - 1, int.Parse(tokens[1]) - 1, false); } return G; }
public static Graph RandGraph(int V, int E, int seed = -1) { if (seed == -1) seed = DateTime.Now.Millisecond; Graph G = new Graph(); G.IsDirected = false; Random random = new Random(seed); double p = 2 * E / V / (V - 1.0); for (int i = 0; i < V; i++) { G.CreateVertex(i); for (int j = 0; j < i; j++) if (random.NextDouble() < p) G.InsertEdge(i, j, G.IsDirected); } return G; }
public BFS(Graph G) : base(G) { }
public DFS(Graph G) : base(G) { EntryTime = new int[G.V]; ExitTime = new int[G.V]; time = 0; }