breadth-first-search

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

女生的网名这么多〃 提交于 2019-12-17 23:04:01
问题 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? 回答1: Level by level traversal is known as Breadth-first traversal. Using a

Parallelizing a Breadth-First Search

∥☆過路亽.° 提交于 2019-12-17 20:24:15
问题 I just taught myself some OpenMP and this could be stupid. Basically I'm trying to parallelize a breadth first search program in c++, with each node taking a long time to process. Here's an example code: queue<node*> q; q.push(head); while (!q.empty()) { qSize = q.size(); for (int i = 0; i < qSize; i++) { node* currNode = q.front(); q.pop(); doStuff(currNode); q.push(currNode); } } The processing function doStuff() is quite expensive and I want to parallelize it. However if I parallelize the

Using BFS for topological sort

梦想的初衷 提交于 2019-12-17 18:41:00
问题 Can Breadth first Search be used for finding topological sorting of vertices and strongly connected components in a graph? If yes how to do that? and If not why not? we generally use Depth first search in these problems but What will be the problem if I try to implement using BFS? Will code like this work? def top_bfs(start_node): queue = [start_node] stack = [] while not queue.empty(): node = queue.dequeue() if not node.visited: node.visited = True stack.push(node) for c in node.children:

Finding the shortest path nodes with breadth first search

ε祈祈猫儿з 提交于 2019-12-17 16:39:09
问题 I am running breadth first search on the above graph to find the shortest path from Node 0 to Node 6 . My code public List<Integer> shortestPathBFS(int startNode, int nodeToBeFound){ boolean shortestPathFound = false; Queue<Integer> queue = new LinkedList<Integer>(); Set<Integer> visitedNodes = new HashSet<Integer>(); List<Integer> shortestPath = new ArrayList<Integer>(); queue.add(startNode); shortestPath.add(startNode); while (!queue.isEmpty()) { int nextNode = queue.peek();

Termination Criteria for Bidirectional Search

有些话、适合烂在心里 提交于 2019-12-17 16:16:11
问题 According to most of the reading I have done, a bidirectional search algorithm is said to terminate when the "forward" and "backward" frontiers first intersect. However, in Section 3.4.6 of Artificial Intelligence: A Modern Approach , Russel and Norvig state: Bidirectional search is implemented by replacing the goal test with a check to see whether the frontiers of the two searches intersect; if they do, a solution has been found. It is important to realize that the first solution found may

How to detect if a directed graph is cyclic?

泄露秘密 提交于 2019-12-17 15:43:19
问题 How can we detect if a directed graph is cyclic? I thought using breadth first search, but I'm not sure. Any ideas? 回答1: Usually depth-first search is used instead. I don't know if BFS is applicable easily. In DFS, a spanning tree is built in order of visiting. If a the ancestor of a node in the tree is visited (i.e. a back-edge is created), then we detect a cycle. See http://www.cs.nyu.edu/courses/summer04/G22.1170-001/6a-Graphs-More.pdf for a more detailed explanation. 回答2: What you really

Complete graph with only two possible costs. What's the shortest path's cost from 0 to N - 1

自闭症网瘾萝莉.ら 提交于 2019-12-17 13:56:01
问题 You are given a complete undirected graph with N vertices. All but K edges have a cost of A. Those K edges have a cost of B and you know them (as a list of pairs). What's the minimum cost from node 0 to node N - 1. 2 <= N <= 500k 0 <= K <= 500k 1 <= A, B <= 500k The problem is, obviously, when those K edges cost more than the other ones and node 0 and node N - 1 are connected by a K-edge. Dijkstra doesn't work. I've even tried something very similar with a BFS. Step1: Let G(0) be the set of

Why use Dijkstra's Algorithm if Breadth First Search (BFS) can do the same thing faster?

旧城冷巷雨未停 提交于 2019-12-17 08:06:28
问题 Both can be used to find the shortest path from single source. BFS runs in O(E+V) , while Dijkstra's runs in O((V+E)*log(V)) . Also, I've seen Dijkstra used a lot like in routing protocols. Thus, why use Dijkstra's algorithm if BFS can do the same thing faster? 回答1: Dijkstra allows assigning distances other than 1 for each step. For example, in routing the distances (or weights) could be assigned by speed, cost, preference, etc. The algorithm then gives you the shortest path from your source

Why use Dijkstra's Algorithm if Breadth First Search (BFS) can do the same thing faster?

巧了我就是萌 提交于 2019-12-17 08:06:27
问题 Both can be used to find the shortest path from single source. BFS runs in O(E+V) , while Dijkstra's runs in O((V+E)*log(V)) . Also, I've seen Dijkstra used a lot like in routing protocols. Thus, why use Dijkstra's algorithm if BFS can do the same thing faster? 回答1: Dijkstra allows assigning distances other than 1 for each step. For example, in routing the distances (or weights) could be assigned by speed, cost, preference, etc. The algorithm then gives you the shortest path from your source

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

社会主义新天地 提交于 2019-12-17 08:01:31
问题 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