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