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

提交图的最短路径 #162

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
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
207 changes: 207 additions & 0 deletions 2017-1/Micylt/hw7/Graph.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
//��غ�����ʵ��
#include"Graph.h"
Status InitQueue(LinkQueue q) {
q.front = q.rear = (QueuePtr)malloc(sizeof(QNode));
if (!q.front) {
exit(OVERFLOW);
}
q.front->next = q.rear->next = NULL;
return OK;
}

Status EnQueue(LinkQueue *q, QElemType e) {
QueuePtr p;
p = (QueuePtr)malloc(sizeof(QNode));
if (!p) {
return OVERFLOW;
}
p->data = e;
p->next = NULL;
p->prious = q->front;
q->rear->next = p;
q->rear = p;
return OK;
}

BOOL QueueEmpty(LinkQueue q) {
if (q.front == q.rear) {
return TRUE;
}
else {
return FALSE;
}
}

Status DeQueue(LinkQueue *q, QElemType *e) {
if (QueueEmpty(*q)) {
return ERROR;
}
q->front = q->front->next;
*e = q->front->data;
return OK;
}

Status TraverseQueue(LinkQueue q) {
QueuePtr p;
printf("��ʼ����:\n");
if (QueueEmpty(q)) {
printf("��ʱ�����ﲻ����Ԫ��\n");
return OK;
}
p = q.front;
while (p != q.rear) {
printf("%d ", p->data);
p = p->next;
}
printf("\n");
return OK;
}



Graph CreatGraph(int size) {
Graph temp;
int i;
int j;
temp.size = size;
temp.matrix = (BOOL*)malloc(size * sizeof(BOOL*));
for (i = 0; i < size; i++)
{
temp.matrix[i] = (BOOL*)malloc(size * sizeof(BOOL));
for (j = 0; j < size; j++)
{
temp.matrix[i][j] = 0;
}
}
return temp;
}

Status InitTestGraph(Graph* G) //��ʼ��ͼ
{
G->matrix[0][1] = 1;
Copy link
Collaborator

Choose a reason for hiding this comment

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

  • 一个循环就能搞定,这样写多不好看啊

G->matrix[1][0] = 1;
G->matrix[0][2] = 1;
G->matrix[2][0] = 1;
G->matrix[0][3] = 1;
G->matrix[3][0] = 1;
G->matrix[0][6] = 1;
G->matrix[6][0] = 1;
G->matrix[1][2] = 1;
G->matrix[2][1] = 1;
G->matrix[3][4] = 1;
G->matrix[4][3] = 1;
G->matrix[3][5] = 1;
G->matrix[5][3] = 1;
G->matrix[4][5] = 1;
G->matrix[5][4] = 1;
G->matrix[5][7] = 1;
G->matrix[7][5] = 1;
G->matrix[6][7] = 1;
G->matrix[7][6] = 1;
G->matrix[6][8] = 1;
G->matrix[8][6] = 1;
G->matrix[7][8] = 1;
G->matrix[8][7] = 1;
return OK;
}

Status TraverseGraph(Graph a) {
int size = a.size;
int i;
int j;
for (i = 0; i <= size; i++) {
printf("%2d", i);
}
printf("\n");
for (j = 0; j < size; j++) {
printf("%2d", j + 1);
for (i = 0; i < size; i++) {
printf("%2d", a.matrix[j][i]);
}
printf("\n");
}
return OK;
}

int FirstAdjVex(Graph p, int i) {
int a;
for (a = 0; a < p.size; a++) {
if (p.matrix[i][a] == TRUE) {
return a;
}
}
return -1;
}

int NextVex(Graph g, int i, int j) {
int a;
for (a = j + 1; a < g.size; a++) {
if (g.matrix[i][a] == TRUE) {
return a;
}
}
return -1;
}

Status TraversePath(LinkQueue Q, int start) {
int a[MAX] = { -1 };
QueuePtr p;
int i = 1;

p = Q.rear;
a[0] = p->data + 1;
p = p->prious;

while (p->data != start) {
a[i] = p->data + 1;
p = p->prious;
i++;
}
a[i] = start + 1;
for (; i >= 0; i--) {
if (a[i] >= 0) {
printf("%d ", a[i]);
}
}
return OK;
}

