-
Notifications
You must be signed in to change notification settings - Fork 46
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
base: master
Are you sure you want to change the base?
提交图的最短路径 #162
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
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; | ||
} | ||
|
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 //������ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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);//��ӡ·�� |
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; | ||
} |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.