[數據結構]圖基於鄰接矩陣的BFS與DFS的C語言簡單實現

上次給大家看了一下圖的鄰接矩陣的存儲。接下來來看下圖的廣度優先搜索和深度優先搜索。
圖
爲了更好的展示BFS和DFS,我把上次的圖改了一個邊,改成上圖那樣。
好了,貼代碼
由於我們的圖廣度遍歷時需要藉助一個隊列來實現
所以我們需要用到一個之前實現的隊列
把下面這個代碼寫成queue.c,因爲我們圖的源文件需要對它導入

#define MAXSIZE 100
#define ElemType char
#define BOOL int
#define TRUE 1
#define FALSE 0

typedef struct{
    ElemType data[MAXSIZE];
    int front, rear;
}Queue;

static void InitQueue(Queue *queue);   //初始化隊列
static BOOL IsEmpty(Queue *queue); //判斷是否爲空
static void EnQueue(Queue *queue, ElemType x); //入隊
static void DeQueue(Queue *queue, ElemType *x);    //出隊

void InitQueue(Queue *queue){
    queue->front = 0;
    queue->rear = 0;
}

void EnQueue(Queue *queue, ElemType e){
    queue->data[queue->rear] = e;
    queue->rear = (queue->rear + 1) % MAXSIZE;
}

void DeQueue(Queue *queue, ElemType *e){
    *e = queue->data[queue->front];
    queue->front = (queue->front + 1) % MAXSIZE;
}

BOOL IsEmpty(Queue *queue){
    if (queue->front == queue->rear) {
        return TRUE;
    } else {
        return FALSE;
    }
}

你可以拿上面的代碼跟之前的代碼做個比較。其實就是聲明函數的那個部分加了static,加這個可以讓別的源文件訪問,然後main函數去掉了而已。
然後下面是圖的代碼。

#include <stdio.h>
#include <stdlib.h>
#define INFINTE 65535
#define MAXSIZE 100
#include "queue.c"

typedef char VertexType;                //頂點類型應由用戶定義
typedef int EdgeType;                   //邊上的權值類型應由用戶定義

typedef struct graph{
    VertexType vexs[MAXSIZE];            //頂點表
    EdgeType   arc[MAXSIZE][MAXSIZE];       //鄰接矩陣
    int numNodes, numEdges;
}Graph;

int visited[200];

void CreateGraph(Graph* graph); //創建圖
VertexType* FirstAdjVex(Graph graph, VertexType data);  //獲取第一鄰接點
VertexType* NextAdjVex(Graph graph, VertexType data, VertexType adj);   //獲取下一鄰接點
void DFSTraversal(Graph graph); //深度優先遍歷
void BFSTraversal(Graph graph); //廣度優先遍歷
void DFS(Graph graph, VertexType data); //深度優先搜索
void BFS(Graph graph, VertexType data); //廣度優先搜索

int main(){
    int i, j;
    Graph graph;
    CreateGraph(&graph);
    //打印鄰接矩陣
    for (i = 0; i < graph.numNodes; ++i){
        for (j = 0; j < graph.numNodes; ++j){
            printf("%d ", graph.arc[i][j]);
        }
        printf("\n");
    }

    VertexType* adj = FirstAdjVex(graph, 'A');
    VertexType x = *adj;
    printf("%c ", x);
    adj = NextAdjVex(graph, 'A', x);
    x = *adj;
    printf("%c ", x);
    x = *adj;
    adj = FirstAdjVex(graph, 'D');
    printf("%c\n", *adj);
    for (i = 0; i < 200; ++i){
        visited[i] = 0;
    }
    BFSTraversal(graph);
    printf("\n");
    for (i = 0; i < 200; ++i){  //訪問數組置空
        visited[i] = 0;
    }
    DFSTraversal(graph);
    return 0;
}

