0% found this document useful (0 votes)
18 views8 pages

Assignment 10

The document contains code to implement topological sorting of a graph and finding strongly connected components (SCC) in a graph. It includes code for a Graph class with functions to add edges, perform DFS, get the transpose graph, and print the SCCs. Main functions provide driver code to test the topological sorting and SCC finding algorithms.

Uploaded by

Nilesh Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views8 pages

Assignment 10

The document contains code to implement topological sorting of a graph and finding strongly connected components (SCC) in a graph. It includes code for a Graph class with functions to add edges, perform DFS, get the transpose graph, and print the SCCs. Main functions provide driver code to test the topological sorting and SCC finding algorithms.

Uploaded by

Nilesh Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

ASSIGNMENT-10

NILESH

KUMAR

06116403222

Btech CSE

USICT

Question-1 WAP to implement Topological sort.


CODE:

#include <iostream>

#include <vector>

#include <stack>

#include <unordered_set>

using namespace std;

class Graph {

int V; // Number of vertices

// Adjacency list

vector<vector<int>> adj;

// Helper function for DFS


void DFSUtil(int v, vector<bool> &visited, stack<int> &Stack) {

visited[v] = true;
// Recur for all vertices adjacent to this vertex

for (int u : adj[v]) {

if (!visited[u])

DFSUtil(u, visited, Stack);

// Push current vertex to stack which stores topological sort

Stack.push(v);

public:

Graph(int V) : V(V) {

adj.resize(V);

// Function to add an edge to the graph

void addEdge(int u, int v) {

adj[u].push_back(v);

}
// Function to perform topological sorting

void topologicalSort() {

stack<int> Stack;

vector<bool> visited(V, false);


// Call the recursive helper function to store topological

// sort starting from all vertices one by one

for (int i = 0; i < V; ++i) {

if (!visited[i])

DFSUtil(i, visited, Stack);

// Print contents of stack

while (!Stack.empty()) {

cout << Stack.top() << " ";

Stack.pop();

};

int main() {
// Create a graph given in the example

Graph g(6);

g.addEdge(5, 2);

g.addEdge(5, 0);

g.addEdge(4, 0);

g.addEdge(4, 1);

g.addEdge(2, 3);
g.addEdge(3, 1);

cout << "Topological Sort: ";

g.topologicalSort();

return 0;

OUTPUT:

Question-2 WAP to find SCC in graph.


CODE:
#include<iostream>
#include<list>

#include<stack>
#include<vector>
using namespace std;

class Graph {

int V;
list<int> *adj;
void fillOrder(int v, bool visited[], stack<int> &Stack);
void DFSUtil(int v, bool visited[]);

public:

Graph(int V);
void addEdge(int v, int w);
Graph getTranspose();
void printSCCs();
};

Graph::Graph(int V) {
this->V = V;

adj = new list<int>[V];


}

void Graph::DFSUtil(int v, bool visited[]) {


visited[v] = true;
cout << v << " ";
list<int>::iterator i;

for (i = adj[v].begin(); i != adj[v].end(); ++i)


if (!visited[*i])

DFSUtil(*i, visited);
}

Graph Graph::getTranspose() {
Graph g(V);
for (int v = 0; v < V; v++) {

list<int>::iterator i;
for(i = adj[v].begin(); i != adj[v].end(); ++i) {
g.adj[*i].push_back(v);

}
return g;

void Graph::addEdge(int v, int w) {

adj[v].push_back(w);
}

void Graph::fillOrder(int v, bool visited[], stack<int> &Stack) {


visited[v] = true;
list<int>::iterator i;

for(i = adj[v].begin(); i != adj[v].end(); ++i)


if(!visited[*i])

fillOrder(*i, visited, Stack);


Stack.push(v);
}

void 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;
while (Stack.empty() == false) {
int v = Stack.top();
Stack.pop();

if (visited[v] == false) {

gr.DFSUtil(v, visited);
cout << endl;
}

}
}

int main() {

Graph g(5);

g.addEdge(1, 0);
g.addEdge(0, 2);

g.addEdge(2, 1);
g.addEdge(0, 3);

g.addEdge(3, 4);

cout << "Strongly Connected Components are:\n";


g.printSCCs();

return 0;
}

OUTPUT:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy