예제 #1
0
        private void Dfs(GraphList g, int v)
        {
            _visited[v] = true;

            foreach (var u in g.Adj(v))
            {
                if (!_visited[u])
                {
                    _edgeTo[u] = v;
                    Dfs(g, u);
                }
            }
        }
예제 #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();
            }
        }
예제 #3
0
        private void Bfs(GraphList g, int vertex)
        {
            Queue <int> queue = new Queue <int>();

            queue.Enqueue(vertex);
            _visited[vertex] = true;

            while (queue.Count != 0)
            {
                int v = queue.Dequeue();
                foreach (var u in g.Adj(v))
                {
                    if (!_visited[u])
                    {
                        queue.Enqueue(u);
                        _visited[u] = true;
                        _edgeTo[u]  = v;
                    }
                }
            }
        }