# Tag Archives: BFS

## Shortest path to reach one prime to other by changing single digit at a timeNovember 19, 2017

Given two four digit prime numbers, suppose 1033 and 8179, we need to find the shortest path from 1033 to 8179 by altering only single… Read More »

## Number of days until all chocolates become unhealthyNovember 16, 2017

Pablo has square chocolate Box of size n x n in which a variety of healthy chocolates are present denoted by ‘H’ initially but he… Read More »

## Diameter of n-ary tree using BFS

N-ary tree refers to the rooted tree in which each node having atmost k child nodes. The diameter of n-ary tree is the longest path… Read More »

## Water Jug problem using BFSNovember 7, 2017

You are given a m litre jug and a n litre jug . Both the jugs are initially empty. The jugs don’t have markings to… Read More »

## BFS using STL for competitive codingOctober 20, 2017

A STL based simple implementation of BFS using queue and vector in STL. The adjacency list is represented using vectors of vector. In BFS, we… Read More »

## Minimum cells traversed to reach corner where every cell represents jumpsSeptember 1, 2017

Suppose A is at position (0, 0) of a 2-D grid containing ‘m’ rows and ‘n’ columns. His aim is to reach the bottom right… Read More »

## Print all paths from a given source to a destination using BFSAugust 12, 2017

Given a directed graph, a source vertex ‘src’ and a destination vertex ‘dst’, print all paths from given ‘src’ to ‘dst’. Consider the following directed… Read More »

## Minimum number of edges between two vertices of a GraphJune 21, 2017

You are given a undirected graph G(V, E) with N vertices and M edges. We need to find the minimum number of edges between a… Read More »

## Count nodes within K-distance from all nodes in a set

Given an undirected tree with some marked nodes and a positive number K. We need to print the count of all such nodes which have… Read More »

## Bidirectional SearchJune 11, 2017

Searching a graph is quite famous problem and have a lot of practical use. We have already discussed here how to search for a goal… Read More »

## BFS for Disconnected GraphMay 17, 2017

In previous post, BFS only with a particular vertex is performed i.e. it is assumed that all vertices are reachable from the starting vertex. But… Read More »

## Best First Search (Informed Search)May 3, 2017

Prerequisites : BFS, DFS In BFS and DFS, when we are at a node, we can consider any of the adjacent as next node. So… Read More »

## 0-1 BFS (Shortest Path in a Binary Weight Graph)April 23, 2017

Given a graph where every edge has weight as either 0 or 1. A source vertex is also given in the graph. Find the shortest… Read More »

## Design data structures for a very large social network like Facebook or LinkedlnApril 15, 2017

How would you design the data structures for a very large social network like Facebook or Linkedln? Describe how you would design an algorithm to… Read More »

## Longest path in an undirected treeMarch 5, 2017

Given an undirected tree, we need to find the longest path of this tree where a path is defined as a sequence of nodes. Example:… Read More »