Skip to content

Commit f3cb855

Browse files
authored
Merge pull request #130 from luyj/master
ok
2 parents ca48162 + d8e366d commit f3cb855

File tree

6 files changed

+316
-0
lines changed

6 files changed

+316
-0
lines changed

2017-1/luyj/Graph.c

Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
#include "Graph.h"
2+
3+
4+
/*=========队列的基本操作=========*/
5+
Status InitQueue(LinkQueue *q)
6+
{
7+
q->front = q->rear = (QueuePtr)malloc(sizeof(QNode));
8+
if (!q->front)
9+
{
10+
return ERROR;
11+
}
12+
q->front->next = q->rear->next = NULL;
13+
return OK;
14+
}
15+
16+
Status EnQueue(LinkQueue*q, QElemType e)
17+
{
18+
QueuePtr p = (QueuePtr)malloc(sizeof(QNode));
19+
if (!p)
20+
{
21+
return ERROR;
22+
}
23+
else
24+
{
25+
p->data = e;
26+
p->next = NULL;
27+
p->pre = q->front;
28+
q->rear->next = p;
29+
q->rear = p;
30+
}
31+
}
32+
33+
Status DeQueue(LinkQueue*q, QElemType*e)
34+
{
35+
if (q->front == q->rear)
36+
{
37+
return ERROR;
38+
}
39+
q->front = q->front->next;
40+
*e = q->front->data;
41+
return OK;
42+
}
43+
44+
bool QueueEmpty(LinkQueue*q)
45+
{
46+
if (q->front == q->rear)
47+
{
48+
return TRUE;
49+
}
50+
else
51+
{
52+
return FALSE;
53+
}
54+
}
55+
56+
Status DestroyQueue(LinkQueue*q)
57+
{
58+
while (q->front)
59+
{
60+
q->rear = q->front->next;
61+
free(q->front);
62+
q->front = q->rear;
63+
}
64+
return OK;
65+
}
66+
/*=========图的基本操作=========*/
67+
68+
Status InsertArc(MGraph *g,int v1,int v2)
69+
{
70+
int i = 0;
71+
int j = 0;
72+
if (LocateVex(g, v1, &i) == TRUE && LocateVex(g, v2, &j) == TRUE)
73+
{
74+
g->arcs[i][j].adj = 1;//两点之间有连线,弧值为1;
75+
g->arcs[j][i] = g->arcs[i][j];
76+
}
77+
}
78+
bool LocateVex(MGraph *g,int v,int *i)
79+
{
80+
int m;
81+
for (m = 0; m <= g->vexnum; m++)
82+
{
83+
if (g->vexs[m]==v)
84+
{
85+
*i = m;
86+
return TRUE;
87+
}
88+
}
89+
return FALSE;
90+
}
91+
92+
//构建图
93+
Status CreateUDN(MGraph *g)
94+
{
95+
int i;
96+
int j;
97+
//根据用例直接赋值。
98+
g->vexnum = 9;
99+
g->arcnum = 12;
100+
for (i = 1; i <= g->vexnum; i++)
101+
{
102+
g->vexs[i] = i;//构建顶点向量;
103+
}
104+
for (i = 0; i <= g->vexnum; i++)//初始化邻接矩阵;
105+
{
106+
for (j = 0; j <= g->vexnum; j++)
107+
{
108+
g->arcs[i][j].adj = INFINITY;
109+
g->arcs[i][j].info = NULL;
110+
}
111+
}
112+
//构建邻接矩阵;
113+
InsertArc(g, 1, 2);
114+
InsertArc(g, 1, 3);
115+
InsertArc(g, 1, 4);
116+
InsertArc(g, 1, 7);
117+
InsertArc(g, 2, 3);
118+
InsertArc(g, 4, 5);
119+
InsertArc(g, 4, 6);
120+
InsertArc(g, 5, 6);
121+
InsertArc(g, 6, 8);
122+
InsertArc(g, 7, 8);
123+
InsertArc(g, 7, 9);
124+
InsertArc(g, 8, 9);
125+
126+
return OK;
127+
}
128+
129+
//找出第一个邻接点
130+
int FirstAdjVex(MGraph *g, int u)
131+
{
132+
int i;
133+
for (i = 1; i <= g->vexnum; i++)
134+
{
135+
if (g->arcs[u][i].adj == 1)
136+
{
137+
return i;
138+
}
139+
}
140+
return -1;
141+
}
142+
143+
//找出下一个邻接点
144+
int NextAdjvex(MGraph *g, int u, int w)
145+
{
146+
int i;
147+
for (i = w + 1; i <= g->vexnum; i++)
148+
{
149+
if (g->arcs[u][i].adj == 1)
150+
{
151+
return i;
152+
}
153+
}
154+
return -1;
155+
}
156+
//广度优先遍历图,求两点a,b间的最短路径;
157+
Status BFSTraverse(MGraph*g, LinkQueue *q,int a,int b)
158+
{
159+
160+
int v;
161+
int u = 0;
162+
int w = 0;
163+
int m = 0;
164+
int n = 0;
165+
bool visited[MAX_VERTEX_NUM];
166+
for (v = 1; v <= g->vexnum; v++)
167+
{
168+
visited[v] = FALSE; //标记数组,标记图中已访问的点
169+
}
170+
171+
EnQueue(q, a); //a先入队列;
172+
while (QueueEmpty(q)!= TRUE)
173+
{
174+
DeQueue(q, &u);
175+
for (w = FirstAdjVex(g, u); w >=0; w = NextAdjvex(g, u, w))
176+
{
177+
if (visited[w] == FALSE)//判断w是否已经访问过
178+
{
179+
visited[w] = TRUE;
180+
EnQueue(q, w);
181+
}
182+
if (w == b)
183+
{
184+
break;
185+
}
186+
}
187+
if (w == b)
188+
{
189+
break;
190+
}
191+
}
192+
}
193+
194+
Status print(LinkQueue *q,int a)
195+
{
196+
if (q->rear->data == a)
197+
{
198+
printf("%d->%d\n", a, a);
199+
return OK;
200+
}
201+
202+
int i = 0;
203+
int j;
204+
int num[MAX_VERTEX_NUM] = { 0 };
205+
while (q->rear->data!=a)//倒序进入数组
206+
{
207+
num[i] = q->rear->data;
208+
q->rear = q->rear->pre;
209+
i++;
210+
}
211+
printf("%d", a);
212+
for (j = i - 1; j >= 0; j--)
213+
{
214+
printf("->%d", num[j]);
215+
}
216+
217+
218+
printf("\n");
219+
return OK;
220+
}
221+
222+

