Exemplo n.º 1
0
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;
}
// 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;
}
// 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;
}
// 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;
}
//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;
			}
		}
	}