## Introduction

Suppose you want to travel from one city to another then first you must see what is optimal route from set of routes so you can choose which has least distance/time. We call this route shortest path. To find shortest path we are given a directed graph G = {V, E}, a source “u” and a destination “v” vertex.

We can find single source shortest path to all destinations where we are given only source and we have to find shortest path to all destinations.

**Weights, Negative Weights and Cycles**

Weight is a value between two vertices or we can say value of an edge. However, we do not see negative weights in real-time scenarios but a graph can be given with negative weighted edges. If a graph has negative values still we can say it is well defined but if a graph has negative weighted cycle between vertex x and y then it can be said that there is no path between x and y so w(x, y) = -∞.

Whether a graph has positive or negative cycle we must remove all cycles to ease the calculation of shortest path.

## Implementation

We will create class for edge like below

public class Edge { public int U { get; set; } public int V { get; set; } public int Weight { get; set; } }

We have to write a method called Relax which sets minimum weight to a vertex and assigns parent to it

static void Relax(int u, int v, int weight, int[] distance, int[] parent) { if (distance[u] != int.MaxValue && distance[v] > distance[u] + weight) { distance[v] = distance[u] + weight; parent[v] = u; } }

Relax compares a destination weight with addition of weight of source vertex and weight of weight and sets which is smaller, given in figure below:

Now let’s implement Bellman-Ford’s algorithm

public static bool BellmanFord(List<Edge> edges, int vertexCount, int[] distance, int[] parent) { //Relaxing each edge by traversing from each vertex and so complexity is O(V.E) for (int i = 1; i < vertexCount; i++) { foreach (Edge edge in edges) { Relax(edge.U, edge.V, edge.Weight, distance, parent); } } //Checking if no negative-weight cycle exist foreach (Edge edge in edges) { if (distance[edge.U] != int.MaxValue && distance[edge.V] > distance[edge.U] + edge.Weight) { return false; } } return true; }

Algorithm returns true if there is path from source to all vertices else false. Now let's write method which prints path between two vertices

public static void PrintPath(int u, int v, int[] distance, int[] parent) { if (v < 0 || u < 0) { return; } if (v != u) { PrintPath(u, parent[v], distance, parent); Console.WriteLine("Vertax {0} weight: {1}", v, distance[v]); } else Console.WriteLine("Vertax {0} weight: {1}", v, distance[v]); }

Now let’s print the path and weights between vertex 0 and 2:

public static void Main(string[] args) { //Source vertex int source = 0; int[][] adjacencyMatrix = new int[][] { new int[] { 0,0,0,3,12 }, new int[] { 0,0,2,0,0 }, new int[] { 0,0,0,-2,0 }, new int[] { 0,5,3,0,0 }, new int[] { 0,0,7,0,0 } }; var numberOfVertex = adjacencyMatrix[0].Length; int[] distance = Enumerable.Repeat(int.MaxValue, numberOfVertex).ToArray(); int[] parent = Enumerable.Repeat(-1, numberOfVertex).ToArray(); distance[source] = 0; List<Edge> edges = new List<Edge>(); //Insering all edges in list for (int i = 0; i < adjacencyMatrix[0].Length; i++) for (int j = 0; j < adjacencyMatrix[0].Length; j++) { if (adjacencyMatrix[i][j] != 0) edges.Add(new Edge() { U = i, V = j, Weight = adjacencyMatrix[i][j] }); } //Calling Bellman-Ford Algorithm BellmanFord(edges, numberOfVertex, distance, parent); //Prints path PrintPath(0, 2, distance, parent); Console.ReadLine(); }

**Output**

Vertax 0 weight: 0

Vertax 3 weight: 3

Vertax 2 weight: 6

Algorithm creates shortest path as shown in below figure:

Algorithm finds shortest path of all routes from source and runs on **O (V.E)** time complexity.

## Comments: