From 5d697a3de1eb81c668f1c5c8f9c7d1ba76974d01 Mon Sep 17 00:00:00 2001 From: Adam Djellouli <37275728+djeada@users.noreply.github.com> Date: Mon, 23 Sep 2024 15:58:24 +0200 Subject: [PATCH] Update brain_teasers.md --- notes/brain_teasers.md | 45 ++++++++++-------------------------------- 1 file changed, 10 insertions(+), 35 deletions(-) diff --git a/notes/brain_teasers.md b/notes/brain_teasers.md index 62e252a..00e1d0c 100644 --- a/notes/brain_teasers.md +++ b/notes/brain_teasers.md @@ -240,41 +240,16 @@ Mastering algorithms is crucial for solving complex programming problems efficie ### Graph Algorithms -- **Breadth-First Search (BFS)**: - - - **Uses**: Finding the shortest path in unweighted graphs, level-order traversal. - - - **Implementation**: Use a queue to process nodes in a FIFO manner. - -- **Depth-First Search (DFS)**: - - - **Uses**: Topological sorting, cycle detection, path finding. - - - **Implementation**: Use recursion or a stack to explore as deep as possible before backtracking. - -- **Dijkstra's Algorithm**: - - - **Uses**: Shortest path in weighted graphs with non-negative weights. - - - **Implementation**: Use a min-heap to select the next node with the smallest tentative distance. - -- **Bellman-Ford Algorithm**: - - - **Uses**: Shortest path in graphs with negative weights, detecting negative cycles. - - - **Implementation**: Relax all edges \( |V| - 1 \) times, where \( |V| \) is the number of vertices. - -- **Floyd-Warshall Algorithm**: - - - **Uses**: All pairs shortest paths. - - - **Implementation**: Use a 2D matrix to store distances, update distances considering each vertex as an intermediate point. - -- **Union-Find Data Structure**: - - - **Uses**: Keeping track of disjoint sets, cycle detection in Kruskal's algorithm. - - - **Implementation**: Optimize with path compression and union by rank. +Below is a detailed comparison of commonly used graph algorithms: + +| **Algorithm/Technique** | **Uses** | **Implementation** | +|---------------------------------|-------------------------------------------------------------|--------------------------------------------------------------------------------------------------| +| **Breadth-First Search (BFS)** | Finding the shortest path in unweighted graphs, level-order traversal. | Use a queue to process nodes in a First In, First Out (FIFO) manner. | +| **Depth-First Search (DFS)** | Topological sorting, cycle detection, pathfinding. | Use recursion or a stack to explore as deep as possible before backtracking. | +| **Dijkstra's Algorithm** | Shortest path in weighted graphs with non-negative weights. | Use a min-heap (priority queue) to select the next node with the smallest tentative distance. | +| **Bellman-Ford Algorithm** | Shortest path in graphs with negative weights, detecting negative cycles. | Relax all edges V-1 times, where V is the number of vertices. | +| **Floyd-Warshall Algorithm** | All pairs shortest paths. | Use a 2D matrix to store distances, updating distances by considering each vertex as an intermediate point. | +| **Union-Find Data Structure** | Keeping track of disjoint sets, cycle detection in Kruskal's algorithm. | Optimize with path compression and union by rank for efficient merging of sets and lookups. | ## Sorting Algorithms