Status BFSTraverse(Graph *g, int start, int end) {
LinkQueue a;
QElemType e;
BOOL* visited = (BOOL*)malloc(g->size * sizeof(BOOL));
int i;
int w;
int flag = 1;

a.front = a.rear = (QueuePtr)malloc(sizeof(QNode));
InitQueue(a);

for (i = 0; i < g->size; i++) {
visited[i] = FALSE;
}
visited[start] =TRUE;
EnQueue(&a, start);

while (!QueueEmpty(a)) {
DeQueue(&a, &e);
for (w = FirstAdjVex(*g, e); w != -1; w = NextVex(*g, e, w)) {
if (w == end) {
EnQueue(&a, w);
flag = 0;
break;
}
if (!visited[w]) {
EnQueue(&a, w);
visited[w] = TRUE;
}
}
if (flag == 0) {
TraversePath(a, start);
printf("\n");
break;
}
}
return OK;
}

58 changes: 58 additions & 0 deletions 2017-1/Micylt/hw7/Graph.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//������еĽṹ�Լ���غ���
typedef int QElemType;

typedef enum

{
FALSE,
TRUE,
}BOOL;

typedef enum
{
OK,
OVERFLOW,
ERROR,
} Status;

typedef struct QNode
{
QElemType data;
struct QNode *next;
struct QNode *prious;
}QNode, *QueuePtr;
typedef struct LinkQueue
{
QueuePtr front;
QueuePtr rear;
}LinkQueue;

/*���ڶ��е���ز���*/
Status InitQueue(LinkQueue q);//��ʼ������
Status EnQueue(LinkQueue *q, QElemType e);//����һ�����
BOOL QueueEmpty(LinkQueue q);//�������Ƿ�Ϊ��
Status DeQueue(LinkQueue *q, QElemType *e);//ɾ��һ�����
Status TraverseQueue(LinkQueue q);//��������

/*-------------------------------------------------------------------------------------------------------------------------*/

//����ͼ�Ľṹ�Լ���صIJ�������

#define MAX 20 //��󶥵����
Copy link
Collaborator

Choose a reason for hiding this comment

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

  • 这些全局定义统一放到最上面会更美观


typedef struct Graph
{
BOOL** matrix;
int size;
}Graph;

Graph CreatGraph(int size);//����ͼ
Status InitTestGraph(Graph* G);//��ʼ��ͼ
Status TraverseGraph(Graph a);//����ͼ
Status BFSTraverse(Graph* g, int start, int end);//�����������
int FirstAdjVex(Graph p, int i);//��ʼ��
int NextVex(Graph g, int i, int j);//��һ����
Status TraversePath(LinkQueue Q, int start);//��ӡ·��
Binary file added 2017-1/Micylt/hw7/a.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 2017-1/Micylt/hw7/b.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 2017-1/Micylt/hw7/c.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 2017-1/Micylt/hw7/d.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 2017-1/Micylt/hw7/e.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 28 additions & 0 deletions 2017-1/Micylt/hw7/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "Graph.h"

int main()
{
Graph a = CreatGraph(9);
int x;
int y;

InitTestGraph(&a);
printf("��ͼ���ڽӾ������£�\n");
TraverseGraph(a);
printf("\n");
printf("��ʼ����ÿ���㵽����һ��������·����\n");
for (x = 1; x <= 9; x++)
{
for (y = 1; y <= 9; y++)
{
if (x != y)
{
printf("<%d -->%d> ", x, y);
BFSTraverse(&a, x - 1, y - 1);
printf("\n");
}
}
}
printf("��������\n");
return 0;
}
Binary file added 2017-1/Micylt/hw7/图的邻接矩阵.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 2017-1/Micylt/hw8/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions 2017-1/Micylt/hw8/BSTOutput.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
8, 3, 1, 6, 4, 5, 7, 10, 14, 19, 22, 30
Copy link
Collaborator

Choose a reason for hiding this comment

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

  • 程序中没有看到C语言文件流相关操作,这个结果输出是手工复制的吗? 建议使用C语言文件操作,自动把查找结果输出到文件中

Copy link
Contributor Author

Choose a reason for hiding this comment

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

= =,不好意思理解错了,结果确实是自己复制的.... 修改后会重新提交


8, 3, 1, 6, 4, 5, 7, 10, 14, 13, 19, 22, 30

7, 3, 1, 6, 4, 5, 10, 14, 13, 19, 22, 30

7, 3, 1, 6, 4, 10, 14, 13, 19, 22, 30

7, 3, 1, 6, 4, 10, 14, 13, 19, 22, 20, 30

7, 3, 1, 4, 10, 14, 13, 19, 22, 20, 30
Loading