美文网首页
Concerning Graph Part II: Depth

Concerning Graph Part II: Depth

作者: 刘煌旭 | 来源:发表于2021-01-06 23:35 被阅读0次

    This post gives the DFS impl, based on which, several other search problem concerning graph will be developed.

    #include "graph.c"
    typedef struct DepthFirstSearchStruct {
        bool *marked;
        int count;
    }*DepthFirstSearch;
    
    void DepthFirstSearchSearch(DepthFirstSearch dfs, Graph g, int v) {
        dfs->marked[v] = true;
        dfs->count++;
        int n;
        int *adj = GraphAdjcency(g, v, &n);
        for (int i = 0; i < n; i++) { if (!dfs->marked[adj[i]]) { DepthFirstSearchSearch(dfs, g, adj[i]); } }
        if (adj != NULL) free(adj);
    }
    
    bool DepthFirstSearchMarked(DepthFirstSearch dfs, int w) { return dfs->marked[w]; }
    
    int DepthFirstSearchCount(DepthFirstSearch dfs) { return dfs->count; }
    
    DepthFirstSearch DepthFirstSearchCreate(Graph g, int s) {
        DepthFirstSearch dfs = (DepthFirstSearch)malloc(sizeof(*dfs));
        dfs->marked = (bool*)malloc(sizeof(bool) * g->v);
        for (int i = 0; i < g->v; i++) { dfs->marked[i] = false; }
        dfs->count = 0;
        DepthFirstSearchSearch(dfs, g, s);
        return dfs;
    }
    

    相关文章

      网友评论

          本文标题:Concerning Graph Part II: Depth

          本文链接:https://www.haomeiwen.com/subject/nrwooktx.html