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.
Related
I have an undirected graph and from it, I redrew the same graph but in a tree-like structure (with levels). I know how the Breadth First Search (BFS) algorithm works, but I am not sure how to do the transitioning from graph --> tree.
Here, in this Wikipedia article, if you scroll down a tiny bit, you'll see the two pictures of German cities. Even after reading the pseduo code on there, I just don't understand how you get from the first picture to the second.
One standard way to get a BFS tree from a graph is to run BFS and, as you do so, keep a table mapping each node in the graph to its parent in the tree. You populate it as follows: the source node has no parent (it's the root, after all). Then, whenever you're processing a node u and explore one of its unexplored neighbors v, you set v's parent to be u. Try tracing this out on some small examples and you'll see that this implicitly builds up the tree, except with the edges going backwards (edges point from children up to parents rather than the other way around). You can then just reverse the edges to get back the BFS tree.
Hope this helps!
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.
Well, I know that a breadth-first-search-tree of an undirected graph can't have a back edge. But I'm wondering how can it even have a cross-edge? I'm not able to image a spanning tree of a graph G constructed out of OFS, that contains a cross-edge.
The process of building a spanning tree using BFS over an undirected graph would generate the following types of edges:
Tree edges
Cross edges (connecting vertices on different branches)
A simple example: Imagine a triangle (a tri-vertice clique) - start a BFS from any node, and you'll reach the other two on the first step. You're left with an edge between them that does not belong to the spanning tree.
What about back-edges (connecting an ancestor with an non-immediate child) ? Well, as you point out, in BFS over an undirected graph you won't have them, since you would have used that edge when first reaching the ancestor.
In fact, you can make a stronger statement - all non-tree edges should be between vertices as the same level, or adjacent ones (you can't use that edge for the tree if the vertice on the other side is a sibling, like in the triangle case, or a sibling of the parent, that was not explored yet). Either way, it's falls under the definition of a cross-edge.
I had this same question...and the answer is that there are no cross edges in the BFS, but that the BFS tree itself encodes all the edges that would have been back-edges and forward-edges in the DFS tree as tree edges in the BFS tree, such that the remaining edges which the undirected graph has, but which are still not present in the BFS, are cross edges--and nothing else.
So the Boolean difference of the set of edges in the undirected graph and the edges in the BFS tree are all cross edges.
...As opposed to the DFS, where the set of missing edges may also include "Back Edges," "Forward Edges," and "Cross Edges."
I don't know why it is in the algorithmic parlance to say that both "tree edges and cross edges are in a BFS"
...I think it is just a short hand, and that in a math class, the professor would have written the relationship in set notation and unions (which I can't do on this stack exchange).
Given an undirected graph and a node, how would you modify the graph into a directed graph such that, any path leads to one particular node.The question is coming up as a popular algorithmic question in SE interviews
This is simply creating a tree rooted at (and directed to) the given node and then completing it to a DAG, any search algorithm (e.g. BFS or DFS) can give you a solution:
Starting with the given node, using the search algorithm - Whenever encountering a node, connect it to an already connected node (usually to the one from which you got to it), you can then set the direction for the remaining edges according to the order in which you encountered the nodes (from the latter to the earlier)
If a graph has back edges, is it singly connected or not? By back edges I mean connections from child node to one of its ancestors, under the same root. If a node is connected to a node higher than it, but not its ancestor, then it's a cross node.
http://en.wikipedia.org/wiki/Polytree
This link clarifies the concept of singly connected graph.
If a graph has back edges, that doesn't prevent it from being singly-connected. But it might not be singly-connected for other reasons. For example, if the graph is undirected.
It seems that you are trying to make an analogy with linked lists (where singly-connected and doubly-connected are common terms with an usual meaning).
However, this isn't a big deal for graphs, and the term connectivity is more usually associated with reachability (ie.: is there a path from a node to another?)
If I understand you question correctly, you want to know whether a Polytree can contain back edges (edges from a node to one of its ancestors).
From the wikipedia article you linked to, a Polytree is a DAG that remains a tree even if the edges are made undirected. If a directed graph contained back edges, it would mean there would be a cycle in the graph (you can reach the node from its ancestor and then go back to the ancestor using the back edge). Thus it would no longer be a DAG, let alone a tree. If it isn;t a DAG, it cannot be a Polytree. So, no a Polytree cannot have a back edge.