MST contains all the the vertices of the graph.
MST contains (n-1) edges in it, where n is number of vertices in graph.
It has the minimum possible weight ( sum of weight of all edges ) of all possible spanning trees of the graph.
So it is not always possible to get MST by modifying our dfs() function as mentioned above, the counter example is given below.
Suppose we have a connected undirected graph G, in which there are 4 vertices and 5 edges in it.
Edges are : 1-2,1-4,2-4,2-3,3-4 and their weights 2,4,1,3,5 respectively.
So our MST will contain 4 vertices and 3(n-1) edges.
We can start the dfs function from any vertex so our answer changes depending on the initial vertex.
1. if we start from vertex 1, then set of edges obtained by our dfs() function is 1-2,2-4,4-3 and then stop as all neighbors of 3 are visited. so we get total 3 edges with all 4 vertices.
so total weight becomes 2+1+5=8.
2.if we start from vertex 2, then set of edges obtained by our dfs() function is 2-4,4-1 and then stop as all neighbors of 1 i.e, 2 and 4 are visited.So we get 2 edges and 3 vertices which is not a spanning tree.
3. if we start from vertex 3, then set of edges obtained by our dfs() function is 3-2,2-4,4-1 and then stop as all neighbors of 1 are visited. so we get total 3 edges with all 4 vertices.
so total weight becomes 3+1+4=8.
4. if we start from vertex 4, then set of edges obtained by our dfs() function is 4-2,2-1 and then stop as all neighbors of 1(i.e, 2 and 4) are visited. so we get total 2 edges with all 3 vertices which is not a spanning tree.
Here is the pseudo code in c++ for the modified dfs() function in which ans is our vector<pair<int,int>> to store the final edges in MST(if obtained) and adj is adjacency list representation of graph:
void dfs( int v )
{
// mark this node as visited.
vis[v]=true;
// take initial weight to be INT_MAX.
int weight=INT_MAX;
// variable to store which neighbor of v has minimum weighted edge
with v.
int neighbor;
// iterate to all neighbors of v.
for(auto u:adj[v])
{
// if unvisited.
if(!vis[u])
{
// compare weight of edge to previous minimum weight of edge
obtained.
if(weight>W[{min(u,v),max(u,v)}])
{
weight=W[{min(u,v),max(u,v)}];
//save this neighbor u as we will recur for this.
neighbor=u;
}
}
}
// add this edge in answer vector to store edges of MST(if
obtained).
ans.push_back({min(neighbor,v),max(neighbor,v)});
//recur for unvisited neighbor u such that W(u-v) is minimum.
dfs(neighbor);
return;
}
So this was my answer to your problem if it helped(maybe very little) you in any way then please upvote my solution and if you have any query regarding this please feel free to comment.
Thank you and have a wonderful day.
Suppose we have a graph G = (V, E) with weights on the edges of E,...
Let G=(V, E) be a connected graph with a weight w(e) associated with each edge e. Suppose G has n vertices and m edges. Let E’ be a given subset of the edges of E such that the edges of E’ do not form a cycle. (E’ is given as part of input.) Design an O(mlogn) time algorithm for finding a minimum spanning tree of G induced by E’. Prove that your algorithm indeed runs in O(mlogn) time. A minimum...
You are given an undirected graph G with weighted edges and a minimum spanning tree T of G. Design an algorithm to update the minimum spanning tree when the weight of a single edge is increased. The input to your algorithm should be the edge e and its new weight: your algorithm should modify T so that it is still a MST. Analyze the running time of your algorithm and prove its correctness.
You are given an undirected graph G with weighted edges and a minimum spanning tree T of G. Design an algorithm to update the minimum spanning tree when the weight of a single edge is decreased. The input to your algorithm should be the edge e and its new weight; your algorithm should modify T so that it is still a MST. Analyze the running time of your algorithm and prove its correctness.
IN JAVA Given is a weighted undirected graph G = (V, E) with positive weights and a subset of its edges F E. ⊆ E. An F-containing spanning tree of G is a spanning tree that contains all edges from F (there might be other edges as well). Give an algorithm that finds the cost of the minimum-cost F-containing spanning tree of G and runs in time O(m log n) or O(n2). Input: The first line of the text file...
For each section, is it TRUE or FALSE? a) Every DAG is a Directed Forest. b) Every Directed Forest is a DAG. c) In a Directed Tree, there is a path from every vertex to every other vertex. d) Suppose that you search a Directed Graph using DFS from all unvisited vertices, coloring edges red if they go between vertex v and an outgoing neighbor of v that you visit for the first time from v. Then the red edges...
You are given an undirected graph G = (V, E) with positive weights on the edges. If the edge weights are distinct, then there is only one MST, so both Prim’s and Kruskal’s algorithms will find the same MST. If some of the edge weights are the same, then there can be several MSTs and the two algorithms could find different MSTs. Describe a method that forces Prim’s algorithm to find the same MST of G that Kruskal’s algorithm finds.
Problem 4 Let G = (V. E) be an undirected, connected graph with weight function w : E → R. Furthermore, suppose that E 2 |V and that all edge weights are distinct. Prove that the MST of G is unique (that is, that there is only one minimum spanning tree of G).
This problem is dealing with Discrete Math. Please answer fully and clearly, and show/explain all steps or proofs that you state in the answer. 4. Let (G, w) be a connected graph with weights on edges so that all weights are distinct positive real numbers. Suppose we find a MST (minimum spanning trees ) in G by using Prim's algorithm. Prove that no matter what vertex we begin with in Prim algorithm, the set of all weights on edges in...
2. Let G = (V, E) be an undirected connected graph with n vertices and with an edge-weight function w : E → Z. An edge (u, v) ∈ E is sparkling if it is contained in some minimum spanning tree (MST) of G. The computational problem is to return the set of all sparkling edges in E. Describe an efficient algorithm for this computational problem. You do not need to use pseudocode. What is the asymptotic time complexity of...
need help filling in the code def prim(G): Use Prim's algorithm to find a MST for the graph G … # Initialize tree T with a single vertex and no edges v = next(iter( )) # while the vertex set of T is smaller than the v+tex set of G, # (i.e. while the vertex set of T is a proper subset of the vertex set of G), find the edge e with minimum weight so that # Tte is...