用c++代码实现邻接矩阵的有向图、邻接矩阵的无向图、无向网、有向网、无向图遍历及操作、最小生成数、最短路径、每一对点之间的最短路径
时间: 2023-07-10 18:41:47 浏览: 102
好的,以下是关于邻接矩阵的有向图、邻接矩阵的无向图、无向网、有向网、无向图遍历及操作、最小生成树、最短路径和每一对点之间的最短路径的简单实现示例代码。请注意,这里的代码仅供参考,可能存在不足或者错误之处,具体实现还需要根据具体情况进行调整。
1. 邻接矩阵的有向图
```c++
#include <iostream>
#include <climits>
#include <vector>
using namespace std;
const int MAXN = 100; // 最大顶点数
class DirectedGraph {
private:
int vertexNum; // 顶点数
int matrix[MAXN][MAXN]; // 邻接矩阵
public:
DirectedGraph(int n) : vertexNum(n) {
// 初始化邻接矩阵
for (int i = 0; i < vertexNum; i++) {
for (int j = 0; j < vertexNum; j++) {
matrix[i][j] = INT_MAX;
}
}
}
// 添加一条边
void addEdge(int from, int to, int weight) {
matrix[from][to] = weight;
}
// 删除一条边
void removeEdge(int from, int to) {
matrix[from][to] = INT_MAX;
}
// 深度优先搜索
void dfs(int start, bool visited[]) {
visited[start] = true;
cout << start << " ";
for (int i = 0; i < vertexNum; i++) {
if (matrix[start][i] != INT_MAX && !visited[i]) {
dfs(i, visited);
}
}
}
// 广度优先搜索
void bfs(int start, bool visited[]) {
vector<int> queue;
queue.push_back(start);
visited[start] = true;
while (!queue.empty()) {
int cur = queue.front();
queue.erase(queue.begin());
cout << cur << " ";
for (int i = 0; i < vertexNum; i++) {
if (matrix[cur][i] != INT_MAX && !visited[i]) {
visited[i] = true;
queue.push_back(i);
}
}
}
}
// 打印邻接矩阵
void printMatrix() {
for (int i = 0; i < vertexNum; i++) {
for (int j = 0; j < vertexNum; j++) {
if (matrix[i][j] == INT_MAX) {
cout << "INF" << " ";
} else {
cout << matrix[i][j] << " ";
}
}
cout << endl;
}
}
};
int main() {
DirectedGraph g(5);
g.addEdge(0, 1, 2);
g.addEdge(0, 3, 1);
g.addEdge(1, 2, 3);
g.addEdge(2, 3, 2);
g.addEdge(3, 1, 1);
g.addEdge(3, 4, 4);
bool visited[5] = {false};
g.dfs(0, visited);
cout << endl;
for (int i = 0; i < 5; i++) {
visited[i] = false;
}
g.bfs(0, visited);
cout << endl;
g.printMatrix();
return 0;
}
```
2. 邻接矩阵的无向图
```c++
#include <iostream>
#include <climits>
#include <vector>
using namespace std;
const int MAXN = 100; // 最大顶点数
class UndirectedGraph {
private:
int vertexNum; // 顶点数
int matrix[MAXN][MAXN]; // 邻接矩阵
public:
UndirectedGraph(int n) : vertexNum(n) {
// 初始化邻接矩阵
for (int i = 0; i < vertexNum; i++) {
for (int j = 0; j < vertexNum; j++) {
matrix[i][j] = INT_MAX;
}
}
}
// 添加一条边
void addEdge(int from, int to, int weight) {
matrix[from][to] = weight;
matrix[to][from] = weight; // 无向图需要添加反向边
}
// 删除一条边
void removeEdge(int from, int to) {
matrix[from][to] = INT_MAX;
matrix[to][from] = INT_MAX;
}
// 深度优先搜索
void dfs(int start, bool visited[]) {
visited[start] = true;
cout << start << " ";
for (int i = 0; i < vertexNum; i++) {
if (matrix[start][i] != INT_MAX && !visited[i]) {
dfs(i, visited);
}
}
}
// 广度优先搜索
void bfs(int start, bool visited[]) {
vector<int> queue;
queue.push_back(start);
visited[start] = true;
while (!queue.empty()) {
int cur = queue.front();
queue.erase(queue.begin());
cout << cur << " ";
for (int i = 0; i < vertexNum; i++) {
if (matrix[cur][i] != INT_MAX && !visited[i]) {
visited[i] = true;
queue.push_back(i);
}
}
}
}
// 打印邻接矩阵
void printMatrix() {
for (int i = 0; i < vertexNum; i++) {
for (int j = 0; j < vertexNum; j++) {
if (matrix[i][j] == INT_MAX) {
cout << "INF" << " ";
} else {
cout << matrix[i][j] << " ";
}
}
cout << endl;
}
}
};
int main() {
UndirectedGraph g(5);
g.addEdge(0, 1, 2);
g.addEdge(0, 3, 1);
g.addEdge(1, 2, 3);
g.addEdge(2, 3, 2);
g.addEdge(3, 1, 1);
g.addEdge(3, 4, 4);
bool visited[5] = {false};
g.dfs(0, visited);
cout << endl;
for (int i = 0; i < 5; i++) {
visited[i] = false;
}
g.bfs(0, visited);
cout << endl;
g.printMatrix();
return 0;
}
```
3. 无向网
```c++
#include <iostream>
#include <climits>
#include <vector>
using namespace std;
const int MAXN = 100; // 最大顶点数
class UndirectedNetwork {
private:
int vertexNum; // 顶点数
int matrix[MAXN][MAXN]; // 邻接矩阵
public:
UndirectedNetwork(int n) : vertexNum(n) {
// 初始化邻接矩阵
for (int i = 0; i < vertexNum; i++) {
for (int j = 0; j < vertexNum; j++) {
matrix[i][j] = INT_MAX;
}
}
}
// 添加一条边
void addEdge(int from, int to, int weight) {
matrix[from][to] = weight;
matrix[to][from] = weight; // 无向图需要添加反向边
}
// 删除一条边
void removeEdge(int from, int to) {
matrix[from][to] = INT_MAX;
matrix[to][from] = INT_MAX;
}
// 深度优先搜索
void dfs(int start, bool visited[]) {
visited[start] = true;
cout << start << " ";
for (int i = 0; i < vertexNum; i++) {
if (matrix[start][i] != INT_MAX && !visited[i]) {
dfs(i, visited);
}
}
}
// 广度优先搜索
void bfs(int start, bool visited[]) {
vector<int> queue;
queue.push_back(start);
visited[start] = true;
while (!queue.empty()) {
int cur = queue.front();
queue.erase(queue.begin());
cout << cur << " ";
for (int i = 0; i < vertexNum; i++) {
if (matrix[cur][i] != INT_MAX && !visited[i]) {
visited[i] = true;
queue.push_back(i);
}
}
}
}
// 打印邻接矩阵
void printMatrix() {
for (int i = 0; i < vertexNum; i++) {
for (int j = 0; j < vertexNum; j++) {
if (matrix[i][j] == INT_MAX) {
cout << "INF" << " ";
} else {
cout << matrix[i][j] << " ";
}
}
cout << endl;
}
}
// Dijkstra算法
void dijkstra(int start, int dist[]) {
bool visited[MAXN] = {false};
for (int i = 0; i < vertexNum; i++) {
dist[i] = matrix[start][i];
}
visited[start] = true;
for (int i = 1; i < vertexNum; i++) {
int minDist = INT_MAX, minIndex = -1;
for (int j = 0; j < vertexNum; j++) {
if (!visited[j] && dist[j] < minDist) {
minDist = dist[j];
minIndex = j;
}
}
if (minIndex == -1) {
break;
}
visited[minIndex] = true;
for (int j = 0; j < vertexNum; j++) {
if (matrix[minIndex][j] != INT_MAX && dist[j] > dist[minIndex] + matrix[minIndex][j]) {
dist[j] = dist[minIndex] + matrix[minIndex][j];
}
}
}
}
};
int main() {
UndirectedNetwork g(5);
g.addEdge(0, 1, 2);
g.addEdge(0, 3, 1);
g.addEdge(1, 2, 3);
g.addEdge(2, 3, 2);
g.addEdge(3, 1, 1);
g.addEdge(3, 4, 4);
bool visited[5] = {false};
g.dfs(0, visited);
cout << endl;
for (int i = 0; i < 5; i++) {
visited[i] = false;
}
g.bfs(0, visited);
cout << endl;
g.printMatrix();
int dist[5];
g.dijkstra(0, dist);
for (int i = 0; i < 5; i++) {
cout << dist[i] << " ";
}
cout << endl;
return 0;
}
```
4. 有向网
```c++
#include <iostream>
#include <climits>
#include <vector>
using namespace std;
const int MAXN = 100; // 最大顶点数
class DirectedNetwork {
private:
int vertexNum; // 顶点数
int matrix[MAXN][MAXN]; // 邻接矩阵
public:
DirectedNetwork(int n) : vertexNum(n) {
// 初始化邻接矩阵
for (int i = 0; i < vertexNum; i++) {
for (int j = 0; j < vertexNum; j++) {
matrix[i][j] = INT_MAX;
}
}
}
// 添加一条边
void addEdge(int from, int to, int weight) {
matrix[from][to] = weight;
}
// 删除一条边
void removeEdge(int from, int to) {
matrix[from][to] = INT_MAX;
}
// 深度优先搜索
void dfs(int start, bool visited[]) {
visited[start] = true;
cout << start << " ";
for (int i = 0; i < vertex
阅读全文