depth-first-search

Functional style early exit from depth-first recursion

走远了吗. 提交于 2019-12-03 07:23:55
I have a question about writing recursive algorithms in a functional style. I will use Scala for my example here, but the question applies to any functional language. I am doing a depth-first enumeration of an n -ary tree where each node has a label and a variable number of children. Here is a simple implementation that prints the labels of the leaf nodes. case class Node[T](label:T, ns:Node[T]*) def dfs[T](r:Node[T]):Seq[T] = { if (r.ns.isEmpty) Seq(r.label) else for (n<-r.ns;c<-dfs(n)) yield c } val r = Node('a, Node('b, Node('d), Node('e, Node('f))), Node('c)) dfs(r) // returns Seq[Symbol]

Is the runtime of BFS and DFS on a binary tree O(N)?

无人久伴 提交于 2019-12-03 06:02:13
I realize that runtime of BFS and DFS on a generic graph is O(n+m), where n is number of nodes and m is number of edges, and this is because for each node its adjacency list must be considered. However, what is the runtime of BFS and DFS when it is executed on a binary tree? I believe it should be O(n) because the possible number of edges that can go out of a node is constant (i.e., 2). Please confirm if this is the correct understanding. If not, then please explain what is the correct time complexity of BFS and DFS on a binary tree? The time complexities for BFS and DFS are just O(|E|) , or

Explanation of runtimes of BFS and DFS

为君一笑 提交于 2019-12-03 03:37:04
问题 Why are the running times of BFS and DFS O(V+E), especially when there is a node that has a directed edge to a node that can be reached from the vertex, like in this example in the following site http://www.personal.kent.edu/~rmuhamma/Algorithms/MyAlgorithms/GraphAlgor/depthSearch.htm 回答1: E is the set of all edges in the graph, as G={V,E}. So, |E| is count of all edges in the graph. This alone should be enough to convince you that the overall complexity can't be |V| times |E|, since we are

Javascript Tree Traversal Algorithm

我与影子孤独终老i 提交于 2019-12-03 03:26:11
I need help traversing a tree structure in a depth first fashion. I can't come up with an algorithm to do it properly. My input is this: [ ["A", "B", "C"], ["1", "2"], ["a", "b", "c", "d"] ] The output should take the form: [ "A/1/a", "A/1/b", "A/1/c", "A/1/d", "A/2/a", "A/2/b", "A/2/c", "A/2/d", "B/1/a", "B/1/b", "B/1/c", "B/1/d", "B/2/a", "B/2/b", "B/2/c", "B/2/d", "C/1/a", "C/1/b", "C/1/c", "C/1/d", "C/2/a", "C/2/b", "C/2/c", "C/2/d" ] icyrock.com This should do the job: function traverse(arr) { var first = arr[0]; var ret = []; if (arr.length == 1) { for (var i = 0; i < first.length; i++)

How to properly label branches of a tree in a depth first search

痞子三分冷 提交于 2019-12-03 03:22:17
I have a tree with a structure like this: __2__3__4 / \__5__6 0__1___7/__8__9 \\ \\__10__11__12 \__ __ __ 13 14 15 Node 1 has four children (2,7,10,13), nodes 2 and 7 have two children each (both sharing node 5 as a child). What I am trying to do is create a CTE that provide records containing the parent node, the node, the distance away from the root, and the branch (or fork) its contained in. IF (OBJECT_ID('tempdb..#Discovered') IS NOT NULL) BEGIN DROP TABLE #Discovered END CREATE TABLE #Discovered ( ID int PRIMARY KEY NOT NULL, Predecessor int NULL, OrderDiscovered int ); INSERT INTO

Explain BFS and DFS in terms of backtracking

安稳与你 提交于 2019-12-03 03:13:31
问题 Wikipedia about Depth First Search: Depth-first search (DFS) is an algorithm for traversing or searching a tree, tree structure, or graph. One starts at the root (selecting some node as the root in the graph case) and explores as far as possible along each branch before backtracking. So what is Breadth First Search? "an algorithm that choose a starting node, checks all nodes backtracks , chooses the shortest path, chose neighbour nodes backtracks , chose the shortest path, finally finds the

Is A* the best pathfinding algorithm?

青春壹個敷衍的年華 提交于 2019-12-03 01:48:43
问题 It is generally said that A* is the best algorithm to solve pathfinding problems. Is there any situation when A* is not the best algorithm to find solution? How good is A* compared to BFS, DFS, UCS, etc? 回答1: The short answer is yes, there are situations in which A* is not the best algorithm to solve a problem. However, there are a number of ways to assess what constitutes the best algorithm for finding a solution. If you are considering best in terms of performance of multiple searches from

How to implement a breadth first search to a certain depth?

独自空忆成欢 提交于 2019-12-02 23:16:33
I understand and can easily implement BFS. My question is, how can we make this BFS limited to a certain depth? Suppose, I just need to go 10 level deep. You can do this with constant space overhead. BFS has the property that unvisited nodes in the queue all have depths that never decrease, and increase by at most 1. So as you read nodes from the BFS queue, you can keep track of the current depth in a single depth variable, which is initially 0. All you need to do is record which node in the queue corresponds to the next depth increase. You can do this simply by using a variable

Time Complexity of modified dfs algorithm

此生再无相见时 提交于 2019-12-02 22:34:08
问题 I want to write an algorithm that finds an optimal vertex cover of a tree in linear time O(n), where n is the number of the vertices of the tree. A vertex cover of a graph G=(V,E) is a subset W of V such that for every edge (a,b) in E, a is in W or b is in W. In a vertex cover we need to have at least one vertex for each edge. If we pick a non-leaf, it can cover more than one edge. That's why I thought we can do it as follows: We visit the root of the tree, then we visit one of its children,

Recording predecessors in a DFS search in an undirected graph

笑着哭i 提交于 2019-12-02 18:04:41
问题 I was trying to use the code from this thread: Boost DFS back_edge, to record the cycles in an undirected graph. To do this I need to store the predecessors for each dfs tree when it finds a back_edge. Since this is an undirected graph I think we can not use directly on_back_edge() from EventVisitor Concept. So I was thinking to record the predecessors in the void back_edge() method of below code. But I am not sure how to do this and return the cycle vertices. Here is the code and the part I