ScholarQuill logoScholarQuillUniversity Notes
  • Notes
  • Past Papers
  • Blogs
  • Todo
Login
ScholarQuill logoScholarQuillUniversity Notes
Login
NotesPast PapersBlogsTodo
More
SubjectsDiscussionCGPA CalculatorGPA CalculatorStudent PortalCourse Outline
About
About usPrivacy PolicyReportContact
Notes
Past Papers
Blogs
Todo
Analytics
    Current Subject
    🧩
    Analysis of Algorithms
    COMP4121
    Progress0 / 28 topics
    Topics
    1. Introduction2. Role of Algorithms in Computing3. Analysis on Nature of Input and Size of Input4. Asymptotic Notations5. Big-O Notation6. Big-Ω Notation7. Big-Θ Notation8. Little-o Notation9. Little-ω Notation10. Sorting Algorithm Analysis11. Loop Invariants12. Recursion and Recurrence Relations13. Algorithm Design Techniques14. Brute Force Approach15. Divide-and-Conquer Approach16. Merge Sort17. Quick Sort18. Greedy Approach19. Dynamic Programming20. Elements of Dynamic Programming21. Search Trees22. Heaps23. Hashing24. Graph Algorithms25. Shortest Paths26. Sparse Graphs27. String Matching28. Introduction to Complexity Classes
    COMP4121›Shortest Paths
    Analysis of AlgorithmsTopic 25 of 28

    Shortest Paths

    8 minread
    1,321words
    Intermediatelevel

    Shortest Path Algorithms

    The shortest path problem is a fundamental problem in graph theory. The goal is to find the shortest path between two vertices in a weighted graph, where the weights represent the cost or distance of traversing an edge. There are various algorithms to solve this problem depending on the graph's properties, such as whether the graph contains negative edge weights, or if you're looking for the shortest path from a single source to all other vertices or from a source to a destination.


    Types of Shortest Path Problems

    1. Single-Source Shortest Path (SSSP):

      • The goal is to find the shortest path from a given source vertex to all other vertices in the graph.
    2. Single-Destination Shortest Path:

      • The goal is to find the shortest path from all vertices to a single destination vertex.
    3. All-Pairs Shortest Path:

      • The goal is to find the shortest paths between all pairs of vertices in the graph.
    4. Shortest Path with Negative Weights:

      • This problem involves graphs that may contain edges with negative weights. In such cases, special algorithms are required.

    Key Algorithms for Shortest Path Problems

    1. Dijkstra’s Algorithm (for graphs with non-negative edge weights)

    • Dijkstra's Algorithm is one of the most widely used algorithms for finding the single-source shortest path in a graph with non-negative edge weights. It uses a greedy approach, choosing the next vertex that has the smallest tentative distance.

    Algorithm Steps:

    1. Initialize the distance to the source vertex as 0 and all other vertices as infinity.
    2. Mark the source vertex as visited and add it to the set of visited vertices.
    3. For each unvisited neighbor of the current vertex, update its tentative distance if a shorter path is found.
    4. Select the unvisited vertex with the smallest tentative distance, mark it as visited, and repeat the process until all vertices are visited.

    Time Complexity:

    • O(V2)O(V^2)O(V2) with a simple array or O(Vlog⁡V+Elog⁡V)O(V \log V + E \log V)O(VlogV+ElogV) using a priority queue (min-heap).

    Applications:

    • Routing algorithms in networking.
    • GPS systems for shortest paths.
    • Network optimization.

    Example: Given a graph with vertices A,B,C,DA, B, C, DA,B,C,D, and edges with weights, the algorithm would iteratively pick the vertex with the smallest distance and update the distances of its neighbors until all vertices are processed.


    2. Bellman-Ford Algorithm (for graphs with negative edge weights)

    • Bellman-Ford Algorithm is used to find the single-source shortest path in a graph, even if it contains negative edge weights. Unlike Dijkstra’s, Bellman-Ford can handle graphs with negative weights, and it can also detect negative weight cycles.

    Algorithm Steps:

    1. Initialize the distance to the source vertex as 0 and all other vertices as infinity.
    2. Relax all edges V−1V - 1V−1 times (where VVV is the number of vertices).
    3. Check for negative weight cycles by trying to relax the edges once more. If any distance changes, it means there is a negative weight cycle.

    Time Complexity:

    • O(VE)O(VE)O(VE), where VVV is the number of vertices and EEE is the number of edges.

    Applications:

    • Used in graphs where edges may have negative weights.
    • Detecting negative weight cycles in graphs.

    Example: In a graph with vertices A,B,C,DA, B, C, DA,B,C,D and edges that include negative weights, Bellman-Ford will iterate through all the edges and adjust the shortest path estimates for each vertex, ensuring no edge is left unprocessed for V−1V - 1V−1 iterations.


    3. Floyd-Warshall Algorithm (All-Pairs Shortest Path)

    • The Floyd-Warshall Algorithm is an algorithm for finding the shortest paths between all pairs of vertices in a graph. It works on both directed and undirected graphs and can handle negative edge weights (but not negative weight cycles).

    Algorithm Steps:

    1. Initialize a matrix where each element [i][j][i][j][i][j] represents the distance from vertex iii to vertex jjj. If there is no direct edge, set the value to infinity.
    2. Use three nested loops to check if a path through another vertex kkk provides a shorter path from iii to jjj. Update the matrix if a shorter path is found.

    Time Complexity:

    • O(V3)O(V^3)O(V3), where VVV is the number of vertices in the graph.

    Applications:

    • Finding shortest paths between all pairs of vertices.
    • Network routing.
    • Clustering and other problems in data analysis.

    Example: For a graph with vertices A,B,C,DA, B, C, DA,B,C,D, the algorithm starts with an initial distance matrix where direct edges are the initial distances and updates them by considering intermediate vertices. After running the algorithm, the matrix will contain the shortest paths between every pair of vertices.


    4. A Algorithm* (for finding the shortest path with a heuristic)

    • The A Algorithm* is an informed search algorithm that finds the shortest path from a source to a target vertex, using both the actual cost to reach a vertex and an estimated cost to reach the target. It is often used in pathfinding problems (like in games or GPS systems).

    Algorithm Steps:

    1. Maintain a priority queue of vertices, ordered by the sum of the current distance and a heuristic estimate of the distance to the target.
    2. At each step, select the vertex with the smallest sum (actual cost + heuristic) and explore its neighbors.
    3. If a shorter path is found to a neighbor, update its cost in the priority queue and continue until the target is reached.

    Time Complexity:

    • The time complexity depends on the heuristic and the graph, but in general, it is O(E)O(E)O(E), where EEE is the number of edges.

    Applications:

    • Pathfinding in games and robotics.
    • GPS navigation.
    • Map routing systems.

    Example: In a grid with obstacles, A* would explore paths towards the target vertex while considering both the traveled distance and the estimated distance to the goal using a heuristic function (e.g., Euclidean distance or Manhattan distance).


    Summary of Algorithms

    Algorithm Graph Type Edge Weights Time Complexity Use Case
    Dijkstra's Directed/Undirected Non-negative O(V2)O(V^2)O(V2) or O(Vlog⁡V+Elog⁡V)O(V \log V + E \log V)O(VlogV+ElogV) Shortest path from a source to all vertices
    Bellman-Ford Directed/Undirected Can handle negative O(VE)O(VE)O(VE) Single-source shortest path (negative weights)
    Floyd-Warshall Directed/Undirected Can handle negative O(V3)O(V^3)O(V3) All-pairs shortest path
    A Algorithm* Directed/Undirected Non-negative O(E)O(E)O(E) Pathfinding with heuristics (game, GPS)

    Conclusion

    Choosing the right shortest path algorithm depends on the graph's characteristics, such as whether it contains negative weights, the number of vertices and edges, and whether you are solving for single-source or all-pairs paths. Common algorithms like Dijkstra and Bellman-Ford are used in routing and network design, while Floyd-Warshall is often used in scenarios where you need the shortest path between all pairs of vertices. The A algorithm*, with its heuristic-based approach, is particularly effective in pathfinding applications such as games and navigation systems.

    Previous topic 24
    Graph Algorithms
    Next topic 26
    Sparse Graphs

    Past Papers

    Open this section to load past papers

    Click on Show Past Papers to see past papers.
    On This Page
      Reading Stats
      Est. reading time8 min
      Word count1,321
      Code examples0
      DifficultyIntermediate