13. 2023-期末复习-图的遍历-广度优先搜索遍历连通图-邻接矩阵法
#include "stdio.h"
#include "stdlib.h"
#include "math.h"
#include "time.h"
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef int Status;
typedef int Boolean;
typedef char VertexType;
typedef int EdgeType;
#define MAXSIZE 9
#define MAXEDGE 15
#define MAXVEX 9
typedef struct
{
VertexType vexs[MAXVEX];
EdgeType arc[MAXVEX][MAXVEX];
int numVertexes, numEdges;
}MGraph;
typedef struct
{
int data[MAXSIZE];
int front;
int rear;
}Queue;
Status InitQueue(Queue* Q)
{
Q->front = 0;
Q->rear = 0;
return OK;
}
Status QueueEmpty(Queue Q)
{
if (Q.front == Q.rear)
return TRUE;
else
return FALSE;
}
Status EnQueue(Queue* Q, char e)
{
if ((Q->rear + 1) % MAXSIZE == Q->front)
return ERROR;
Q->data[Q->rear] = e;
Q->rear = (Q->rear + 1) % MAXSIZE;
return OK;
}
Status DeQueue(Queue* Q, char* e)
{
if (Q->front == Q->rear)
return ERROR;
*e = Q->data[Q->front];
Q->front = (Q->front + 1) % MAXSIZE;
return OK;
}
void CreateMGraph(MGraph* G)
{
int i, j;
G->numEdges = 15;
G->numVertexes = 9;
G->vexs[0] = 'A';
G->vexs[1] = 'B';
G->vexs[2] = 'C';
G->vexs[3] = 'D';
G->vexs[4] = 'E';
G->vexs[5] = 'F';
G->vexs[6] = 'G';
G->vexs[7] = 'H';
G->vexs[8] = 'I';
for (i = 0; i < G->numVertexes; i++)
{
for (j = 0; j < G->numVertexes; j++)
{
G->arc[i][j] = 0;
}
}
G->arc[0][1] = 1;
G->arc[0][5] = 1;
G->arc[1][2] = 1;
G->arc[1][8] = 1;
G->arc[1][6] = 1;
G->arc[2][3] = 1;
G->arc[2][8] = 1;
G->arc[3][4] = 1;
G->arc[3][7] = 1;
G->arc[3][6] = 1;
G->arc[3][8] = 1;
G->arc[4][5] = 1;
G->arc[4][7] = 1;
G->arc[5][6] = 1;
G->arc[6][7] = 1;
for (i = 0; i < G->numVertexes; i++)
{
for (j = i; j < G->numVertexes; j++)
{
G->arc[j][i] = G->arc[i][j];
}
}
}
void BFSTraverse(MGraph G)
{
int i, j;
Queue Q;
InitQueue(&Q);
int visit[MAXVEX] ;
for(i = 0 ;i < MAXVEX; i ++)
{
visit[i] = FALSE;
}
for(i = 0; i < G.numVertexes ;i ++)
{
if(!visit[i])
{
printf("%c ",G.vexs[i]);
visit[i] = TRUE;
EnQueue(&Q, i);
while(!QueueEmpty(Q))
{
char j;
DeQueue(&Q, &j);
int k = 0;
for(; k < G.numVertexes; k ++)
{
if(G.arc[j][k] != 0 && visit[k] != TRUE)
{
printf("%c ", G.vexs[k]);
visit[k] = TRUE;
EnQueue(&Q, k);
}
}
}
}
}
}
int main(void)
{
MGraph G;
CreateMGraph(&G);
printf("广度遍历:");
BFSTraverse(G);
printf("\n");
return 0;
}
14. 2023级-04班-期末复习题-图的遍历-深度优先遍历连通图-邻接矩阵法
#include "stdio.h"
#include "stdlib.h"
#include "math.h"
#include "time.h"
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef int Status;
typedef int Boolean;
typedef char VertexType;
typedef int EdgeType;
#define MAXSIZE 9
#define MAXEDGE 15
#define MAXVEX 9
typedef struct
{
VertexType vexs[MAXVEX];
EdgeType arc[MAXVEX][MAXVEX];
int numVertexes, numEdges;
}MGraph;
void CreateMGraph(MGraph* G)
{
int i, j;
G->numEdges = 15;
G->numVertexes = 9;
G->vexs[0] = 'A';
G->vexs[1] = 'B';
G->vexs[2] = 'C';
G->vexs[3] = 'D';
G->vexs[4] = 'E';
G->vexs[5] = 'F';
G->vexs[6] = 'G';
G->vexs[7] = 'H';
G->vexs[8] = 'I';
for (i = 0; i < G->numVertexes; i++)
{
for (j = 0; j < G->numVertexes; j++)
{
G->arc[i][j] = 0;
}
}
G->arc[0][1] = 1;
G->arc[0][5] = 1;
G->arc[1][2] = 1;
G->arc[1][8] = 1;
G->arc[1][6] = 1;
G->arc[2][3] = 1;
G->arc[2][8] = 1;
G->arc[3][4] = 1;
G->arc[3][7] = 1;
G->arc[3][6] = 1;
G->arc[3][8] = 1;
G->arc[4][5] = 1;
G->arc[4][7] = 1;
G->arc[5][6] = 1;
G->arc[6][7] = 1;
for (i = 0; i < G->numVertexes; i++)
{
for (j = i; j < G->numVertexes; j++)
{
G->arc[j][i] = G->arc[i][j];
}
}
}
void DFSTraverse(MGraph G)
{
int i = 0;
int visit[G.numVertexes + 1];
for(i = 0; i < G.numVertexes; i++)
{
visit[i] = FALSE;
}
int stack[MAXVEX];
int top = -1;
for(i = 0 ;i < G.numVertexes ;i ++)
{
if(visit[i] == FALSE)
{
stack[++top] = i;
while(top != -1)
{
int temp = stack[top--];
if(visit[temp] == FALSE)
{
printf("%c ",G.vexs[temp]);
visit[temp] = TRUE;
int k = 0;
for(k = G.numVertexes - 1; k >= 0 ; k --)
{
if(G.arc[temp][k] != 0 && visit[k] == FALSE)
{
stack[++top] = k;
}
}
}
}
}
}
}
int main(void)
{
MGraph G;
CreateMGraph(&G);
printf("深度遍历:");
DFSTraverse(G);
printf("\n");
return 0;
}
15. 2023-期末复习-图的广度优先搜索-邻接表法
#include "stdio.h"
#include "stdlib.h"
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define MAXSIZE 8
#define MAXEDGE 9
#define MAXVEX 8
typedef int Status;
typedef struct EdgeNode
{
int adjvex;
struct EdgeNode *next;
}EdgeNode;
typedef struct VertexNode
{
char data;
EdgeNode *firstedge;
}VertexNode;
typedef struct
{
VertexNode adjList[MAXVEX];
int numVertexes,numEdges;
}GraphAdjList;
void CreateALGraph(GraphAdjList *p)
{
int k;
int i;
int j;
GraphAdjList *Gp=(GraphAdjList *)malloc(sizeof(GraphAdjList));
EdgeNode *pe;
printf("请输入顶点数和边数:\n");
scanf("%d %d",&(Gp->numVertexes),&(Gp->numEdges));
printf("请输入顶点数的数据:\n");
getchar();
for (k = 0; k < Gp->numVertexes; k++)
{
scanf("%c",&(Gp->adjList[k].data));
getchar();
Gp->adjList[k].firstedge = NULL;
}
printf("请输入边的数据:\n");
for (k = 0; k < Gp->numEdges; k++)
{
scanf("%d %d",&i,&j);
getchar();
pe = (EdgeNode *)malloc(sizeof(EdgeNode));
pe->adjvex = j;
pe->next = Gp->adjList[i].firstedge;
Gp->adjList[i].firstedge = pe;
pe = (EdgeNode *)malloc(sizeof(EdgeNode));
pe->adjvex = i;
pe->next = Gp->adjList[j].firstedge;
Gp->adjList[j].firstedge = pe;
}
*p=*Gp;
}
typedef struct
{
int data[MAXSIZE];
int front;
int rear;
}Queue;
Status InitQueue(Queue* Q)
{
Q->front = 0;
Q->rear = 0;
return OK;
}
Status QueueEmpty(Queue Q)
{
if (Q.front == Q.rear)
return TRUE;
else
return FALSE;
}
Status EnQueue(Queue* Q, char e)
{
if ((Q->rear + 1) % MAXSIZE == Q->front)
return ERROR;
Q->data[Q->rear] = e;
Q->rear = (Q->rear + 1) % MAXSIZE;
return OK;
}
Status DeQueue(Queue* Q, int* e)
{
if (Q->front == Q->rear)
return ERROR;
*e = Q->data[Q->front];
Q->front = (Q->front + 1) % MAXSIZE;
return OK;
}
void BFSTraverse(GraphAdjList G)
{
int i , j ;
int visit[MAXVEX] ;
for(i = 0; i < MAXVEX; i++)
{
visit[i] = FALSE;
}
Queue Q;
InitQueue(&Q);
for(i = 0; i < G.numVertexes; i ++)
{
if(visit[i] == FALSE)
{
printf("%c ",G.adjList[j].data);
visit[j] = TRUE;
EnQueue(&Q, i);
while(!QueueEmpty(Q))
{
int j;
DeQueue(&Q, &j);
EdgeNode *p = G.adjList[j].firstedge;
while (p)
{
if (!visit[p->adjvex])
{
visit[p->adjvex] = TRUE;
printf("%c ", G.adjList[p->adjvex].data);
EnQueue(&Q, p->adjvex);
}
p = p->next;
}
}
}
}
}
int main(void)
{
GraphAdjList p;
CreateALGraph(&p);
BFSTraverse(p);
printf("\n");
return 0;
}
16. 2023级-04班-期末复习题-图的深度优先搜索-邻接表法
#include "stdio.h"
#include "stdlib.h"
typedef struct EdgeNode
{
int adjvex;
struct EdgeNode *next;
}EdgeNode;
typedef struct VertexNode
{
char data;
EdgeNode *firstedge;
}VertexNode;
typedef struct
{
VertexNode adjList[4];
int numVertexes,numEdges;
}GraphAdjList;
void CreateALGraph(GraphAdjList *p)
{
int k;
int i;
int j;
GraphAdjList *Gp=(GraphAdjList *)malloc(sizeof(GraphAdjList));
EdgeNode *pe;
printf("请输入顶点数和边数:\n");
scanf("%d %d",&(Gp->numVertexes),&(Gp->numEdges));
printf("请输入顶点数的数据:\n");
getchar();
for (k = 0; k < Gp->numVertexes; k++)
{
scanf("%c",&(Gp->adjList[k].data));
getchar();
Gp->adjList[k].firstedge = NULL;
}
printf("请输入边的数据:\n");
for (k = 0; k < Gp->numEdges; k++)
{
scanf("%d %d",&i,&j);
getchar();
pe = (EdgeNode *)malloc(sizeof(EdgeNode));
pe->adjvex = j;
pe->next = Gp->adjList[i].firstedge;
Gp->adjList[i].firstedge = pe;
pe = (EdgeNode *)malloc(sizeof(EdgeNode));
pe->adjvex = i;
pe->next = Gp->adjList[j].firstedge;
Gp->adjList[j].firstedge = pe;
}
*p=*Gp;
}
void DFSTraverse(GraphAdjList G)
{
int stack[G.numVertexes];
int top = -1;
int visit[G.numVertexes];
int i;
for(i = 0 ;i <G.numVertexes ;i ++)
{
visit[i] = 0;
}
for(i = 0; i < G.numVertexes ;i ++)
{
if(visit[i] == 0){
printf("%c ", G.adjList[i].data);
visit[i] = 1;
stack[++top] = i;
while(top != -1){
int u = stack[top --];
EdgeNode *p = G.adjList[u].firstedge;
if(visit[p->adjvex] == 0){
printf("%c ", G.adjList[p->adjvex].data);
visit[p->adjvex] = 1;
stack[++top] = p->adjvex;
}
}
}
}
}
int main(void)
{
GraphAdjList p;
CreateALGraph(&p);
DFSTraverse(p);
printf("\n");
return 0;
}
17. 2023-期末复习-图的连通性判断和连通分量计算
#include "stdio.h"
#include "stdlib.h"
#include "math.h"
#include "time.h"
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef int Status;
typedef int Boolean;
typedef char VertexType;
typedef int EdgeType;
#define MAXSIZE 9
#define MAXEDGE 4
#define MAXVEX 6
typedef struct
{
VertexType vexs[MAXVEX];
EdgeType arc[MAXVEX][MAXVEX];
int numVertexes, numEdges;
}MGraph;
int count =0;
void CreateMGraph(MGraph* G)
{
int i, j;
G->numEdges = 4;
G->numVertexes = 6;
G->vexs[0] = 'A';
G->vexs[1] = 'B';
G->vexs[2] = 'C';
G->vexs[3] = 'D';
G->vexs[4] = 'E';
G->vexs[5] = 'F';
for (i = 0; i < G->numVertexes; i++)
{
for (j = 0; j < G->numVertexes; j++)
{
G->arc[i][j] = 0;
}
}
G->arc[0][1] = 1;
G->arc[0][5] = 1;
G->arc[1][2] = 1;
G->arc[3][4] = 1;
for (i = 0; i < G->numVertexes; i++)
{
for (j = i; j < G->numVertexes; j++)
{
G->arc[j][i] = G->arc[i][j];
}
}
}
int DFSTraverse(MGraph G)
{
int i;
int stack[G.numVertexes];
int top = -1;
int visit[G.numVertexes];
for(i = 0;i < G.numVertexes ;i ++)
{
visit[i] = 0;
}
for(i = 0;i < G.numVertexes;i ++)
{
if(visit[i] == 0)
{
visit[i] = 1;
stack[++top] = i;
while(top != -1){
int u = stack[top --];
int k = 0;
for(k = 0;k < G.numVertexes;k ++)
{
if(G.arc[u][k] != 0 && visit[k] == 0)
{
visit[k] = 1;
stack[++top] = k;
}
}
}
count++;
}
}
return count > 1 ? 0 : 1 ;
}
int main(void)
{
MGraph G;
int connect;
CreateMGraph(&G);
connect=DFSTraverse(G);
if(connect)
{
printf("该无向图全连通:\n");
}
else
{
printf("该无向图不是全连通,其连通分量个数为:%d\n",count);
}
return 0;
}
18. 2023级-04班-期末复习题-判断图的顶点连通性
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#define TRUE 1
int visited[4];
typedef struct EdgeNode
{
int adjvex;
struct EdgeNode *next;
}EdgeNode;
typedef struct VertexNode
{
char data;
EdgeNode *firstedge;
}VertexNode;
typedef struct
{
VertexNode adjList[4];
int numVertexes,numEdges;
}GraphAdjList;
void CreateALGraph(GraphAdjList *p)
{
int k;
int i;
int j;
GraphAdjList *Gp=(GraphAdjList *)malloc(sizeof(GraphAdjList));
EdgeNode *pe;
printf("请输入顶点数和边数:\n");
scanf("%d %d",&(Gp->numVertexes),&(Gp->numEdges));
printf("请输入顶点数的数据:\n");
getchar();
for (k = 0; k < Gp->numVertexes; k++)
{
scanf("%c",&(Gp->adjList[k].data));
getchar();
Gp->adjList[k].firstedge = NULL;
visited[k]=0;
}
printf("请输入边的数据:\n");
for (k = 0; k < Gp->numEdges; k++)
{
scanf("%d %d",&i,&j);
getchar();
pe = (EdgeNode *)malloc(sizeof(EdgeNode));
pe->adjvex = j;
pe->next = Gp->adjList[i].firstedge;
Gp->adjList[i].firstedge = pe;
pe = (EdgeNode *)malloc(sizeof(EdgeNode));
pe->adjvex = i;
pe->next = Gp->adjList[j].firstedge;
Gp->adjList[j].firstedge = pe;
}
*p=*Gp;
}
int LocateVex(GraphAdjList G,char u)
{
int i;
for(i=0;i < G.numVertexes;i ++)
{
if(G.adjList[i].data == u) return i;
}
return -1;
}
int NextAdjVex(GraphAdjList G,int v,int w)
{
EdgeNode *p;
p=G.adjList[v].firstedge;
while(p&&p->adjvex!=w)
{
p=p->next;
}
if(!p||!p->next)
return -1;
else
return p->next->adjvex;
}
void isConnected(GraphAdjList G, int v1, int v2, int *flag)
{
if(v1 == v2)
{
*flag = TRUE;
return ;
}
EdgeNode *p;
p=G.adjList[v1].firstedge;
while(p)
{
int res = NextAdjVex(G, p->adjvex, v2);
if(res == -1)
p = p->next;
else
{
*flag = 1;
return;
}
}
}
int main(void)
{
GraphAdjList p;
char u1;
char u2;
int v1;
int v2;
int flag=0;
CreateALGraph(&p);
printf("输入两个顶点:\n");
scanf("%c %c",&u1,&u2);
v1=LocateVex(p,u1);
v2=LocateVex(p,u2);
isConnected(p,v1,v2,&flag);
if(flag)
{
printf("顶点相通\n");
}
else
{
printf("顶点不相通\n");
}
return 0;
}
19. 2023级-04班-期末复习题-图的回路判断
/* PRESET CODE BEGIN - NEVER TOUCH CODE BELOW */
#include "stdio.h"
#include "stdlib.h"
#include "math.h"
#include "time.h"
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef int Status; /* Status是函数的类型,其值是函数结果状态代码,如OK等 */
typedef char VertexType; /* 顶点类型 */
typedef int EdgeType; /* 边上的权值 */
#define MAXSIZE 9 /* 存储空间初始分配量 */
#define MAXEDGE 6
#define MAXVEX 6
int visited[MAXVEX]; /* 访问标志的数组 */
int parent[MAXVEX]; /*父链接结点*/
typedef struct
{
VertexType vexs[MAXVEX]; /* 顶点表 */
EdgeType arc[MAXVEX][MAXVEX];/* 邻接矩阵,可看作边表 */
int numVertexes, numEdges; /* 图中当前的顶点数和边数 */
}MGraph;
int count =0;//连通分量的个数,全局变量
void CreateMGraph(MGraph* G)
{
int i, j;
G->numEdges = MAXEDGE;
G->numVertexes = MAXVEX;
/* 读入顶点信息,建立顶点表 */
G->vexs[0] = 'A';
G->vexs[1] = 'B';
G->vexs[2] = 'C';
G->vexs[3] = 'D';
G->vexs[4] = 'E';
G->vexs[5] = 'F';
for (i = 0; i < G->numVertexes; i++)/* 初始化图 */
{
for (j = 0; j < G->numVertexes; j++)
{
G->arc[i][j] = 0;
}
}
G->arc[0][1] = 1;
G->arc[0][4] = 1;
G->arc[0][5] = 1;
G->arc[1][2] = 1;
G->arc[2][3] = 1;
G->arc[3][4] = 1;
for (i = 0; i < G->numVertexes; i++)
{
for (j = i; j < G->numVertexes; j++)
{
G->arc[j][i] = G->arc[i][j];
}
}
}
int LocateVex(MGraph G, VertexType e)
{
// 初始条件:图G存在,u和G中顶点有相同特征
// 操作结果:若G中存在顶点u,则返回该顶点在图中位置;否则返回-1
//补充函数
int i = 0;
for(;i < G.numVertexes;i ++)
{
if(G.vexs[i] == e) return i;
}
return -1;
}
Status FindCycle(MGraph G, int v0) {
int i, u, v;
for (i = 0; i < G.numVertexes; i++)
{
visited[i] = 0; // Initialize all vertices as not visited
parent[i] = -1; // Initialize parent array
}
for (u = 0; u < G.numVertexes; u++)
{
if (!visited[u])
{ // Don't recur for already visited vertices
int stack[MAXVEX], top = -1;
stack[++top] = u; // Push the current source node
while (top != -1)
{
v = stack[top--]; // Pop a vertex from stack
visited[v] = 1; // Mark the node as visited
for (i = 0; i < G.numVertexes; i++)
{
if (G.arc[v][i])
{ // If there is an edge
if (!visited[i])
{ // If the adjacent node is not visited
stack[++top] = i; // Push the adjacent node onto the stack
parent[i] = v; // Mark the parent of the adjacent node
}
else if (i != parent[v])
{ // If an adjacent is visited and not parent of current vertex
return TRUE; // Cycle detected
}
}
}
}
}
}
return FALSE; // If no cycle is detected
}
/* PRESET CODE BEGIN - NEVER TOUCH CODE BELOW */
int main(void)
{
MGraph G;
int flag=0;
int i;
CreateMGraph(&G);
for(i=0;i< MAXVEX ;i++)
{
visited[i]=0;
parent[i]=-1;
}
flag=FindCycle(G,0);
if(!flag)
{
printf("图没有回路\n");
}
else
{
printf("图有回路\n");
}
return 0;
}
/* PRESET CODE END - NEVER TOUCH CODE ABOVE */