void CreateGraph(Graph* graph){
    int i, j;
    //先把圖的鄰接矩陣置爲0(0表示沒邊,1表示有邊)
    for (i = 0; i < graph->numNodes; ++i){
        for (j = 0; j < graph->numNodes; ++j){
            graph->arc[i][j] = 0;
        }
    }
    //printf("請輸入頂點數, 邊數:");
    //scanf("%d %d", &graph->numNodes, &graph->numEdges);
    //getchar();
    graph->numNodes = 6;
    graph->numEdges = 6;
    /*
    for (i = 0; i < graph->numNodes; ++i){
        printf("請輸入頂點:");
        scanf("%c", &graph->vexs[i]);
        getchar();  //消除空白符
    }
    */
    graph->vexs[0] = 'A';
    graph->vexs[1] = 'B';
    graph->vexs[2] = 'C';
    graph->vexs[3] = 'D';
    graph->vexs[4] = 'E';
    graph->vexs[5] = 'F';
    VertexType start, end;
    /*
    for (i = 0; i < graph->numEdges; ++i){
        printf("請輸入起點, 終點:");
        scanf("%c %c", &start, &end);
        getchar();  //消除空白符
        int startIndex, endIndex;
        for (j = 0; j < graph->numNodes; ++j){  //找到起始點,終點
            if (start == graph->vexs[j]){
                startIndex = j;
            }
            if (end == graph->vexs[j]){
                endIndex = j;
            }
        }
        graph->arc[startIndex][endIndex] = 1;
        //如果是無向圖,需要雙向保存
        graph->arc[endIndex][startIndex] = 1;
    }
    */
    graph->arc[0][2] = 1;
    graph->arc[0][3] = 1;
    graph->arc[3][1] = 1;
    graph->arc[2][4] = 1;
    graph->arc[3][5] = 1;
    graph->arc[4][5] = 1;
    //如果是無向圖,需要保存兩個邊
    /*
    graph->arc[2][0] = 1;
    graph->arc[3][0] = 1;
    graph->arc[1][3] = 1;
    graph->arc[4][2] = 1;
    graph->arc[5][3] = 1;
    graph->arc[5][4] = 1;
    */

}


VertexType* FirstAdjVex(Graph graph, VertexType vex){
    //先找到data這個結點
    int i, j;
    for (i = 0; i < graph.numNodes; ++i){
        if (graph.vexs[i] == vex){
            for (j = 0; j < graph.numNodes; ++j){
                if (graph.arc[i][j] == 1){  //找到第一個鄰接點
                    return &(graph.vexs[j]);
                }
            }
        }
    }
    return NULL;    //這步說明沒找到
}


VertexType* NextAdjVex(Graph graph, VertexType vex, VertexType adj){
    int vexIndex, adjIndex, i;
    for (i = 0; i < graph.numNodes; ++i){
        if (graph.vexs[i] == vex){
            vexIndex = i;
        }
        if (graph.vexs[i] == adj){
            adjIndex = i;
        }
    }
    for (i = adjIndex + 1; i < graph.numNodes; ++i){  //從當前鄰接點的後面尋找
        if (graph.arc[vexIndex][i] == 1){
            return &(graph.vexs[i]);
        }
    }
    return NULL;    //這步說明沒找到
}

/* 深度優先遍歷 */
void DFSTraversal(Graph graph){
    int i;
    for (i = 0; i < graph.numNodes; ++i){
        if (visited[graph.vexs[i]] != 1) {
            DFS(graph, graph.vexs[i]);
        }
    }
}

/* 廣度優先遍歷 */
void BFSTraversal(Graph graph){
    int i;
    for (i = 0; i < graph.numNodes; ++i){
        if (visited[graph.vexs[i]] != 1) {
            DFS(graph, graph.vexs[i]);
        }
    }
}

/*
    廣度優先搜索
*/
void BFS(Graph graph, VertexType vetex){
    Queue queue;
    InitQueue(&queue);
    EnQueue(&queue, vetex);
    VertexType adj;
    VertexType *p;
    visited[vetex] = 1;
    while (!IsEmpty(&queue)){
        DeQueue(&queue, &vetex);
        printf("%c ", vetex);
        for (p = FirstAdjVex(graph, vetex); p != NULL; p=NextAdjVex(graph, vetex, adj)){
            adj = *p; //應該可以寫成*p,但是不行
            if (visited[adj] != 1){
                EnQueue(&queue, adj);
                visited[adj] = 1;
            }
        }
    }
}

/*
    深度優先搜索
*/
void DFS(Graph graph, VertexType vex){
    visited[vex] = 1;
    printf("%c ", vex);
    VertexType *p;
    VertexType x;
    for (p = FirstAdjVex(graph, vex); p != NULL; p=NextAdjVex(graph, vex, x)){
        x = *p;
        if (visited[x] != 1){    //沒訪問過
            DFS(graph, x);
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章