depth-first-search

Javascript tree search algorithm which return subset of the tree which contains the found term and its parents

白昼怎懂夜的黑 提交于 2020-03-28 03:57:12
问题 I have a tree selection in which every node has a name. I want to search through the node names and return a subset of the tree which only contains the found nodes and its parents. Does anybody know an efficient algorithm in JavaScript for this problem which return the node with all of its parents. Here is an example. when user type a search term, example "Elephant" and the tree looks like this: Elephant Hamster Fish Duck Hamster dog Fish Elephant Fish Dog Fish Unicorn Elephant Elephant

detecting mutual edges in a graph

时光怂恿深爱的人放手 提交于 2020-02-05 04:37:05
问题 I have an adjacency list representation of a graph but it is not symmetric i.e,. if a node A has an edge to B , it is not true that B has an edge with A . I guess this will be a directional graph (digraph). What is a good way to detect all the bidirectional paths from a node. I know I can use DFS to detect the paths from a node to another nodes of the graph. I guess what I am looking for is a bidirectional DFS where only the bidirectional edges are taken into account. So one way to do that is

How to track the depth in this object graph depth-first search algorithm?

我只是一个虾纸丫 提交于 2020-02-03 09:42:27
问题 I have this code which iterates over a tree, doing a depth-first search. Every element is tackled exactly once. Very good. -(void)iterateOverTree:(TreeNode *)node { NSMutableArray * elements = [NSMutableArray array]; [elements addObject:node]; while([elements count]) { TreeNode * current = [elements objectAtIndex:0]; [self doStuffWithNode:current]; for(TreeNode * child in current.children) { [elements addObject:child]; } [elements removeLastObject]; } } BUT: How can I keep track of the

How to track the depth in this object graph depth-first search algorithm?

混江龙づ霸主 提交于 2020-02-03 09:41:25
问题 I have this code which iterates over a tree, doing a depth-first search. Every element is tackled exactly once. Very good. -(void)iterateOverTree:(TreeNode *)node { NSMutableArray * elements = [NSMutableArray array]; [elements addObject:node]; while([elements count]) { TreeNode * current = [elements objectAtIndex:0]; [self doStuffWithNode:current]; for(TreeNode * child in current.children) { [elements addObject:child]; } [elements removeLastObject]; } } BUT: How can I keep track of the

Python Search Algorithm from Implied Graphs

混江龙づ霸主 提交于 2020-02-02 13:35:06
问题 A little while ago I asked a question (depth first search algorithm in python), which was answered brilliantly by @6502. It's allowed me to think about this problem more precisely and leads to this follow up question. In the previous question you have an implied directed tree from a function like this: def neighbors1(node): "Returns neighboring nodes in directed tree" #some code yield node_1 #some more code yield node_2 #... yield node_n and a success criteria function like this: def goal

Same result for two arrays of counters of different algorithms

不想你离开。 提交于 2020-01-23 17:13:46
问题 I'm trying to build up a program comparing number of strokes of algorithms BFS, DFS, A* (which has two heuristics) on a game of fifteen. My counter count the same result for the two arrays of counters of BFS and DFS and both A*. Yet, I'm using actually using four different arrays from a main (class Project) and I'm assigning four different variables for those strokes. The part of the code that isn't right is, to my mind, a while loop which explores the son of a vertice as far as possible (for

Same result for two arrays of counters of different algorithms

≯℡__Kan透↙ 提交于 2020-01-23 17:12:30
问题 I'm trying to build up a program comparing number of strokes of algorithms BFS, DFS, A* (which has two heuristics) on a game of fifteen. My counter count the same result for the two arrays of counters of BFS and DFS and both A*. Yet, I'm using actually using four different arrays from a main (class Project) and I'm assigning four different variables for those strokes. The part of the code that isn't right is, to my mind, a while loop which explores the son of a vertice as far as possible (for

breadth first or depth first search

血红的双手。 提交于 2020-01-12 02:21:10
问题 I know how this algorithm works, but cant decide when to use which algorithm ? Are there some guidelines, where one better perform than other or any considerations ? Thanks very much. 回答1: If you want to find a solution with the shortest number of steps or if your tree has infinite height (or very large) you should use breadth first. If you have a finite tree and want to traverse all possible solutions using the smallest amount of memory then you should use depth first. If you are searching

What is the number of all possible non-cyclic simple paths in a fully-connected directed graph?

橙三吉。 提交于 2020-01-03 04:25:09
问题 Let's say we have a fully connected digraph G with N vertices and M edges. How many edges does the graph have? Is it M = N^2 ? If we take one vertex and start visiting its neighbors in a 'depth-first search' manner and avoiding loops, how many non-cyclic simple paths will we get? For example, if we start from vertex 1 in a graph of 4 vertices, here are the paths: - 1 - 1,2 - 1,3 - 1,4 - 1,2,3 - 1,2,4 - 1,3,2 - 1,3,4 - 1,4,2 - 1,4,3 Is it N! or more for a graph with N vertices? I could not

Find all possible paths from one vertex in a directed cyclic graph in Erlang

匆匆过客 提交于 2020-01-01 03:21:05
问题 I would like to implement a function which finds all possible paths to all possible vertices from a source vertex V in a directed cyclic graph G. The performance doesn't matter now, I just would like to understand the algorithm. I have read the definition of the Depth-first search algorithm, but I don't have full comprehension of what to do. I don't have any completed piece of code to provide here, because I am not sure how to: store the results (along with A->B->C-> we should also store A->B