Skip to main content

Posts

Showing posts with the label graph theory

Breadth-First Search (BFS) Algorithm Explained with Examples and Applications

  What is Breadth-First Search (BFS)? Breadth-First Search (BFS) is a fundamental graph traversal algorithm that explores all nodes at the present depth level before moving on to nodes at the next depth level. It is widely used for solving shortest path problems in unweighted graphs, searching in trees, and analyzing connected components in graphs. BFS Algorithm Explanation BFS uses a  queue  data structure to keep track of the nodes to be explored next. The key idea is: Visit the starting node and enqueue it. While the queue is not empty: Dequeue a node from the front. Process the node (e.g., print or record). Enqueue all its adjacent unvisited neighbors. Algorithm Steps (for Binary Tree) Start with the root node and put it into the queue. While the queue is not empty: Remove the node from the queue. Process the current node. If the left child exists, enqueue it. If the right child exists, enqueue it. Algorithm Steps (for General Graph) Start with the given node and enqu...

What is Dijkstra's Algorithm? Explanation and Example

Common applications of Dijkstra's algorithm include: GPS navigation systems Computer network routing Game AI pathfinding Robotics and autonomous vehicles Map-based location recommendation services 1. How It Works Dijkstra's algorithm is based on a greedy approach and operates in the following steps: Set the distance of the starting node to 0, and all other nodes to ∞ $\text{(infinity)}$ . Select the node with the smallest tentative distance $\text{(initially the start node)}$ . For each neighboring node of the current node: 1) Calculate the tentative distance through the current node. 2) If this new distance is shorter than the currently recorded one, update it. 3) Mark the current node as visited (processed). 4) Repeat steps 2–4 until all nodes have been visited. Example Graph               Graph represented as an edge list: $\text{A -> B (1), A -> C (4)}$ $\text{B -> C (2), B -> D (5)}$ $\text{C -...