Programming paths / DSA Tutorial / Bellman-Ford and Floyd-Warshall Shortest Paths

Course contents

Foundations

  1. Data Structures and Algorithms: A Practical Roadmap
  2. Abstract Data Types and Data-Structure Operations
  3. A Repeatable DSA Problem-Solving Method
  4. Pseudocode, Loop Invariants, and Correctness
  5. Time Complexity and Input Growth
  6. Big O, Big Omega, and Big Theta
  7. Auxiliary Space and Memory Complexity
  8. Recursion, Base Cases, and Recurrence Relations

Arrays, Searching, and Sorting

  1. Arrays and Dynamic Arrays
  2. String Algorithms and Character Sequences
  3. Prefix Sums for Fast Range Queries
  4. Difference Arrays and Offline Range Updates
  5. Two-Pointer Techniques
  6. Sliding Window for Contiguous Subarrays
  7. Linear Search
  8. Binary Search on Sorted Data
  9. Lower Bound, Upper Bound, and Binary Search on Answers
  10. Bubble Sort
  11. Selection Sort and Insertion Sort
  12. Merge Sort
  13. Quicksort and Partitioning
  14. Heap Sort
  15. Counting Sort, Radix Sort, and Bucket Sort
  16. Sorting Stability, In-Place Behavior, and Comparers

Core Data Structures

  1. Singly, Doubly, and Circular Linked Lists
  2. Stacks, Queues, and Deques
  3. Monotonic Stacks and Monotonic Queues
  4. Hash Tables, Hash Maps, and Collision Handling
  5. Tree Terminology and Traversal Algorithms
  6. Binary Search Trees
  7. AVL Trees, Red-Black Trees, and Self-Balancing Search
  8. Binary Heaps and Priority Queues
  9. Tries and Prefix Search
  10. Fenwick Trees and Segment Trees

Graph Algorithms

  1. Graphs, Vertices, Edges, and Representations
  2. Breadth-First Search (BFS)
  3. Depth-First Search (DFS)
  4. Topological Sorting and Dependency Ordering
  5. Connected Components and Cycle Detection
  6. Dijkstra’s Shortest-Path Algorithm
  7. Bellman-Ford and Floyd-Warshall Shortest Paths
  8. Minimum Spanning Trees: Kruskal and Prim
  9. Disjoint Set Union (Union-Find)
  10. Strongly Connected Components
  11. A* Search and Heuristics
  12. Maximum Flow and Residual Graphs

Problem-Solving Paradigms

  1. Greedy Algorithms and the Exchange Argument
  2. Interval Scheduling and Meeting-Room Algorithms
  3. Huffman Coding and Prefix-Free Codes
  4. Divide and Conquer
  5. Backtracking, Pruning, and Search Trees
  6. N-Queens and Constraint-Based Backtracking
  7. Dynamic Programming: Overlapping Subproblems and State
  8. Memoization and Tabulation
  9. 0/1 Knapsack and Subset-Sum Dynamic Programming
  10. LCS, LIS, and Edit Distance
  11. Knuth-Morris-Pratt (KMP) Pattern Matching
  12. Rabin-Karp and the Z-Algorithm
  13. GCD, Sieve of Eratosthenes, and Fast Exponentiation
  14. Bit Manipulation and Integer Masks

Advanced Topics and Practice

  1. Amortized Analysis and Dynamic-Array Growth
  2. Randomized Algorithms and Randomized Quicksort
  3. Bloom Filters and Probabilistic Membership
  4. B-Trees and External-Memory Data Structures
  5. P, NP, NP-Complete, and Approximation Algorithms
  6. Testing, Benchmarking, and Adversarial Inputs
  7. DSA Capstone: Build a Searchable Route Planner

Data Structures and Algorithms • C# examples

Bellman-Ford and Floyd-Warshall Shortest Paths

Choose between single-source negative-edge handling and all-pairs dynamic programming.

Concept

Bellman-Ford relaxes every edge repeatedly and can detect a reachable negative cycle. Floyd-Warshall considers each vertex as an intermediate point to compute all-pairs distances. These algorithms handle negative edges under their stated conditions, unlike Dijkstra, but cost more time.

How to approach it

Bellman-Ford performs up to V - 1 relaxation passes; Floyd-Warshall updates distance[i,j] through each intermediate k.

Complexity

Bellman-Ford O(VE); Floyd-Warshall O(V³) time and O(V²) storage.

C# example

for (int pass = 0; pass < vertexCount - 1; pass++)
    foreach (var edge in edges)
        if (distance[edge.From] != long.MaxValue)
            distance[edge.To] = Math.Min(
                distance[edge.To], distance[edge.From] + edge.Weight);

for (int k = 0; k < vertexCount; k++)
    for (int i = 0; i < vertexCount; i++)
        for (int j = 0; j < vertexCount; j++)
            if (allPairs[i, k] != long.MaxValue &&
                allPairs[k, j] != long.MaxValue)
                allPairs[i, j] = Math.Min(
                    allPairs[i, j], allPairs[i, k] + allPairs[k, j]);

Expected result or behavior

Bellman-Ford relaxes single-source edges; Floyd-Warshall considers every vertex as an intermediate point.

Practice exercise

Describe the extra Bellman-Ford pass that reveals a reachable negative cycle, then trace one Floyd-Warshall update.

Key takeaway: Shortest-path algorithms are selected by graph size, edge signs, and query pattern.