breadth-first-search

Why DFS and not BFS for finding cycle in graphs

∥☆過路亽.° 提交于 2019-11-27 06:00:43
Predominantly DFS is used to find a cycle in graphs and not BFS. Any reasons? Both can find if a node has already been visited while traversing the tree/graph. Mark Byers 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 you got there. Otherwise you might think you have found a cycle but in reality all you have is

How does a Breadth-First Search work when looking for Shortest Path?

非 Y 不嫁゛ 提交于 2019-11-27 05:52:55
I've done some research, and I seem to be missing one small part of this algorithm. I understand how a Breadth-First Search works, but I don't understand how exactly it will get me to a specific path, as opposed to just telling me where each individual node can go. I guess the easiest way to explain my confusion is to provide an example: So for instance, let's say I have a graph like this: And my goal is to get from A to E (all edges are unweighted). I start at A, because that's my origin. I queue A, followed by immediately dequeueing A and exploring it. This yields B and D, because A is

How to trace the path in a Breadth-First Search?

◇◆丶佛笑我妖孽 提交于 2019-11-27 05:49:17
How do you trace the path of a Breadth-First Search, such that in the following example: If searching for key 11 , return the shortest list connecting 1 to 11. [1, 4, 7, 11] qiao You should have look at http://en.wikipedia.org/wiki/Breadth-first_search first. Below is a quick implementation, in which I used a list of list to represent the queue of paths. # graph is in adjacent list representation graph = { '1': ['2', '3', '4'], '2': ['5', '6'], '5': ['9', '10'], '4': ['7', '8'], '7': ['11', '12'] } def bfs(graph, start, end): # maintain a queue of paths queue = [] # push the first path into

When is it practical to use Depth-First Search (DFS) vs Breadth-First Search (BFS)?

时光总嘲笑我的痴心妄想 提交于 2019-11-27 02:28:17
I understand the differences between DFS and BFS, but I'm interested to know when it's more practical to use one over the other? Could anyone give any examples of how DFS would trump BFS and vice versa? Hans-Peter Störr That heavily depends on the structure of the search tree and the number and location of solutions (aka searched-for items). If you know a solution is not far from the root of the tree, a breadth first search (BFS) might be better. If the tree is very deep and solutions are rare, depth first search (DFS) might take an extremely long time, but BFS could be faster. If the tree is

Using BFS for Weighted Graphs

℡╲_俬逩灬. 提交于 2019-11-27 00:37:30
问题 I was revising single source shortest path algorithms and in the video, the teacher mentions that BFS/DFS can't be used directly for finding shortest paths in a weighted graph (I guess everyone knows this already) and said to work out the reason on your own. I was wondering the exact reason/explanation as to why it can't be used for weighted graphs. Is it due to the weights of the edges or anything else ? Can someone explain me as I feel a little confused. PS: I went through this question and

Shortest path (fewest nodes) for unweighted graph

故事扮演 提交于 2019-11-27 00:11:57
问题 I'm trying build a method which returns the shortest path from one node to another in an unweighted graph. I considered the use of Dijkstra's but this seems a bit overkill since I only want one pair. Instead I have implemented a breadth-first search, but the trouble is that my returning list contains some of the nodes that I don't want - how can I modify my code to achieve my goal? public List<Node> getDirections(Node start, Node finish){ List<Node> directions = new LinkedList<Node>(); Queue

How do implement a breadth first traversal?

╄→гoц情女王★ 提交于 2019-11-27 00:03:21
This is what I have. I thought pre-order was the same and mixed it up with depth first! import java.util.LinkedList; import java.util.Queue; public class Exercise25_1 { public static void main(String[] args) { BinaryTree tree = new BinaryTree(new Integer[] {10, 5, 15, 12, 4, 8 }); System.out.print("\nInorder: "); tree.inorder(); System.out.print("\nPreorder: "); tree.preorder(); System.out.print("\nPostorder: "); tree.postorder(); //call the breadth method to test it System.out.print("\nBreadthFirst:"); tree.breadth(); } } class BinaryTree { private TreeNode root; /** Create a default binary

Performing Breadth First Search recursively

跟風遠走 提交于 2019-11-26 23:36:14
Let's say you wanted to implement a breadth-first search of a binary tree recursively . How would you go about it? Is it possible using only the call-stack as auxiliary storage? Tanzelax (I'm assuming that this is just some kind of thought exercise, or even a trick homework/interview question, but I suppose I could imagine some bizarre scenario where you're not allowed any heap space for some reason [some really bad custom memory manager? some bizarre runtime/OS issues?] while you still have access to the stack...) Breadth-first traversal traditionally uses a queue, not a stack. The nature of

When is it practical to use Depth-First Search (DFS) vs Breadth-First Search (BFS)?

孤街浪徒 提交于 2019-11-26 22:14:10
问题 I understand the differences between DFS and BFS, but I'm interested to know when it's more practical to use one over the other? Could anyone give any examples of how DFS would trump BFS and vice versa? 回答1: That heavily depends on the structure of the search tree and the number and location of solutions (aka searched-for items). If you know a solution is not far from the root of the tree, a breadth first search (BFS) might be better. If the tree is very deep and solutions are rare, depth

How can I find the actual path found by BFS?

孤者浪人 提交于 2019-11-26 17:46:38
The problem I'm trying to solve concerns a tree of MRT system. Each node can be connected to 4 points at most, which simplify thing by a lot. Here's my thought. struct stop { int path, id; stop* a; stop* b; stop* c; stop* d; }; I can write code to save all the information I need for BFS to search for all the points, but my main concern is that, even though BFS finds the point properly, how can I know its path? BFS will search each level, and when one of it reaches my destination, it will jump out of the run loop, and then, I will get a visited queue and an unvisited queue, how am i supposed to