A spanning tree as a result of running Dijkstra's algorithm? - 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)

Related

Dijkstra's shortest path algorithm on directed tree with negative weight edges

Will Dijkstra's shortest path algorithm return correct results on a directed tree with negative weight edges?
On a general graph with negative weights, the algorithm will fail, but since it’s a directed tree it feels like the algorithm will succeed.
From other answers, you know that there is no good reason to run Dijkstra's algorithm if you know that the graph is a tree.
If you do run it, though, it will work even if the tree has negative edge weights.
The reason that Dijkstra's algorithm doesn't work for graphs with negative weights, is that negative weights allow a 2nd, shorter, path to be found to a vertex after its distance has already been decided. In a tree there are no 2nd paths.
In a tree there is only one path between any two given nodes, so searching for the "shortest" path in a tree makes little sense: when you find a path it is the shortest, and this search does not need to take weights into account, so there is no need to use Dijkstra's algorithm. A simple depth-first search will do.
If the graph is not a tree, but a directed acyclic graph (DAG) with negative edges, then Dijkstra's algorithm cannot be used to find a shortest path. Take this counter example:
If we have to look for the shortest path from A to C, Dijkstra's algorithm will proceed to visit B and C, and as it hits the target, it will stop looking further, never considering the edge from B to C.
Other attempts to apply Dijkstra
Another (now deleted) answer proposed to make all edge weights positive by adding an absolute value to all weights, but this does not yield correct results: what is the shortest path in the original graph, is not guaranteed to be still the shortest path in the derived graph.
Counter example:
Where in the original graph the shortest path from A to C runs via B, in the adjusted graph, the shortest path is A-C.

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.

Create a MST with depth-first search?

I have a symmetrical graph and created a tree with all shortest path from a random vertex to any other vertex. Can I use the tree to construct a Minimum Spanning Tree(MST)? My algorithm is similar to depth-first algorithm.
In the worst case, a shortest path tree does not help in finding a minimum spanning tree. Consider a graph where we want to find the MST. Add a source vertex with edges of an identical large length to each other vertex. The shortest path tree from that source consists of the very long edges, which we knew a priori, hence the shortest path tree is not useful in this case.

Graph algorithms: Prim

I was wondering if any minimum spanning tree of a graph G can be provided by an execution of the algorithm Prim on this graph?
Does the Prim algorithm give us all the possible MST?
I was wondering if any minimum spanning tree of a graph G can be
provided by an execution of the algorithm Prim on this graph?
Yes.
Prim's algorithm is known to be a good algorithm to find a minimum
spanning tree.
Since Prim's algorithm constructs a MST from a weighted, connected, undirected graph, yes, you can use it to get a minimum spanning tree from such a graph. If your graph is not connected it won't work (but neither will any other algorithm because there is no spanning tree, then). If your graph is not weighted it will just create a spanning tree.
If you use Prim's once to get a MST then delete an edge. Then use prim's again to see if you can still get a MST of the same length. If you do, repeat, otherwise put the edge back and remove another edge. It will be slowish ... Perhaps only remove heavy edges?

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