Which graph has same Breadth-First traversal and Depth-First Traversal? - algorithm

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.

Related

Relation between Dijkstra and MST

This question came to my mind when I see this question. For simplicity, we can limit our discussion to undirected, weighted, connected graphs. It is clear that Dijkstra cannot guarantee to produce a MST if we choose an arbitrary node from a graph as the source. However, is it guaranteed that there must exist one node in an undirected, weighted, connected graph, which will produce a MST for the graph if we choose it as the source and apply Dijkstra's algorithm? Maybe you can give a proof or a counterexample. Thanks!
However, is it guaranteed that there must exist one node in an
undirected, weighted, connected graph, which will produce a MST for
the graph if we choose it as the source and apply Dijkstra's
algorithm?
Nope, Dijkstra's algorithm minimizes the path weight from a single node to all other nodes. A minimum spanning tree minimizes the sum of the weights needed to connect all nodes together. There's no reason to expect that those disparate requirements will result in identical solutions.
Consider a complete graph where the sum of the weight of any two edges exceeds the weight of any single edge. That forces Dijkstra to always select the direct connection as the shortest path between two nodes. Then, if the lowest weight edges in the graph don't all originate from a single node, the minimum spanning tree won't be the same as any of the trees that Dijkstra will produce.
Here's an example:
The minimum spanning tree consists of the three edges with weight 3 (total weight 9). The trees returned by Dijkstra's algorithm will be whichever three edges connect directly to the source node (total weight 10 or 11).

DFS on undirected graph complexity?

As when we traverse the undirected connected graph using DFS and mark out only edges which we travel during DFS, we end up with a DFS tree which is basically a tree and traversing a tree requires O(v) complexity where v is the number of vertices, then why it stated that complexity is O(v + e)?
I know it's a noob question but I am confused.
The DFS tree is the tree made of the edges you traversed. You indeed only traverse O(V) edges. But in order to traverse an edge, you first examine the edge to check if it will lead to a vertex you already encountered. This means you examine more edges than you traverse. Indeed, you examine O(E) edges. So the total work is O(V+E).
Note: Because your graph is connected, you are sure that E > V. In that case the complexity can be rewritten O(E).
you find all the nodes of graph through edges so the time complexity depends upon no. of edges as well that's why the O(e) is also included. If you consider complete graph TC will be O(V^2).
Consider two different graphs:
A graph having more edges than vertices, for instance a connected graph with a high minimum degree.
When the DFS algorithm visits a vertex, it will have to follow each of the edges that connect this vertex, repeating a DFS traversal from the neighboring vertices. Of course, if that neighbor was already visited, the DFS algorithm will backtrack, but at least we can state that the edge had to be processed.
This procedure will process all edges of the graph. So in this case we can say the algorithm is O(e)
A graph having fewer edges than vertices, often a disconnected graph (in the extreme case there are no edges).
When the DFS algorithm has visited all vertices that it can reach from a traversal that started in vertex A, it must still iterate over the remaining vertices, to find vertices that might not have been visited. These unvisted vertices do not belong to the same connected component). Another DFS traversal must start from there.
This way all vertices are processed. So in this case the algorithm has a O(v) time complexity
So in general, the algorithm has a O(max(e, v)) time complexity. You could also say that the algorithm must visit all edges and all vertices, and so the algorithm has a O(e+v) time complexity. Both are equivalent.

Spanning Tree VS. Spanning Forest

