Question

In: Computer Science

maximumST is a function that should take an n × n vector (representing the adjacency matrix...

maximumST is a function that should take an n × n vector (representing the adjacency matrix of a graph) as input and return the value of the maximum spanning tree of the graph defined by the n × n vector.

Code:

#include <vector>

#include <cmath>

#include <iostream>

#include <cstdio>

using namespace std;

double maximumST( vector< vector<double> > adjacencyMatrix ){

  vector<int> row(4,-1);

  vector< vector<int> > matrix(5,row);

  cerr << matrix.size() << endl;

  cerr << matrix[0].size() << endl;

  for( int i = 0; i < 5; i++ ){

    for( int j = 0; j < 4; j++ ){

      fprintf( stderr , "% 2d " , matrix[i][j] );

    }

    cerr << endl;

  }

  return 42.0;

}

C++ please.

Solutions

Expert Solution

Part01.h

#ifndef PART01_H

#define PART01_H

#include <vector>

using namespace std;
double maximumST( vector< vector<double> > );
#endif

.cpp

#include "part01.h"

#include <vector>

#include <queue>

#define pii pair<int, int>
using namespace std;

/**

* Algorithm: Kruskal algorithm to find the minimum spanning tree including disjoint set algorithm for implementation.

* Kruskal Algorithm:

* 1. Sort edges with weights from min to max.

* 2. Remove all the edges from graph

* 3. Now take edges one by one from min weight to max, add one and then check if it forms a loop

* 4. If it doesn't, add it to the graph, else do not add it.

* 5. For a graph with n nodes, after adding n-1 edges, the graph becomes connected without any loops.

*

* Disjoint-set

* 1. For a tree, store the parents of respective nodes.

* 2. Traverse iteratively/recursively to find the respective series of parents.

*/

int parent[10005];

int getParent(int n) {

int x = n;

//Iterative traversal to find the root of the tree

while(parent[n] != n) {

n = parent[n];

}

parent[x] = n;

return n;

}
void setParent(int n,int p) {

int x=n;

//Set the root of the tree as the parent of the node. We maintain the parents such that

//for any node, the value of its parent will be smaller.

while(1) {

x = parent[n];

parent[n] = p;

if(x == n) {

break;

}

n = x;

}

}
double maximumST( vector< vector<double> > graph ){

int n = graph[0].size(), edgeCount = 0, t1, t2;

double weight = 0;

pair<double, pii> p;

vector< pair < double, pii > > v;
//Set the parent of the node itself, as it is still unconnected.

for(int i=0; i<n; i++) {

parent[i] = i;

}
//Create a vector with 3 objects weight, and the vertices of the node

for(int i=0; i<n; i++) {

for(int j=i+1; j<n; j++) {

v.push_back(make_pair(graph[i][j], make_pair(i,j)));

}

}
//Priority queue is a heap. This is a min heap and for every pop, we get an entry of an edge with

//min weight among the existing edges

//priority_queue< pair< double,pii >, vector< pair< double,pii > >, greater<pair< double,pii > > > pq(v.begin(),v.end());
priority_queue< pair< double,pii >, vector< pair< double,pii > > > pq(v.begin(),v.end());

while(edgeCount < n-1) {

p = pq.top();

pq.pop();

t1 = getParent(p.second.first);

t2 = getParent(p.second.second);

//Since we maintain array such that the parent of the node is less than the node,

//we find out the smaller among the parents.

//If the parents are same, that means, adding the current edge with create a cycle. So we ignore it.

if(t1 != t2) {

if(t1 < t2) {

setParent(p.second.second,t1);

} else {

setParent(p.second.first,t2);

}

edgeCount++;

weight += p.first;

}

}
return weight;

}
Sample Input/output:
For input
0 0.5 1 1

0.5 0 3 6

1 3 0 1

1 6 1 0
Output:

The maximum spanning tree value: 10


Related Solutions

One simple representation for a graph is to use an adjacency matrix: each of the N...
One simple representation for a graph is to use an adjacency matrix: each of the N nodes is given a unique number in the range 0 to N-1 to identify it. A large two dimensional array A with N rows and N columns is created so that A[x][y] stores the cost of travelling directly from node x to node y. if A[x][y] is zero, then there is no direct connection from x to y. A[x][y] does not need to equal...
Write a function in R named counts. This function should take as parameters a numeric vector...
Write a function in R named counts. This function should take as parameters a numeric vector x and also a number indicating a number of bins n. The function will consider the range [min(x),max(x)], and then consider a parti- tion of this interval into n non-overlapping equally sized half open intervals: I1 = [min(x),b1),I2 = [b1,b − 2),...,In = (bn−1,max(x)]. Note that since the intervals are equally sized, the value of bi is constrained. The function will then return a...
can a vector with dimensions R^N and a vector with dimensions R^N+1 be a matrix? and...
can a vector with dimensions R^N and a vector with dimensions R^N+1 be a matrix? and if so what would it dimension size be?
4. The product y = Ax of an m × n matrix A times a vector...
4. The product y = Ax of an m × n matrix A times a vector x = (x1, x2, . . . , xn) T can be computed row-wise as y = [A(1,:)*x; A(2,:)*x; ... ;A(m,:)*x]; that is y(1) = A(1,:)*x y(2) = A(2,:)*x ... y(m) = A(m,:)*x Write a function M-file that takes as input a matrix A and a vector x, and as output gives the product y = Ax by row, as defined above (Hint: use...
im trying to write a java code that take a matrix of vector and fine it's...
im trying to write a java code that take a matrix of vector and fine it's inverse (the the inverse in linear algebra) then multiple this matrix with a vector to fine other vector (matrix)-1 × ( vector ) = (vector)
Write a function named “compileStats” that has 4 parameters: a vector of integers, an integer representing...
Write a function named “compileStats” that has 4 parameters: a vector of integers, an integer representing the smallest value contained in the vector, an integer representing the largest value contained in the vector, and a double that represents the average of the values in the vector. The compileStats function will not “return” a value, it will set the Pass By Reference parameters to the correct values (smallest, largest, and average). Use the following main function in driver1b.cpp as a starting...
Write a function decimalToBinary(n) that converts a positive decimal integer n to a string representing the...
Write a function decimalToBinary(n) that converts a positive decimal integer n to a string representing the corresponding binary number. Do the conversion by repeatedly dividing the number n by 2 using integer division, keepting track of the remainders, until the number is reduced to 0. The remainders written in reverse order form the binary number string. Do integer division of 5 by 2, so that N//2 is 2 with remainder 1. Now divide 2 by 2 to get 1 with...
4. The product y = Ax of an m n matrix A times a vector x...
4. The product y = Ax of an m n matrix A times a vector x = (x1; x2; : : : ; xn)T can be computed row-wise as y = [A(1,:)*x; A(2,:)*x; ... ;A(m,:)*x]; that is y(1) = A(1,:)*x y(2) = A(2,:)*x ... y(m) = A(m,:)*x Write a function M-file that takes as input a matrix A and a vector x, and as output gives the product y = Ax by row, as denoted above (Hint: use a for...
Your program should take a string representing a sentence in English and format it properly. The...
Your program should take a string representing a sentence in English and format it properly. The input sentence may have any or all of the following errors: Random letters may be capitalized. The sentence may not end with a proper punctuation mark (period, question mark, or exclamation point). There may be spaces at the beginning or end, or more than one space between words. Format the sentence to fit the following rules: The first letter of the first word should...
Write a function such that given a number N, display the N*N multiplication matrix from 1...
Write a function such that given a number N, display the N*N multiplication matrix from 1 to N. Then, write a C++ program such that, it prints the multiplication matrices of numbers 1,4,7, and 10 using a loop concept. Check the sample output below, to see how the program should work. Make sure to have your output exactly the same as the below output.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT