bfs

Variable naming schemes for objects in C++?

Hey everyone, I am implementing a BFS, and what it is going to do is go through an ordered tree to find the shortest solution to a puzzle. What i will be doing is creating a Snapshot object that holds the current position of each piece in a puzzle. I will add this Snapshot object into the queue and check if it is the solution. However,...

inserting into a queue while enumerating it

I want to do a breadth first search of a tree using a Queue var q = new Queue<T>(); q.Enqueue(Root); foreach(T root in q) { foreach(T t in root.Children) q.Enqueue(t); } However I get a "Collection was modified after the enumerator was instantiated." Exception. Is there a C# type that I can do this with? Edit: a little rea...

is breadth first search or breadth first traversal possible without using a queue?

as i rememeber and checked, that the usual way for traversing a tree or crawling the web breadth first (BFS) is by using a queue. Is there actually a way to implement it not using a queue? ...

How do I stop the breadth-first search using Boost Graph Library when using a custom visitor?

Say I found the node that meets my criteria and I need to stop the search. ...

Please explain the algorithm for this problem from code jam 2009

This is problem from round 1B 2009 Problem C "Square Math". I know contest analysis is posted. But I am not getting on how to implement BFS when a node can be visited more than once. I could only implement using DFS. (because context is saved implicitly in recursive DFS). How to do that using BFS? ...

Tackling the 8-puzzle problem via BFS

I've heard that the 8-puzzle problem can be tackled via BFS, but I don't understand how. I wanna know the intermediate steps that I need to get from a board like this: 3 1 2 6 4 5 0 7 8 to 1 2 3 4 5 6 7 8 0 Are the intermediate steps "levels" on a BFS search? By the way, this is basic homework, I don't care about optimality. ...

Printing BFS (Binary Tree) in Level Order with _specific formatting_

Hello, To begin with, this question is not a dup of this one, but builds on it. Taking the tree in that question as an example, 1 / \ 2 3 / / \ 4 5 6 How would you modify your program to print it so, 1 2 3 4 5 6 rather than the general 1 2 3 4 5 6 I'm basically looking for intuitions on the most efficie...

Time complexity/MySQL performance analysis

Set up(MySQL): create table inRelation( party1 integer unsigned NOT NULL, party2 integer unsigned NOT NULL, unique (party1,party2) ); insert into inRelation(party1,party2) values(1,2),(1,3),(2,3),(1,4),(2,5),(3,5),(1,6),(1,7),(2,7),(5,7); mysql> select * from inRelation a -> join inRelation b on a.party2=b.party1 -...

Performing Breadth First Search recursively

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? ...

BFS traversal of directed graph from a given node

Hi, My understanding of basic BFS traversal for a graph is: BFS { Start from any node . Add it to que. Add it to visited array While(que is not empty) { remove head from queue. Print node; add all unvisited direct subchilds to que; mark them as visited } } However, if we have to traverse a DIRECTED graph from a...

Finding all the shortest paths between two nodes in unweighted directed graphs using BFS algorithm

Hi All, I am working on a problem that I need to find all the shortest path between two nodes in a given directed unweighted graph. I have used BFS algorithm to do the job, but unfortunately I can only print one shortest path not all of them, for example if they are 4 paths having lenght 3, my algorithm only prints the first one but I w...

Explain BFS and DFS in terms of backtracking

Wikipedia about DFS Depth-first search (DFS) is an algorithm for traversing or searching a tree, tree structure, or graph. One starts at the root (selecting some node as the root in the graph case) and explores as far as possible along each branch before backtracking. So is BFS? "an algorithm that choose a starting ...

Debugging BFS tree travesal algorithm

I'm working alone on this project and could use another set of eyes to look at this to see what I am doing wrong. The first loop runs infinitely. public void bfs(String start) { //Initial Case add_queue.add(start); graph.visit(start); Iterator<String> neighbors; String neighbor; w...

Difference between Breadth First Search, and Iterative deepening

I understand BFS, and DFS, but for the life of me cannot figure out the difference between iterative deepening and BFS. Apparently Iterative deepening has the same memory usage as DFS, but I am unable to see how this is possible, as it just keeps expanding like BFS. If anyone can clarify that would be awesome. tree to work on if require...

Efficiently finding the shortest path in large graphs

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. F...

When is it it practical to use DFS vs BFS?

Hi, 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? Thanks! ...

Python optimization with dfs/bfs searches

Can you see any immediately changes I can do to increase speed and optimization in the script below? We are suppose to write a script which implements dfs/bfs searches to find "ratatosk". Input is which function to use (dfs or bfs), number of nodes in the tree, root node, which node "ratatosk" is in and the rest of the node numbers. ...

left-right BFS travesal on a binary tree

Hi all, This is not a home work. I was thinking of some new questions in tree traversal and this seems to be very obvious so thought of solving it. Question is very similar to level order traversal like BFS. In BFS, we normally travel left to right in each level of tree, but here if we are travelling left to right at level i then leve...

How can I remember which data structures are used by DFS and BFS?

I always mix up whether I use a stack or a queue for DFS or BFS. Can someone please provide some intuition about how to remember which algorithm uses which data structure? Thanks!! ...