Posts

Showing posts from October, 2025

Graph Traversal & Tree Traversal

  Graph Traversal Graph traversal means visiting all the vertices (nodes) of a graph in a specific order, usually to process or search data. It helps in finding paths, connectivity, and other properties in graphs. There are two main traversal methods: Depth First Search (DFS) Breadth First Search (BFS)   1. Depth First Search (DFS) DFS explores as far as possible along each branch before backtracking. It uses a stack (can be implemented using recursion).   Algorithm (Using Recursion): DFS(G, v):     mark v as visited     for each neighbor u of v:         if u is not visited:             DFS(G, u) Steps: Start from any vertex. Visit and mark it as visited. Move to an adjacent unvisited vertex. Repeat until no unvisited adjacent vertex remains. Backtrack to the previous verte...

Data Structure

Image
  Data Structure Stack   A stack is a linear data structure that follows the LIFO (Last In, First Out) principle. That means the last element inserted is the first one to be removed.   Characteristics Linear data structure Follows LIFO order Supports insertion and deletion from only one end called the top Efficient for managing recursive function calls, undo operations, etc.   Basic Operations on Stack Operation Description push(x) Inserts an element x on top of the stack pop() Removes the top element of the stack peek() / top() Returns the top element isEmpty() Checks if the stack is empty isFull() Checks if the stack is full (for fixed size array implementation)     Stack Representation Stacks can be implemented in two ways: Using Arrays Using Linked Lists ...