- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Program to Check if a Directed Graph is a Tree or Not Using DFS
A graph is a tree if it does not contain any cycle. This is a C++ program to check whether a directed graph is tree or not using DFS.
Algorithm
Begin function cyclicUtil() : a) Mark the current node as visited and part of recursion stack b) Recur for all the vertices adjacent to this vertex. c) Remove the vertex from recursion stack. function cyclic() : a) Mark all the vertices as not visited and not part of recursion stack b) Call the CyclicUtill() function to detect cycle in different trees End
Example
#include<iostream> #include <list> #include <limits.h> using namespace std; class G { int n; list<int> *adj; //contain adjacent list. bool CyclicUtil(int v, bool visited[], bool *rs); public: G(int V); // Constructor void addEd(int v, int w); bool cyclic(); }; G::G(int n) { this->n = n; adj = new list<int> [n]; } void G::addEd(int v, int u) //to add edges in the graph { adj[v].push_back(u); //add u to v’s list } bool G::CyclicUtil(int v, bool visited[], bool *recurS) { if (visited[v] == false) { visited[v] = true; //Mark the current node as visited and part of recursion stack recurS[v] = true; // Recur for all the vertices adjacent to this vertex. list<int>::iterator i; for (i = adj[v].begin(); i != adj[v].end(); ++i) { if (!visited[*i] && CyclicUtil(*i, visited, recurS)) return true; else if (recurS[*i]) return true; } } recurS[v] = false; //Remove the vertex from recursion stack. return false; } //check if the graph is tree or not bool G::cyclic() { //Mark all the vertices as not visited and not part of recursion stack bool *visited = new bool[n]; bool *recurS = new bool[n]; for (int i = 0; i < n; i++) { visited[i] = false; recurS[i] = false; } // Call the CyclicUtill() function to detect cycle in different trees for (int i = 0; i < n; i++) if (CyclicUtil(i, visited, recurS)) return true; return false; } int main() { G g(4); g.addEd(0, 2); g.addEd(1, 2); g.addEd(2, 0); g.addEd(3, 2); if (g.cyclic()) cout << "Directed Graph isn't a tree"; else cout << "Directed Graph is a tree"; return 0; }
Output
Directed Graph isn't a tree
- Related Articles
- C++ Program to Check if an UnDirected Graph is a Tree or Not Using DFS
- C++ Program to Check the Connectivity of Directed Graph Using DFS
- Check if a directed graph is connected or not in C++
- Check if a given graph is Bipartite using DFS in C++ program
- Check if a given graph is tree or not
- C++ Program to Check whether Graph is a Bipartite using DFS
- Check if a given graph is Bipartite using DFS using C++
- Check if a given tree graph is linear or not in C++
- C++ program to Check if a Given Binary Tree is an AVL Tree or Not
- A program to check if a binary tree is BST or not in C ?
- C++ Program to Apply DFS to Perform the Topological Sorting of a Directed Acyclic Graph
- Check if a graph is strongly connected - Set 1 (Kosaraju using DFS) in C++
- C++ Program to Check Whether it is Weakly Connected or Strongly Connected for a Directed Graph
- Check if a given directed graph is strongly connected in C++
- C++ Program to Check the Connectivity of Undirected Graph Using DFS

Advertisements