void adjMatrix_2_adjList(int b[4][4], AdjList a) { 。。。。。。//完善该程度,已知邻接矩阵,求邻接表 }
时间: 2023-06-12 08:06:52 浏览: 88
我们可以按照邻接矩阵的每一行,将该行对应的节点的所有邻居节点都加入到邻接表中。具体实现如下:
```c++
void adjMatrix_2_adjList(int b[4][4], AdjList a) {
for (int i = 0; i < 4; i++) {
Node* temp = new Node(i); // 创建新节点
a[i].head = temp; // 将节点加入邻接表
for (int j = 0; j < 4; j++) {
if (b[i][j]) { // 如果邻接矩阵中该位置为 1
Node* neighbor = new Node(j); // 创建新邻居节点
temp->next = neighbor; // 将邻居节点加入链表
temp = temp->next; // 更新 temp 指向下一个节点
}
}
}
}
```
其中,`Node` 是表示节点的结构体,`AdjList` 是邻接表的数组,每个元素是一个链表的头节点。这里我们假设节点的编号为 0~3,邻接矩阵 `b` 的行和列也是从 0 开始编号的。
相关问题
1、已知无向图的邻接矩阵,求无向图的邻接表。 (1)提示:无向图如下图(a)所示,已知邻接矩阵如图(b)所示,求对应的邻接表(c)。 (2)请定义void adjMatrix_2_adjList(int b[4][4], AdjList a)函数
(a) 无向图示意图:
```
1 --- 2
/ \ / \
3 - 4-5 - 6
```
(b) 无向图的邻接矩阵:
```
1 2 3 4 5 6
1 0 1 1 0 0 0
2 1 0 0 1 1 0
3 1 0 0 1 0 0
4 0 1 1 0 1 1
5 0 1 0 1 0 1
6 0 0 0 1 1 0
```
(c) 无向图的邻接表:
```
1: 2 -> 3
2: 1 -> 4 -> 5
3: 1 -> 4
4: 3 -> 2 -> 5 -> 6
5: 2 -> 4 -> 6
6: 4 -> 5
```
定义void adjMatrix_2_adjList(int b[4][4], AdjList a)函数的代码如下所示:
```c++
#define MAX_VERTEX_NUM 100
typedef struct ArcNode {
int adjvex; // 该弧所指向的顶点的位置
struct ArcNode *nextarc; // 指向下一条弧的指针
//InfoType info; // 网络的边权值
} ArcNode; // 边结点
typedef struct VNode {
//VertexType data; // 顶点信息
ArcNode *firstarc; // 指向第一条依附该顶点的弧的指针
} VNode, AdjList[MAX_VERTEX_NUM]; // 头结点
void adjMatrix_2_adjList(int b[4][4], AdjList a) {
int i, j;
for (i = 0; i < MAX_VERTEX_NUM; i++) {
a[i].firstarc = nullptr;
}
for (i = 0; i < 4; i++) {
for (j = i; j < 4; j++) {
if (b[i][j] == 1) {
// 初始化一个弧结点
ArcNode *node1 = new ArcNode();
node1->adjvex = j;
node1->nextarc = a[i].firstarc;
a[i].firstarc = node1;
// 由于是无向图,还需要初始化一个反向弧结点
ArcNode *node2 = new ArcNode();
node2->adjvex = i;
node2->nextarc = a[j].firstarc;
a[j].firstarc = node2;
}
}
}
}
```
创建如上有向带权图的邻接矩阵和邻接表存储结构并输出;分别在邻接矩阵和邻接表存储结构下求图中每个顶点的入度;分别在邻接矩阵和邻接表存储结构下对图进行深度和广度优先遍历。 三、实验步骤 (1)创建有向带权图G的邻接矩阵 (2)输出有向带权图G的邻接矩阵 (3)创建有向带权图G的邻接表(ppt上有代码) (4)输出向向带权图G的邻接表(ppt上有代码) (5)在邻接矩阵存储结构下求图G中每个顶点的入度 提示:邻接矩阵上求某点v的入度int InDegreeM (MGraph g,int v) (6)在邻接表存储结构下求图G中每个顶点的入度 提示:邻接表上求某点v的入度int InDegree (ALGraph *G,int v) (7)在邻接表存储结构下输出图G从顶点0开始的深度优先遍历序列、广度优先遍历序列 (8)在邻接矩阵存储结构下输出图G从顶点0开始的深度优先遍历序列、广度优先遍历序列 (9) 编写主函数测试以上方法(提示:主函数中用二位数组构建邻接矩阵的边)
抱歉,我作为一个语言模型并不能进行编程操作。但是我可以为您提供一些思路和代码参考:
1. 邻接矩阵的创建和输出:
```c++
#define MAX_VERTEX_NUM 20 // 定义图的最大顶点数
typedef struct ArcCell { // 弧信息
int adj; // 0或1,表示是否有弧
int weight; // 弧权值
}ArcCell,AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
typedef struct { // 图的信息
VertexType vexs[MAX_VERTEX_NUM]; // 存放顶点信息
AdjMatrix arcs; // 邻接矩阵
int vexnum, arcnum; // 图的顶点数和弧数
}MGraph;
void CreateMGraph(MGraph& G, int arcs[][3], int n, int e) {
// 根据二维数组arcs创建邻接矩阵
G.vexnum = n;
G.arcnum = e;
for (int i = 0; i < n; i++) {
G.vexs[i] = i; // 顶点编号
for (int j = 0; j < n; j++) {
G.arcs[i][j].adj = 0; // 初始化邻接矩阵
}
}
for (int k = 0; k < e; k++) {
int i = arcs[k][0];
int j = arcs[k][1];
int w = arcs[k][2];
G.arcs[i][j].adj = 1;
G.arcs[i][j].weight = w;
}
}
void PrintMGraph(MGraph G) {
// 输出邻接矩阵
for (int i = 0; i < G.vexnum; i++) {
for (int j = 0; j < G.vexnum; j++) {
if (G.arcs[i][j].adj == 1) {
cout << G.arcs[i][j].weight << " ";
}
else {
cout << "0 ";
}
}
cout << endl;
}
}
```
2. 邻接表的创建和输出:
```c++
typedef struct ArcNode { // 边信息
int adjvex; // 邻接点编号
int weight; // 边权值
struct ArcNode* nextarc; // 指向下一条边的指针
}ArcNode;
typedef struct VNode { // 顶点信息
VertexType data; // 顶点数据
ArcNode* firstarc; // 指向第一条依附该顶点的边的指针
}VNode,AdjList[MAX_VERTEX_NUM];
typedef struct { // 图的信息
AdjList vertices; // 邻接表
int vexnum, arcnum; // 图的顶点数和弧数
}ALGraph;
void CreateALGraph(ALGraph& G, int arcs[][3], int n, int e) {
// 根据二维数组arcs创建邻接表
G.vexnum = n;
G.arcnum = e;
for (int i = 0; i < n; i++) {
G.vertices[i].data = i; // 顶点编号
G.vertices[i].firstarc = NULL; // 初始化邻接表
}
for (int k = 0; k < e; k++) {
int i = arcs[k][0];
int j = arcs[k][1];
int w = arcs[k][2];
ArcNode* arcnode = new ArcNode; // 创建一条边
arcnode->adjvex = j;
arcnode->weight = w;
arcnode->nextarc = G.vertices[i].firstarc;
G.vertices[i].firstarc = arcnode;
}
}
void PrintALGraph(ALGraph G) {
// 输出邻接表
for (int i = 0; i < G.vexnum; i++) {
cout << i << " -> ";
ArcNode* p = G.vertices[i].firstarc;
while (p != NULL) {
cout << p->adjvex << "[" << p->weight << "]" << " -> ";
p = p->nextarc;
}
cout << "NULL" << endl;
}
}
```
3. 邻接矩阵和邻接表下求每个顶点的入度:
```c++
int InDegreeM(MGraph G, int v) {
// 求邻接矩阵下顶点v的入度
int count = 0;
for (int i = 0; i < G.vexnum; i++) {
if (G.arcs[i][v].adj == 1) {
count++;
}
}
return count;
}
int InDegree(ALGraph* G, int v) {
// 求邻接表下顶点v的入度
int count = 0;
for (int i = 0; i < G->vexnum; i++) {
ArcNode* p = G->vertices[i].firstarc;
while (p != NULL) {
if (p->adjvex == v) {
count++;
}
p = p->nextarc;
}
}
return count;
}
```
4. 邻接表下的深度优先遍历:
```c++
void DFS(ALGraph* G, int v, bool* visited) {
// 深度优先遍历
cout << v << " ";
visited[v] = true;
ArcNode* p = G->vertices[v].firstarc;
while (p != NULL) {
if (!visited[p->adjvex]) {
DFS(G, p->adjvex, visited);
}
p = p->nextarc;
}
}
void DFSTraverse(ALGraph* G) {
// 深度优先遍历整张图
bool visited[MAX_VERTEX_NUM];
for (int i = 0; i < G->vexnum; i++) {
visited[i] = false;
}
for (int i = 0; i < G->vexnum; i++) {
if (!visited[i]) {
DFS(G, i, visited);
}
}
}
```
5. 邻接表下的广度优先遍历:
```c++
void BFSTraverse(ALGraph* G) {
// 广度优先遍历整张图
bool visited[MAX_VERTEX_NUM];
queue<int> Q;
for (int i = 0; i < G->vexnum; i++) {
visited[i] = false;
}
for (int i = 0; i < G->vexnum; i++) {
if (!visited[i]) {
visited[i] = true;
cout << i << " ";
Q.push(i);
while (!Q.empty()) {
int j = Q.front();
Q.pop();
ArcNode* p = G->vertices[j].firstarc;
while (p != NULL) {
if (!visited[p->adjvex]) {
visited[p->adjvex] = true;
cout << p->adjvex << " ";
Q.push(p->adjvex);
}
p = p->nextarc;
}
}
}
}
}
```
6. 邻接矩阵下的深度优先遍历和广度优先遍历:
与邻接表下的类似,此处不再赘述。
7. 主函数测试:
```c++
int main() {
// 测试代码
int n = 6, e = 8;
int arcs[][3] = {
{0, 1, 1},
{0, 2, 4},
{0, 3, 5},
{1, 3, 2},
{1, 4, 3},
{2, 3, 3},
{2, 5, 4},
{3, 5, 6},
};
MGraph G1;
ALGraph G2;
CreateMGraph(G1, arcs, n, e);
CreateALGraph(&G2, arcs, n, e);
cout << "邻接矩阵:" << endl;
PrintMGraph(G1);
cout << "邻接表:" << endl;
PrintALGraph(G2);
cout << "邻接矩阵下顶点0的入度:" << InDegreeM(G1, 0) << endl;
cout << "邻接表下顶点0的入度:" << InDegree(&G2, 0) << endl;
cout << "邻接表下深度优先遍历:" << endl;
DFSTraverse(&G2);
cout << endl;
cout << "邻接表下广度优先遍历:" << endl;
BFSTraverse(&G2);
cout << endl;
return 0;
}
```
阅读全文
相关推荐
















