感兴趣的小伙伴可以先看看我的这篇文章哦,打开看看,说不定能帮到你一些~~
7-1 邻接矩阵表示法创建无向图
采用邻接矩阵表示法创建无向图G ,依次输出各顶点的度。
输入格式:
输入第一行中给出2个整数i(0<i≤10),j(j≥0),分别为图G的顶点数和边数。
输入第二行为顶点的信息,每个顶点只能用一个字符表示。
依次输入j行,每行输入一条边依附的顶点。
输出格式:
依次输出各顶点的度,行末没有最后的空格。
输入样例:
5 7
ABCDE
AB
AD
BC
BE
CD
CE
DE
输出样例:
2 3 3 3 3
代码:
#include <stdio.h>
#define MAXVEX 10
#define INFINITY 65535
typedef struct {
char vexs[MAXVEX];
int edges[MAXVEX][MAXVEX];
int n,e;
}MGraph;
void CreateMGraph(MGraph *G) {
int i,j,x,y;
char a,b;
scanf("%d %d",&G->n,&G->e);
getchar();
for (i = 0;i < G->n; i++){
scanf("%c",&G->vexs[i]);
}
for (i = 0;i < G->n; i++){
for (j = 0;j < G->n; j++){
G->edges[i][j] = INFINITY;
}
}
for (i = 0;i < G->e; i++){
getchar();
scanf("%c%c",&a,&b);
for (j = 0;j < G->n; j++){
if (a == G->vexs[j]) x = j;
if (b == G->vexs[j]) y = j;
}
G->edges[x][y] = G->edges[y][x] = 1;
}
}
int main() {
MGraph *G = (MGraph *)malloc(sizeof(MGraph));
int i,j,sum = 0;
CreateMGraph(G);
for (i = 0;i < G->n; i++){
sum = 0;
for (j = 0;j < G->n; j++){
if (G->edges[i][j] == 1)
sum++;
}
if (i != 0) printf(" ");
printf("%d",sum);
}
return 0;
}
7-2 邻接表创建无向图
采用邻接表创建无向图G ,依次输出各顶点的度。
输入格式:
输入第一行中给出2个整数i(0<i≤10),j(j≥0),分别为图G的顶点数和边数。
输入第二行为顶点的信息,每个顶点只能用一个字符表示。
依次输入j行,每行输入一条边依附的顶点。
输出格式:
依次输出各顶点的度,行末没有最后的空格。
输入样例:
5 7
ABCDE
AB
AD
BC
BE
CD
CE
DE
输出样例:
2 3 3 3 3
代码:
#include <stdio.h>
#define MAXVEX 10
typedef struct EdgeNode{
int adjvex;
struct EdgeNode *next;
}EdgeNode;
typedef struct VertexNode{
char data;
EdgeNode *firstedge;
}VertexNode, AdjList[MAXVEX];
typedef struct{
AdjList adjList;
int n,e;
}GraphAdjList;
void CreateALGraph(GraphAdjList *G){
int i,j,x,y;
char a,b;
EdgeNode *e;
scanf("%d %d",&G->n,&G->e);
getchar();
for (i = 0;i < G->n; i++){
scanf("%c",&G->adjList[i].data);
G->adjList[i].firstedge = NULL;
}
for (i = 0;i < G->e; i++){
getchar();
scanf("%c%c",&a,&b);
for (j = 0;j < G->n;j++){
if (a == G->adjList[j].data) x = j;
if (b == G->adjList[j].data) y = j;
}
e = (EdgeNode *)malloc(sizeof(EdgeNode));
e->adjvex = x;
e->next = G->adjList[y].firstedge;
G->adjList[y].firstedge = e;
e = (EdgeNode *)malloc(sizeof(EdgeNode));
e->adjvex = y;
e->next = G->adjList[x].firstedge;
G->adjList[x].firstedge = e;
}
}
int main() {
GraphAdjList *G = (GraphAdjList *)malloc(sizeof(GraphAdjList));
int i,j,sum = 0;
CreateALGraph(G);
EdgeNode *e;
for (i = 0;i < G->n; i++){
sum = 0;
e = G->adjList[i].firstedge;
for (;e != NULL;e = e->next)
sum++;
if (i != 0) printf(" ");
printf("%d",sum);
}
return 0;
}
7-3 列出连通集
给定一个有N个顶点和E条边的无向图,请用DFS和BFS分别列出其所有的连通集。假设顶点从0到N−1编号。进行搜索时,假设我们总是从编号最小的顶点出发,按编号递增的顺序访问邻接点。
输入格式:
输入第1行给出2个整数N(0<N≤10)和E,分别是图的顶点数和边数。随后E行,每行给出一条边的两个端点。每行中的数字之间用1空格分隔。
输出格式:
按照"{ v1 v2… vk}"的格式,每行输出一个连通集。先输出DFS的结果,再输出BFS的结果。
输入样例:
8 6
0 7
0 1
2 0
4 1
2 4
3 5
输出样例:
{ 0 1 4 2 7 }
{ 3 5 }
{ 6 }
{ 0 1 2 7 4 }
{ 3 5 }
{ 6 }
代码:
#include <stdio.h>
#define MAXVEX 10
#define INFINITY 65535
#define MAXSIZE 10
int visited[MAXVEX];
typedef struct{
int data[MAXSIZE];
int front,rear;
}Queue;
typedef struct{
int vexs[MAXVEX];
int edges[MAXVEX][MAXVEX];
int n,e;
}MGraph;
void InitQueue(Queue *Q){
Q->front = 0;
Q->rear = 0;
}
void EnQueue(Queue *Q,int e){
if ((Q->rear + 1) % MAXSIZE == Q->front) return ;
Q->data[++Q->rear] = e;
}
void DeQueue(Queue *Q,int *e){
if (Q->front == Q->rear) return ;
*e = Q->data[++Q->front];
}
int QueueEmpty(Queue Q){
if (Q.front == Q.rear) return 1;
else return 0;
}
void CreateMGraph(MGraph *G){
int i,j,k;
scanf("%d %d",&G->n,&G->e);
for (i = 0;i < G->n; i++){
G->vexs[i] = i;
for (j = 0;j < G->n; j++)
G->edges[i][j] = INFINITY;
}
for (k = 0;k < G->e;k++){
scanf("%d %d",&i,&j);
G->edges[i][j] = 1;
G->edges[j][i] = 1;
}
}
void DFS(MGraph G,int i){
int j;
visited[i] = 1;
printf("%d ",G.vexs[i]);
for (j = 0;j < G.n; j++)
if (G.edges[i][j] == 1 && !visited[j])
DFS(G,j);
}
void DFSTraverse(MGraph G){
int i;
for (i = 0;i < G.n; i++)
visited[i] = 0;
for (i = 0;i < G.n; i++)
if (!visited[i]){
printf("{ ");
DFS(G,i);
printf("}\n");
}
}
void BFSTraverse(MGraph G){
int i,j;
int temp_i;
Queue Q;
for (i = 0;i < G.n; i++)
visited[i] = 0;
InitQueue(&Q);
for (i = 0;i < G.n; i++){
temp_i = i;
if (!visited[i]){
visited[i] = 1;
printf("{ ");
printf("%d ",G.vexs[i]);
EnQueue(&Q,i);
while (!QueueEmpty(Q)){
DeQueue(&Q,&i);
for (j = 0;j < G.n; j++){
if (G.edges[i][j] == 1 && !visited[j]){
visited[j] = 1;
printf("%d ",G.vexs[j]);
EnQueue(&Q,j);
}
}
}
printf("}\n");
}
i = temp_i;
}
}
int main(){
MGraph G;
CreateMGraph(&G);
DFSTraverse(G);
BFSTraverse(G);
return 0;
}