Beispiel #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;
     }
 }
Beispiel #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);
                    }
                }
            }
        }
Beispiel #4
0
 protected override void CreateTraverse(ICollection <int> items, bool[] marked, GraphBase graph)
 {
     dfs(_StartVertex, items, marked, graph);
 }
Beispiel #5
0
 protected abstract void CreateTraverse(ICollection <int> items, bool[] marked, GraphBase graph);