Minimal spanning tree
up vote
0
down vote
favorite
I have a code that finds minimal weight of spanning tree. There edges are declared as int, but I need them as string. How can I modify it for normal work?
If I modify vector to string, I see a lot of errors in compilator. Please help me to finish this code, I'm new in programming.
#include<bits/stdc++.h>
using namespace std;
typedef pair<int, int> iPair;
struct Graph
{
int Edges, Connections;
vector< pair<int, iPair> > edges;
Graph(int Edges, int Connections)
{
this->Edges = Edges;
this->Connections = Connections;
}
void addEdge(int u, int v, int w)
{
edges.push_back({w, {u, v}});
}
int kruskalMST();
};
struct DisjointSets
{
int *parent, *rnk;
int n;
DisjointSets(int n)
{
// Allocate memory
this->n = n;
parent = new int[n+1];
rnk = new int[n+1];
// Initially, all vertices are in
// different sets and have rank 0.
for (int i = 0; i <= n; i++)
{
rnk[i] = 0;
//every element is parent of itself
parent[i] = i;
}
}
// Find the parent of a node 'u'
// Path Compression
int find(int u)
{
/* Make the parent of the nodes in the path
from u--> parent[u] point to parent[u] */
if (u != parent[u])
parent[u] = find(parent[u]);
return parent[u];
}
// Union by rank
void merge(int x, int y)
{
x = find(x), y = find(y);
/* Make tree with smaller height
a subtree of the other tree */
if (rnk[x] > rnk[y])
parent[y] = x;
else // If rnk[x] <= rnk[y]
parent[x] = y;
if (rnk[x] == rnk[y])
rnk[y]++;
}
};
/* Functions returns weight of the MST*/
int Graph::kruskalMST()
{
int mst_wt = 0; // Initialize result
// Sort edges in increasing order on basis of cost
sort(edges.begin(), edges.end());
// Create disjoint sets
DisjointSets ds(Edges);
// Iterate through all sorted edges
vector< pair<int, iPair> >::iterator it;
for (it=edges.begin(); it!=edges.end(); it++)
{
int u = it->second.first;
int v = it->second.second;
int set_u = ds.find(u);
int set_v = ds.find(v);
if (set_u != set_v)
{
cout << u << " - " << v << " = " << it->first << endl;
mst_wt += it->first;
ds.merge(set_u, set_v);
}
}
return mst_wt;
}
// Driver program to test above functions
int main()
{
int from,to,weight,i;
/* Let us create above shown weighted
and unidrected graph */
int Edges, Connections;
cout<<"How many edges?:"; cin>>Edges;
cout<<"How many connections?:"; cin>>Connections;
Graph g(Edges, Connections);
system("cls");
// making above shown graph
for(i=0;i<Connections;i++){
cout<<endl;
cout<<"From "; cin>>from;
cout<<"To "; cin>>to;
cout<<"Weight "; cin>>weight;
g.addEdge(from,to,weight);
system("cls");
}
cout << "Edges of MST are n";
int mst_wt = g.kruskalMST();
cout << "nWeight of MST is " << mst_wt;
return 0;
}
Example of 9 edges and 14 connections. Minimal Weight = 37.
From To Weight
0 1 4
0 7 8
1 2 8
1 7 11
2 3 7
2 8 2
2 5 4
3 4 9
3 5 14
4 5 10
5 6 2
6 7 1
6 8 6
7 8 7
Min. Weight = 37
You can put your values for input, this is only an example.
c++ vector graph-theory minimum-spanning-tree
add a comment |
up vote
0
down vote
favorite
I have a code that finds minimal weight of spanning tree. There edges are declared as int, but I need them as string. How can I modify it for normal work?
If I modify vector to string, I see a lot of errors in compilator. Please help me to finish this code, I'm new in programming.
#include<bits/stdc++.h>
using namespace std;
typedef pair<int, int> iPair;
struct Graph
{
int Edges, Connections;
vector< pair<int, iPair> > edges;
Graph(int Edges, int Connections)
{
this->Edges = Edges;
this->Connections = Connections;
}
void addEdge(int u, int v, int w)
{
edges.push_back({w, {u, v}});
}
int kruskalMST();
};
struct DisjointSets
{
int *parent, *rnk;
int n;
DisjointSets(int n)
{
// Allocate memory
this->n = n;
parent = new int[n+1];
rnk = new int[n+1];
// Initially, all vertices are in
// different sets and have rank 0.
for (int i = 0; i <= n; i++)
{
rnk[i] = 0;
//every element is parent of itself
parent[i] = i;
}
}
// Find the parent of a node 'u'
// Path Compression
int find(int u)
{
/* Make the parent of the nodes in the path
from u--> parent[u] point to parent[u] */
if (u != parent[u])
parent[u] = find(parent[u]);
return parent[u];
}
// Union by rank
void merge(int x, int y)
{
x = find(x), y = find(y);
/* Make tree with smaller height
a subtree of the other tree */
if (rnk[x] > rnk[y])
parent[y] = x;
else // If rnk[x] <= rnk[y]
parent[x] = y;
if (rnk[x] == rnk[y])
rnk[y]++;
}
};
/* Functions returns weight of the MST*/
int Graph::kruskalMST()
{
int mst_wt = 0; // Initialize result
// Sort edges in increasing order on basis of cost
sort(edges.begin(), edges.end());
// Create disjoint sets
DisjointSets ds(Edges);
// Iterate through all sorted edges
vector< pair<int, iPair> >::iterator it;
for (it=edges.begin(); it!=edges.end(); it++)
{
int u = it->second.first;
int v = it->second.second;
int set_u = ds.find(u);
int set_v = ds.find(v);
if (set_u != set_v)
{
cout << u << " - " << v << " = " << it->first << endl;
mst_wt += it->first;
ds.merge(set_u, set_v);
}
}
return mst_wt;
}
// Driver program to test above functions
int main()
{
int from,to,weight,i;
/* Let us create above shown weighted
and unidrected graph */
int Edges, Connections;
cout<<"How many edges?:"; cin>>Edges;
cout<<"How many connections?:"; cin>>Connections;
Graph g(Edges, Connections);
system("cls");
// making above shown graph
for(i=0;i<Connections;i++){
cout<<endl;
cout<<"From "; cin>>from;
cout<<"To "; cin>>to;
cout<<"Weight "; cin>>weight;
g.addEdge(from,to,weight);
system("cls");
}
cout << "Edges of MST are n";
int mst_wt = g.kruskalMST();
cout << "nWeight of MST is " << mst_wt;
return 0;
}
Example of 9 edges and 14 connections. Minimal Weight = 37.
From To Weight
0 1 4
0 7 8
1 2 8
1 7 11
2 3 7
2 8 2
2 5 4
3 4 9
3 5 14
4 5 10
5 6 2
6 7 1
6 8 6
7 8 7
Min. Weight = 37
You can put your values for input, this is only an example.
c++ vector graph-theory minimum-spanning-tree
Hi there, I'm slightly confused with your use of "edges" and "connections". I think, in your program, you meant to ask forNodes
andEdges
instead ofEdges
andConnections
? I've never heard of "connections" before, but I guess it's another term for "edges".
– TrebuchetMS
Nov 19 at 15:11
If I modify vector to string, I see a lot of errors in compilator.
okay, so what happened? What did you try? Did you substitute all appropriateint
withstd::string
? What did the compiler complain about? You've given us your original, working code (and that does help in some way), but then it becomes next to "Please do my homework for me":Please help me to finish this code
.
– TrebuchetMS
Nov 19 at 15:18
1
Why would you store a number (the edge weight) as a string rather than anint
as you do right now? You can't do math on strings (e.g. compare two weights). Leave the edge weights as numbers. Everything else is just wrong.
– Nico Schertler
Nov 19 at 16:33
@NicoSchertler I feel as though <, ==, and > are the subset of 'math' you can in fact perform with numerical strings. "123"s is less than "124"s.
– schulmaster
Nov 19 at 19:06
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a code that finds minimal weight of spanning tree. There edges are declared as int, but I need them as string. How can I modify it for normal work?
If I modify vector to string, I see a lot of errors in compilator. Please help me to finish this code, I'm new in programming.
#include<bits/stdc++.h>
using namespace std;
typedef pair<int, int> iPair;
struct Graph
{
int Edges, Connections;
vector< pair<int, iPair> > edges;
Graph(int Edges, int Connections)
{
this->Edges = Edges;
this->Connections = Connections;
}
void addEdge(int u, int v, int w)
{
edges.push_back({w, {u, v}});
}
int kruskalMST();
};
struct DisjointSets
{
int *parent, *rnk;
int n;
DisjointSets(int n)
{
// Allocate memory
this->n = n;
parent = new int[n+1];
rnk = new int[n+1];
// Initially, all vertices are in
// different sets and have rank 0.
for (int i = 0; i <= n; i++)
{
rnk[i] = 0;
//every element is parent of itself
parent[i] = i;
}
}
// Find the parent of a node 'u'
// Path Compression
int find(int u)
{
/* Make the parent of the nodes in the path
from u--> parent[u] point to parent[u] */
if (u != parent[u])
parent[u] = find(parent[u]);
return parent[u];
}
// Union by rank
void merge(int x, int y)
{
x = find(x), y = find(y);
/* Make tree with smaller height
a subtree of the other tree */
if (rnk[x] > rnk[y])
parent[y] = x;
else // If rnk[x] <= rnk[y]
parent[x] = y;
if (rnk[x] == rnk[y])
rnk[y]++;
}
};
/* Functions returns weight of the MST*/
int Graph::kruskalMST()
{
int mst_wt = 0; // Initialize result
// Sort edges in increasing order on basis of cost
sort(edges.begin(), edges.end());
// Create disjoint sets
DisjointSets ds(Edges);
// Iterate through all sorted edges
vector< pair<int, iPair> >::iterator it;
for (it=edges.begin(); it!=edges.end(); it++)
{
int u = it->second.first;
int v = it->second.second;
int set_u = ds.find(u);
int set_v = ds.find(v);
if (set_u != set_v)
{
cout << u << " - " << v << " = " << it->first << endl;
mst_wt += it->first;
ds.merge(set_u, set_v);
}
}
return mst_wt;
}
// Driver program to test above functions
int main()
{
int from,to,weight,i;
/* Let us create above shown weighted
and unidrected graph */
int Edges, Connections;
cout<<"How many edges?:"; cin>>Edges;
cout<<"How many connections?:"; cin>>Connections;
Graph g(Edges, Connections);
system("cls");
// making above shown graph
for(i=0;i<Connections;i++){
cout<<endl;
cout<<"From "; cin>>from;
cout<<"To "; cin>>to;
cout<<"Weight "; cin>>weight;
g.addEdge(from,to,weight);
system("cls");
}
cout << "Edges of MST are n";
int mst_wt = g.kruskalMST();
cout << "nWeight of MST is " << mst_wt;
return 0;
}
Example of 9 edges and 14 connections. Minimal Weight = 37.
From To Weight
0 1 4
0 7 8
1 2 8
1 7 11
2 3 7
2 8 2
2 5 4
3 4 9
3 5 14
4 5 10
5 6 2
6 7 1
6 8 6
7 8 7
Min. Weight = 37
You can put your values for input, this is only an example.
c++ vector graph-theory minimum-spanning-tree
I have a code that finds minimal weight of spanning tree. There edges are declared as int, but I need them as string. How can I modify it for normal work?
If I modify vector to string, I see a lot of errors in compilator. Please help me to finish this code, I'm new in programming.
#include<bits/stdc++.h>
using namespace std;
typedef pair<int, int> iPair;
struct Graph
{
int Edges, Connections;
vector< pair<int, iPair> > edges;
Graph(int Edges, int Connections)
{
this->Edges = Edges;
this->Connections = Connections;
}
void addEdge(int u, int v, int w)
{
edges.push_back({w, {u, v}});
}
int kruskalMST();
};
struct DisjointSets
{
int *parent, *rnk;
int n;
DisjointSets(int n)
{
// Allocate memory
this->n = n;
parent = new int[n+1];
rnk = new int[n+1];
// Initially, all vertices are in
// different sets and have rank 0.
for (int i = 0; i <= n; i++)
{
rnk[i] = 0;
//every element is parent of itself
parent[i] = i;
}
}
// Find the parent of a node 'u'
// Path Compression
int find(int u)
{
/* Make the parent of the nodes in the path
from u--> parent[u] point to parent[u] */
if (u != parent[u])
parent[u] = find(parent[u]);
return parent[u];
}
// Union by rank
void merge(int x, int y)
{
x = find(x), y = find(y);
/* Make tree with smaller height
a subtree of the other tree */
if (rnk[x] > rnk[y])
parent[y] = x;
else // If rnk[x] <= rnk[y]
parent[x] = y;
if (rnk[x] == rnk[y])
rnk[y]++;
}
};
/* Functions returns weight of the MST*/
int Graph::kruskalMST()
{
int mst_wt = 0; // Initialize result
// Sort edges in increasing order on basis of cost
sort(edges.begin(), edges.end());
// Create disjoint sets
DisjointSets ds(Edges);
// Iterate through all sorted edges
vector< pair<int, iPair> >::iterator it;
for (it=edges.begin(); it!=edges.end(); it++)
{
int u = it->second.first;
int v = it->second.second;
int set_u = ds.find(u);
int set_v = ds.find(v);
if (set_u != set_v)
{
cout << u << " - " << v << " = " << it->first << endl;
mst_wt += it->first;
ds.merge(set_u, set_v);
}
}
return mst_wt;
}
// Driver program to test above functions
int main()
{
int from,to,weight,i;
/* Let us create above shown weighted
and unidrected graph */
int Edges, Connections;
cout<<"How many edges?:"; cin>>Edges;
cout<<"How many connections?:"; cin>>Connections;
Graph g(Edges, Connections);
system("cls");
// making above shown graph
for(i=0;i<Connections;i++){
cout<<endl;
cout<<"From "; cin>>from;
cout<<"To "; cin>>to;
cout<<"Weight "; cin>>weight;
g.addEdge(from,to,weight);
system("cls");
}
cout << "Edges of MST are n";
int mst_wt = g.kruskalMST();
cout << "nWeight of MST is " << mst_wt;
return 0;
}
Example of 9 edges and 14 connections. Minimal Weight = 37.
From To Weight
0 1 4
0 7 8
1 2 8
1 7 11
2 3 7
2 8 2
2 5 4
3 4 9
3 5 14
4 5 10
5 6 2
6 7 1
6 8 6
7 8 7
Min. Weight = 37
You can put your values for input, this is only an example.
c++ vector graph-theory minimum-spanning-tree
c++ vector graph-theory minimum-spanning-tree
edited Nov 19 at 16:19
TrebuchetMS
1,03315
1,03315
asked Nov 19 at 14:15
Ion Crismaru
41
41
Hi there, I'm slightly confused with your use of "edges" and "connections". I think, in your program, you meant to ask forNodes
andEdges
instead ofEdges
andConnections
? I've never heard of "connections" before, but I guess it's another term for "edges".
– TrebuchetMS
Nov 19 at 15:11
If I modify vector to string, I see a lot of errors in compilator.
okay, so what happened? What did you try? Did you substitute all appropriateint
withstd::string
? What did the compiler complain about? You've given us your original, working code (and that does help in some way), but then it becomes next to "Please do my homework for me":Please help me to finish this code
.
– TrebuchetMS
Nov 19 at 15:18
1
Why would you store a number (the edge weight) as a string rather than anint
as you do right now? You can't do math on strings (e.g. compare two weights). Leave the edge weights as numbers. Everything else is just wrong.
– Nico Schertler
Nov 19 at 16:33
@NicoSchertler I feel as though <, ==, and > are the subset of 'math' you can in fact perform with numerical strings. "123"s is less than "124"s.
– schulmaster
Nov 19 at 19:06
add a comment |
Hi there, I'm slightly confused with your use of "edges" and "connections". I think, in your program, you meant to ask forNodes
andEdges
instead ofEdges
andConnections
? I've never heard of "connections" before, but I guess it's another term for "edges".
– TrebuchetMS
Nov 19 at 15:11
If I modify vector to string, I see a lot of errors in compilator.
okay, so what happened? What did you try? Did you substitute all appropriateint
withstd::string
? What did the compiler complain about? You've given us your original, working code (and that does help in some way), but then it becomes next to "Please do my homework for me":Please help me to finish this code
.
– TrebuchetMS
Nov 19 at 15:18
1
Why would you store a number (the edge weight) as a string rather than anint
as you do right now? You can't do math on strings (e.g. compare two weights). Leave the edge weights as numbers. Everything else is just wrong.
– Nico Schertler
Nov 19 at 16:33
@NicoSchertler I feel as though <, ==, and > are the subset of 'math' you can in fact perform with numerical strings. "123"s is less than "124"s.
– schulmaster
Nov 19 at 19:06
Hi there, I'm slightly confused with your use of "edges" and "connections". I think, in your program, you meant to ask for
Nodes
and Edges
instead of Edges
and Connections
? I've never heard of "connections" before, but I guess it's another term for "edges".– TrebuchetMS
Nov 19 at 15:11
Hi there, I'm slightly confused with your use of "edges" and "connections". I think, in your program, you meant to ask for
Nodes
and Edges
instead of Edges
and Connections
? I've never heard of "connections" before, but I guess it's another term for "edges".– TrebuchetMS
Nov 19 at 15:11
If I modify vector to string, I see a lot of errors in compilator.
okay, so what happened? What did you try? Did you substitute all appropriate int
with std::string
? What did the compiler complain about? You've given us your original, working code (and that does help in some way), but then it becomes next to "Please do my homework for me": Please help me to finish this code
.– TrebuchetMS
Nov 19 at 15:18
If I modify vector to string, I see a lot of errors in compilator.
okay, so what happened? What did you try? Did you substitute all appropriate int
with std::string
? What did the compiler complain about? You've given us your original, working code (and that does help in some way), but then it becomes next to "Please do my homework for me": Please help me to finish this code
.– TrebuchetMS
Nov 19 at 15:18
1
1
Why would you store a number (the edge weight) as a string rather than an
int
as you do right now? You can't do math on strings (e.g. compare two weights). Leave the edge weights as numbers. Everything else is just wrong.– Nico Schertler
Nov 19 at 16:33
Why would you store a number (the edge weight) as a string rather than an
int
as you do right now? You can't do math on strings (e.g. compare two weights). Leave the edge weights as numbers. Everything else is just wrong.– Nico Schertler
Nov 19 at 16:33
@NicoSchertler I feel as though <, ==, and > are the subset of 'math' you can in fact perform with numerical strings. "123"s is less than "124"s.
– schulmaster
Nov 19 at 19:06
@NicoSchertler I feel as though <, ==, and > are the subset of 'math' you can in fact perform with numerical strings. "123"s is less than "124"s.
– schulmaster
Nov 19 at 19:06
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53376526%2fminimal-spanning-tree%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Hi there, I'm slightly confused with your use of "edges" and "connections". I think, in your program, you meant to ask for
Nodes
andEdges
instead ofEdges
andConnections
? I've never heard of "connections" before, but I guess it's another term for "edges".– TrebuchetMS
Nov 19 at 15:11
If I modify vector to string, I see a lot of errors in compilator.
okay, so what happened? What did you try? Did you substitute all appropriateint
withstd::string
? What did the compiler complain about? You've given us your original, working code (and that does help in some way), but then it becomes next to "Please do my homework for me":Please help me to finish this code
.– TrebuchetMS
Nov 19 at 15:18
1
Why would you store a number (the edge weight) as a string rather than an
int
as you do right now? You can't do math on strings (e.g. compare two weights). Leave the edge weights as numbers. Everything else is just wrong.– Nico Schertler
Nov 19 at 16:33
@NicoSchertler I feel as though <, ==, and > are the subset of 'math' you can in fact perform with numerical strings. "123"s is less than "124"s.
– schulmaster
Nov 19 at 19:06