Exemplo n.º 1
0
 public UndirectedGraphTraverse(GraphBase graph)
 {
     _Graph  = graph;
     _Items  = new LinkedList <int>();
     _EdgeTo = new int[graph.V];
     for (int i = 0; i < _EdgeTo.Length; i++)
     {
         _EdgeTo[i] = -1;
     }
 }
Exemplo n.º 2
0
        void dfs(int vertex, ICollection <int> items, bool[] marked, GraphBase graph)
        {
            if (marked[vertex])
            {
                return;
            }

            marked[vertex] = true;
            items.Add(vertex);

            foreach (var w in graph.GetVertexAdjcency(vertex))
            {
                if (!marked[w])
                {
                    dfs(w, items, marked, graph);
                    _EdgeTo[w] = vertex;
                }
            }
        }
        void bfs(int startVertex, ICollection <int> items, bool[] marked, GraphBase graph)
        {
            var queue = new Queue <int>();

            queue.Enqueue(startVertex);
            items.Add(startVertex);
            marked[startVertex] = true;

            while (queue.Count > 0)
            {
                var vertex = queue.Dequeue();
                foreach (var w in graph.GetVertexAdjcency(vertex))
                {
                    if (!marked[w])
                    {
                        marked[w] = true;
                        items.Add(w);
                        _EdgeTo[w] = vertex;
                        queue.Enqueue(w);
                    }
                }
            }
        }
Exemplo n.º 4
0
 protected override void CreateTraverse(ICollection <int> items, bool[] marked, GraphBase graph)
 {
     dfs(_StartVertex, items, marked, graph);
 }
Exemplo n.º 5
0
 protected abstract void CreateTraverse(ICollection <int> items, bool[] marked, GraphBase graph);