当前位置: 首页>>代码示例>>C++>>正文


C++ Graph::DFSUtil方法代码示例

本文整理汇总了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;
}
开发者ID:Alex-Tsvetanov,项目名称:Informatics,代码行数:33,代码来源:B5.cpp

示例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;
}
开发者ID:michaelmeganet,项目名称:test-singly-grapgh,代码行数:35,代码来源:Strong-Connected.c

示例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;
}
开发者ID:sidneifilho,项目名称:maratona,代码行数:43,代码来源:1835-promessa_campanha.cpp

示例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;
}
开发者ID:guluuu3,项目名称:spoj,代码行数:43,代码来源:string_chaining_using_eulerian_circuit_graph.cpp

示例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;
            }
        }
    }	
开发者ID:s1syphus,项目名称:ComplexSystems511FinalProject,代码行数:38,代码来源:scc.cpp


注:本文中的Graph::DFSUtil方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。