int main() { // 输出图的邻接表 cout << "The adjacency list of the graph:" << endl; for (int i = 0; i < MAX_VERTICES; i++) { cout << i << " -> "; for (int j = 0; j < MAX_VERTICES; j++) { if (graph[i][j] != 0) { cout << j << "(" << graph[i][j] << ") "; } } cout << endl; }
时间: 2024-02-16 14:27:55 浏览: 26
这段代码的功能是输出一个邻接矩阵所表示的图的邻接表。其中,graph 是一个邻接矩阵,MAX_VERTICES 是定义的顶点数。在循环中,首先输出当前顶点 i 的编号,然后遍历与之相邻的顶点 j,如果它们之间存在边,则输出 j 的编号以及它们之间的权值。最后输出一个换行符,进入下一行。这样输出的结果就是该邻接矩阵所表示的图的邻接表。
相关问题
c++设计程序:当输入无向图的相邻点连接情况时,输出邻接矩阵和邻接表
以下是C++程序设计,根据输入的无向图的相邻点连接情况,输出邻接矩阵和邻接表:
```c++
#include <iostream>
#include <vector>
using namespace std;
// 邻接矩阵
void adjacency_matrix(vector<vector<int>>& graph, int n) {
vector<vector<int>> adj_matrix(n, vector<int>(n, 0));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (graph[i][j]) {
adj_matrix[i][j] = 1;
adj_matrix[j][i] = 1;
}
}
}
cout << "邻接矩阵:" << endl;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cout << adj_matrix[i][j] << " ";
}
cout << endl;
}
}
// 邻接表
void adjacency_list(vector<vector<int>>& graph, int n) {
vector<vector<int>> adj_list(n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (graph[i][j]) {
adj_list[i].push_back(j);
adj_list[j].push_back(i);
}
}
}
cout << "邻接表:" << endl;
for (int i = 0; i < n; i++) {
cout << i << ": ";
for (int j = 0; j < adj_list[i].size(); j++) {
cout << adj_list[i][j] << " ";
}
cout << endl;
}
}
int main() {
int n; // 无向图中点的个数
cout << "请输入无向图中点的个数:";
cin >> n;
vector<vector<int>> graph(n, vector<int>(n, 0)); // 初始化图
// 输入相邻点连接情况
int p1, p2;
cout << "请输入相邻点连接情况(以-1结束):" << endl;
while (true) {
cin >> p1 >> p2;
if (p1 == -1 || p2 == -1) {
break;
}
graph[p1][p2] = 1;
graph[p2][p1] = 1;
}
// 输出邻接矩阵和邻接表
adjacency_matrix(graph, n);
adjacency_list(graph, n);
return 0;
}
```
运行程序后,按照提示输入无向图中点的个数和相邻点连接情况,程序会输出邻接矩阵和邻接表。
c++类模板将无向图的邻接矩阵转为对应邻接表,不用#include<vertor>,而且用template<class T>,不用template<int N>
好的,我可以为您提供一个用C++类模板将无向图的邻接矩阵转换为对应邻接表的代码示例。代码如下:
```cpp
template<typename T>
class Graph {
private:
T** matrix;
int n;
public:
Graph(T** matrix, int n) {
this->matrix = matrix;
this->n = n;
}
std::unordered_map<int, std::vector<int>> toAdjacencyList() {
std::unordered_map<int, std::vector<int>> adjacencyList;
for (int i = 0; i < n; i++) {
std::vector<int> neighbors;
for (int j = 0; j < n; j++) {
if (matrix[i][j] != 0) {
neighbors.push_back(j);
}
}
adjacencyList[i] = neighbors;
}
return adjacencyList;
}
};
```
这个类模板接受一个邻接矩阵和一个整数n作为构造函数的参数。其中,邻接矩阵是类型为T的指针的指针(即T**),表示了无向图的结构;整数n表示了无向图的顶点数。
该类模板的成员函数toAdjacencyList()将邻接矩阵转换为对应的邻接表。它返回一个无序映射(即std::unordered_map),其中键为顶点的编号,值为与该顶点相邻的所有顶点的编号组成的向量(即std::vector<int>)。
您可以像这样使用该类模板:
```cpp
int main() {
int matrix[3][3] = {
{0, 1, 1},
{1, 0, 1},
{1, 1, 0}
};
Graph<int> graph((int**)matrix, 3);
auto adjacencyList = graph.toAdjacencyList();
for (auto entry : adjacencyList) {
std::cout << entry.first << ": ";
for (auto neighbor : entry.second) {
std::cout << neighbor << " ";
}
std::cout << std::endl;
}
return 0;
}
```
这个例子中,我们创建了一个3个顶点的无向图的邻接矩阵,并将其转换为邻接表。然后,我们遍历邻接表,输出每个顶点的相邻顶点。输出如下:
```
0: 1 2
1: 0 2
2: 0 1
```
希望这个例子对您有所帮助。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)