Exemple #1
0
void DepthFirstSearch::dfs(Graph g,int s){
	count++;
	marked[s] = true;
	for(int j: g.Adj(s)){
		if(!marked[j]) dfs(g,j);
		edgeTo[j] = s;
	}
}
void DepthFirstPaths::dfs(const Graph &G, std::size_t v)
{
	marked[v] = true;
	for(const int &w : G.Adj(v))
	{
		if(!marked[w])
		{	edgeTo[w] = v;
			dfs(G,w);
		}
	}
}
Exemple #3
0
void BreadFirstSearch::bfs(Graph g,int s){
	std::deque<int> candi;
	for (int i = 0; i < size; ++i)
	{
		distTo[i] = INFINITY;
	}
	
	marked[s] = true;
	distTo[s] = 0;
	candi.push_back(s);
	int cur = -1;
	while(candi.size() > 0){
		cur = candi.front();
		candi.pop_front();
		for(int j: g.Adj(cur)){
			if(!marked[j]){
				marked[j] = true;
				edgeTo[j] = cur;
				distTo[j] = distTo[cur] + 1;
				candi.push_back(j);
			}
		}
	}
}