#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
std::default_random_engine generator;
#define INF 9999999
std::uniform_int_distribution<int> dis(1, 9);
void myRandom(vector<int>&datasets, int n) {
for (int i = 0; i < n; i++)
datasets.push_back(dis(generator));
}
void dijkstra(int v_num = 6, int e_num = 10) {
vector<int> X(1), Y(v_num - 1);
X[0] = 1;
for (int i = 0; i < v_num - 1; i++)
Y[i] = i + 2;
vector<int> dis(v_num + 1);
vector<int> record(v_num + 1);
vector<vector<int>> G;
for (int i = 0; i < v_num + 1; i++) {
G.push_back(vector<int>(v_num + 1, INF));
G[i][i] = 0;
G[0][i] = i;
G[i][0] = i;
}
G[1][2] = 2;
G[1][5] = 3;
G[1][3] = 12;
G[2][3] = 3;
G[2][4] = 6;
G[4][3] = 4;
G[4][6] = 15;
G[5][4] = 2;
G[5][6] = 4;
G[3][5] = 5;
for (int i = 1; i <= v_num; i++) {
dis[i] = G[1][i];
record[i] = (dis[i] != INF ? 1 : i);
}
for (auto it = G.begin(); it != G.end(); it++) {
for (auto it2 = it->begin(); it2 != it->end(); it2++) {
cout << *it2 << '\t';
}
cout << endl;
}
cout << "选择:1" << '\n' << "dis:\t";;
for_each(dis.begin(), dis.end(), [&](auto &each) {cout << each << '\t'; });
cout << endl;
cout << "X:" << '\t';
for_each(X.begin(), X.end(), [&](auto &each) {cout << each << '\t'; });
cout << endl;
cout << "Y:" << '\t';
for_each(Y.begin(), Y.end(), [&](auto &each) {cout << each << '\t'; });
cout << endl;
while (!Y.empty()) {
int d = 0;
for (int i = 1; i < Y.size(); i++) {
if (dis[Y[d]]>dis[Y[i]]) {
d = i;
}
}
X.push_back(Y[d]);
for (int i = 0; i < Y.size(); i++) {
int temp = dis[Y[d]] + G[Y[d]][Y[i]];
if (temp < dis[Y[i]]) {
dis[Y[i]] = temp;
record[Y[i]] = Y[d];
}
}
Y.erase(Y.begin() + d);
cout << "选择:" << X.back() << '\n' << "dis:\t";
for_each(dis.begin(), dis.end(), [&](auto &each) {cout << each << '\t'; });
cout << endl;
cout << "X:" << '\t';
for_each(X.begin(), X.end(), [&](auto &each) {cout << each << '\t'; });
cout << endl;
cout << "Y:" << '\t';
for_each(Y.begin(), Y.end(), [&](auto &each) {cout << each << '\t'; });
cout << endl;
}
for_each(record.begin(), record.end(), [&](auto &each) {cout << each << '\t'; });
cout << endl;
}
typedef struct _node {
int val;
int start;
int end;
struct _node():val(0),start(0),end(0){}
struct _node(int val, int start, int end) :val(val),start(start),end(end) {}
}Node;
int FIND(int x, vector<vector<int>> &VSet) {
for (int i = 0; i < VSet.size(); i++) {
for (int j = 0; j < VSet[i].size(); j++) {
if (VSet[i][j] == x)
return i;
}
}
return 0;
}
void UNION(int x, int y, vector<vector<int>> &VSet) {
int p1 = FIND(x, VSet);
int p2 = FIND(y, VSet);
VSet[p1].insert(VSet[p1].end(), VSet[p2].begin(), VSet[p2].end());
VSet.erase(VSet.begin() + p2);
}
void kruskal(int v_num = 6, int e_num = 9) {
vectoredges;
vectorT;
edges.push_back(Node(1, 1, 2));
edges.push_back(Node(2, 1, 3));
edges.push_back(Node(6, 2, 3));
edges.push_back(Node(11, 2, 4));
edges.push_back(Node(9, 3, 4));
edges.push_back(Node(13, 3, 5));
edges.push_back(Node(7, 4, 5));
edges.push_back(Node(3, 4, 6));
edges.push_back(Node(4, 5, 6));
vector<vector<int>> VSet(v_num);
for (int i = 0; i < v_num; i++) {
VSet[i].push_back(i + 1);
}
sort(edges.begin(), edges.end(), [](auto&edge1, auto&edge2) {return edge1.val < edge2.val; });
for_each(edges.begin(), edges.end(), [](auto&edge1) {cout << edge1.val << '\t'; });
cout << endl;
while (T.size() < v_num - 1) {
if (!edges.empty()) {
int p1 = FIND(edges[0].start, VSet);
int p2 = FIND(edges[0].end, VSet);
if (p1 != p2) {
T.push_back(edges[0]);
UNION(edges[0].start, edges[0].end, VSet);
}
edges.erase(edges.begin());
}
}
for_each(T.begin(), T.end(), [](auto&edge1) {cout << edge1.val << '\t'; });
cout << endl;
}
void prim(int v_num=6,int e_num=9) {
vector<vector<int> > G(v_num + 1, vector<int>(v_num + 1, INF));
vector<int>X,Y;
vectorT;
X.push_back(1);
for (int i = 0; i < v_num - 1; i++) {
Y.push_back(i + 2);
}
G[1][2] = G[2][1] = 1;
G[1][3] = G[3][1] = 2;
G[2][3] = G[3][2] = 6;
G[2][4] = G[4][2] = 11;
G[4][3] = G[3][4] = 9;
G[3][5] = G[5][3] = 13;
G[4][5] = G[5][4] = 7;
G[4][6] = G[6][4] = 3;
G[5][6] = G[6][5] = 4;
while (!Y.empty()) {
Node min_edge(G[X[0]][Y[0]], X[0],Y[0]);
for (int i = 0; i < X.size(); i++) {
for (int j = 0; j < Y.size(); j++) {
if (min_edge.val > G[X[i]][Y[j]]) {
min_edge.val = G[X[i]][Y[j]];
min_edge.start = X[i];
min_edge.end = Y[j];
}
}
}
T.push_back(min_edge);
X.push_back(min_edge.end);
auto it = Y.begin();
for (; it != Y.end(); it++) {
if (*it == min_edge.end)
break;
}
Y.erase(it);
}
for_each(T.begin(), T.end(), [](auto&edge1) {cout << edge1.val << '\t'; });
cout << endl;
}
int main() {
prim();
system("pause");
return 0;
}