Implement the Depth First Search Algorithm in Graph using Simple
- 时间:2020-09-19 10:45:07
- 分类:网络文摘
- 阅读:118 次
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) —
推荐阅读:诗词名句鉴赏:我思古人,实获我心。 诗词名句鉴赏:投我以木桃,报之以琼瑶。 诗词名句鉴赏:知我者,谓我心忧;不知我者,谓我何求。 齐桓公伐楚盟屈完原文及翻译 曹刿论战原文及翻译 数学题:组成只能被3整除不能被9整除的4位数 数学题:将某款服装按标价打8折出售,仍可盈利10% 数学题:把一根长30cm,底面半径是8cm的圆木平均锯成4段 数学题:甲乙两种皮鞋的原价相同,换季时 奥数题:1994年“世界杯”足球赛中
- 评论列表
-
- 添加评论