Implement the Depth First Search Algorithm in Graph using Simple
- 时间:2020-09-19 10:45:07
- 分类:网络文摘
- 阅读:113 次
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) —
推荐阅读:山东卫视直播-山东卫视在线直播观看「高清」 辽宁卫视直播-辽宁卫视在线直播观看「高清」 吉林卫视直播-吉林卫视在线直播观看「高清」 黑龙江卫视直播-黑龙江卫视在线直播观看「高清」 内蒙古卫视直播-内蒙古卫视在线直播观看「高清」 重庆卫视直播-重庆卫视在线直播观看「高清」 广东卫视直播-广东卫视在线直播观看「高清」 深圳卫视直播-深圳卫视在线直播观看「高清」 四川卫视直播-四川卫视在线直播观看「高清」 厦门卫视直播-厦门卫视在线直播观看「高清」
- 评论列表
-
- 添加评论