#include
#include
#include
#include
#include
#include
using namespace std;
int primMST(vector<vector<int>> &graph)
{
int V = graph.size();
vector<int> key(V, INT_MAX);
vector<bool> inMST(V, false);
int totalWeight = 0;
key[0] = 0;
for (int count = 0; count < V - 1; ++count)
{
int u = -1;
for (int v = 0; v < V; ++v)
{
if (!inMST[v] && (u == -1 || key[v] < key[u]))
u = v;
}
cout << "primMST当前u : " << u << endl;
inMST[u] = true;
for (int v = 0; v < V; ++v)
{
if (graph[u][v] && !inMST[v] && graph[u][v] < key[v])
{
key[v] = graph[u][v];
cout << "primMST当前key[v] : " << key[v] << endl;
}
}
}
for (int i = 0; i < V; ++i)
totalWeight += key[i];
return totalWeight;
}
class Kruskal
{
private:
vector<int> parent;
int V;
public:
Kruskal(int vertices) : V(vertices)
{
parent.resize(V);
iota(parent.begin(), parent.end(), 0);
}
int find(int v)
{
if (v == parent[v])
return v;
return parent[v] = find(parent[v]);
}
void unionSets(int a, int b)
{
a = find(a);
b = find(b);
if (a != b)
parent[b] = a;
}
int kruskalMST(vector<pair<int, pair<int, int>>> &edges)
{
sort(edges.begin(), edges.end());
int mstWeight = 0;
for (auto edge : edges)
{
int u = edge.second.first;
int v = edge.second.second;
int weight = edge.first;
if (find(u) != find(v))
{
mstWeight += weight;
unionSets(u, v);
}
}
return mstWeight;
}
};
void dijkstra(vector<vector<int>> &graph, int src)
{
int V = graph.size();
vector<int> dist(V, INT_MAX);
vector<bool> visited(V, false);
dist[src] = 0;
for (int count = 0; count < V - 1; ++count)
{
int u = -1;
for (int v = 0; v < V; ++v)
{
if (!visited[v] && (u == -1 || dist[v] < dist[u]))
u = v;
}
visited[u] = true;
for (int v = 0; v < V; ++v)
{
if (!visited[v] && graph[u][v] && dist[u] != INT_MAX && dist[u] + graph[u][v] < dist[v])
dist[v] = dist[u] + graph[u][v];
}
}
cout << "Dijkstra算法 - 从源点 " << src << " 到各顶点的最短路径长度:" << endl;
for (int i = 0; i < V; ++i)
cout << "到顶点 " << i << " 的距离为 " << dist[i] << endl;
}
void bellmanFord(vector<vector<int>> &graph, int src)
{
int V = graph.size();
vector<int> dist(V, INT_MAX);
dist[src] = 0;
for (int i = 0; i < V - 1; ++i)
{
for (int u = 0; u < V; ++u)
{
for (int v = 0; v < V; ++v)
{
if (graph[u][v] && dist[u] != INT_MAX && dist[u] + graph[u][v] < dist[v])
dist[v] = dist[u] + graph[u][v];
}
}
}
for (int u = 0; u < V; ++u)
{
for (int v = 0; v < V; ++v)
{
if (graph[u][v] && dist[u] != INT_MAX && dist[u] + graph[u][v] < dist[v])
{
cout << "图中含有负权回路,无法求解最短路径。" << endl;
return;
}
}
}
cout << "Bellman-Ford算法 - 从源点 " << src << " 到各顶点的最短路径长度:" << endl;
for (int i = 0; i < V; ++i)
cout << "到顶点 " << i << " 的距离为 " << dist[i] << endl;
}
void floydWarshall(vector<vector<int>> &graph)
{
int V = graph.size();
vector<vector<int>> dist(graph);
for (int k = 0; k < V; ++k)
{
for (int i = 0; i < V; ++i)
{
for (int j = 0; j < V; ++j)
{
if (dist[i][k] != INT_MAX && dist[k][j] != INT_MAX && dist[i][k] + dist[k][j] < dist[i][j])
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}
cout << "Floyd算法 - 所有顶点对之间的最短路径长度:" << endl;
for (int i = 0; i < V; ++i)
{
for (int j = 0; j < V; ++j)
{
if (dist[i][j] == INT_MAX)
cout << "INT_MAX\t";
else
cout << dist[i][j] << "\t";
}
cout << endl;
}
}
int main()
{
vector<vector<int>> primGraph = {
{0, 6, 1, 5, 0, 0},
{6, 0, 5, 0, 3, 0},
{1, 5, 0, 5, 6, 4},
{5, 0, 5, 0, 0, 2},
{0, 3, 6, 0, 0, 6},
{0, 0, 4, 2, 6, 0}
};
int primWeight = primMST(primGraph);
cout << "Prim算法 - 最小生成树的权值为: " << primWeight << endl;
vector<pair<int, pair<int, int>>> kruskalEdges = {
{6, {0, 1}},
{1, {0, 2}},
{5, {0, 3}},
{5, {1, 2}},
{3, {1, 4}},
{5, {2, 3}},
{6, {2, 4}},
{4, {2, 5}},
{2, {3, 5}},
{6, {4, 5}},
};
Kruskal kruskal(6);
int kruskalWeight = kruskal.kruskalMST(kruskalEdges);
cout << "Kruskal算法 - 最小生成树的权值为: " << kruskalWeight << endl;
vector<vector<int>> dijkstraGraph1 = {
{0, 4, 0, 0, 0, 0, 0, 8, 0},
{4, 0, 8, 0, 0, 0, 0, 11, 0},
{0, 8, 0, 7, 0, 4, 0, 0, 2},
{0, 0, 7, 0, 9, 14, 0, 0, 0},
{0, 0, 0, 9, 0, 10, 0, 0, 0},
{0, 0, 4, 14, 10, 0, 2, 0, 0},
{0, 0, 0, 0, 0, 2, 0, 1, 6},
{8, 11, 0, 0, 0, 0, 1, 0, 7},
{0, 0, 2, 0, 0, 0, 6, 7, 0}
};
vector<vector<int>> dijkstraGraph = {
{0, 10, 0, 0, 5},
{0, 0, 1, 0, 0},
{0, 0, 0, 4, 0},
{7, 0, 6, 0, 0},
{0, 3, 9, 2, 0},
};
cout << " " << endl;
dijkstra(dijkstraGraph, 0);
vector<vector<int>> bellmanFordGraph1 = {
{0, 4, 0, 0, 0, 0, 0, 8, 0},
{4, 0, 8, 0, 0, 0, 0, 11, 0},
{0, 8, 0, 7, 0, 4, 0, 0, 2},
{0, 0, 7, 0, 9, 14, 0, 0, 0},
{0, 0, 0, 9, 0, 10, 0, 0, 0},
{0, 0, 4, 14, 10, 0, 2, 0, 0},
{0, 0, 0, 0, 0, 2, 0, 1, 6},
{8, 11, 0, 0, 0, 0, 1, 0, 7},
{0, 0, 2, 0, 0, 0, 6, 7, 0}
};
vector<vector<int>> bellmanFordGraph = {
{0, 10, 0, 0, 5},
{0, 0, 1, 0, 0},
{0, 0, 0, 4, 0},
{7, 0, 6, 0, 0},
{0, 3, 9, 2, 0},
};
cout << " " << endl;
bellmanFord(bellmanFordGraph, 0);
vector<vector<int>> floydGraph = {
{0, 4, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, 8, INT_MAX},
{4, 0, 8, INT_MAX, INT_MAX, INT_MAX, INT_MAX, 11, INT_MAX},
{INT_MAX, 8, 0, 7, INT_MAX, 4, INT_MAX, INT_MAX, 2},
{INT_MAX, INT_MAX, 7, 0, 9, 14, INT_MAX, INT_MAX, INT_MAX},
{INT_MAX, INT_MAX, INT_MAX, 9, 0, 10, INT_MAX, INT_MAX, INT_MAX},
{INT_MAX, INT_MAX, 4, 14, 10, 0, 2, INT_MAX, INT_MAX},
{INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, 2, 0, 1, 6},
{8, 11, INT_MAX, INT_MAX, INT_MAX, INT_MAX, 1, 0, 7},
{INT_MAX, INT_MAX, 2, INT_MAX, INT_MAX, INT_MAX, 6, 7, 0}
};
vector<vector<int>> floydGraph1 = {
{0, 10, INT_MAX, INT_MAX, 5},
{INT_MAX, 0, 1, INT_MAX, INT_MAX},
{INT_MAX, INT_MAX, 0, 4, INT_MAX},
{7, INT_MAX, 6, 0, INT_MAX},
{INT_MAX, 3, 9, 2, 0},
};
cout << " " << endl;
floydWarshall(floydGraph1);
return 0;
}