Beispiel #1
0
        public DepthFirstSearch(GraphList g, int source)
        {
            _visited = new bool[g.V()];
            _edgeTo  = new int[g.V()];
            _source  = source;

            for (int i = 0; i < _edgeTo.Length; ++i)
            {
                _edgeTo[i] = -1;
            }

            Dfs(g, source);
        }
Beispiel #2
0
        public static void DisplayGraph(GraphList g)
        {
            Console.WriteLine("{0} verticies, {1} edges", g.V(), g.E());

            for (int i = 0; i < g.V(); ++i)
            {
                Console.Write("{0}: ", i);
                foreach (var u in g.Adj(i))
                {
                    Console.Write("{0} ", u);
                }
                Console.WriteLine();
            }
        }
Beispiel #3
0
        public static void bfs(GraphList g, int vertex)
        {
            BreadthFirstSearch search = new BreadthFirstSearch(g, vertex);

            for (int v = 0; v < g.V(); v++)
            {
                Console.Write(vertex + " To " + v + ": ");
                if (search.HasPathTo(v))
                {
                    foreach (int x in search.PathTo(v))
                    {
                        if (x == vertex)
                        {
                            Console.Write(x);
                        }
                        else
                        {
                            Console.Write("-" + x);
                        }
                    }
                }
                else
                {
                    Console.Write("No path");
                }

                Console.WriteLine();
            }
        }