Breadth First Search

Shortest path in an unweighted graph

Problem Statement Given the directed, connected and unweighted graph G, a Source and a Destination and the task to find the shortest path possible between two given vertices in the graph. Note: The path does not contain any cycle which means path have finite number of vertices. Example Input Expected Output Path : 0 3 Implementation in …

Shortest path in an unweighted graph Read More »

Shortest Path with Alternating Colors

Problem Statement Consider a directed graph, with nodes labelled 0, 1, …, n-1.  In this graph, each edge is either red or blue, and there could be self-edges or parallel edges. Each [i, j] in red_edges denotes a red directed edge from node i to node j.  Similarly, each [i, j] in blue_edges denotes a blue directed edge from node i to node j. Return an array answer of length n, where each answer[X] is the length of …

Shortest Path with Alternating Colors Read More »

Breadth First Traversal for a Graph

Breadth First Search (BFS) is a graph traversal algorithm that is used to traverse all the vertices of a graph in a breadthward motion. In other words, it starts the graph traversal from root node and explores all the neighboring nodes first instead of going deep. Example of Breadth First Traversal for a Graph Breadth First …

Breadth First Traversal for a Graph Read More »

Scroll to Top
[gravityforms id="5" description="false" titla="false" ajax="true"]