How do I check if a graph is not a tree? - data-structures

How do I check if a given undirected graph is a tree or not? Is there any correlation between the two?
I thought of checking if the graph is connected or not? But is it sufficient?

No for undirected graph to be a tree u have to check for 2 conditions.
1) The graph has to be connected.
2) There has to be no cycle.
DFS/BFS can be used to check both cases.

Related

directed graph is strongly connected

An undirected graph G is given as an input.
I need to tell whether it is possible to direct each edge of G so that the resulting
directed graph is strongly connected.
What algorithm should I use?
This is equivalent to determining whether the graph is connected and bridgeless (where a "bridge" is an edge such that, if you removed it, some vertices would become disconnected).
I trust you'll have no difficulty figuring out whether the graph is connected. For determining whether it's bridgeless, you can use Tarjan's bridge-finding algorithm, which will find a bridge if and only if one exists.

Pre-Requisite for Graphs for a directed acyclic graph

Every tree is a directed, acyclic graph (DAG), but there exist DAGs that are not trees.
a) How can we tell whether a given DAG is a tree?
b) Devise an algorithm to test whether a given DAG is a tree??
Check that there are exactly n - 1 edges(where n is the number of vertices).
Check that there is a vertex with zero indegree.
Run depth first search from this vertex and check that all vertices are reachable from it.
If at least one of this condition does not hold true, it is not a tree. Otherwise, it is a tree.

Determining whether or not a directed or undirected graph is a tree

I would like to know of a fast algorithm to determine if a directed or undirected graph is a tree.
This post seems to deal with it, but it is not very clear; according to this link, if the graph is acyclic, then it is a tree. But if you consider the directed and undirected graphs below: in my opinion, only graphs 1 and 4 are trees. I suppose 3 is neither cyclic, nor a tree.
What needs to be checked to see if a directed or undirected graph is a tree or not, in an efficient way? And taking it one step ahead: if a tree exists then is it a binary tree or not?
For a directed graph:
Find the vertex with no incoming edges (if there is more than one or no such vertex, fail).
Do a breadth-first or depth-first search from that vertex. If you encounter an already visited vertex, it's not a tree.
If you're done and there are unexplored vertices, it's not a tree - the graph is not connected.
Otherwise, it's a tree.
To check for a binary tree, additionally check if each vertex has at most 2 outgoing edges.
For an undirected graph:
Check for a cycle with a simple depth-first search (starting from any vertex) - "If an unexplored edge leads to a node visited before, then the graph contains a cycle." If there's a cycle, it's not a tree.
If the above process leaves some vertices unexplored, it's not a tree, because it's not connected.
Otherwise, it's a tree.
To check for a binary tree, if the graph has more than one vertex, additionally check that all vertices have 1-3 edges (1 to the parent and 2 to the children).
Checking for the root, i.e. whether one vertex contains 1-2 edges, is not necessary as there has to be vertices with 1-2 edges in an acyclic connected undirected graph.
Note that identifying the root is not generically possible (it may be possible in special cases) as, in many undirected graphs, more than one of the nodes can be made the root if we were to make it a binary tree.
If an undirected given graph is a tree:
the graph is connected
the number of edges equals the number of nodes - 1.
An undirected graph is a tree when the following two conditions are true:
The graph is a connected graph.
The graph does not have a cycle.
A directed graph is a tree when the following three conditions are true:
The graph is a connected graph.
The graph does not have a cycle.
Each node except root should have exactly one parent.

Are Trees Directed or Undirected Graphs?

I have read that Trees are special cases of Graphs.
Graphs can be directed or undirected. But if we consider tree as a data structure is it directed or undirected graph?
Unless qualified otherwise, trees in Mathematics or Graph Theory are usually assumed to be undirected, but in Computer Science or Programming or Data Structure, trees are usually assumed to be directed and rooted.
You need to be aware of the context of discussion.
See Tree on Wikipedia :
A tree is an undirected graph.
Both are acceptable.
You may have some cases where you want to be able to go up from a leaf and then go back down (usually in another branch), or you may want to be able to go only down.
Trees are connected acyclic graphs. that means you should be able to traverse from any node u to any node v. If we say trees are directed then it may not be possible to traverse from every node u to every node v.
In context of rooted trees, direction just tells which node of tree is treated as root (starting point) or to show parent child relationship between nodes and that's it all it says ... this direction does not limit the connectivity of graph or a connection between any node u to node v of tree.[1]
[1] if we considered directions in rooted as actual path which can be traversed in tree to go from node u to node v, then connectivity would be broken and that graph would not be a tree any more.

Biconnected Graph

How do you find out whether an undirected graph is Biconnected or not using its depth first search traversal.
Is there any other way than traversing the whole graph to find disconnected pieces of the graph.
You calculate the low(v) for every vertex in linear time (i.e. during the execution of the DFS). And there exists a bridge (i.e. an edge whose removal will disconnect the graph ==> not biconnected) iff there's a non-root vertex whose low value is itself OR if the root has more than one child.
It's explained here on point 5.2 http://www.cse.ohio-state.edu/~lai/780/graph.pdf
I have no answer to this, but my gut feeling would suggest that you would have to do the depth first search traversal as the biconnected property of the graph is a property of the whole graph and not any subset of the graph.

Resources