Exemplo n.º 1
0
 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("}");
 }
Exemplo n.º 2
0
 public ConnectedComponents(Graph G)
 {
     this.G = G;
 }
Exemplo n.º 3
0
        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;
        }
Exemplo n.º 4
0
        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;
        }
Exemplo n.º 5
0
        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;
        }
Exemplo n.º 6
0
 public BFS(Graph G)
     : base(G)
 {
 }
Exemplo n.º 7
0
 public DFS(Graph G)
     : base(G)
 {
     EntryTime = new int[G.V];
     ExitTime = new int[G.V];
     time = 0;
 }