Graph algorithms: Prim - algorithm

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?

Related

Are Prim's and Kruskal's Algorithm, shortest path algorithms?

Can these algorithms come under Dijkshtra's , Bellman-Ford , BFS, DFS algorithms?
I think no, and this is why;
Prim's and Kruskal's algorithms solve the Minimum Spanning Tree problem, and MST problem is different than Shortest Path problem.
What is the difference between them?:
MST: requirement is to reach each vertex once (create graph tree) and total (collective) cost of reaching each vertex is required to be minimum among all possible combinations.
SP: requirement is to reach destination vertex from source vertex with lowest possible cost (shortest weight). So here we do not worry about reaching each vertex instead only focus on source and destination vertices and thats where lies the difference.

Converting cyclic graph to acyclic in a weighted graph

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.

Finding the minimum spanning tree of a graph using Kruskal's Algorithm

Here is a Graph where I need to find the minimum spanning tree of G using Prim's and Kruskal's algorithms.
I found the minimum spanning tree using Prim's algorithm. Here is my attempt.
I am having difficulty in finding the minimum spanning tree using Kruskal's algorithm. I have seen many videos related to Kruskal's graph algorithm but I ended up getting the same graph as Prim's algorithm.
Can anyone please show me how to find the minimum spanning tree of the graph using Kruskal's algorithm?
Prims and Kruskals will always give you the same answer if all the
edges of the graph have distinct weights, as there is only a single min-spanning tree that exists. For graph having many edges with
same weights, the algorithms could give you a different answer but not
always. Depends on the way the nodes are explored in the
implementation. This graph can have many different min-spanning trees.
As your graph has all distinct edge weights, you will always get the same answer.
Prim's and Kruskal's algorithm both find minimum spanning trees. Because the graph that you gave has edge weights that are all different from each other there will be no different spanning trees that are both minimum.as long as both algorithms are correctly implemented they will find the same tree. meaning there can be no variation between the MST.

What algorithms are used to find a minimum spanning forest?

As Wikipedia says:
Minimum spanning forest is a union of the minimum spanning trees for
its connected components.
For finding minimum spanning tree we can use for example Prim's algorithm, Kruskal's algorithm, or Borůvka's algorithm.
What algorithm can we use to find minimum spanning forest?
I don't see how you need any other algorithm than you use for trees - you may need to adapt them a bit.
If you use for example Kruskal's algorithm you get all cheapest edges in every sub graph/minimum spanning tree of your (now also minimum spanning) forest. Or you can use Prim's algorithm and if your iteration stops, restart it with a node that is not connected yet (i.e. with another tree).
So my answer in one sentence: "The algorithms used to find a minimum spanning forest are the same ones that are used to find a minimum spanning tree - in some cases with adaptions and in some cases without them."

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)

Resources