本文整理汇总了C++中Graph::DFSUtil方法的典型用法代码示例。如果您正苦于以下问题:C++ Graph::DFSUtil方法的具体用法?C++ Graph::DFSUtil怎么用?C++ Graph::DFSUtil使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Graph
的用法示例。
在下文中一共展示了Graph::DFSUtil方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: printSCCs
int Graph::printSCCs()
{
stack<int> Stack;
bool *visited = new bool[V];
for(int i = 0; i < V; i++)
visited[i] = false;
for(int i = 0; i < V; i++)
if(visited[i] == false)
fillOrder(i, visited, Stack);
Graph gr = getTranspose();
for(int i = 0; i < V; i++)
visited[i] = false;
int ans = 0;
while (Stack.empty() == false)
{
int v = Stack.top();
Stack.pop();
if (visited[v] == false)
{
ans ++;
gr.DFSUtil(v, visited);
// cout << endl;
}
}
return ans;
}
示例2: isSC
// The main function that returns true if graph is strongly connected
bool Graph::isSC()
{
// St1p 1: Mark all the vertices as not visited (For first DFS)
bool visited[V];
for (int i = 0; i < V; i++)
visited[i] = false;
// Step 2: Do DFS traversal starting from first vertex.
DFSUtil(0, visited);
// If DFS traversal doesn’t visit all vertices, then return false.
for (int i = 0; i < V; i++)
if (visited[i] == false)
return false;
// Step 3: Create a reversed graph
Graph gr = getTranspose();
// Step 4: Mark all the vertices as not visited (For second DFS)
for(int i = 0; i < V; i++)
visited[i] = false;
// Step 5: Do DFS for reversed graph starting from first vertex.
// Staring Vertex must be same starting point of first DFS
gr.DFSUtil(0, visited);
// If all vertices are not visited in second DFS, then
// return false
for (int i = 0; i < V; i++)
if (visited[i] == false)
return false;
return true;
}
示例3: printSCCs
// The main function that finds and prints all strongly connected
// components
int Graph::printSCCs()
{
stack<int> Stack;
// Mark all the vertices as not visited (For first DFS)
int visited[V];
memset(visited,0, V*sizeof(int));
// Fill vertices in stack according to their finishing times
for(int i = 0; i < V; i++)
if(!visited[i])
fillOrder(i, visited, Stack);
// Create a reversed graph
Graph gr = getTranspose();
// Mark all the vertices as not visited (For second DFS)
memset(visited, 0, V*sizeof(int));
// Now process all vertices in order defined by Stack
int qt = 0;
while (!Stack.empty())
{
// Pop a vertex from stack
int v = Stack.top();
Stack.pop();
// Print Strongly connected component of the popped vertex
if (!visited[v])
{
qt++;
gr.DFSUtil(v, visited);
//cout << endl;
}
}
/// verificando o numero de componentes fortemente conexos distintos,
/// o resultado será o número de componentes - 1 pois indica a estrada que interconecta dois ou mais
qt--;
return qt;
}
示例4: isSC
// This function returns true if all non-zero degree vertices of
// graph are strongly connected. Please refer
// http://www.geeksforgeeks.org/connectivity-in-a-directed-graph/
bool Graph::isSC()
{
// Mark all the vertices as not visited (For first DFS)
bool visited[V];
for (int i = 0; i < V; i++)
visited[i] = false;
// Find the first vertex with non-zero degree
int n;
for (n = 0; n < V; n++)
if (adj[n].size() > 0)
break;
// Do DFS traversal starting from first non zero degree vertex.
DFSUtil(n, visited);
// If DFS traversal doesn’t visit all vertices, then return false.
for (int i = 0; i < V; i++)
if (adj[i].size() > 0 && visited[i] == false)
return false;
// Create a reversed graph
Graph gr = getTranspose();
// Mark all the vertices as not visited (For second DFS)
for (int i = 0; i < V; i++)
visited[i] = false;
// Do DFS for reversed graph starting from first vertex.
// Staring Vertex must be same starting point of first DFS
gr.DFSUtil(n, visited);
// If all vertices are not visited in second DFS, then
// return false
for (int i = 0; i < V; i++)
if (adj[i].size() > 0 && visited[i] == false)
return false;
return true;
}
示例5: printSCCs
//The main function that finds and prints all strongly connected components
void Graph::printSCCs(){
stack<int> Stack;
//Mark all the vertices as not visited (For first DFS)
bool *visited = new bool[V];
for(int i = 0; i < V; i++){
visited[i] = false;
}
//Fill the vertices in stack according to their finishing times
for(int i = 0; i < V; i++){
if(visited[i] == false){
fillOrder(i, visited, Stack);
}
}
//Create a reversed Graph
Graph gr = getTranspose();
//Mark all the vertices as not visited (For second DFS)
for(int i = 0; i < V; i++){
visited[i] = false;
}
//Now process all vertices in order defined by Stack
while(!Stack.empty()){
//Pop a vertex from stack
int v = Stack.top();
Stack.pop();
//Print Strongly Connected Component of the popped vertex
if(visited[v] == false){
gr.DFSUtil(v, visited);
cout<<endl;
}
}
}