From 95cf3db74305e1ffb6990fa40f9d0563bb50c986 Mon Sep 17 00:00:00 2001 From: asus <13860948875> Date: Thu, 7 Jan 2021 18:08:15 +0800 Subject: [PATCH 1/2] 12 --- "\344\275\234\344\270\232/\345\233\276.txt" | 203 ++++++++++++++++++++ 1 file changed, 203 insertions(+) create mode 100644 "\344\275\234\344\270\232/\345\233\276.txt" diff --git "a/\344\275\234\344\270\232/\345\233\276.txt" "b/\344\275\234\344\270\232/\345\233\276.txt" new file mode 100644 index 0000000..eecf93d --- /dev/null +++ "b/\344\275\234\344\270\232/\345\233\276.txt" @@ -0,0 +1,203 @@ +#include +#include + +#define OK 1 +#define ERROR 0 +#define MAX_VERTAX_SIZE 20 + +typedef char VerElemType; +typedef char ElemType; +typedef int Status; + +typedef struct Graph{ + VerElemType VertaxMatrix[MAX_VERTAX_SIZE]; + int AdjacentMatrix[MAX_VERTAX_SIZE][MAX_VERTAX_SIZE]; + int VertaxNum; + int EageNum; +}Graph; + + +typedef struct QueueNode{ + ElemType data; + struct QueueNode* next; +}QueueNode, *QueueNodePtr; +typedef struct Queue{ + QueueNodePtr front; + QueueNodePtr rear; +}Queue; + +Status InitQueue(Queue* q){ + (*q).front = (QueueNodePtr)malloc(sizeof(struct QueueNode)); + (*q).rear = (*q).front; + (*q).rear->next = NULL; + return OK; +} +Status EnterQueue(Queue* q, ElemType e){ + QueueNodePtr n; + n = (QueueNode*)malloc(sizeof(struct QueueNode)); + n->data = e; + n->next = q->rear->next; + q->rear->next = n; + q->rear = n; + return OK; +} +Status DeleteQueue(Queue* q, ElemType* e){ + QueueNodePtr p; + if( q->front == q->rear ){ + printf("Empty\n"); + return ERROR; + } + p = q->front->next; + *e = p->data; + q->front->next = p->next; + free(p); + if( p == q->rear ) + q->rear = q->front; + return OK; +} +Status IsQueueEmpty(Queue q){ + return q.front == q.rear ? OK : ERROR; +} + + +int LocateVertax(Graph G, VerElemType ver){ + int i; + for( i = 0; i < G.VertaxNum; i++ ){ + if( G.VertaxMatrix[i] == ver ) + return i; + } + return -1; +} +Status CreateUDG(Graph* G){ + int i,j,k; + VerElemType x,y; + printf(" Create Undigraph.\n"); + printf("Please enter the number of Vertax and Eage: \n"); + scanf("%d %d%*c",&(*G).VertaxNum, &(G->EageNum)); + printf("ok, please input value of %d Vertax.\n", G->VertaxNum ); + for( i = 0; i < G->VertaxNum; i++ ){ + scanf("%c%*c", &(G->VertaxMatrix[i])); + } + + for( i = 0; i < G->VertaxNum; i++ ) + for( j = 0; j < G->VertaxNum; j++ ) + G->AdjacentMatrix[i][j] = 0; + //for( i = 0; i < G->VertaxNum; i++ ){ //ʼڽӱ + // for( j = 0; j < G->VertaxNum; j++ ) + // printf("%d ", G->AdjacentMatrix[i][j]); + // printf("\n"); + //} + + for( k = 0; k < G->EageNum; k++ ){ + printf("ok, please input two Vertax of Eage: %d,separated by Spaces.\n", k+1 ); + scanf("%c %c%*c", &x, &y); + i = LocateVertax(*G, x); + j = LocateVertax(*G, y); + G->AdjacentMatrix[i][j] = G->AdjacentMatrix[j][i] = 1; + } + return OK; +} + +Status PrintAdjacentMatrix(Graph G){ + int i, j; + printf(" Adjacent Matrix\n"); + for( i = 0; i < G.VertaxNum; i++ ){ + for( j = 0; j < G.VertaxNum; j++){ + printf("%3d", G.AdjacentMatrix[i][j]); + } + printf("\n"); + } + return OK; +} + + int FirstAdjacentVertax(Graph G, VerElemType v){ + int index_v = LocateVertax(G, v); + int i; + for( i = 0; i < G.VertaxNum; i++ ){ + if( G.AdjacentMatrix[index_v][i] == 1) + return i; + } + return -1; +} + +int NextAdjacentVertax(Graph G, VerElemType v, VerElemType w){ + int index_v = LocateVertax(G, v); + int index_w = LocateVertax(G, w); + int i; + for( i = index_w + 1; i < G.VertaxNum; i++ ){ + if( G.AdjacentMatrix[index_v][i] == 1 ) + return i; + } + return -1; +} + + +int visitedArray[MAX_VERTAX_SIZE]; + +void visit(VerElemType c){ + printf("%c ", c); +} +VerElemType GetVertaxValue(Graph G, int position){ + return G.VertaxMatrix[position]; +} +Status DFS(Graph G, VerElemType v){ + VerElemType w; + visit(v); + visitedArray[LocateVertax(G, v)] = 1; + + for(w = GetVertaxValue(G, FirstAdjacentVertax(G, v)); LocateVertax(G, w) != -1; w = GetVertaxValue(G, NextAdjacentVertax(G, v, w))){ + if( visitedArray[LocateVertax(G, w)] != 1 ) + DFS(G, w); + } + return OK; +} +Status DFSTraverse(Graph G){ + int i; + for( i = 0; i < G.VertaxNum; i++ ){ + visitedArray[i] = 0; + } + for( i = 0; i < G.VertaxNum; i++){ + if( visitedArray[i] == 0 ){ + DFS(G, GetVertaxValue(G, i)); + } + } + return OK; +} + +Status BFSTraverse(Graph G){ + ElemType c; + Queue q; + InitQueue(&q); + int i,j; + for( i = 0; i < G.VertaxNum; i++ ) + visitedArray[i] = 0; + + for( i = 0; i < G.VertaxNum; i++ ){ + if( visitedArray[i] == 0 ){ + EnterQueue(&q, G.VertaxMatrix[i]); + visitedArray[i] = 1; + while( IsQueueEmpty(q) != OK ){ + DeleteQueue(&q, &c); + visit(c); + for( j = FirstAdjacentVertax(G, c); j != -1; j = NextAdjacentVertax(G, c, GetVertaxValue(G, j))){ + if( visitedArray[j] == 0 ){ + EnterQueue(&q, GetVertaxValue(G, j)); + visitedArray[j] = 1; + } + } + } + } + } + +} + +int main(){ + Graph G; + CreateUDG(&G); + PrintAdjacentMatrix(G); + printf("the Result of DFS(Depth First Search) is: "); + DFSTraverse(G); + printf("\nthe REsult of BFS(Breadth First Srarch) is: "); + BFSTraverse(G); + return 0; +} \ No newline at end of file -- Gitee From c27d4e887866e59aa8cda54ddad731080b79f331 Mon Sep 17 00:00:00 2001 From: asus <13860948875> Date: Thu, 7 Jan 2021 18:10:09 +0800 Subject: [PATCH 2/2] 12 --- .../\345\233\276.txt" => "\344\275\234\344\270\232/tu.txt" | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename "\344\275\234\344\270\232/\345\233\276.txt" => "\344\275\234\344\270\232/tu.txt" (100%) diff --git "a/\344\275\234\344\270\232/\345\233\276.txt" "b/\344\275\234\344\270\232/tu.txt" similarity index 100% rename from "\344\275\234\344\270\232/\345\233\276.txt" rename to "\344\275\234\344\270\232/tu.txt" -- Gitee