breadth-first-search

How to find the number of different shortest paths between two vertices, in directed graph and with linear-time?

三世轮回 提交于 2019-11-26 15:28:20
问题 Here is the exercise: Let v and w be two vertices in a directed graph G = (V, E). Design a linear-time algorithm to find the number of different shortest paths (not necessarily vertex disjoint) between v and w. Note: the edges in G are unweighted For this excise, I summarise as follows: It is a directed graph It asks for the number of different shortest paths . First, the paths should be shortest, then there might be more than one such shortest paths whose length are the same. between v and w

Why is the time complexity of both DFS and BFS O( V + E )

吃可爱长大的小学妹 提交于 2019-11-26 15:07:10
问题 The basic algorithm for BFS: set start vertex to visited load it into queue while queue not empty for each edge incident to vertex if its not visited load into queue mark vertex So I would think the time complexity would be: v1 + (incident edges) + v2 + (incident edges) + .... + vn + (incident edges) where v is vertex 1 to n Firstly, is what I've said correct? Secondly, how is this O(N + E) , and intuition as to why would be really nice. Thanks 回答1: Your sum v1 + (incident edges) + v2 +

Why is depth-first search claimed to be space efficient?

和自甴很熟 提交于 2019-11-26 12:05:44
问题 In an algorithms course I\'m taking, it\'s said that depth-first search (DFS) is far more space efficient than breadth-first search (BFS). Why is that? Although they are basically doing the same thing, in DFS we\'re stacking the current node\'s successors while in BFS we\'re enqueueing the successors. 回答1: Your confusion is stemming from the fact that you apparently assume that DFS algorithm can be obtained from BFS algorithm by replacing the FIFO queue with a LIFO stack. This is a popular

Finding all the shortest paths between two nodes in unweighted undirected graph

喜夏-厌秋 提交于 2019-11-26 11:58:39
问题 I need help finding all the shortest paths between two nodes in an unweighted undirected graph . I am able to find one of the shortest paths using BFS, but so far I am lost as to how I could find and print out all of them. Any idea of the algorithm / pseudocode I could use? 回答1: As a caveat, remember that there can be exponentially many shortest paths between two nodes in a graph. Any algorithm for this will potentially take exponential time. That said, there is a relatively straightforward

Why DFS and not BFS for finding cycle in graphs

前提是你 提交于 2019-11-26 11:48:41
问题 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. 回答1: 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

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

瘦欲@ 提交于 2019-11-26 11:45:18
问题 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] 回答1: 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',

How do implement a breadth first traversal?

别来无恙 提交于 2019-11-26 09:18:12
问题 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

Performing Breadth First Search recursively

一个人想着一个人 提交于 2019-11-26 08:43:49
问题 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? 回答1: (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

How can I find the actual path found by BFS?

。_饼干妹妹 提交于 2019-11-26 05:37:44
问题 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

Breadth First Vs Depth First

点点圈 提交于 2019-11-26 03:46:43
问题 When Traversing a Tree/Graph what is the difference between Breadth First and Depth first? Any coding or pseudocode examples would be great. 回答1: These two terms differentiate between two different ways of walking a tree. It is probably easiest just to exhibit the difference. Consider the tree: A / \ B C / / \ D E F A depth first traversal would visit the nodes in this order A, B, D, C, E, F Notice that you go all the way down one leg before moving on. A breadth first traversal would visit