# Bellman Ford

## Problem Statement

\
Given a weighted, directed and connected graph of V vertices and E edges, Find the shortest distance of all the vertex's from the source vertex S. If a vertices can't be reach from the S then mark the distance as 10^8. Note: If the Graph contains a negative cycle then return an array consisting of only -1.

**Example 1:**

<pre><code><strong>Input:
</strong>
<strong>E = [[0,1,9]]
</strong><strong>S = 0
</strong><strong>Output:
</strong>0 9
<strong>Explanation:
</strong>Shortest distance of all nodes from
source is printed.
</code></pre>

**Example 2:**

<pre><code><strong>Input:
</strong>
<strong>E = [[0,1,5],[1,0,3],[1,2,-1],[2,0,1]]
</strong><strong>S = 2
</strong><strong>Output:
</strong>1 6 0
<strong>Explanation:
</strong>For nodes 2 to 0, we can follow the path-
2-0. This has a distance of 1.
For nodes 2 to 1, we cam follow the path-
2-0-1, which has a distance of 1+5 = 6,
</code></pre>

&#x20;

**Your Task:**\
You don't need to read input or print anything. Your task is to complete the function **bellman\_ford( )** which takes a number of vertices **V** and an **E**-sized list of lists of three integers where the three integers are **u,v**, and **w**; denoting there's an edge from **u to v**, which has a weight of **w** and source node **S** as input parameters and returns a list of integers where the ith integer denotes the distance of an ith node from the source node.

If some node isn't possible to visit, then its distance should be 100000000(1e8). Also, If the Graph contains a negative cycle then return an array consisting of a single -1.\ <br>

## Intuition

```
Relax The edges V-1 times , Extra 1 to check cycle

TC = V*E
```

### Links

<https://practice.geeksforgeeks.org/problems/distance-from-the-source-bellman-ford-algorithm/1>

### Video Links

<https://www.youtube.com/watch?v=0vVofAhAYjc&ab_channel=takeUforward>

### Approach 1:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
vector<int> bellman_ford(int v, vector<vector<int>>& edges, int s) {

        vector<int> distance (v, 1e8);
        distance[s] = 0;

        for(int i = 0; i < v-1; i++) {
            for(auto &it: edges){
                int src = it[0];
                int des = it[1];
                int dis = it[2];

                if( distance[src] + dis < distance[des] )
                    distance[des] = distance[src] + dis;
            }
        }

        for(auto &it: edges){
            int src = it[0];
            int des = it[1];
            int dis = it[2];

            if( distance[src] + dis < distance[des] ){
                return {-1};
            }
        }

        return distance;
    }
```

{% endcode %}

### Approach 2:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
```

{% endcode %}

### Approach 3:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
```

{% endcode %}

### Approach 4:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
```

{% endcode %}

### Similar Problems

###


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://coding-9.gitbook.io/untitled/graph/shortest-path/bellman-ford.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
