[數據結構]圖的DFS用棧消除遞歸的C語言簡單實現

大家都知道,我們通常做DFS是用遞歸的方式。
其實遞歸也就用到了棧。
那麼我們可以用棧來代替遞歸。
以下代碼是之前《棧的簡單實現》中的代碼,其中去掉了main函數,在函數聲明部分加上了static。

#include <stdio.h>
#include <stdlib.h>

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

typedef struct{
    int data[MAXSIZE];
    int top;    //棧頂指針
}Stack;

static void InitStack(Stack *s);   //初始化棧
static void Push(Stack *s, ElemType e);    //壓棧操作
static void Pop(Stack *s, ElemType *e); //出棧操作
static BOOL IsEmpty(Stack s);  //是否爲空

static void InitStack(Stack *s){
    s->top = -1;
}

static void Push(Stack *s, ElemType e){
    ++s->top;
    s->data[s->top] = e;
}

static void Pop(Stack *s, ElemType *e){
    *e = s->data[s->top];
    --s->top;
}

static BOOL IsEmpty(Stack s){
    if (s.top <= -1){
        return TRUE;
    }else{
        return FALSE;
    }
}
#include <stdio.h>
#include <stdlib.h>
#define INFINTE 65535
#define MAXSIZE 100
#include "stack.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 DFSByStack(Graph graph, VertexType vex);

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;
    }
    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) {
            DFSByStack(graph, graph.vexs[i]);
        }
    }
}

void DFSByStack(Graph graph, VertexType vex){
    VertexType x;
    VertexType *p;
    Stack stack;
    InitStack(&stack);
    Push(&stack, vex);
    visited[vex] = 1;
    printf("%c ", vex);
    while (!IsEmpty(stack)){
        ElemType e;
        Pop(&stack, &e);    //出棧
        for (p = FirstAdjVex(graph, e); p != NULL; p=NextAdjVex(graph, e, x)){
            x = *p;
            if (visited[x] != 1){    //沒訪問過
                Push(&stack, e);
                Push(&stack, x);//如果有未訪問過的就壓棧
                printf("%c ", x);
                visited[x] = 1;
                break;
            }
        }
    }

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章