Tag Archives: BFS

Longest path in an undirected tree

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: Input : Below shown Tree using adjacency list representation: Output : 5 In below tree longest path is of length 5 from node 5 to node 7 This problem is… Read More »

Minimum step to reach one

Given a positive number N, we need to reach to 1 in minimum number of steps where a step is defined as converting N to (N-1) or converting N to its one of the bigger divisor. Formally, if we are at N, then in 1 step we can reach to (N – 1) or if… Read More »

Dinic’s algorithm for Maximum Flow

Problem Statement : Given a graph which represents a flow network where every edge has a capacity. Also given two vertices source ‘s’ and sink ‘t’ in the graph, find the maximum possible flow from s to t with following constraints : Flow on an edge doesn’t exceed the given capacity of the edge. Incoming… Read More »

Minimum steps to reach target by a Knight

Given a square chessboard of N x N size, the position of Knight and position of a target is given. We need to find out minimum steps a Knight will take to reach the target position. Examples: In above diagram Knight takes 3 step to reach from (4, 5) to (1, 1) (4, 5) ->… Read More »

Minimum number of operation required to convert number x into y

Given a initial number x and two operations which are given below: Multiply number by 2. Subtract 1 from the number. The task is to find out minimum number of operation required to convert number x into y using only above two operations. We can apply these operations any number of times. Constraints: 1 <=… Read More »

Nearest 1 in a binary matrix

Given a binary matrix of order m*n, the task is to find the distance of nearest 1 for each 0 in the matrix and print final distance matrix. From any cell (i,j), we can move only in four directions up, down, left and right. Note : Distance from one cell to immediate another cell is… Read More »