Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

第七次作业广度优先遍历最短路径 #143

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
221 changes: 221 additions & 0 deletions 2017-1/zjc/第七次作业/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
#include<stdio.h>
#include<stdlib.h>
#include <malloc.h>

#define INFINITY INT_MAX //���ֵΪ����
#define MAX_VERTEX_NUM 20 //��󶥵���

typedef enum {
DG,DN,UGD,UDN
}GraphKind;//{����ͼ��������������ͼ��������}

typedef int VRType, InfoType;

typedef struct ArcCell {
VRType adj;//VRType�Ƕ����ϵ���ͣ�����Ȩͼ����1��0
//��ʾ���ڷ񣻶���Ȩͼ����ΪȨֵ����
InfoType *info;//�û������Ϣ��ָ��
}ArcCell,AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];

typedef struct {
AdjMatrix arcs; // �ڽӾ���
int vexnum, arcnum; // ͼ�ĵ�ǰ�������ͻ���
}Graph;

typedef enum {
OK,
ERROR,
OVERFLOW
}Status;

Status CreateGraph(Graph *G) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

虽然就题论题你这里这样写是能得到正确答案的,但建议以后在实际应用编程时,尽量把这种参数配置类的工作交给调用者(函数)在调用函数时执行传参操作,代码的封装可复用就会一点点积累起来了。

//�����ڽӾ��󷨱�ʾ����������֪��ͼ
G->vexnum = 9; //��ͼ�Ķ�����
G->arcnum = 12; //��ͼ�Ļ���
for (int i = 0; i < G->vexnum; i++) {
for (int j = 0; j < G->vexnum; j++) {
G->arcs[i][j].adj = INFINITY; //��ʼ���ڽӾ���,INFINITY��ʾ������
}
}//��ʼ��ͼ
//���ڵĶ��㸳ֵ
Add(G, 0, 1); Add(G, 0, 2); Add(G, 0, 3); Add(G, 0, 6);
Add(G, 1, 2); Add(G, 3, 4); Add(G, 3, 5); Add(G, 4, 5);
Add(G, 5, 7); Add(G, 6, 7); Add(G, 6, 8); Add(G, 7, 8);
//��ʾ0-1,0-2.��������������
return OK;
}

Status Add(Graph*G, int x, int y) {
//�����ڽӾ������ڸ�ֵΪ1
if (x >= MAX_VERTEX_NUM || y >= MAX_VERTEX_NUM) {
return ERROR;//��������ͼ�ķ�Χ���ͷ���ERROR
}
G->arcs[x][y].adj = G->arcs[y][x].adj = 1;//����ͼ�����ڽӶ�Ϊ1���ҵ���������ʱ��������ֵ
return OK;
}
/*-------------ͼ������------------------*/

typedef int ElemType;

#define MAXQSIZE 100 //��������

typedef struct QNode {
ElemType data;
struct QNode *priou;//�ڵ����һ���ڵ�
struct QNode *next;//�ڵ����һ���ڵ�
}QNode, LinkList, *QueuePtr;

typedef struct {
QueuePtr front;//ǰ���
QueuePtr rear;//�����
}LinkQueue;

Status InitQueue(LinkQueue *Q) {
//���еij�ʼ��
Q->front = Q->rear = (QueuePtr)malloc(MAXQSIZE * sizeof(QNode));
if (!(Q->front)) {
return ERROR;
}
Q->front->next = Q->rear->next = NULL;//ʹ����Ϊ��
return OK;
}

Status EnQueue(LinkQueue *Q, ElemType e) {
//����еIJ���
QueuePtr p;
p = (QueuePtr)malloc(sizeof(QNode));
if (!p) {
return ERROR;
}
p->data = e;//���浽�����������
p->next = NULL;//��һ���ڵ�Ϊ��
p->priou = Q->front;
Q->rear->next = p;
Q->rear = p;
return OK;
}

Status DeQueue(LinkQueue *Q, ElemType *e) {
//�������
if (QueueEmpty(*Q)) {
//����Ϊ��ʱֹͣ����������ERROR
return ERROR;
}
Q->front = Q->front->next;//ͷ���ָ��������һ���ڵ�
*e = Q->front->data;
return OK;
}

typedef enum {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

所有代码都在一个文件里已经比较拥挤了,建议把所有的数据结构定义都前置在源代码文件的最开始处,起码不会像现在这样有点凌乱

false,
true
}bool;

bool QueueEmpty(LinkQueue Q) {
//�ж����Dz���һ���ն���
if (Q.front == Q.rear) {
return true;//ͷ���ͺ�һ���ڵ�Ϊͬһ������ô�������Ϊ��
}
return false;//������в�Ϊ��
}

int FirstAdjVex(Graph G, int i) {
//���ص�һ���ڽӶ��㣬û�еĻ�����-1
for (int k = 0; k < G.vexnum; k++) {
if (G.arcs[i][k].adj == 1) {
return k;//������ڽӽڵ㣬��������ڽӵĽڵ�
}
}
return -1;
}

int NextAdjVex(Graph G, int i, int j) {
//������һ���ڽӶ��㣬û�еĻ�����-1
for (int k = j + 1; k < G.vexnum; k++) {
if (G.arcs[i][k].adj == 1) {
return k;
}
}
return -1;
}
/*----------------���еIJ���------------*/
#define FALSE 0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

前面刚刚定义过bool型枚举变量,这里没必要再来用宏重复造轮子了。

#define TRUE 1

int visited[MAX_VERTEX_NUM];

void BFSTraverse(Graph G, int a, int b) {
//��������ȵķǵݹ����ͼG��ʹ�ø�������Q�ͷ��ʱ�־����visited.
LinkQueue Q; //��������
for (int v = 0; v < G.vexnum; ++v) {
visited[v] = FALSE; //��ʼ�����ʱ�־
}
InitQueue(&Q); // �ÿյĸ�������Q
EnQueue(&Q, a); //�������
visited[a] = TRUE;
bool flag = false;
int u, w;
while (!QueueEmpty(Q)) //���в�Ϊ��ʱִ��
{
DeQueue(&Q, &u);//�������
for (w = FirstAdjVex(G, u); w >= 0; w = NextAdjVex(G, u, w))
{
//���ν����ڶ�������
if (w == b) {
EnQueue(&Q, w); //������
PrintFoot(Q, a); //���·��
flag = true;
break;
}//if
if (!visited[w]) {
//wΪu��δ���ʵ��ڽӶ���
EnQueue(&Q, w); //�����
visited[w] = true;
}//if
}//for
if (flag) {
break;
}//if
}//while
}//BFSTraverse
/*---------ͼ�ı���--------------*/

Status PrintFoot(LinkQueue Q, int start) {
//������·��
int foot[MAX_VERTEX_NUM];///��󶥵���
int i;
QueuePtr p;//����һ������
p = Q.rear;//pָ���β�ڵ�
for (i = 0; i < MAX_VERTEX_NUM; i++) {
foot[i] = -1;
}//���еij�ʼ��
foot[0] = p->data;
p = p->priou;
for (i = 1; p->data != start; i++) {
foot[i] = p->data;
p = p->priou;
}
foot[i] = p->data;
for (; i >= 0; i--) {
//�������
if (foot[i] >= 0) {
printf("%d ", foot[i] + 1);
}
}
}

int main()
{
Graph G;
CreateGraph(&G);//����һ��ͼͼ�����ҳ�ʼ����1
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (j != i) {
printf("%d<->%d:", i + 1, j + 1);
BFSTraverse(G, i, j);//��ȱ���
printf("\n");
}//if
}//for j
}//for i
return 0;
}