I was trying to answer the following question from a textbook.
Given n nodes of a forest and their edges, describe and prove an algorithm that finds the number of trees in the forest.
Take for instance this graph:
Suppose that we explore it with a DFS, starting at vertex 0, that visits the vertices in the following order: 0, 2, 1, 5, 4, 3, 6.
The corresponding DFS forest will look like this:
.
The above forest contains two trees and I think we can use the below algorithm to count the number of trees in a forest. [Source of algorithm]
Apply DFS on every node.
Increment count by one if every connected node is visited from one source.
Again perform DFS traversal if some nodes yet not visited.
Count will give the number of trees in forest.
My question is: How can I prove that this algorithm is correct, and is there a more efficient algorithm to get the number of trees in a forest?
Related
I have some interesting question that I would like to get your help:
Let's say I have a graph (Data structure) with n(n-1)/2 edges, which means, completed graph.
How many possible different DFS trees can I get from one DFS scan from some random element in the graph?
Your question is interesting. I believe you are talking about complete graph with n vertices and n(n-1)/2 edges in between them.
If we begin depth first search (DFS) from any vertex, it will end up visiting exactly n vertices. In DFS, we keep track of visited vertices so that we will not visit them once they are visited and hence outgoing option reduces as long as DFS progresses. We can summarize this as:
There are total n options to choose first vertex.
There are total n-1 options to choose second vertex as 1 vertex is already visited.
There are total n-2 options to choose third vertex as 2 vertices are already visited.
There are total n-3 options to choose third vertex as 3 vertices are already visited.
And so on . . .
There is only 1 option to choose nth vertex.
Hence, different possible DFS trees that we can get from DFS in such graph is :
Total ways = n*(n-1)*(n-2)*(n-3)*....*1
= n! ( n factorial )
In fact it's not a tree but a list of nodes of the given complete graph. Thus, the question is: How many permutations of n nodes of the graph? Apparently, the answer is n!.
I was just wondering if it is possible for two graphs to have the same breadth-first and depth-first traversal. The graph may be directed or undirected.
A connected, undirected graph where there are N nodes and (N-1) edges, and where there is only two nodes with the degree 1 (i.e. the other (N-2) nodes would have the degree 2) could have the same visiting order for BFS and DFS provided that you start from one of the nodes with degree 1. If you start from one of the nodes with rank 2, the traversal order would differ.
Thinking of linearly traversing through a singly linked list may help you visualize it better.
I have a question that asks me to make a graph such that the BFS and DFS trees from the graph is not a minimum spanning tree and the order of the adjacency list does not matter, I know the properties of BFS DFS and MST but I'm a confused by the question. How should I be approaching the problem? (not looking for the solution)
Imagine a complete graph on k vertices. For k > 3, a DFS tree will always look different from a BFS tree. For k > 4, you can have a MST that is different from both the BFS and DFS trees. You can choose the shape of the MST to be different from the DFS tree by ensuring one vertex needs three edges coming out of it. You can choose the shape of the MST to be different from the BFS tree by ensuring no vertex has more than three edges coming out of it. You choose the shape of the MST by assigning weights to make the edges you choose, and only those edges, part of the MST.
DFS Tree
1-----2----3
|
|
4-----5
BFS Tree
1
____|____
/ / \ \
2 3 4 5
MST
2
|
1---3---5
|
4
The complete graph on five vertices has 5 * 4 / 2 = 10 edges, of which only four are needed in any tree.
My suggestion would be to look at an algorithm for finding the MST from a graph, and think about why it is not just a BFS or DFS search.
For any algorithm, finding an edge case that actually tests the tricky parts is an important part of implementation. You could start with a simple example, and try adding new edges or altering the input in a way that would make a simple BFS/DFS search fail.
The condition that it should be independent of the adjacency list order is to ensure that your test graph has a truly hard structure, not just that you have artificially fed the edges to the algorithm in a way that makes it fail.
Suppose G is an undirected graph with n vertices, there are weighted edges between each pair of vertices. Can you construct a tree in following structure:
v_1-v_2-v_3-...-v_n such that each node in the tree corresponding to a vertex in G and each node only has one child except the leaf. Also the total weight of the tree edges is minimized.
If use an algorithm similar to Kruskal’s Algorithm: sort all weights of edges in the original graph in ascending order. Starts from the minimal weight edge, if add this edge does not violate the tree structure described above, then add this one in the final tree, otherwise, move on to the next.
Can this algorithm give the tree with minimal weights? If not, is it possible to find an algorithm to get this tree?
It can, but it may not. Consider a 4-node graph with edge weights as follows:
AB: 3
AC: 1
AD: 100
BC: 2
BD: 100
CD: 2
The minimal tree is ABCD with length 7, but your algorithm will always start with the (length 1) edge AC which is not part of the minimal tree required.
I am trying to find a suitable algorithm to solve this: suppose I have some (oriented graph) nodes. Each node might have or not a parent (meaning at most one parent). Suppose this notation for a node: (id, id_parent). Some nodes will be (id_i, NULL) while there will be nodes (id_j, id_i) as "sons" of id_i . Having an array of these nodes in a particular order, I want to get them sorted in this order: parent-son-son of son-son-son of son, etc.
Example: nodes (1, NULL), (2,NULL), (3,1), (4,3), (5,2), (6,3)
The sorted array will be: (1,NULL), (3,1), (4,3), (6,3), (2, NULL), (5,2) . A kind of in-depth tree exploration.
Which algorithm would be suitable for achieving this? Thanks
If the graph has no cycles - it is a DAG, and you are looking for topoloical sort.
If it has cycles - there is no such ordering, since in the cycle, there will be a node, which its son is also its ancestor.
EDIT:
If the graph is a forest (disjoint union of trees) - then a simple DFS on it from sources will do. Just construct the graph (It is O(nlogn) to sort, if it is not already sorted, or O(n) using radix sort), find the list of sources, and do the DFS from each source, and each time you visit a node, store it in an output array. Iterate while there are undiscovered vertices.