鄰接表儲存結構
/*鄰接表的邊*/ typedef struct ArcNode { int adjvex; struct ArcNode *next; }ArcNode; /*鄰接表的結點*/ typedef struct VNode { char date; ArcNode *firstarc; }VNode;
創建一個鄰接表儲存結構的圖
//創建一個鄰接表類型的圖 void CreatArcGraph(int count , VNode G[]) { ArcNode *p , *q; char c; //儲存結點內數據 int number; //儲存要連接的結點 printf("請輸入各個結點的數據:\n"); for (size_t i = 0; i < count; i++) //初始化數組,輸入各結點數據 { fflush(stdin); printf("請輸入第%d個結點內存入的字符:" , i); c = getchar(); G[i].date = c; G[i].firstarc = NULL; }
圖的遍歷(1)-----深度優先搜索
//深度優先搜索一個連通圖 void DFS(VNode G[] , int v) { int w; printf("%c" , G[v].date); //訪問當前結點 printfed[v] = 1; w = FirstAdj(G , v); while (w!=-1) { if (printfed[w]==0) DFS(G,w); w = NextAdj(G , v); } } //對圖G = (V,E)進行深度優化搜索的主算法 void Travel_DFS(VNode G[] , int n) { for (size_t i = 0; i < n; i++) { printfed[i] = 0; } for (size_t i = 0; i < n; i++) { if (printfed[i]==0) { DFS(G,i); } } }
深度優先搜索實例
#include<stdio.h> #include<stdlib.h> /*鄰接表的邊*/ typedef struct ArcNode { int adjvex; struct ArcNode *next; }ArcNode; /*鄰接表的結點*/ typedef struct VNode { char date; ArcNode *firstarc; }VNode; //創建一個鄰接表類型的圖 void CreatArcGraph(int count , VNode G[]) { ArcNode *p , *q; char c; //儲存結點內數據 int number; //儲存要連接的結點 printf("請輸入各個結點的數據:\n"); for (size_t i = 0; i < count; i++) //初始化數組,輸入各結點數據 { fflush(stdin); printf("請輸入第%d個結點內存入的字符:" , i); c = getchar(); G[i].date = c; G[i].firstarc = NULL; //putchar('\n'); } for (size_t i = 0; i < count; i++) //創建邊 { printf("請輸入第%d個結點要連接的結點(各個結點以空格隔開):",i); scanf("%d" , &number); while (number!=-1) { p = (ArcNode*)malloc(sizeof(ArcNode)); p->next = NULL; p->adjvex = number; if (G[i].firstarc == NULL) { G[i].firstarc = p; }else { q->next = p; } q = p; scanf("%d" , &number); } } } int printfed[3]; //尋找第一個鄰接點 int FirstAdj(VNode G[] , int v) { return G[v].firstarc->adjvex; } //尋找下一個鄰接點 int NextAdj(VNode G[] , int v) { ArcNode *p; p = G[v].firstarc->next; while (p->next!=NULL ) { p = p->next; if (printfed[p->adjvex]==0) { return p->adjvex; } } return -1; } //深度優先搜索一個連通圖 void DFS(VNode G[] , int v) { int w; printf("%c" , G[v].date); //訪問當前結點 printfed[v] = 1; w = FirstAdj(G , v); while (w!=-1) { if (printfed[w]==0) DFS(G,w); w = NextAdj(G , v); } } //對圖G = (V,E)進行深度優化搜索的主算法 void Travel_DFS(VNode G[] , int n) { for (size_t i = 0; i < n; i++) { printfed[i] = 0; } for (size_t i = 0; i < n; i++) { if (printfed[i]==0) { DFS(G,i); } } } #define MAXG 100 int main() { int count; VNode G[MAXG]; printf("請輸入要創建的圖的結點數:"); scanf("%d",&count); CreatArcGraph(count , G); Travel_DFS(G,count); return 0; }
運行結果
圖的遍歷(2)-------廣度優先搜索
//廣度優先搜索一個連通圖 void BFS(VNode G[] , int v) { initQueue(&q); //首先訪問當前結點 printf("%c" , G[v].date); visited[v] = 1; //訪問標記 EnQueue(&q , v); //入隊列 // int w; while (q.front==q.rear) { DeQueue(&q , &v); w = Firstadj(G , v); while (w!=-1) { if (visited[w]==0) { printf("%c",G[w].date); EnQueue(&q , w); visited[w] = 1; } w = Nextadj(G,v); } } } //廣度優先搜索的主算法 void Travel_BFS(VNode G[] , int v) { for (size_t i = 0; i < v; i++) { visited[i] = 0; } for (size_t i = 0; i < v; i++) { BFS(G , i); } }
廣度優先搜索實例
如圖:
#include<stdio.h> #include<stdlib.h> ////////////////////////////////////////////////////////////////////////////// //儲存類型定義 //圖邊 typedef struct ArcNode { int adjvex; struct ArcNode *next; }ArcNode; //圖結點 typedef struct VNode { char date; ArcNode *firstarc; }VNode; //隊列結點 typedef struct QNode { int date; struct QNode *next; }QNode , *QueuePtr; //隊列指針 typedef struct { QueuePtr front; QueuePtr rear; }LinkQueue; //////////////////////////////////////////////////////////////////////////////// //隊列操作 //創建隊列 void initQueue(LinkQueue *q) { q->front = q->rear = (QueuePtr)malloc(sizeof(QNode)); if(!q->front) exit(0); q->front->next = NULL; } //入隊列 void EnQueue(LinkQueue *q , int v ) { QueuePtr p; p = (QueuePtr)malloc(sizeof(QNode)); if(!q->front) exit(0); p->date = v; p->next = NULL; q->rear->next = p; q->rear = p; } //出隊列 void DeQueue(LinkQueue *q , int *v) { if (q->front==q->rear) { exit(0); } QueuePtr p; p = q->front->next; *v = p->date; q->front->next = p->next; if (q->rear==p) { q->front = q->rear =NULL; } free(p); } ///////////////////////////////////////////////////////////////////////////////////// //圖操作 //創建一個圖(鄰接表) void CreatArcNode(VNode G[] , int count) { //為結點數組輸入數據 char c; printf("請輸入要在各個結點存入的字符..."); for (size_t i = 0; i < count; i++) { fflush(stdin); printf("請輸入在第%d個結點存入的數據:",i); c = getchar(); G[i].date = c; G[i].firstarc = NULL; } //創建幾條單鏈表連接在數組后面(創建邊) int number; ArcNode *p , *q; for (size_t i = 0; i < count; i++) { printf("請輸入結點%d要指向的結點:",i); scanf("%d",&number); while (number!=-1) { p = (ArcNode*)malloc(sizeof(ArcNode)); p->adjvex = number; p->next = NULL; if (G[i].firstarc==NULL) { G[i].firstarc = p; }else { q->next = p; } q = p; scanf("%d",&number); } } } /***********************************全局變量****************************************/ int visited[100]; LinkQueue q; ///////////////////////////////////////////////////////////////////////////////// //廣度優先搜索操作 //尋找第一個鄰接點 int Firstadj(VNode G[] , int v) { return G[v].firstarc->adjvex; } //尋找下一個鄰接點 int Nextadj(VNode G[] , int v) { ArcNode *p; p = G[v].firstarc->next; while (!p->next) { p = p->next; if (visited[p->adjvex]==0) { return p->adjvex; } } return -1; } //廣度優先搜索一個連通圖 void BFS(VNode G[] , int v) { initQueue(&q); //首先訪問當前結點 printf("%c" , G[v].date); visited[v] = 1; //訪問標記 EnQueue(&q , v); //入隊列 // int w; while (q.front==q.rear) { DeQueue(&q , &v); w = Firstadj(G , v); while (w!=-1) { if (visited[w]==0) { printf("%c",G[w].date); EnQueue(&q , w); visited[w] = 1; } w = Nextadj(G,v); } } } //廣度優先搜索的主算法 void Travel_BFS(VNode G[] , int v) { for (size_t i = 0; i < v; i++) { visited[i] = 0; } for (size_t i = 0; i < v; i++) { BFS(G , i); } } int main() { //創建一個圖 int count; printf("請輸入創建的圖有幾個結點:"); scanf("%d" , &count); VNode G[count]; CreatArcNode(G,count); //打印鄰接表 for (size_t i = 0; i < count; i++) { ArcNode *p; p = G[i].firstarc; printf("第%d個結點:%c",i,G[i].date); do { if(p!=NULL) { printf("----<%d,%c>",p->adjvex,G[p->adjvex].date); p = p->next; } }while(p!=NULL); putchar('\n'); } //廣度優先搜索 Travel_BFS(G , count); getchar(); return 0; }
運行結果: