Vishwajith S.S. 205002127 Ex 11 1) Design, Develop and Implement a Program in C for the following operations on Graph(G) of Cities. (a) Create a Graph of N cities using Adjacency Matrix. (b) Print all the nodes reachable from a given starting node in a digraph using DFS and BFS method. (c) Find the complexity of the Graph. Code: #include <iostream> using namespace std; int adjacency[50][50], n, visited[50]; int q[20], front = -1, rear = -1; int s[20], top = -1, count = 0; void bfs(int v){ int i, cur; visited[v] = 1; q[++rear] = v; while (front != rear){ cur = q[++front]; for (i = 1; i <= n; i++){ if ((adjacency[cur][i] == 1) && (visited[i] == 0)){ q[++rear] = i; visited[i] = 1; cout << i << " "; } } } } void dfs(int v){ int i; visited[v] = 1; s[++top] = v; for (i = 1; i <= n; i++){ if (adjacency[v][i] == 1 && visited[i] == 0){ cout << i << " "; dfs(i); } } } int main(){ int ch, start, i, j; cout << "\nEnter the number of vertices in graph: "; cin >> n; cout << "\nEnter the adjacency matrix:\n"; for (i = 1; i <= n; i++){ for (j = 1; j <= n; j++) cin >> adjacency[i][j]; } for (i = 1; i <= n; i++) visited[i] = 0; cout << "\nEnter the starting vertex: "; cin >> start; cout << "\n1. BFS search"; cout << "\n2. DFS search"; cout << "\n3:Exit"; cout << "\nEnter your choice: "; cin >> ch; switch (ch){ case 1: cout << "\nNodes reachable from starting vertex " << start << " are: " << start; bfs(start); for (i = 1; i <= n; i++){ if (visited[i] == 0) cout << "\nThe vertex that is not reachable is " << i; } break; case 2: cout << "\nNodes reachable from starting vertex " << start << " are:\n"; dfs(start); break; } } Output: 2) Show the ordering of vertices produced by the topological sort algorithm. Code: #include <iostream> #include <list> #include <stack> using namespace std; class Graph{ int V; list<int> *adj; void topologicalSortUtil(int v, bool visited[], stack<int> &Stack); public: Graph(int V); void addEdge(int v, int w); void topologicalSort(); }; Graph::Graph(int V){ this->V = V; adj = new list<int>[V]; } void Graph::addEdge(int v, int w){ adj[v].push_back(w); } void Graph::topologicalSortUtil(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]) topologicalSortUtil(*i, visited, Stack); Stack.push(v); } void Graph::topologicalSort(){ 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) topologicalSortUtil(i, visited, Stack); while (Stack.empty() == false){ cout << Stack.top() << " "; Stack.pop(); } } int main(){ Graph g(6); g.addEdge(0, 1); g.addEdge(0, 3); g.addEdge(1, 2); g.addEdge(1, 3); g.addEdge(2, 3); g.addEdge(2, 4); g.addEdge(3, 4); g.addEdge(2, 5); g.addEdge(3, 5); g.addEdge(4, 5); cout << "Topological Sort of the given graph n is: g.topologicalSort(); return 0; } ";