Q.) given an unweighted and undirected graph write a program to check is it a tree or not.
input:
In first line contain n (numbers of nodes) and m (number of edges)
then m lines contain m egdes of grapg (two integer u and v)
1<=N<=10000 ,1<=M<=20000
output:
print YES if it is a tree else print NO.
Just we have to check the graph is connected and have no cycle then it will be tree else no.
----------------------------------------------------------------
#include
#include
#include
#include
using namespace std;
// This class represents a directed graph using adjacency list representation
class Graph
{
int V; // No. of vertices
list *adj; // Pointer to an array containing adjacency lists
bool isCyclicUtil(int v, bool visited[], bool *rs);
public:
Graph(int V); // Constructor
void addEdge(int v, int w); // function to add an edge to graph
bool BFS(int s); // prints BFS traversal from a given source s
bool isCyclic(); // returns true if there is a cycle in this graph
};
Graph::Graph(int V)
{
this->V = V;
adj = new list[V];
}
void Graph::addEdge(int v, int w)
{
adj[v].push_back(w); // Add w to v’s list.
}
bool Graph::BFS(int s)
{
// Mark all the vertices as not visited
bool *visited = new bool[V];
for(int i = 0; i < V; i++)
visited[i] = false;
// Create a queue for BFS
list queue;
// Mark the current node as visited and enqueue it
visited[s] = true;
queue.push_back(s);
// 'i' will be used to get all adjacent vertices of a vertex
list::iterator i;
while(!queue.empty())
{
// Dequeue a vertex from queue and print it
s = queue.front();
// cout << s << " ";
queue.pop_front();
// Get all adjacent vertices of the dequeued vertex s
// If a adjacent has not been visited, then mark it visited
// and enqueue it
for(i = adj[s].begin(); i != adj[s].end(); ++i)
{
if(!visited[*i])
{
visited[*i] = true;
queue.push_back(*i);
}
}
}
for(int k=0;k
{
if(visited[k]==false)
return false;
}
return true;
}
bool Graph::isCyclicUtil(int v, bool visit[], bool *recStack)
{
if(visit[v] == false)
{
// Mark the current node as visited and part of recursion stack
visit[v] = true;
recStack[v] = true;
// Recur for all the vertices adjacent to this vertex
list::iterator i;
for(i = adj[v].begin(); i != adj[v].end(); ++i)
{
if ( !visit[*i] && isCyclicUtil(*i, visit, recStack) )
return true;
else if (recStack[*i])
return true;
}
}
recStack[v] = false; // remove the vertex from recursion stack
return false;
}
// Returns true if the graph contains a cycle, else false.
bool Graph::isCyclic()
{
// Mark all the vertices as not visited and not part of recursion
// stack
bool *visit = new bool[V];
bool *recStack = new bool[V];
for(int i = 0; i < V; i++)
{
visit[i] = false;
recStack[i] = false;
}
// Call the recursive helper function to detect cycle in different
// DFS trees
for(int i = 0; i < V; i++)
if (isCyclicUtil(i, visit, recStack))
return true;
return false;
}
int main()
{
int j,n,m,a,b;
scanf("%d %d",&n,&m);
Graph g(n);
for(j=0;j
{
scanf("%d %d",&a,&b);
g.addEdge(a-1,b-1);
}
if(g.BFS(0))
{
if(g.isCyclic())
cout << "NO"<
else
cout << "YES"<
}
else
printf("NO\n");
return 0;
}