#include <stdio.h> #define MAX 100 #define MAXNUM 100000 typedef struct { int n, e, weight; char vexs[MAX]; int edges[MAX][MAX]; }MGraph; void CreateMGraph(MGraph* G) { int i, j, k; int p; char ch1, ch2; int weight; printf("请输入顶点数:"); scanf_s("%d", &G->e); printf("请输入各顶点信息(每个顶点以回车作为结束):\n"); for (i = 0; i < G->n; i++) { getchar(); printf("输入第%d个顶点:", i + 1); scanf_s("%c", &(G->vexs[i])); } for (i = 0; i < G->n; i++) for (j = 0; j < G->n; j++) G->edges[i][j] = MAXNUM; for (k = 1; k <= G->e; k++) { getchar(); printf("建立第%d条边(输入格式:顶点1,顶点2)和权值:",k); scanf_s("%c,%c,%d", &ch1,&ch2,&weight); for (p = 0; p < G->n; p++) { if (ch1 == G->vexs[p]) i = p; if (ch2 == G->vexs[p]) j= p; } G->edges[i][j] = weight; } } void CountDegree(MGraph G) { int indegree[MAX] = { 0 }, outdegree[MAX] = { 0 }; int i, j; for (i = 0; i < G.n; i++) for (j = 0; j < G.n; j++) }补全代码,代码功能建立有向带权图,输出有向带权图
时间: 2023-12-09 08:06:14 浏览: 172
的各个顶点的入度和出度。
补全代码如下:
```
void CountDegree(MGraph G) {
int indegree[MAX] = { 0 }, outdegree[MAX] = { 0 };
int i, j;
for (i = 0; i < G.n; i++)
for (j = 0; j < G.n; j++) {
if (G.edges[i][j] != MAXNUM) {
outdegree[i]++;
indegree[j]++;
}
}
printf("各个顶点的出度为:\n");
for (i = 0; i < G.n; i++) {
printf("%c:%d\n", G.vexs[i], outdegree[i]);
}
printf("各个顶点的入度为:\n");
for (i = 0; i < G.n; i++) {
printf("%c:%d\n", G.vexs[i], indegree[i]);
}
}
```
其中,我们首先定义了两个数组分别记录每个顶点的入度和出度,然后遍历邻接矩阵,如果某个顶点 i 到顶点 j 有一条边,那么顶点 i 的出度加 1,顶点 j 的入度加 1。最后输出各个顶点的入度和出度即可。
相关问题
2、实现图的深度优先遍历和广度优先遍历 /*用邻接矩阵法创建有向网的算法如下:*/ //#include "adjmatrix.h" #include<stdio.h> *最多顶点个数*/ #define MAX VERTEX NUM20 *表示极大值,即∞*/ #define INFINITY 32768 #defne True 1 #define False 0 #define Error -1 16896 #define Ok 1
深度优先遍历(DFS):
```c++
#include <stdio.h>
#include <stdlib.h>
#define MAX_VERTEX_NUM 20
typedef char VertexType;
typedef int EdgeType;
typedef struct {
VertexType vexs[MAX_VERTEX_NUM];
EdgeType arcs[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
int vexnum, arcnum;
} MGraph;
bool visited[MAX_VERTEX_NUM];
void CreateGraph(MGraph* G) {
printf("请输入顶点数和边数:");
scanf("%d %d", &(G->vexnum), &(G->arcnum));
printf("请输入顶点:");
for (int i = 0; i < G->vexnum; i++) {
scanf(" %c", &(G->vexs[i]));
}
for (int i = 0; i < G->vexnum; i++) {
for (int j = 0; j < G->vexnum; j++) {
if (i == j) {
G->arcs[i][j] = 0;
} else {
G->arcs[i][j] = INFINITY;
}
}
}
printf("请输入每条边的起点、终点、权值:\n");
for (int k = 0; k < G->arcnum; k++) {
int i, j, w;
scanf("%d %d %d", &i, &j, &w);
G->arcs[i][j] = w;
}
}
void DFS(MGraph* G, int v) {
visited[v] = true;
printf("%c ", G->vexs[v]);
for (int j = 0; j < G->vexnum; j++) {
if (G->arcs[v][j] != INFINITY && !visited[j]) {
DFS(G, j);
}
}
}
void DFSTraverse(MGraph* G) {
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);
}
}
}
int main() {
MGraph G;
CreateGraph(&G);
printf("深度优先遍历结果:");
DFSTraverse(&G);
return 0;
}
```
广度优先遍历(BFS):
```c++
#include <stdio.h>
#include <stdlib.h>
#define MAX_VERTEX_NUM 20
typedef char VertexType;
typedef int EdgeType;
typedef struct {
VertexType vexs[MAX_VERTEX_NUM];
EdgeType arcs[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
int vexnum, arcnum;
} MGraph;
bool visited[MAX_VERTEX_NUM];
void CreateGraph(MGraph* G) {
printf("请输入顶点数和边数:");
scanf("%d %d", &(G->vexnum), &(G->arcnum));
printf("请输入顶点:");
for (int i = 0; i < G->vexnum; i++) {
scanf(" %c", &(G->vexs[i]));
}
for (int i = 0; i < G->vexnum; i++) {
for (int j = 0; j < G->vexnum; j++) {
if (i == j) {
G->arcs[i][j] = 0;
} else {
G->arcs[i][j] = INFINITY;
}
}
}
printf("请输入每条边的起点、终点、权值:\n");
for (int k = 0; k < G->arcnum; k++) {
int i, j, w;
scanf("%d %d %d", &i, &j, &w);
G->arcs[i][j] = w;
}
}
void BFS(MGraph* G, int v) {
int queue[MAX_VERTEX_NUM], front = 0, rear = 0;
printf("%c ", G->vexs[v]);
visited[v] = true;
queue[rear++] = v;
while (front != rear) {
int i = queue[front++];
for (int j = 0; j < G->vexnum; j++) {
if (G->arcs[i][j] != INFINITY && !visited[j]) {
printf("%c ", G->vexs[j]);
visited[j] = true;
queue[rear++] = j;
}
}
}
}
void BFSTraverse(MGraph* G) {
for (int i = 0; i < G->vexnum; i++) {
visited[i] = false;
}
for (int i = 0; i < G->vexnum; i++) {
if (!visited[i]) {
BFS(G, i);
}
}
}
int main() {
MGraph G;
CreateGraph(&G);
printf("广度优先遍历结果:");
BFSTraverse(&G);
return 0;
}
```
#include "stdio.h" #define MAX 100 #define MAXNUM 100000 typedef struct { int n, e, weight; char vexs[MAX]; int edges[MAX][MAX]; }MGraph; void CreateMGraph(MGraph* G) { int i, j, k; int p; char ch1, ch2; int weight; printf("请输入顶点数:"); scanf_s("%d", &G->n); printf("请输入边数:"); scanf_s("%d", &G->e); printf("请输入各顶点信息(每个顶点以回车作为结束):\n"); for (i = 0; i < G->n; i++) { getchar(); printf("输入第%d个顶点:", i + 1); scanf_s("%c", &(G->vexs[i])); } for (i = 0; i < G->n; i++) for (j = 0; j < G->n; j++) G->edges[i][j] = MAXNUM; for (k = 1; k <= G->e; k++) { getchar(); printf("建立第%d条边(输入格式:顶点1,顶点2)和权值:", k); scanf_s("%c,%c,%d", &ch1,&ch2,&weight); for (p = 0; p < G->n; p++) { if (ch1 == G->vexs[p]) i = p; if (ch2 == G->vexs[p]) j= p; } G->edges[i][j] = weight; } } void CountDegree(MGraph G) { int indegree[MAX] = { 0 }, outdegree[MAX] = { 0 }; int i, j; for (i = 0; i < G.n; i++) for (j = 0; j < G.n; j++) { if (G.edges[i][j] != MAXNUM) { outdegree[i]++; indegree[j]++; } } printf("各个顶点的出度为:\n"); for (i = 0; i < G.n; i++) { printf("%c:%d\n", G.vexs[i], outdegree[i]); } printf("各个顶点的入度为:\n"); for (i = 0; i < G.n; i++) { printf("%c:%d\n", G.vexs[i], indegree[i]); } }修复数据结构代码
在输入边信息时,scanf_s函数的格式字符串中应该包含两个逗号,即"%c,%c,%d",并且在输入权值时应该使用"&"符号获取输入的值。同时,在获取顶点信息时应该使用getchar函数将回车键去掉,否则会影响后面的输入。修复后的代码如下:
#include "stdio.h"
#define MAX 100
#define MAXNUM 100000
typedef struct {
int n, e, weight;
char vexs[MAX];
int edges[MAX][MAX];
}MGraph;
void CreateMGraph(MGraph* G) {
int i, j, k;
int p;
char ch1, ch2;
int weight;
printf("请输入顶点数:");
scanf_s("%d", &G->n);
printf("请输入边数:");
scanf_s("%d", &G->e);
printf("请输入各顶点信息(每个顶点以回车作为结束):\n");
getchar();
for (i = 0; i < G->n; i++)
{
printf("输入第%d个顶点:", i + 1);
scanf_s("%c", &(G->vexs[i]));
getchar();
}
for (i = 0; i < G->n; i++)
for (j = 0; j < G->n; j++)
G->edges[i][j] = MAXNUM;
for (k = 1; k <= G->e; k++)
{
printf("建立第%d条边(输入格式:顶点1,顶点2)和权值:", k);
scanf_s("%c,%c,%d", &ch1, 1, &ch2, 1, &weight);
for (p = 0; p < G->n; p++)
{
if (ch1 == G->vexs[p])
i = p;
if (ch2 == G->vexs[p])
j = p;
}
G->edges[i][j] = weight;
}
}
void CountDegree(MGraph G) {
int indegree[MAX] = { 0 }, outdegree[MAX] = { 0 };
int i, j;
for (i = 0; i < G.n; i++)
for (j = 0; j < G.n; j++)
{
if (G.edges[i][j] != MAXNUM)
{
outdegree[i]++;
indegree[j]++;
}
}
printf("各个顶点的出度为:\n");
for (i = 0; i < G.n; i++)
{
printf("%c:%d\n", G.vexs[i], outdegree[i]);
}
printf("各个顶点的入度为:\n");
for (i = 0; i < G.n; i++)
{
printf("%c:%d\n", G.vexs[i], indegree[i]);
}
}
阅读全文