breadth-first-search

Find all nodes in a binary tree on a specific level (Interview Query)

左心房为你撑大大i 提交于 2019-11-30 23:53:14
I mean on a specific level, NOT up to that specific level. Could someone please check my modified BFS algorithm? (most of which is taken from Wikipedia) Queue levelorder(root, levelRequested){ int currentLevel = 0; q = empty queue q.enqueue(root) while not q.empty do{ if(currentLevel==levelRequested) return q; node := q.dequeue() visit(node) if(node.left!=null || node.right!=null){ currentLevel++; if node.left ≠ null q.enqueue(node.left) if node.right ≠ null q.enqueue(node.right) } } } I think a recursive solution would be much more concise: /* * node - node being visited * clevel - current

Finding the number of paths of given length in a undirected unweighted graph

大憨熊 提交于 2019-11-30 12:07:42
问题 'Length' of a path is the number of edges in the path. Given a source and a destination vertex, I want to find the number of paths form the source vertex to the destination vertex of given length k. We can visit each vertex as many times as we want, so if a path from a to b goes like this: a -> c -> b -> c -> b it is considered valid. This means there can be cycles and we can go through the destination more than once. Two vertices can be connected by more than one edge. So if vertex a an

What is breadth-first search useful for?

时光总嘲笑我的痴心妄想 提交于 2019-11-30 06:44:04
问题 Usually when I've had to walk a graph, I've always used depth-first search because of the lower space complexity. I've honestly never seen a situation that calls for a breadth-first search, although my experience is pretty limited. When does it make sense to use a breadth-first search? UPDATE : I suppose my answer here shows a situation where I've used a BFS (because I thought was a DFS). I'm still curious to know though, why it was useful in this case. 回答1: When you want to reach a node by

Finding the number of paths of given length in a undirected unweighted graph

冷暖自知 提交于 2019-11-30 02:07:07
'Length' of a path is the number of edges in the path. Given a source and a destination vertex, I want to find the number of paths form the source vertex to the destination vertex of given length k. We can visit each vertex as many times as we want, so if a path from a to b goes like this: a -> c -> b -> c -> b it is considered valid. This means there can be cycles and we can go through the destination more than once. Two vertices can be connected by more than one edge. So if vertex a an vertex b are connected by two edges, then the paths , a -> b via edge 1 and a -> b via edge 2 are

Topological order using bfs

风流意气都作罢 提交于 2019-11-29 14:57:13
The following question was found in Sedgewick and Wayne book about algorithms in java: 4.2.19 Topological sort and BFS. Explain why the following algorithm does not necessarily produce a topological order: Run BFS, and label the vertices by increasing distance to their respective source. I was trying to prove it finding a counter example. But, everytime I try, I get a topological order. I mean, I don't understand why this not work: If the source of a vertex comes before it, why don't we have a topological order? I think to prove it, we need to find a vertex that its source comes before, but I

BFS traversal of all paths in graph using adjacency list

坚强是说给别人听的谎言 提交于 2019-11-29 12:10:43
I am currently trying to traverse all paths from source to destination in a graph which uses adjacency matrix. I have been trying to do it in BFS way.Thanks for the help. I am getting only one path. How do I get to print other paths as well ? public class AllPossiblePaths { static int v; static ArrayList<Integer> adj[]; public AllPossiblePaths(int v) { this.v = v; adj = new ArrayList[v]; for (int i = 0; i < v; i++) { adj[i] = new ArrayList<>(); } } // add edge from u to v public static void addEdge(int u, int v) { adj[u].add(v); } public static void findpaths(int source, int destination) {

Level Order Traversal of a Binary Tree

时光毁灭记忆、已成空白 提交于 2019-11-29 06:51:26
void traverse(Node* root) { queue<Node*> q; Node* temp_node= root; while(temp_node) { cout<<temp_node->value<<endl; if(temp_node->left) q.push(temp_node->left); if(temp_node->right) q.push(temp_node->right); if(!q.empty()) { temp_node = q.front(); q.pop(); } else temp_node = NULL; } } The above posted code is my level order traversal code. This code works fine for me but One thing I dont like is I am explicitly initializing temp_node = NULL or I use break. But it does not seem to be a good code to me. Is there a neat implementation than this or how can I make this code better? void traverse

Efficiently finding the shortest path in large graphs

十年热恋 提交于 2019-11-28 21:23:22
问题 I'm looking to find a way to in real-time find the shortest path between nodes in a huge graph. It has hundreds of thousands of vertices and millions of edges. I know this question has been asked before and I guess the answer is to use a breadth-first search, but I'm more interested in to know what software you can use to implement it. For example, it would be totally perfect if it already exist a library (with python bindings!) for performing bfs in undirected graphs. 回答1: python-graph added

How would you print out the data in a binary tree, level by level, starting at the top?

[亡魂溺海] 提交于 2019-11-28 17:37:16
This is an interview question I think of a solution. It uses queue. public Void BFS() { Queue q = new Queue(); q.Enqueue(root); Console.WriteLine(root.Value); while (q.count > 0) { Node n = q.DeQueue(); if (n.left !=null) { Console.Writeln(n.left); q.EnQueue(n.left); } if (n.right !=null) { Console.Writeln(n.right); q.EnQueue(n.right); } } } Can anything think of better solution than this, which doesn't use Queue? CodeFusionMobile Level by level traversal is known as Breadth-first traversal. Using a Queue is the proper way to do this. If you wanted to do a depth first traversal you would use a

Using BFS for Weighted Graphs

女生的网名这么多〃 提交于 2019-11-28 17:35:28
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 this question. Consider a graph like this: A---(3)-----B | | \-(1)-C--(1)/ The shortest path from A to