diff --git a/2017-1/luyj/Graph.c b/2017-1/luyj/Graph.c new file mode 100644 index 00000000..ef53cc87 --- /dev/null +++ b/2017-1/luyj/Graph.c @@ -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; +} + + diff --git a/2017-1/luyj/Graph.h b/2017-1/luyj/Graph.h new file mode 100644 index 00000000..a6737c54 --- /dev/null +++ b/2017-1/luyj/Graph.h @@ -0,0 +1,71 @@ +#include +#include + +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); + + diff --git "a/2017-1/luyj/Graph\350\277\220\350\241\214\347\273\223\346\236\2341.png" "b/2017-1/luyj/Graph\350\277\220\350\241\214\347\273\223\346\236\2341.png" new file mode 100644 index 00000000..7105a72d Binary files /dev/null and "b/2017-1/luyj/Graph\350\277\220\350\241\214\347\273\223\346\236\2341.png" differ diff --git a/2017-1/luyj/s_Graph.c b/2017-1/luyj/s_Graph.c new file mode 100644 index 00000000..71727182 --- /dev/null +++ b/2017-1/luyj/s_Graph.c @@ -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); + } + } + +} \ No newline at end of file diff --git "a/2017-1/luyj/\345\233\276.png" "b/2017-1/luyj/\345\233\276.png" new file mode 100644 index 00000000..803ff3b5 Binary files /dev/null and "b/2017-1/luyj/\345\233\276.png" differ diff --git "a/2017-1/luyj/\350\277\220\350\241\214\347\273\223\346\236\2342.png" "b/2017-1/luyj/\350\277\220\350\241\214\347\273\223\346\236\2342.png" new file mode 100644 index 00000000..b20637f3 Binary files /dev/null and "b/2017-1/luyj/\350\277\220\350\241\214\347\273\223\346\236\2342.png" differ