depth-first-search

How to implement Depth-first search through a tree of objects?

点点圈 提交于 2019-12-11 06:54:41
问题 Looking for some code examples with explanation. Wikipedia is too abstract on this. 回答1: You could check the Wikipedia sources. Here's what I found for you: http://www.algolist.net/Algorithms/Graph/Undirected/Depth-first_search It gives example and some code sample. Pretty easy to follow. 回答2: Get Complete Implementation of DFS in Java using Tree here http://c-madeeasy.blogspot.com/2011/08/dfsdepth-first-search-program-in-java.html 来源: https://stackoverflow.com/questions/5780754/how-to

Can't we find Shortest Path by DFS(Modified DFS) in an unweighted Graph? and if not then Why?

馋奶兔 提交于 2019-12-11 06:08:37
问题 It is said that DFS can't be used to find the shortest path in the unweighted graph. I have read multiple post and blogs but not get satisfied as a little modification in DFS can make it possible. I think if we use Modified DFS in this way, then we can find the shortest distances from the source. Initialise a array of distances from root with infinity and distance of root from itself as 0. While traversing, we keep track of no. of edges. On moving forward increment no. of edges and while back

Trying to fix 3D mesh normals

有些话、适合烂在心里 提交于 2019-12-11 04:07:03
问题 I have triangle collection that define mesh surface of my 3D shape, I would like to fix normal of each triangle to point outshape. I was trying the following (pseudo): 1. define that first triangle normal direction is right direction 2. go over the mesh using kind of DFS like this: 3. triangle = first triangle 4. foreach neigbour in triangle.getNeighbours 5. if angle between neighbor and triangle greater then 180 do neighbor.flip() 6. triangle = neighbor 7. if neighbor already picked then

All possible paths

≯℡__Kan透↙ 提交于 2019-12-11 00:58:33
问题 I am currently working on an AI for playing the game Dots (link). The objective is to remove as many dots as possible by connecting similarly colored dots with a line. I've gone through the board and grouped each set of neighboring dots with the same color. The groups all currently share the same highlight color (black). So, for example, the four red dots in the top left form a single group, as do the three yellow dots on the top right. I need to calculate every possible path through one of

Find all paths in a maze using DFS [duplicate]

时光总嘲笑我的痴心妄想 提交于 2019-12-11 00:35:45
问题 This question already has answers here : Find all possible paths through a maze (2 answers) Closed 2 years ago . Given a source and a destination cell in a maze, I would like to find all possible solutions to a maze using DFS. 0 represents a wall in my maze. Visualise the maze I've successfully written a program, but that gives me only one path. I've thought of all the possibilities to extend it to all paths but sadly even after being hours(2 days to be precise) at it, I was unable to come up

Find first null in binary tree with limited memory

蹲街弑〆低调 提交于 2019-12-10 17:29:45
问题 I have a binary tree where each node can have a value. I want to find the node in the tree that has value null and is closest to the root. If there are two nodes with the same distance from the root, either will do. I need to minimize the number of read accesses to the binary tree. Assume that working memory is limited to just k nodes. DFS to depth k is exhaustive but will not find the closest node unless I run through the whole tree first. BFS will find the closest, but it might fail because

How to find if a graph has a cycle?

流过昼夜 提交于 2019-12-10 15:17:52
问题 I know this question has been asked many times in this forum and everywhere else in the internet. But before you attack me with your claws outstretched please bear with me. I am a newbie learning graph. As part of my excercise I am given to add a method hasCycle() in the Graph class here http://homepage.cs.uiowa.edu/~sriram/21/fall05/ExamplePrograms/ReaderFiles/Chap13/dfs/dfs.java. My approach, doing a DFS as suggested in this forum here Finding a cycle in an undirected graph vs finding one

How to detect if an undirected graph has a cycle and output it using BFS or DFS

心不动则不痛 提交于 2019-12-10 13:47:10
问题 The other question on this only answered how to detect a cycle, not also output it. So, I'd like to write an algorithm run BFS or DFS in O(V + E) time (V=vertices, E=edges), on an undirected graph, and output the cycle if it has one. What I know so far is how BFS/DFS works and that you can detect a cycle using BFS if you visit a node that already has been marked as visited. 回答1: To detect and output a cycle with DFS, just mark each vertex as you get to it; if any child of the current vertex

Network X , which depth searching option to select for this use?

北城以北 提交于 2019-12-10 11:45:56
问题 I am trying to ensure a global correct orientation of my normals. I.e make sure they point outwards everywhere. This is my method: I have a coordinate list x y c n . At each point in coordinate list I create edges from the point to its 5 nearest neighbours. I Weight each edge with ( 1-n1.n2 ). Compute a minimal spanning tree. Traverse this tree in depth first order. Start traversing at the point with the greatest z value, force the normal of this point to be orientated towards the positive z

Is backtracking absolutely necessary for cycle detection using DFS in directed graph?

痞子三分冷 提交于 2019-12-10 10:53:27
问题 I came across this SO post where it is suggested that cycle detection using DFS in a directed graph is faster because of backtracking. Here I quote from that link: Depth first search is more memory efficient than breadth first search as you can backtrack sooner. It is also easier to implement if you use the call stack but this relies on the longest path not overflowing the stack. Also if your graph is directed then you have to not just remember if you have visited a node or not, but also how