Skip to content

图两点间最短路径算法实现 #130

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

Merged
merged 2 commits into from
Jun 6, 2017
Merged
Show file tree
Hide file tree
Changes from all 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
222 changes: 222 additions & 0 deletions 2017-1/luyj/Graph.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
#include "Graph.h"


/*=========���еĻ�������=========*/
Status InitQueue(LinkQueue *q)
{
q->front = q->rear = (QueuePtr)malloc(sizeof(QNode));
if (!q->front)
{
return ERROR;
}
q->front->next = q->rear->next = NULL;
return OK;
}

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

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

bool QueueEmpty(LinkQueue*q)
{
if (q->front == q->rear)
{
return TRUE;
}
else
{
return FALSE;
}
}

Status DestroyQueue(LinkQueue*q)
{
while (q->front)
{
q->rear = q->front->next;
free(q->front);
q->front = q->rear;
}
return OK;
}
/*=========ͼ�Ļ�������=========*/

Status InsertArc(MGraph *g,int v1,int v2)
{
int i = 0;
int j = 0;
if (LocateVex(g, v1, &i) == TRUE && LocateVex(g, v2, &j) == TRUE)
{
g->arcs[i][j].adj = 1;//����֮�������ߣ���ֵΪ1;
g->arcs[j][i] = g->arcs[i][j];
}
}
bool LocateVex(MGraph *g,int v,int *i)
{
int m;
for (m = 0; m <= g->vexnum; m++)
{
if (g->vexs[m]==v)
{
*i = m;
return TRUE;
}
}
return FALSE;
}

//����ͼ
Status CreateUDN(MGraph *g)
{
int i;
int j;
//��������ֱ�Ӹ�ֵ��
g->vexnum = 9;
g->arcnum = 12;
for (i = 1; i <= g->vexnum; i++)
{
g->vexs[i] = i;//������������;
}
for (i = 0; i <= g->vexnum; i++)//��ʼ���ڽӾ���;
{
for (j = 0; j <= g->vexnum; j++)
{
g->arcs[i][j].adj = INFINITY;
g->arcs[i][j].info = NULL;
}
}
//�����ڽӾ���;
InsertArc(g, 1, 2);
InsertArc(g, 1, 3);
InsertArc(g, 1, 4);
InsertArc(g, 1, 7);
InsertArc(g, 2, 3);
InsertArc(g, 4, 5);
InsertArc(g, 4, 6);
InsertArc(g, 5, 6);
InsertArc(g, 6, 8);
InsertArc(g, 7, 8);
InsertArc(g, 7, 9);
InsertArc(g, 8, 9);

return OK;
}

//�ҳ���һ���ڽӵ�
int FirstAdjVex(MGraph *g, int u)
{
int i;
for (i = 1; i <= g->vexnum; i++)
{
if (g->arcs[u][i].adj == 1)
{
return i;
}
}
return -1;
}

//�ҳ���һ���ڽӵ�
int NextAdjvex(MGraph *g, int u, int w)
{
int i;
for (i = w + 1; i <= g->vexnum; i++)
{
if (g->arcs[u][i].adj == 1)
{
return i;
}
}
return -1;
}
//������ȱ���ͼ,������a,b������·��;
Status BFSTraverse(MGraph*g, LinkQueue *q,int a,int b)
{

int v;
int u = 0;
int w = 0;
int m = 0;
int n = 0;
bool visited[MAX_VERTEX_NUM];
for (v = 1; v <= g->vexnum; v++)
{
visited[v] = FALSE; //������飬���ͼ���ѷ��ʵĵ�
}

EnQueue(q, a); //a�������;
while (QueueEmpty(q)!= TRUE)
{
DeQueue(q, &u);
for (w = FirstAdjVex(g, u); w >=0; w = NextAdjvex(g, u, w))
{
if (visited[w] == FALSE)//�ж�w�Ƿ��Ѿ����ʹ�
{
visited[w] = TRUE;
EnQueue(q, w);
}
if (w == b)
{
break;
}
}
if (w == b)
{
break;
}
}
}

Status print(LinkQueue *q,int a)
{
if (q->rear->data == a)
{
printf("%d->%d\n", a, a);
return OK;
}

int i = 0;
int j;
int num[MAX_VERTEX_NUM] = { 0 };
while (q->rear->data!=a)//�����������
{
num[i] = q->rear->data;
q->rear = q->rear->pre;
i++;
}
printf("%d", a);
for (j = i - 1; j >= 0; j--)
{
printf("->%d", num[j]);
}


printf("\n");
return OK;
}


71 changes: 71 additions & 0 deletions 2017-1/luyj/Graph.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include <stdio.h>
#include <malloc.h>

typedef enum
{
OK,
ERROR,
OVERFLOW
}Status;

typedef enum
{
TRUE,
FALSE
}bool;


/*=========ͼ������洢�ṹ=========*/
#define MAX_VERTEX_NUM 10
#define VRType int
#define InfoType int
#define VertexType int
#define INFINITY -1

typedef struct ArcCell
{
VRType adj;
InfoType *info;
}ArcCell, AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];

typedef struct
{
VertexType vexs[MAX_VERTEX_NUM];
AdjMatrix arcs;
int vexnum, arcnum; //ͼ�Ķ������ͻ���;
}MGraph;


/*=========���е�˫���洢�ṹ=========*/
#define QElemType int

typedef struct QNode
{
QElemType data;
struct QNode *next;
struct QNode *pre;
}QNode, *QueuePtr;

typedef struct LinkQueue
{
QueuePtr front;
QueuePtr rear;
}LinkQueue;

/*=========���еĻ�������=========*/
Status InitQueue(LinkQueue *Q);
Status EnQueue(LinkQueue*Q, QElemType e);
Status DeQueue(LinkQueue*Q, QElemType*e);
bool QueueEmpty(LinkQueue*Q);
Status DestroyQueue(LinkQueue*Q);

/*=========ͼ�Ļ�������=========*/
bool LocateVex(MGraph *g, int v, int *i);
Status CreateUDN(MGraph *G);
int FirstAdjVex(MGraph *G, int u);
int NextAdjvex(MGraph *G, int u, int w);
Status BFSTraverse(MGraph*G, LinkQueue *Q, int a, int b);
Status print(LinkQueue *Q, int a);
Status InsertArc(MGraph *G, int v1, int v2);


Binary file added 2017-1/luyj/Graph运行结果1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions 2017-1/luyj/s_Graph.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "Graph.h"

int main()
{
int i;
int j;

MGraph graph;
CreateUDN(&graph);
for (i = 1; i <= graph.vexnum; i++)
{
for (j = 1; j <= graph.vexnum; j++)
{
LinkQueue Q;
InitQueue(&Q);
printf("%d<->%d:", i, j);
BFSTraverse(&graph, &Q, i, j);
print(&Q, i);
DestroyQueue(&Q);
}
}

}
Binary file added 2017-1/luyj/图.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/luyj/运行结果2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.