What's the difference between a Spanning Tree and a Spanning Forest in graphs, conceptually.
Also, is it possible to construct a Spanning Forest through DFS or BFS traversals? Why? How?
I understand the Spanning Tree, but I couldn't find any clear explanations about spanning forests. Even Wikipedia (https://en.wikipedia.org/wiki/Spanning_tree), doesn't give a clear definition about it.
My book (Data Structures & Algorithms, Wiley - sixth edition) also has no definition for spanning forests.
I was wondering, if we have a graph with for example three connected components in it, is it possible to construct a spanning forest by DFS/BFS traversals?
When there is only one connected component in your graph, the spanning tree = spanning forest.
But when there are multiple connected components in your graph. For example in following picture we have 3 connected components.:
So for each component, we will have a spanning tree, and all 3 spanning trees will constitute spanning forest
I was wondering, if we have a graph with for example three connected
components in it, is it possible to construct a spanning forest by
DFS/BFS traversals?
Yes it is possible. When there is only 1 connected component, your BFS or DFS will terminate visiting all the vertices and you will have a spanning tree (which in this case is equal to spanning forest).
But when you have more than 1 connected component, like in the picture, the only thing you have to do is start another BFS or DFS from an unvisited vertex. Your algorithm terminates when there is no unvisited vertex left and each BFS or DFS traversal will yield a spanning tree.
Non-trivial spanning forests can be constructed even for complete graphs via the following algorithm:
preconditions
all vertices are unmarked
the edges are enumerated from 1 to m
edge processing
if both of its adjacent vertices are marked, skip it because then that edge would either merge trees of the forest or creates a cycle in one of its trees
else mark its unmarked adjacent vertices
algorithm
process the edges in the order of their enumeration
explanaton:
while it is feasible in the construction of spanning trees to add edges that "bridge" connected components, those edges are not added in the above algorithm.
interpretation:
if the edges are enumerated according to ascending length, the edges of the resulting spanning forest will be a subsets of the MST and the trees of the forest will resemble "basins" i.e. the length of edges is smallest for the one that created the connected component and increases with every edge that is attached in later steps.
In that case the properties of spanning forest may provide insight into the structural properties of the original graph and/or be utilized in algorithms.

Does for a given graph G, both DSF and BFS will be same only when the graph is not cyclic in nature?

Does for a given graph G, both depth first search and breadth first search will be same only when the graph is not cyclic in nature ??
It is easier to explain and visualize in terms of a rooted tree.
In the case of BFS, we will visit the tree 'level-by-level'. All nodes of a level will be printed before the next level is visited.
In the case of DFS, we will go through all levels of a node to the leaf before we visit the next. So it's more of a 'strand-by-strand' traversal.
If you visualize the two, it is clear that BFS and DFS traversal are identical when the graph is such that each node has one incoming and outgoing edge, except for the end nodes which have one edge. Hence, the graph should be of the form A_4->A_3->A_4..->A_N. Basically, there is exactly one strand of DFS here.
The other possibility is a star-configuration, i.e., one root whose children are all leaves. There the strand-by-strand and level-by-level approaches are identical. Basically there is one level of BFS here.
I doubt there will be a case where there are both multiple levels of BFS and multiple strands of DFS where the identical traversal will hold.
And in either of these cases, the graphic is not cyclic. So we can conclude (not a rigorous proof) that if the BFS and DFS traversals are identical, the graph is acyclic.
Note that there are a number of graphs (any tree which does not fit into the above patterns) different from the above cases where the DFS and BFS traversals are different. Hence the converse of the statement is not true.

How will applying breadth first algorithm on an undirected graph produce a star graph?

I got this question from a data structure and algorithm textbook saying
A simple undirected graph is complete if it contains an edge between every pair of distinct vertices. A star graph is a tree of n nodes with one node having vertex degree n-1 and the other n-1 having vertex degree 1.
(a) Draw a complete undirected graph with 6 vertices.
(b) Show that applying breath-first algorithm on the undirected graph in (a) will produce a star graph.
I know how the BFS works using queues and I can provide a result of the traversal. What I'm confused about is on part (b) how can I show that applying BFS on an undirected graph will produce a star graph?
In a, there is n * (n - 1) / 2 edges in total.
It means that for every two nodes, there is an edge between them.
if applying BFS on (a) using a queue, Steps as follow:
1.) you pick up a random node, which is the root of the graph.
2.) you travel from the root node, and put all the nodes having edges with it to the queue. In addition, you have a boolean array to mark who is already processed.
in 2.), all the nodes except the root will be put into the queue.
At last, the root node have N - 1 edges, others have only one edge, and this edge is with the root

Resources