Tag Archives: DFS

Stepping Numbers

Given two integers ‘n’ and ‘m’, find all the stepping numbers in range [n, m]. A number is called stepping number if all adjacent digits have an absolute difference of 1. 321 is a Stepping Number while 421 is not. Examples: Input : n = 0, m = 21 Output : 0 1 2 3… Read More »

Detect Cycle in a direct graph using colors

Given a directed graph, check whether the graph contains a cycle or not. Your function should return true if the given graph contains at least one cycle, else return false. For example, the following graph contains three cycles 0->2->0, 0->1->2->0 and 3->3, so your function must return true. Solution Depth First Traversal can be used… Read More »

BFS vs DFS for Binary Tree

What are BFS and DFS for Binary Tree? A Tree is typically traversed in two ways: Breadth First Traversal (Or Level Order Traversal) Depth First Traversals Inorder Traversal (Left-Root-Right) Preorder Traversal (Root-Left-Right) Postorder Traversal (Left-Right-Root) BFS and DFSs of above Tree Breadth First Traversal : 1 2 3 4 5 Depth First Traversals: Preorder Traversal… Read More »

Top 10 Algorithms and Data Structures for Competitive Programming

In this post “Important top 10 algorithms and data structures for competitive coding “. Topics : Graph algorithms Dynamic programming Searching and Sorting: Number theory and Other Mathematical Geometrical and Network Flow Algorithms Data Structures The below links cover all most  important algorithms and data structure topics: Graph Algorithms Breadth First Search (BFS) Depth First… Read More »

Iterative Depth First Traversal of Graph

Depth First Traversal (or Search) for a graph is similar to Depth First Traversal (DFS) of a tree. The only catch here is, unlike trees, graphs may contain cycles, so we may come to the same node again. To avoid processing a node more than once, we use a boolean visited array. For example, a… Read More »