breadth-first-search

Finding the shortest path nodes with breadth first search

痞子三分冷 提交于 2019-11-28 11:32:37
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(); shortestPathFound = (nextNode == nodeToBeFound) ? true : false; if(shortestPathFound)break; visitedNodes.add(nextNode)

Using BFS for topological sort

我只是一个虾纸丫 提交于 2019-11-28 09:19:19
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: queue.enqueue(c) stack.reverse() return stack The fact that they have similar names doesn't make them

BFS traversal of all paths in graph using adjacency list

无人久伴 提交于 2019-11-28 06:02:09
问题 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

Shortest path (fewest nodes) for unweighted graph

泄露秘密 提交于 2019-11-28 03:52:13
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<Node> q = new LinkedList<Node>(); Node current = start; q.add(current); while(!q.isEmpty()){ current =

Termination Criteria for Bidirectional Search

放肆的年华 提交于 2019-11-27 21:56:54
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 not be optimal, even if the two searches are both breadth-first; some additional search is required to

How to detect if a directed graph is cyclic?

…衆ロ難τιáo~ 提交于 2019-11-27 19:43:33
How can we detect if a directed graph is cyclic? I thought using breadth first search, but I'm not sure. Any ideas? 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. What you really need, I believe, is a topological sorting algorithm like the one described here: http://en.wikipedia.org/wiki

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

五迷三道 提交于 2019-11-27 11:15:12
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, so both from v to w and from w to v should be counted. linear-time. The graph is not weighted. From

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

流过昼夜 提交于 2019-11-27 10:20:51
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 Your sum v1 + (incident edges) + v2 + (incident edges) + .... + vn + (incident edges) can be rewritten as (v1 + v2 + ... + vn) + [(incident_edges v1) +

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

做~自己de王妃 提交于 2019-11-27 06:38:25
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. 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 misconception - it is simply not true. The classic DFS algorithm cannot be obtained by replacing the BFS queue with

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

╄→гoц情女王★ 提交于 2019-11-27 06:12:10
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? 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 to every node in the traversed graph. Meanwhile BFS basically just expands the search by one “step” (link,