From e35ab3e1548f6f6391714814c2575353a335b1c2 Mon Sep 17 00:00:00 2001 From: sameer-19 <63365261+sameer-19@users.noreply.github.com> Date: Thu, 6 Oct 2022 10:39:58 +0530 Subject: [PATCH] Create Bellman Ford Algorithm.cpp --- Bellman Ford Algorithm.cpp | 94 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 Bellman Ford Algorithm.cpp diff --git a/Bellman Ford Algorithm.cpp b/Bellman Ford Algorithm.cpp new file mode 100644 index 0000000..f7f1121 --- /dev/null +++ b/Bellman Ford Algorithm.cpp @@ -0,0 +1,94 @@ +#include +using namespace std; + +struct node{ + int u; + int v; + int wt; + node(int a,int b,int w) + { + u=a; + v=b; + wt=w; + } + +}; + +void BellmanFord(int src,int n,vector adj) +{ + + vector distance(n+1,100000); + distance[src]=0; + + int i,j; + + // Here, Relax all the edges n-1 times as max distance between two nodes in a graph can be n-1 + for(i=0;i>n>>edges; + + vector adj; + + for(i=0;i>a>>b>>wt; + adj.push_back(node(a,b,wt)); + adj.push_back(node(b,a,wt)); // This is for the undirected graph else not needed for directed graph + } + + // for(auto x: adj) cout<