public GraphDepthFirst(Graph_Adjacency_List G, int s) { int length = G.numberVertices; _hasBeenVisited = new bool[length]; edgeTo = new int[length]; DepthFirstSearch(G, s); }
private void DepthFirstSearch(Graph_Adjacency_List G, int vertex) { _hasBeenVisited[vertex] = true; _connectedVertexIdGroup[vertex] = _count; foreach (var adjacentVertex in G.FindAdjacentVertices(vertex)) { if (!_hasBeenVisited[adjacentVertex]) { DepthFirstSearch(G, adjacentVertex); } } }
private int _count; // # of components public GraphConnectedComponents(Graph_Adjacency_List G) { _hasBeenVisited = new bool[G.numberVertices]; _connectedVertexIdGroup = new int[G.numberVertices]; for (int v = 0; v < G.numberVertices; v++) { if (!_hasBeenVisited[v]) { DepthFirstSearch(G, v); _count++; } } }
private void DepthFirstSearch(Graph_Adjacency_List G, int vertex) { _hasBeenVisited[vertex] = true; foreach (var adjacentVertex in G.FindAdjacentVertices(vertex)) { if (!_hasBeenVisited[adjacentVertex]) { DepthFirstSearch(G, adjacentVertex); edgeTo[adjacentVertex] = vertex; } } }
private void BreadthFirstSearch(Graph_Adjacency_List G, int s) { Queue <int> queue = new Queue <int>(); queue.Enqueue(s); marked[s] = true; while (queue.Count > 0) { int v = queue.Dequeue(); foreach (var w in G.FindAdjacentVertices(v)) { if (!marked[w]) { queue.Enqueue(w); marked[w] = true; edgeTo[w] = v; } } } }