1+ #include < iostream>
2+ #include < stdlib.h>
3+ #include < string.h>
4+ #include < limits.h>
5+
6+ using namespace std ;
7+
8+ struct Edge
9+ {
10+ // This structure is equal to an edge. Edge contains two end points. These edges are directed edges so they
11+ // contain source and destination and some weight. These 3 are elements in this structure
12+ int source, destination, weight;
13+ };
14+
15+ // a structure to represent a connected, directed and weighted graph
16+ struct Graph
17+ {
18+ int V, E;
19+ // V is number of vertices and E is number of edges
20+
21+ struct Edge * edge;
22+ // This structure contain another structure which we already created edge.
23+ };
24+
25+ struct Graph * createGraph (int V, int E)
26+ {
27+ struct Graph * graph = (struct Graph *) malloc ( sizeof (struct Graph ));
28+ // Allocating space to structure graph
29+
30+ graph->V = V; // assigning values to structure elements that taken form user.
31+
32+ graph->E = E;
33+
34+ graph->edge = (struct Edge *) malloc ( graph->E * sizeof ( struct Edge ) );
35+ // Creating "Edge" type structures inside "Graph" structure, the number of edge type structures are equal to number of edges
36+
37+ return graph;
38+ }
39+
40+ void FinalSolution (int dist[], int n)
41+ {
42+ // This function prints the final solution
43+ cout<<" \n Vertex\t Distance from Source Vertex\n " ;
44+ int i;
45+
46+ for (i = 0 ; i < n; ++i){
47+ cout<<i<<" \t\t " <<dist[i]<<" \n " ;
48+ }
49+ }
50+
51+ void BellmanFord (struct Graph * graph, int source)
52+ {
53+ int V = graph->V ;
54+
55+ int E = graph->E ;
56+
57+ int StoreDistance[V];
58+
59+ int i,j;
60+
61+ // This is initial step that we know , we initialize all distance to infinity except source.
62+ // We assign source distance as 0(zero)
63+
64+ for (i = 0 ; i < V; i++)
65+ StoreDistance[i] = INT_MAX;
66+
67+ StoreDistance[source] = 0 ;
68+
69+ // The shortest path of graph that contain V vertices, never contain "V-1" edges. So we do here "V-1" relaxations
70+ for (i = 1 ; i <= V-1 ; i++)
71+ {
72+ for (j = 0 ; j < E; j++)
73+ {
74+ int u = graph->edge [j].source ;
75+
76+ int v = graph->edge [j].destination ;
77+
78+ int weight = graph->edge [j].weight ;
79+
80+ if (StoreDistance[u] + weight < StoreDistance[v])
81+ StoreDistance[v] = StoreDistance[u] + weight;
82+ }
83+ }
84+
85+ // Actually upto now shortest path found. But BellmanFord checks for negative edge cycle. In this step we check for that
86+ // shortest distances if graph doesn't contain negative weight cycle.
87+
88+ // If we get a shorter path, then there is a negative edge cycle.
89+ for (i = 0 ; i < E; i++)
90+ {
91+ int u = graph->edge [i].source ;
92+
93+ int v = graph->edge [i].destination ;
94+
95+ int weight = graph->edge [i].weight ;
96+
97+ if (StoreDistance[u] + weight < StoreDistance[v])
98+ cout<<" \n This graph contains negative edge cycle\n " ;
99+ }
100+
101+ FinalSolution (StoreDistance, V);
102+
103+ return ;
104+ }
105+
106+ int main ()
107+ {
108+ int V,E,S; // V = no.of Vertices, E = no.of Edges, S is source vertex
109+
110+ cout<<" Enter number of vertices in graph\n " ;
111+ cin>>V;
112+
113+ cout<<" Enter number of edges in graph\n " ;
114+ cin>>E;
115+
116+ cout<<" Enter your source vertex number\n " ;
117+ cin>>S;
118+
119+ struct Graph * graph = createGraph (V, E); // calling the function to allocate space to these many vertices and edges
120+
121+ int i;
122+ for (i=0 ;i<E;i++){
123+ cout<<" \n Enter edge " <<i+1 <<" properties Source, destination, weight respectively\n " ;
124+ cin>>graph->edge [i].source ;
125+ cin>>graph->edge [i].destination ;
126+ cin>>graph->edge [i].weight ;
127+ }
128+
129+ BellmanFord (graph, S);
130+ // passing created graph and source vertex to BellmanFord Algorithm function
131+
132+ return 0 ;
133+ }
0 commit comments