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

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.

Related

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.

Algorithm for finding least weight spanning tree of G that contains forest

I have a graph having the definition of G = (V;E) . This graph is a weighted undirected connected graph. F is a forest on the vertices V using edges in E.
So I need to have an algorithm to compute a least weight spanning tree of G that contains F. How can I do that ?
My trying:
I am trying to apply BFS to find the connected component of the graph and then think to apply Prims or kruskal algorithm to find the minimum spanning tree.
But I could not relate my thinking to the above problems. How can I proceed to solve the above questions?

A spanning tree as a result of running Dijkstra's algorithm?

Just need to make sure about this:
When I run Dijkstra's algorithm on a graph, at the end I will have a spanning tree right ? (Not necessarily Minimum spanning tree)
So the difference between Dijkstra and PRIM/Kruskal that the later two algorithms will return a minimum spanning tree?
Thanks
You are correct, with one condition - the graph should have a spanning tree from the source (i.e. - every vertex v in the graph has a path from the given source).
Also, as commented by #Henry - you should continue the algorithm until you found path to all vertices, and do not 'stop' once a target is reached.
Also note that Dijkstra's algorithm (and in general - shortest path solver) is defined for directed graphs, and MST is usually for undirected graphs.
(Note that it easy to define every undirected graph as a directed graph - by simply adding (u,v) and (v,u) for each edge {u,v} in the undirected graph)

Visiting edges, vertices in an undirected graph

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.

How to remove cycles in an unweighted directed graph, such that the number of edges is maximised?

Let G be an unweighted directed graph containing cycles. I'm looking for an algorithm which finds/creates all acyclic graphs G', composed of all vertices in G and a subset of edges of G, just small enough to make G' acyclic.
More formal: The desired algorithm consumes G and creates a set of acyclic graphs S, where each graph G' in S satisfies following properties:
G' contains all vertices of G.
G' contains a subset of edges of G, such that G' is acyclic.
The number of edges of G' is maximised. Which means: There is no G'' satisfying properties 1 and 2, such that G'' contains more edges then G' and G'' is acyclic.
Background: The original graph G models a pairwise ordering between elements. This can't be exploited as an ordering over all elements due to cycles in the graph. The maximal acyclic graphs G' therefore should model a best-possible approximation to this ordering, trying to respect as much of the pairwise ordering relation as possible.
In a naive approach, one could remove all possible combinations of edges and check for acyclicity after each removal. In this case there is a strongly branching tree of variations meaning bad time and space complexity.
Note: The problem may be related to a spanning tree, and you could define the G' graphs as a kind of directed spanning tree. But keep in mind that in my scenario a pair of edges in G' may have the same starting or the same ending vertex. This conflicts with some definitions of directed spanning trees used in literature.
EDIT: Added intuitive description, background information and note related to spanning trees.
This problem is called Feedback Arc Set. Since it is NP-hard, it is unlikely that you will find a scalable fast algorithm. However, if your instances are small, then algorithms such as the one from the paper “On enumerating all minimal solutions of feedback problems” by B. Schwikowski and E. Speckenmeyer might work.

Resources