Implement the Depth First Search Algorithm in Graph using Simple
- 时间:2020-09-19 10:45:07
- 分类:网络文摘
- 阅读:145 次
Given a graph represented by G(V, E) where V is the vertices and E represents the edges, we can do a Depth First Search Algorithm (DFS) on any node/vertex. The DFS will mark the current node visited and visit the node using using the (*visit) function (C++ function pointer), and recursively call itself with the connected edges.
1 2 3 4 5 6 7 8 9 10 | void traverseDepthFirstSearch(int node, void(*visit)(int)) { link t; (*visit)(k); visited[k] = 1; // mark the node as visited for (t = adj[k]; t != NULL; t = t->next) { if (!visited[t->v]) { // avoid cycle traverseDepthFirstSearch(t->v, visit); } } } |
void traverseDepthFirstSearch(int node, void(*visit)(int)) {
link t;
(*visit)(k);
visited[k] = 1; // mark the node as visited
for (t = adj[k]; t != NULL; t = t->next) {
if (!visited[t->v]) { // avoid cycle
traverseDepthFirstSearch(t->v, visit);
}
}
}
three-nodes
The algorithimic complexity is O(N) where N is the number of nodes connected to the given vertex. The space complexity is also O(N).
–EOF (The Ultimate Computing & Technology Blog) —
推荐阅读:转基因食品是福还是祸? 保健养生:花生炖食最适宜 西红柿具有较强的防癌功效 蔡甸海欣教育 面对食品安全危机,你应有的态度! 竹笋的营养与食疗保健功能 膳食平衡健康新概念“伴侣食品” 老酸奶调查:由酸奶添明胶,营养价值不高 惩罚性判罚或许可治食品安全问题之根 食物中的必需氨基酸和非必需氨基酸
- 评论列表
-
- 添加评论