Visiting edges, vertices in an undirected graph - algorithm

Problem: You have an Undirected graph G = (V, E) (V = vertices, E = edges) and you must visit each vertex and pass each edge in both directions.
The only algorithms I know for graphs are DFS, BFS, and a few MST's (Kruskal, etc.) My friend and I were discussing this problem, if it were directed I would simply DFS, and then DFS the transpose but the graph is unfortunately undirected. My friend proposed we perform an MST and DFS the MST and then find the remaining edges by iterating through those that arent in MST. I sort of see what he means but I am not sure this is a good approach? opinions? Also, how would I be able to pass by an edge in both directions if it is undirected?

It doesn't matter if the graph is directed or undirected. You could just replace every undirected edge with two directed edges and perform whatever algorithm you have for a directed graph. Both DFS and BFS will traverse the whole vertices and edges.
I think what you are looking for is called Graph Traversal. BFS and DFS are two graph traversal algorithms and they do not require the graph to be directed. MST on the other hand, is not a graph traversal algorithm.

Related

how to take tree roots in a DFS forest?

A DFS starting at some vertex v explores the graph by building up a
tree that contains all vertices that are reachable from v and all
edges that are used to reach these vertices. We call this tree a DFS
tree. A complete DFS exploring the full graph (and not only the part
reachable from a given vertex v) builds up a collection of trees, or
forest, called a DFS forest.
If I have a directed graph and I need to find the roots of these trees that are part of the dfs forest, do I have to modify the dfs algorithm to do this?
To find the roots of the trees in a directed graph, loop over the nodes and list any that have out edges but no in edges
Like this:

Two BFS produce the same set of edges

Under what kind of constraints, will two BFS (could start from different vertex) on a simple undirected graph produce the same set of edges?
If the graph is a minimum spanning tree, then it will produce all the edges in the breadth-first search which can start from any vertex.
Reason:
In BFS, all nodes are going to be visited.
The minimum spanning tree contains a minimal number of edges to connect all the nodes. So, the BFS traversal will traverse all the edges in order to visit all the nodes.
So, all the edges of the graph are in the set. That means if you start BFS from any vertex, all the edges are going to be included in the set. Thus, the same set of edges for any node.
Say you run BFS on graph G with starting vertex v (BFS(G, v)) and there is some edge e = (u,w) that isn't traversed. Running BFS(G, u) guarantees that (u,w) is traversed. Thus BFS will only produce a unique set of edges when it produces all edges. I.e. in acyclic graphs.

Does a DFS produce a MST for an unweighted directed graph?

I'm getting confused from reading online posts. I know that a BFS traversal on an unweighted directed graph will produce a minimum spanning tree and shortest path. Can a DFS traversal on an unweighted directed graph do the same?
Yes, Breadth-First and Depth-First both yield spanning trees. It doesn't make much sense to discuss "minimum spanning tree" for an unweighted graph, because all spanning trees on a given graph with n vertices have the same number of vertices (n) and the same number of edges (n-1).
No, Depth-First does not guarantee shortest path. You really need Breadth-First for that. Consider a cyclic graph:
a - h - g - f
| |
b - c - d - e
Starting from vertex a, there are two possible results to depth-first search: a->b->c->d->e->f->g->h, and a->h->g->f->e->d->c->b. The former returns a very long path from a to h, and the latter returns a very long path from a to b.
Note that in this example, the graph is undirected. But undirected graphs are a special case of directed graphs; you can replace every undirected edge by two directed edges in opposing directions.

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.

Minimum spanning tree in a graph with multiple root vertices

I would like to know if there is an algorithm which computes a minimum spanning tree (optimum branching) in a directed graph given a set of root vertices between all of these root vertices, but not only one root vertex and all other vertices in a graph.
Given a set of root vertices [1,4,6] and a graph G like the one on the following picture:
...the algorighm should return something like a green sub-graph on the same picture.
I would like to get such an MST that connects all the root vertices provided to the algorithm. I tend to think that the result of the would-be algorithm is a sub-graph of the graph G which contains all root vertices and some other vertices from G.
Notes:
I know that there is no MST for a directed graph, but there is Chu–Liu/Edmonds algorithm.
I guess that a result of such an algorithm (if it is actually possible) will return an optimum branching, which includes some vertices of a graph along with all root vertices.
Minimum Spanning Trees are supposed to span all the vertices. I think you might be actually dealing with a Steiner Tree problem, given that you only need to connect a subset of them. Unfortunately, the traditional Steiner tree problem with undirected edges is already NP complete so you have a tough road ahead of you.

Resources