0% found this document useful (0 votes)
25 views5 pages

Beyond Syllabus - 1

The document discusses the Bellman-Ford algorithm for finding the shortest path between one source and one destination vertex in a weighted graph. It provides an explanation of the algorithm along with code to implement it in C++. The code finds the shortest path from vertex 2 to 3 in a sample graph with both positive and negative edge weights.

Uploaded by

Aman Goyal
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)
25 views5 pages

Beyond Syllabus - 1

The document discusses the Bellman-Ford algorithm for finding the shortest path between one source and one destination vertex in a weighted graph. It provides an explanation of the algorithm along with code to implement it in C++. The code finds the shortest path from vertex 2 to 3 in a sample graph with both positive and negative edge weights.

Uploaded by

Aman Goyal
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/ 5

BEYOND SYLLABUS

EXPERIMENT NO-1
AIM:
Write a program to compute shortest path for one source –one destination using Bellman ford
Algorithm.

SOFTWARE REQUIRED:
Turbo C

THEORY:
The Shortest Path problem involves finding a path from a source vertex to a destination vertex
which has the least length among all such paths.

One Source –one destination:


Bellman-Ford Algorithm
Bellman Ford algorithm helps us find the shortest path from a vertex to all other vertices of a
weighted graph. It is similar to Dijkstra's algorithm but it can work with graphs in which edges
can have negative weights.

Algorithm
Following are the detailed steps.

Input: Graph and a source vertex src.


Output: Shortest distance to all vertices from src. If there is a negative weight cycle, then
shortest distances are not calculated, negative weight cycle is reported.

1) This step initializes distances from source to all vertices as infinite and distance to source
itself as 0. Create an array dist[] of size |V| with all values as infinite except dist[src] where src is
source vertex.
2) This step calculates shortest distances. Do following |V|-1 times where |V| is the number of
vertices in given graph.
a) Do following for each edge u-v
If dist[v] >dist[u] + weight of edge uv, then update dist[v]
dist[v] = dist[u] + weight of edge uv
3) This step reports if there is a negative weight cycle in graph.
Do following for each edge u-v.

If dist[v] >dist[u] + weight of edge uv, then “Graph contains negative weight cycle”
The idea of step 3 is, step 2 guarantees shortest distances if graph doesn’t contain negative
weight cycle. If we iterate through all edges one more time and get a shorter path for any vertex,
then there is a negative weight cycle.

Figure 1:Directed Negative Weighted graph

C code for One Source –all destination:

#include<iostream>
#include <list>
using namespace std;

// A directed graph using adjacency list representation


class Graph
{
int V; // No. of vertices in graph
list<int> *adj; // Pointer to an array containing adjacency lists

// A recursive function used by printAllPaths()


void printAllPathsUtil(int , int , bool [], int [], int &);

public:
Graph(int V); // Constructor
void addEdge(int u, int v);
void printAllPaths(int s, int d);
};

Graph::Graph(int V)
{
this->V = V;
adj = new list<int>[V];
}

void Graph::addEdge(int u, int v)


{
adj[u].push_back(v); // Add v to u’s list.
}

// Prints all paths from 's' to 'd'


void Graph::printAllPaths(int s, int d)
{
// Mark all the vertices as not visited
bool *visited = new bool[V];

// Create an array to store paths


int *path = new int[V];
int path_index = 0; // Initialize path[] as empty

// Initialize all vertices as not visited


for (int i = 0; i< V; i++)
visited[i] = false;

// Call the recursive helper function to print all paths


printAllPathsUtil(s, d, visited, path, path_index);
}

// A recursive function to print all paths from 'u' to 'd'.


// visited[] keeps track of vertices in current path.
// path[] stores actual vertices and path_index is current
// index in path[]
void Graph::printAllPathsUtil(int u, int d, bool visited[],
int path[], int &path_index)
{
// Mark the current node and store it in path[]
visited[u] = true;
path[path_index] = u;
path_index++;

// If current vertex is same as destination, then print


// current path[]
if (u == d)
{
for (int i = 0; i<path_index; i++)
cout<< path[i] << " ";
cout<<endl;

}
else // If current vertex is not destination
{
// Recur for all the vertices adjacent to current vertex
list<int>::iterator i;
for (i = adj[u].begin(); i != adj[u].end(); ++i)
if (!visited[*i])
printAllPathsUtil(*i, d, visited, path, path_index);
}

// Remove current vertex from path[] and mark it as unvisited


path_index--;
visited[u] = false;
}

// Driver program
int main()
{
// Create a graph given in the above diagram
Graph g(4);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(0, 3);
g.addEdge(2, 0);
g.addEdge(2, 1);
g.addEdge(1, 3);

int s = 2, d = 3;
cout<< "Following are all different paths from " << s
<< " to " << d <<endl;
g.printAllPaths(s, d);

return 0;
}
OUTPUT:
Following are all different paths from 2 to 3
2013
203
213

CONCLUSION:
The Shortest Path problem involves finding a path from a source vertex to a destination vertex
which has the least length among all such paths between source and destination. In computer
network, there are many shortest path algorithms. If edges weight is negative then we use
Bellman Ford Algorithm Otherwise Dijkstra’s Shortest Path Algorithm. In this algorithm edges
weight may be positive or negative. The weight and shortest path is calculated repeatedly
between one source and all destination nodes (nodes except source node) until it is achieved.

DISCUSSION:

Q.1 What is graph traversing technique.?


Q.2 Data structure used in traversing of graph.
Q.3 What is minimum spanning tree algorithm in graph?
Q.4 Algorithm used for negative weight on graph.
Q.5 Write down the different category of graph.

You might also like