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).
Related
Consider we are trying to apply prim's algorithm on a disconnected graph. Consider that this disconnected graph has vertices a,b,c and d. Where this vertex d is disconnected. Now I need to check my understanding, if we apply prim's algorithm on this disconnected graph, the algorithm will not reach the vertex d and therefore will return a MST with vertices a,b and c only. So, is this assumption right ?
While it is true that the actual definition of MST applies to connected graphs, you could also say that, for a disconnected graph, a minimum spanning forest is composed of a minimum spanning tree for each connected component.
The problem adds an initial step to isolate each connected sub-graph, and apply Prim's algorithm to each of these.
There's no point in applying a Minimum Spanning Tree on a disconnected graph because by definition it is disconnected and it will not span all the vertices. By definition it is relevant only to connected graphs:
A minimum spanning tree (MST) or minimum weight spanning tree is a
subset of the edges of a connected, edge-weighted undirected graph
that connects all the vertices together, without any cycles and with
the minimum possible total edge weight.
I am given a connected, weighted graph, with non-negative weights. I want to convert it to a connected, acyclic graph such that sum of weights of the removed edges is minimised. The output would be the removed edges.
My thoughts: Since a connected, acyclic graph is a tree, I can simply take the maximum n-1 edges, and remove all others. But, this may not always be correct. It may lead to a disconnected graph.
Then, I thought of using dfs. I know how we can detect if a graph has cycle using dfs, but I don't know how to detect all the edges that were involved and how we can convert it to a acyclic graph. Any help (code/pseudocode/algo in words) would be appreciated. Thanks...
You need maximum spanning tree.
use Kruskal's algorithm for minimal spanning tree with the negating weights.
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.
Prim's and Kruskal's algorithms are used to find the minimum spanning tree of a graph that is connected and undirected. Why can't they be used on a graph that is directed?
It's a minor miracle that these algorithms work in the first place -- most greedy algorithms just crash and burn on some instances. Assuming that you want to use them to find a minimum spanning arborescence (directed paths from one vertex to all others), then one problematic graph for Kruskal looks like this.
5
--> a
/ / ^
s 1| |2
\ v /
--> b
3
We'll take the a->b arc of cost 1, then get stuck because we really wanted s->b of cost 3 and b->a of cost 2.
For Prim, this graph is problematic.
5
--> a
/ /
s 1|
\ v
--> b
3
We'll take s->b of cost 3, but we really wanted s->a of cost 5 and a->b of cost 1.
Prim's and Kruskal's algorithms output a minimum spanning tree for connected and "undirected" graphs. If the graph is not connected, we can tweak the algorithms to output minimum spanning forests.
In Prim's algorithm, we divide the graph in two sets of vertices. One set of the explored vertices which have already formed a MST (Set 1) and another set of unexplored vertices which will eventually join the first set to complete "spanning" (Set 2). At each instant, we select a minimum weighted edge in the cut joining the two disjoint sets. If there is no directed edge from explored nodes of the MST to the remaining unexplored nodes, the algorithm gets stuck even though there are edges from unexplored nodes to explored nodes in the MST.
In Kruskal's algorithm, the idea is to sort the edges in ascending order by their weight and pick them up in order and include them in the MST of explored nodes/edges if they do not already form a cycle with any explored nodes. This is done using the Union-Find data structure. But detection of cycles for directed graphs fails with this method. For example, a graph containing edges [1->2] [2->3] [1->3] will be reported to contain a cycle with the Union-Find method.
So Prim's fails because it assumes every node is reachable from every node which, though valid for undirected graphs, may not be true for digraphs. Kruskal's fails because of failure to detect cycles and sometimes it is essential to add edges making cycles to satisfy the "minimum" weighted property of MST.
Also, in case of digraphs, MST doesn't make complete sense. Its equivalent for digraphs is "minimum spanning arborescence" which will produce a tree where every vertex can be reached from a single vertex.
I have to create a solution for a weighted undirected graph, passing through all the nodes, with a total minimum cost. Several paths, with no defined starting nodes, should end up and meet at one intersecting node. The number of the paths, and the number of the nodes included in a path are not pre-determined. The nodes can be passed more than once.
What kind of problem am I dealing with, possible algorithms as solution?
I suppose it should be a variation of a Minimum spanning tree (meaning using the intersection node as a starting point for the paths in stead of ending point)
It's called Minimum Cost Hamiltonian Circuit problem.
Here you can read more about it.
It is a tree you are looking for and the problem is Minimum Spanning Tree-- MST: building a tree that spans all the nodes in graph and the cost of edges on the tree is minimum possible. It is a polynomial problem. Prim and Kruskal each have well-known algorithms for the solution.
See http://en.wikipedia.org/wiki/Kruskal's_algorithm for Kruskal's algorithm.
Note: the problem is NP-complete when the tree is supposed to span a given proper subset of nodes instead of all nodes in the graph. This time it is known as the Steiner Minimal Tree problem.