2017-1/luyj/Graph.h

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#include <stdio.h>
2+
#include <malloc.h>
3+
4+
typedef enum
5+
{
6+
OK,
7+
ERROR,
8+
OVERFLOW
9+
}Status;
10+
11+
typedef enum
12+
{
13+
TRUE,
14+
FALSE
15+
}bool;
16+
17+
18+
/*=========图的数组存储结构=========*/
19+
#define MAX_VERTEX_NUM 10
20+
#define VRType int
21+
#define InfoType int
22+
#define VertexType int
23+
#define INFINITY -1
24+
25+
typedef struct ArcCell
26+
{
27+
VRType adj;
28+
InfoType *info;
29+
}ArcCell, AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
30+
31+
typedef struct
32+
{
33+
VertexType vexs[MAX_VERTEX_NUM];
34+
AdjMatrix arcs;
35+
int vexnum, arcnum; //图的顶点数和弧数;
36+
}MGraph;
37+
38+
39+
/*=========队列的双链存储结构=========*/
40+
#define QElemType int
41+
42+
typedef struct QNode
43+
{
44+
QElemType data;
45+
struct QNode *next;
46+
struct QNode *pre;
47+
}QNode, *QueuePtr;
48+
49+
typedef struct LinkQueue
50+
{
51+
QueuePtr front;
52+
QueuePtr rear;
53+
}LinkQueue;
54+
55+
/*=========队列的基本操作=========*/
56+
Status InitQueue(LinkQueue *Q);
57+
Status EnQueue(LinkQueue*Q, QElemType e);
58+
Status DeQueue(LinkQueue*Q, QElemType*e);
59+
bool QueueEmpty(LinkQueue*Q);
60+
Status DestroyQueue(LinkQueue*Q);
61+
62+
/*=========图的基本操作=========*/
63+
bool LocateVex(MGraph *g, int v, int *i);
64+
Status CreateUDN(MGraph *G);
65+
int FirstAdjVex(MGraph *G, int u);
66+
int NextAdjvex(MGraph *G, int u, int w);
67+
Status BFSTraverse(MGraph*G, LinkQueue *Q, int a, int b);
68+
Status print(LinkQueue *Q, int a);
69+
Status InsertArc(MGraph *G, int v1, int v2);
70+
71+

2017-1/luyj/Graph运行结果1.png

24.9 KB
Loading

2017-1/luyj/s_Graph.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include "Graph.h"
2+
3+
int main()
4+
{
5+
int i;
6+
int j;
7+
8+
MGraph graph;
9+
CreateUDN(&graph);
10+
for (i = 1; i <= graph.vexnum; i++)
11+
{
12+
for (j = 1; j <= graph.vexnum; j++)
13+
{
14+
LinkQueue Q;
15+
InitQueue(&Q);
16+
printf("%d<->%d:", i, j);
17+
BFSTraverse(&graph, &Q, i, j);
18+
print(&Q, i);
19+
DestroyQueue(&Q);
20+
}
21+
}
22+
23+
}

2017-1/luyj/图.png

217 KB
Loading

2017-1/luyj/运行结果2.png

20 KB
Loading

0 commit comments

Comments
 (0)