Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions MinimumSpanningTree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#include"MinimumSpanningTree.h"

using namespace mst;
node::node(int invalue) :value(invalue) {

}
void MSTTree::init() {
const int nodesize = 5;
node node[nodesize]{0,1,2,3,4};
list<Line> lines;
Line line;
line.node1 = &node[0];
line.node2 = &node[1];
line.cost = 2;
lines.push_back(line);

line.node1 = &node[0];
line.node2 = &node[2];
line.cost = 1;
lines.push_back(line);

line.node1 = &node[1];
line.node2 = &node[2];
line.cost = 3;
lines.push_back(line);

line.node1 = &node[3];
line.node2 = &node[2];
line.cost = 3;
lines.push_back(line);

line.node1 = &node[2];
line.node2 = &node[4];
line.cost = 1;
lines.push_back(line);

line.node1 = &node[3];
line.node2 = &node[4];
line.cost = 2;
lines.push_back(line);

list<Line> result=getMST(nodesize, lines);

cout << endl;
for (list<Line>::iterator it = result.begin(); it != result.end(); it++) {
cout << it->cost<<"\n";
}
}
list<Line> MSTTree::getMST(int nodesize,list<Line> linelist) {
int check[6] = {0};
list<Line> result;
for (int target = 0; target < nodesize; target++) {
list<Line> lines;
for (list<Line>::iterator it = linelist.begin(); it != linelist.end(); it++) {
if (it->node1->value == target || it->node2->value==target) {
lines.push_back(*it);
}
}
//minvalue check
int minvalue=99999;
Line* checkline=NULL;
for (list<Line>::iterator it = lines.begin(); it != lines.end(); it++) {
if (it->cost < minvalue) {
//overlap check
if (find_if(result.begin(), result.end(), [it](Line value)-> bool{
if (value.node1->value == it->node1->value) {
if (value.node2->value == it->node2->value) {
return true;
}
}else if (value.node2->value == it->node1->value) {
if (value.node1->value == it->node2->value) {
return true;
}
}
return false;
}) == result.end()) {
minvalue = it->cost;
checkline = &(*it);
}
}
}

if (checkline!=NULL) {
result.push_back(*checkline);
}
}

return result;
}
44 changes: 44 additions & 0 deletions MinimumSpanningTree.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#ifndef MinimumSpenningTree_H
#define MinimumSpenningTree_H
#ifndef vector_h
#define vector_h
#include<vector>
#endif // !vector

#ifndef iostream_h
#define iostream_h
#include<iostream>
#endif // !iostream

#ifndef list_h
#define list_h
#include<list>
#endif

#ifndef algorithm_h
#define algorithm_h
#include <algorithm>
#endif // !algorithm_h

namespace mst {
using namespace std;

class node {
public:
int value;
node()=default;
node(int);
};
class Line {
public:
node* node1;
node* node2;
int cost;
};
class MSTTree {
public:
void init();
list<Line> getMST(int,list<Line>);
};
}
#endif