- Concerning Graph Part II: Depth
- Concerning Graph Part IV: Single
- Concerning Graph Part VI Symbol
- Concerning Graph Part III: Singl
- Concerning Graph Part V: Connect
- Concerning Graph Part VII Digrap
- Concerning Graph Part VIII Cycle
- Concerning Graph Part I: Basic I
- Concerning Graph Part IX Edge We
- Concerning Graph Part X MST Algo
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;
}
网友评论