コード例 #1
0
void Graph::DFS(int v)
{
	// Mark all the vertices as not visited
	bool *visited = new bool[V*V];
	for (int i = 0; i < V*V; i++)
		visited[i] = false;

	// Call the recursive helper function to print DFS traversal
	DFSUtil(v, visited);
}
コード例 #2
0
ファイル: graph.c プロジェクト: WangGL1985/boolan
void Graph::DFSUtil(int v, bool visited[])
{
    // Mark the current node as visited and print it
    visited[v] = true;
    cout << v << " ";
 
    // Recur for all the vertices adjacent to this vertex
    list<int>::iterator i;
    for(i = adj[v].begin(); i != adj[v].end(); ++i)
        if(!visited[*i])
            DFSUtil(*i, visited);
}
コード例 #3
0
void islandRowWiseColWise(char a[ROW][COL],int rows,int cols){
	int visited[ROW][COL];
	for(int i=0;i<rows;i++){
		for(int j=0;j<cols;j++){
			visited[i][j]=false;
		}
	}

	for(int i=0;i<rows;i++){
		for(int j=0;j<cols;j++){
			if(visited[i][j]==false && a[i][j]=='X'){
				DFSUtil(a,visited,i,j,rows,cols);
			}
		}
	}
}
コード例 #4
0
bool Graph::DFSUtil(int v, bool visited[])
{
	if (v == V*V-1){
		int count = 0;
		for (int t = 0; t < V*V; t++)
		if (visited[t]) count++;
		if (2* count < V*V) return false;
		return true;
	}

	visited[v] = true;

	// Recur for all the vertices adjacent to this vertex
	
	
	random_shuffle(adj[v].begin(), adj[v].end()); 
	std::vector<int>::iterator i = adj[v].begin();
	
	while (i != adj[v].end()){
		int j = *i;

		if (!visited[j]){
			std::vector <int>::iterator k = find(adj[j].begin(), adj[j].end(), v);
			adj[j].erase(adj[j].begin() + distance(adj[j].begin(), k));
			i = adj[v].erase(i);

			//
			temp.a = j;
			temp.b = v;

			ab.push_back(temp);
			//

			if (DFSUtil(j, visited))return true;

		}
		else{
			i++;
		}
	}
		

	return false;
}