0% found this document useful (0 votes)
5 views

Presentation 15

The document presents a detailed overview of the Bellman-Ford Algorithm, which is used to find the shortest path in weighted graphs, particularly those with negative weight edges. It compares the Bellman-Ford Algorithm with Dijkstra's Algorithm, highlighting its ability to detect negative weight cycles and its time complexity of O(VE). The document also outlines the working principle, algorithm steps, and complexity analysis, providing a comprehensive understanding of the algorithm's functionality and applications.

Uploaded by

rohangupta1258
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Presentation 15

The document presents a detailed overview of the Bellman-Ford Algorithm, which is used to find the shortest path in weighted graphs, particularly those with negative weight edges. It compares the Bellman-Ford Algorithm with Dijkstra's Algorithm, highlighting its ability to detect negative weight cycles and its time complexity of O(VE). The document also outlines the working principle, algorithm steps, and complexity analysis, providing a comprehensive understanding of the algorithm's functionality and applications.

Uploaded by

rohangupta1258
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

CA-1 PRESENTATION

Name : Rohan Gupta


Department : CSE(IOT)
Subject : DESIGN & ANALYSIS OF ALGORITHMS
Subject Code : PCCCS404
Semester : Fourth
University Roll No. : 13031423040
Reg. No. : 231300110058
TABLE OF CONTENTS

2️⃣ Comparison
3️⃣ Working 4️⃣ Algorithm
1️⃣ Introduction with Dijkstra’s
Principle Steps
Algorithm

6️⃣ Time 7️⃣ Best, Worst,


5️⃣ Pseudocode Complexity and Average
Analysis Case Analysis
BELLMAN-FORD ALGORITHM (INTRODUCTION)
1. What is the Bellman-Ford Algorithm?
❖The Bellman-Ford Algorithm is an algorithm used to find
the shortest path from a single source vertex to all other vertices in
a weighted graph.
❖It works even when the graph contains negative weight edges, which
Dijkstra’s algorithm cannot handle.
2. Why is it Important?
❖Unlike Dijkstra’s algorithm, Bellman-Ford can handle graphs
with negative weight edges.
❖It helps in detecting negative weight cycles, which can cause
infinite loops in path calculations.
❖Used in network routing protocols like the Routing Information
Protocol (RIP).
PROBLEM
STATEMENT
1. What Problem Does Bellman-Ford Solve?
• The algorithm finds the shortest path from a single source
vertex to all other vertices in a weighted graph.
• Unlike Dijkstra’s algorithm, it can handle graphs with negative
weight edges.
2. Challenges in Shortest Path Calculation:
Graphs with negative weight edges – Some graphs have edges with
negative weights, which can cause issues in traditional shortest path
algorithms.
Negative weight cycles – If a graph contains a cycle where the total
weight is negative, the shortest path can become infinitely small, making
the problem more complex.
Efficiency – The algorithm should be able to handle graphs with
multiple vertices and edges efficiently
COMPARISON WITH DIJKSTRA’S ALGORITHM

Feature Bellman-Ford Algorithm Dijkstra’s Algorithm 1. Key Differences


Handles Negative
Between Bellman-Ford
Yes No
Weights?
and Dijkstra
Detects Negative Weight
Yes No
Cycles?

O(V log V)(using priority


Time Complexity O(VE)
queue)

Works with Directed


Yes Yes
Graphs?

Algorithm Type Dynamic Programming Greedy Algorithm

Graphs with negative Graphs with non-negative


Best Used For
weights weights
2. When to Use Which Algorithm?
• Use Bellman-Ford when the graph contains negative weights or when
detecting negative weight cycles is required.
• Use Dijkstra when the graph has only non-negative weights and you
need a faster solution.
3. Example Scenario
• If a graph contains a negative weight edge, Dijkstra fails to
give the correct shortest path.
• Bellman-Ford, on the other hand, handles it correctly.
WORKING PRINCIPLE OF BELLMAN-FORD ALGORITHM
1. Basic Idea
•The algorithm relaxes all edges in the graph (V - 1) times, where V is the number
of vertices.
•In each iteration, it updates the shortest path estimate for each vertex.
•After (V - 1) iterations, all shortest paths are guaranteed to be found.

2. Key Concept: Edge Relaxation


•If there is an edge (u → v) with weight w, and the current known distance to v is
greater than the distance to u plus w, then update v’s distance.
•Mathematical Representation:

where d[v] is the shortest known distance to vertex v.


HOW THE ALGORITHM WORKS? (STEP-BY-STEP)
Initialize distances:
➢ Set the source vertex’s distance to 0.
➢Set all other vertices’ distances to ∞ (infinity).
Relax all edges (V - 1) times:
➢For each edge (u → v, weight w), update d[v] if
a shorter path is found.
Check for negative weight cycles:
➢If an edge (u → v, w) still reduces d[v], then the
graph contains a negative weight cycle.
ALGORITHM STEPS
1. Steps of Bellman-Ford Algorithm
1️⃣ Initialize distances
• Set the distance of the source vertex to 0.
• Set all other vertices’ distances to ∞ (infinity).
2️⃣ Relax all edges (V - 1 times)
• For each edge (u → v, weight w), check if a shorter path exists:

• Repeat this process V - 1 times (where V is the number of vertices).


3️⃣ Check for negative weight cycles
• Go through all edges one more time.
• If any edge (u → v, weight w) still reduces d[v], then a negative
weight cycle exists.
PSEUDOCODE OF BELLMAN-FORD
ALGORITHM
Step 1: Initialize distances
✓Set the source vertex distance to 0.
✓Set all other vertex distances to infinity (∞).
Step 2: Relax all edges (V - 1 times)
✓For each edge (u → v, weight w), update distance[v] if a shorter path is found.
Step 3: Detect negative weight cycles
✓If any distance can still be reduced, then the graph contains a negative weight
cycle.
Step 4: Return results
✓If no negative cycle exists, return the computed shortest path distances.
TIME COMPLEXITY ANALYSIS
Time Complexity Breakdown
The Bellman-Ford algorithm consists of two main steps:
1️⃣ Initialization → O(V)
• Setting all vertex distances to ∞ (except the source).
2️⃣ Edge Relaxation (V - 1 times) → O(VE)
• Each edge is checked and relaxed in every iteration.
• This process runs for V - 1 times (since in the worst case, the shortest
path can take V−1V−1 edges).
3️⃣ Negative Cycle Detection → O(E)O(E)
• One final pass over all edges to check for further relaxation.
Total Time Complexity:
O(VE)
• V = Number of vertices
• E = Number of edges
BEST, WORST, AND AVERAGE CASE COMPLEXITY

Case Time Complexity Explanation

If the shortest path is found before V−1V−1 iterations (early


Best Case O(E)O(E)
stopping).

Average Case O(VE)O(VE) The algorithm runs for V−1V−1 iterations in a general graph.

If the graph has negative weight edges but no negative cycles,


Worst Case O(VE)O(VE)
all V−1V−1 iterations are required.
REFERENCES

Books:
1."Algorithm Design" – Jon Kleinberg and Éva
Tardos
2."Algorithms" – Robert Sedgewick and Kevin
Wayne
Web Resources:
4. Geeks for Geeks – Bellman-Ford Algorithm
Explanation
5. Khan Academy – Shortest Path Algorithms
THANK YOU

You might also like