Question

IN JAVA Given is a weighted undirected graph G = (V, E) with positive weights and...

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 in input stores n and m. The following lines store data about edges (u, v) in order u, v, weight, 0 or 1. It writes 0 if the edge is not in the set F, 1 otherwise.

Output: Only one value, the cost of the tree or -1 if there is no such tree. The output is written to Java standard output.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program:

File name: "Graph.java"

Output 1:

Output 2:

Editable code:

//Import necessary header files
import java.util.*;

//Define the class "Graph"
public class Graph
{
//Declare and initialize the necessary variables
public int isVisited[] = new int[15];
public int cost[][] = new int[10][10];
int tot=0;
public int min_cost;

//Define the function "calculate()"
public void calculate(int n)
{
//Declare and initialize the necessary variables
int flag[] = new int[n+1];
int a,b,min=999,num_edges=1,c=1,d=1,minpos_a=1,minpos_b=1;

//Check the condition
while(num_edges < n)
{
//For loop to iterate the cost matrix   
for(a=1,min=999;a<=n;a++)
for(b=1;b<=n;b++)
  
//Check the node cost is less than minimum cost
if(this.cost[a][b]<min)
  
//Check the node is not visited
if(this.isVisited[a]!=0)
{
//True, assign the cost as minimum cost
min=this.cost[a][b];
  
//Assign the value to "c"
c=minpos_a=a;
  
//Assign the value to "d"
d=minpos_b=b;
}
  
//Check the condition
if(this.isVisited[minpos_a]==0 || this.isVisited[minpos_b]==0)
{
//True, assign the value of "min" to "tot"
tot+=min;
  
//Add the value to "min_cost"
this.min_cost=this.min_cost+min;
  
//Incrememt the edges by "1"
num_edges=num_edges+1;
  
//Initialize the value
this.isVisited[d]=1;
}
  
//Initialize the value
this.cost[c][d]=this.cost[d][c]=999;   
}
  
//Print the cost of the tree
System.out.println("cost of the tree is: "+tot);
}

//Define the "main()" function
public static void main(String args[])
{
//Declare the necessary variables
int nodes,a,b;
  
//Scanner method to get the input from the user
Scanner in = new Scanner(System.in);
  
//Print the statement
System.out.println("Enter the Number of Nodes \n");
  
//Get the number of nodes from the user
nodes = in.nextInt();
  
//Create object for the class "Graph"
Graph g = new Graph();
  
//If the tree is empty
if (nodes == 0)
{
//Print the message
System.out.println("Cost of the tree is:-1, there is no such tree.");
}
  
//Otherwise
else
{
//Print the cost of the tree
System.out.println("Enter the Cost Matrix Weights : \n");
  
//Loop to get the cost matrix
for(a=1;a<=nodes;a++)
for(b=1;b<=nodes;b++)
{
//Get the value from the user
g.cost[a][b]=in.nextInt();
  
//If the node value is equal to zero
if(g.cost[a][b]==0)
  
//Then, initialize the value
g.cost[a][b]=999;
}
  
//Initialize the value
g.isVisited[1]=1;
  
//Call the function "calculate()"
g.calculate(nodes);
}
}   
}

Add a comment
Know the answer?
Add Answer to:
IN JAVA Given is a weighted undirected graph G = (V, E) with positive weights and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • You are given an undirected graph G = (V, E) with positive weights on the 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 5. (15 marks) Given a connected, undirected, weighted graph G- (V, E), define the cost...

    Problem 5. (15 marks) Given a connected, undirected, weighted graph G- (V, E), define the cost of a spanning tree to be the maximum weight among the weights associated with the edges of the spanning tree. Design an efficient algorithm to find the spanning tree of G which maximize above defined cost What is the complexity of your algorithm.

  • Problem 3's picture are given below. 5. (a) Let G = (V, E) be a weighted connected undirected simple graph. For n...

    Problem 3's picture are given below. 5. (a) Let G = (V, E) be a weighted connected undirected simple graph. For n 1, let cycles in G. Modify {e1, e2,.. . ,en} be a subset of edges (from E) that includes no Kruskal's algorithm in order to obtain a spanning tree of G that is minimal among all the spanning trees of G that include the edges e1, e2, . . . , Cn. (b) Apply your algorithm in (a)...

  • Let G=(V, E) be a connected graph with a weight w(e) associated with each edge 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...

  • 2. Let G = (V, E) be an undirected connected graph with n vertices and with...

    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...

  • You are given an undirected graph G with weighted edges and a minimum spanning tree T of G.

    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.

    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.

  • Let G = (V, E) be a weighted undirected connected graph that contains a cycle. Let...

    Let G = (V, E) be a weighted undirected connected graph that contains a cycle. Let k ∈ E be the edge with maximum weight among all edges in the cycle. Prove that G has a minimum spanning tree NOT including k.

  • 1) Professor Sabatier conjectures the following converse of Theorem 23.1. Let G=(V,E) be a connected, undirected...

    1) Professor Sabatier conjectures the following converse of Theorem 23.1. Let G=(V,E) be a connected, undirected graph with a real-valued weight function w defined on E. Let A be a subset of E that is included in some minimum spanning tree for G, let (S,V−S) be any cut of G that respects A, and let (u,v) be a safe edge for A crossing (S,V−S). Then, (u,v) is a light edge for the cut. Show that the professor's conjecture is incorrect...

  • C++ programing question22 Minimum spanning tree Time limit: 1 second Problem Description For a connected undirected...

    C++ programing question22 Minimum spanning tree Time limit: 1 second Problem Description For a connected undirected graph G = (V, E), edge e corresponds to a weight w, a minimum weight spaning tree can be found on the graph. Into trees. Input file format At the beginning, there will be a positive integer T, which means that there will be T input data. The first line of each input has two positive integers n,m, representing n points and m edges...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT