depth-first-search

Even length path algorithm

风流意气都作罢 提交于 2019-12-22 09:16:08
问题 I was asked for my homework to write an efficient algorithm that finds all the vertices in a directed graph which have even length of path to them from the given vertex. This is what I thought of: (It's very similar to "Visit" algorithm of DFS) Visit(vertex u) color[u]<-gray for each v E adj[u] for each w E adj[v] if color[w] = white then print w Visit(w) I think it works but I'm having hard time calculating it's efficiency, especially when the graph is with cycles. Could you help me? 回答1: If

What's the best way to perform DFS on a very large tree?

倾然丶 夕夏残阳落幕 提交于 2019-12-22 08:16:35
问题 Here's the situation: The application world consists of hundreds of thousands of states. Given a state, I can work out a set of 3 or 4 other reachable states. A simple recursion can build a tree of states that gets very large very fast. I need to perform a DFS to a specific depth in this tree from the root state, to search for the subtree which contains the 'minimal' state (calculating the value of the node is irrelevant to the question). Using a single thread to perform the DFS works, but is

How can I calculate the level of a node in a perfect binary tree from its depth-first order index?

这一生的挚爱 提交于 2019-12-22 06:02:39
问题 I have a perfect binary tree, i.e. each node in the tree is either a leaf node, or has two children, and all leaf nodes are on the same level. Each node has an index in depth-first order. (E.g. in a tree with 3 levels the root node has index 0, the first child has 1, the first child of the first child has 2, the second child of the first child has 3, the second child has 4, the first child of the second child has 5, the second child of the second child has index 6. 0 / \ 1 4 / \ / \ 2 3 5 6 )

Peg solitaire – checking pegs vs. checking holes in a depth-first search

此生再无相见时 提交于 2019-12-22 05:37:28
问题 I am trying to solve Peg Solitaire with a depth-first search algorithm – it should be possible to solve the game since "modern computers can easily examine all game positions in a reasonable time". Even after 23 hours, the algorithm didn't find any solution. I did a web search and found the paper "Depth-first search solves peg solitaire". I tried the c-program from the paper and the first solution was found immediately after the program started. I compared the algorithms. The main difference

Stop boost::depth_first_search along a particular depth if certain criteria is met

醉酒当歌 提交于 2019-12-22 05:36:09
问题 I'm using BGL to store my DAG. Vertices have states. Given a change in state in one of the vertices i want to update dependent vertices. This i'm able to do using boost::depth_first_search and a custom visitor. Now the logic is that i dont want to update a searched vertex and its dependent if the vertex is in a particular state. Basically i want to control over en-queuing of vertices in either dfs or bfs. What is the best way to achieve this in BGL. Thanks. 回答1: It seems that boost::depth

How do I learn Tarjan's algorithm?

大兔子大兔子 提交于 2019-12-21 03:34:22
问题 I have been trying to learn Tarjan's algorithm from Wikipedia for 3 hours now, but I just can't make head or tail of it. :( http://en.wikipedia.org/wiki/Tarjan's_strongly_connected_components_algorithm#cite_note-1 Why is it a subtree of the DFS tree? (actually DFS produces a forest? o_O) And why does v.lowlink=v.index imply that v is a root? Can someone please explain this to me / give the intuition or motivation behind this algorithm? 回答1: The idea is: When traversing the tree, every time

Functional style early exit from depth-first recursion

拟墨画扇 提交于 2019-12-20 23:49:47
问题 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))

Genetic algorithm for optimization in game playing agent heuristic evaluation function

守給你的承諾、 提交于 2019-12-19 11:43:19
问题 This is in response to an answer given in this question: How to create a good evaluation function for a game?, particularly by @David (it is the first answer). Background : I am using a genetic algorithm to optimize the hyper parameters in a game playing agent that is using minimax / alpha beta pruning (with iterative deepening). In particular, I would like to optimize the heuristic (evaluation) function parameters using a genetic algorithm. The evaluation function I use is: f(w) = w * num_my

trying to find all the path in a graph using DFS recursive in Python

守給你的承諾、 提交于 2019-12-19 11:42:24
问题 I have found a solution that was posted some times ago and I have tried to apply it to my exercise but it doesn't work. I have a class graph that has nodes and edges and a method childrenOf that gives all the children of a node. All this works fine. This is my code for the DFS search and I want to find all the paths: def myDFS(graph,start,end,path=[]): path=path+[start] if start==end: return path paths=[] for node in graph.childrenOf(start): if node not in path: paths.extend(myDFS(graph,node

trying to find all the path in a graph using DFS recursive in Python

随声附和 提交于 2019-12-19 11:41:13
问题 I have found a solution that was posted some times ago and I have tried to apply it to my exercise but it doesn't work. I have a class graph that has nodes and edges and a method childrenOf that gives all the children of a node. All this works fine. This is my code for the DFS search and I want to find all the paths: def myDFS(graph,start,end,path=[]): path=path+[start] if start==end: return path paths=[] for node in graph.childrenOf(start): if node not in path: paths.extend(myDFS(graph,node