Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions Graph algorithm/BellmanFord.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
//Snehal Kumar
#include <bits/stdc++.h>

// a structure to represent a weighted edge in graph
struct Edge {
int src, dest, weight;
};

// a structure to represent a connected, directed and
// weighted graph
struct Graph {
int V, E;
struct Edge* edge;
};

// Creates a graph with V vertices and E edges
struct Graph* createGraph(int V, int E)
{
struct Graph* graph = new Graph;
graph->V = V;
graph->E = E;
graph->edge = new Edge[E];
return graph;
}

void printArr(int dist[], int n)
{
printf("Vertex Distance from Source\n");
for (int i = 0; i < n; ++i)
printf("%d \t\t %d\n", i, dist[i]);
}


void BellmanFord(struct Graph* graph, int src)
{
int V = graph->V;
int E = graph->E;
int dist[V];

// Initialize distances as INFINITE
for (int i = 0; i < V; i++)
dist[i] = INT_MAX;
dist[src] = 0;

// Relax the edges
for (int i = 1; i <= V - 1; i++) {
for (int j = 0; j < E; j++) {
int u = graph->edge[j].src;
int v = graph->edge[j].dest;
int weight = graph->edge[j].weight;
if (dist[u] != INT_MAX && dist[u] + weight < dist[v])
dist[v] = dist[u] + weight;
}
}

// check for negative-weight cycles
for (int i = 0; i < E; i++) {
int u = graph->edge[i].src;
int v = graph->edge[i].dest;
int weight = graph->edge[i].weight;
if (dist[u] != INT_MAX && dist[u] + weight < dist[v]) {
printf("Graph contains negative weight cycle");
return;
}
}

printArr(dist, V);

return;
}

// Driver program to test above functions
int main()
{
int V = 5; // Number of vertices in graph
int E = 8; // Number of edges in graph
struct Graph* graph = createGraph(V, E);
for(int i=0;i<E;i++)
{
int a,b,w;
scanf("%d %d %d",&a,&b,&w);
graph->edge[i].src = a;
graph->edge[i].dest = b;
graph->edge[i].weight = w;
}
BellmanFord(graph, 0);

return 0;
}
53 changes: 53 additions & 0 deletions Graph algorithm/BellmanFord.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#Snehal Kumar
#Python program for Bellman Ford
class Graph:

def __init__(self, vertices):
self.V = vertices # No. of vertices
self.graph = []

# function to add an edge to graph
def addEdge(self, u, v, w):
self.graph.append([u, v, w])

# utility function used to print the solution
def printArr(self, dist):
print("Vertex Distance from Source")
for i in range(self.V):
print("{0}\t\t{1}".format(i, dist[i]))

# The main function that finds shortest distances
def BellmanFord(self, src):

# Initialize distances as INFINITE
dist = [float("Inf")] * self.V
dist[src] = 0


# Relax all edges
for _ in range(self.V - 1):
# Update dist value and parent index of the adjacent vertices
for u, v, w in self.graph:
if dist[u] != float("Inf") and dist[u] + w < dist[v]:
dist[v] = dist[u] + w

# Check for negative-weight cycles.
for u, v, w in self.graph:
if dist[u] != float("Inf") and dist[u] + w < dist[v]:
print("Graph contains negative weight cycle")
return

self.printArr(dist)

g = Graph(5)
g.addEdge(0, 1, -1)
g.addEdge(0, 2, 4)
g.addEdge(1, 2, 3)
g.addEdge(1, 3, 2)
g.addEdge(1, 4, 2)
g.addEdge(3, 2, 5)
g.addEdge(3, 1, 1)
g.addEdge(4, 3, -3)

# Print the solution
g.BellmanFord(0)
Loading