बेलमैन फोर्ड का एल्गोरिथम

बेलमैन फोर्ड एल्गोरिदम हमें एक वेट ग्राफ के अन्य शीर्षों के लिए एक शीर्ष से सबसे छोटा रास्ता खोजने में मदद करता है।

यह दिज्क्स्ट्रा के एल्गोरिथ्म के समान है लेकिन यह ग्राफ़ के साथ काम कर सकता है जिसमें किनारों पर नकारात्मक भार हो सकते हैं।

वास्तविक जीवन में कभी भी नकारात्मक भार के साथ किनारे क्यों होंगे?

नकारात्मक भार किनारों को पहली बार में बेकार लग सकता है, लेकिन वे बहुत सारी घटनाओं को समझा सकते हैं जैसे कि कैशफ्लो, गर्मी जारी / एक रासायनिक प्रतिक्रिया में अवशोषित, आदि।

उदाहरण के लिए, यदि एक रसायन A से दूसरे रासायनिक B तक पहुंचने के लिए अलग-अलग तरीके हैं, तो प्रत्येक विधि में उप-प्रतिक्रियाएं होंगी जिनमें गर्मी अपव्यय और अवशोषण दोनों शामिल होंगे।

यदि हम प्रतिक्रियाओं का समूह ढूंढना चाहते हैं जहां न्यूनतम ऊर्जा की आवश्यकता होती है, तो हमें गर्मी अवशोषण में कारक के रूप में नकारात्मक भार और गर्मी अपव्यय को सकारात्मक भार के रूप में सक्षम करने की आवश्यकता होगी।

हमें नकारात्मक भार से सावधान रहने की आवश्यकता क्यों है?

ऋणात्मक भार के किनारे नकारात्मक भार चक्र बना सकते हैं अर्थात एक चक्र जो एक ही बिंदु पर वापस आकर कुल पथ दूरी को कम करेगा।

सबसे छोटा रास्ता खोजने की कोशिश करते समय नकारात्मक वजन चक्र एक गलत परिणाम दे सकता है

डायजेस्ट्रा के एल्गोरिथ्म जैसे सबसे छोटे पथ एल्गोरिदम जो इस तरह के चक्र का पता लगाने में सक्षम नहीं हैं, एक गलत परिणाम दे सकते हैं क्योंकि वे एक नकारात्मक वजन चक्र के माध्यम से जा सकते हैं और पथ की लंबाई कम कर सकते हैं।

बेलमैन फोर्ड का एल्गोरिदम कैसे काम करता है

बेलमैन फोर्ड एल्गोरिथ्म मार्ग की लंबाई को शुरुआती शीर्ष से अन्य सभी शीर्षों तक कम करके काम करता है। फिर यह नए रास्तों को खोजकर उन अनुमानों को शिथिल कर देता है जो पहले से कम कर दिए गए रास्तों से कम हैं।

सभी चक्करों के लिए बार-बार ऐसा करने से, हम गारंटी दे सकते हैं कि परिणाम अनुकूलित है।

बेलमैन फोर्ड के एल्गोरिदम के लिए चरण -1, बेलमैन फोर्ड के एल्गोरिथ्म के लिए चरण -2 में बेलमैन फोर्ड के एल्गोरिथ्म के लिए चरण -3 में बेलमैन फोर्ड के एल्गोरिथ्म के लिए चरण -4 में बेलमैन फोर्ड के एल्गोरिथ्म के लिए चरण -5 में बेलमैन फोर्ड के एल्गोरिथ्म के लिए चरण -6।

बेलमैन फोर्ड स्यूडोकोड

हमें हर शीर्ष की पथ दूरी बनाए रखने की आवश्यकता है। हम इसे आकार v की एक सरणी में संग्रहीत कर सकते हैं, जहाँ v शीर्षकों की संख्या है।

हम सबसे छोटा रास्ता पाने में भी सक्षम होना चाहते हैं, न केवल सबसे छोटे रास्ते की लंबाई जानना चाहते हैं। इसके लिए, हम प्रत्येक शीर्ष को उस शीर्ष बिंदु पर मैप करते हैं जिसने पिछली बार इसकी पथ लंबाई को अपडेट किया था।

एक बार एल्गोरिथ्म खत्म हो जाने के बाद, हम मार्ग को खोजने के लिए गंतव्य शीर्ष से स्रोत के शीर्ष पर वापस जा सकते हैं।

 फ़ंक्शन बेलनफ़ॉर्ड (जी, एस) प्रत्येक शीर्ष के लिए जी दूरी में (वी) <- अनंत पिछले (वी) <- पूर्ण दूरी (एस) <- 0 प्रत्येक किनारे के लिए जी में प्रत्येक किनारे के लिए (यू, वी) जी में। tempDistance <- दूरी (U) + edge_weight (U, V) अगर tempDistance <दूरी (V) दूरी (V) <- tempDistance पिछला (V) <- U प्रत्येक किनारे के लिए (U, V) G में यदि दूरी (U) + एज_वेट (यू, वी) <दूरी (वी) त्रुटि: नकारात्मक चक्र अस्तित्व वापसी दूरी (), पिछले ()

बेलमैन फोर्ड बनाम दिक्जस्त्र

बेलमैन फोर्ड का एल्गोरिथ्म और डायजेक्स्ट्रा का एल्गोरिदम संरचना में बहुत समान हैं। जबकि दिज्क्स्ट्रा केवल एक शीर्ष के पड़ोसी को देखता है, बेलमैन प्रत्येक पुनरावृत्ति में प्रत्येक किनारे से गुजरता है।

डिज्स्ट्रा का बनाम बेलमैन फोर्ड का एल्गोरिथम

पायथन, जावा और सी / सी ++ उदाहरण

पायथन जावा सी सी ++
 # Bellman Ford Algorithm in Python class Graph: def __init__(self, vertices): self.V = vertices # Total number of vertices in the graph self.graph = () # Array of edges # Add edges def add_edge(self, s, d, w): self.graph.append((s, d, w)) # Print the solution def print_solution(self, dist): print("Vertex Distance from Source") for i in range(self.V): print("(0) (1)".format(i, dist(i))) def bellman_ford(self, src): # Step 1: fill the distance array and predecessor array dist = (float("Inf")) * self.V # Mark the source vertex dist(src) = 0 # Step 2: relax edges |V| - 1 times for _ in range(self.V - 1): for s, d, w in self.graph: if dist(s) != float("Inf") and dist(s) + w < dist(d): dist(d) = dist(s) + w # Step 3: detect negative cycle # if value changes then we have a negative cycle in the graph # and we cannot find the shortest distances for s, d, w in self.graph: if dist(s) != float("Inf") and dist(s) + w < dist(d): print("Graph contains negative weight cycle") return # No negative weight cycle found! # Print the distance and predecessor array self.print_solution(dist) g = Graph(5) g.add_edge(0, 1, 5) g.add_edge(0, 2, 4) g.add_edge(1, 3, 3) g.add_edge(2, 1, 6) g.add_edge(3, 2, 2) g.bellman_ford(0)
 // Bellman Ford Algorithm in Java class CreateGraph ( // CreateGraph - it consists of edges class CreateEdge ( int s, d, w; CreateEdge() ( s = d = w = 0; ) ); int V, E; CreateEdge edge(); // Creates a graph with V vertices and E edges CreateGraph(int v, int e) ( V = v; E = e; edge = new CreateEdge(e); for (int i = 0; i < e; ++i) edge(i) = new CreateEdge(); ) void BellmanFord(CreateGraph graph, int s) ( int V = graph.V, E = graph.E; int dist() = new int(V); // Step 1: fill the distance array and predecessor array for (int i = 0; i < V; ++i) dist(i) = Integer.MAX_VALUE; // Mark the source vertex dist(s) = 0; // Step 2: relax edges |V| - 1 times for (int i = 1; i < V; ++i) ( for (int j = 0; j < E; ++j) ( // Get the edge data int u = graph.edge(j).s; int v = graph.edge(j).d; int w = graph.edge(j).w; if (dist(u) != Integer.MAX_VALUE && dist(u) + w < dist(v)) dist(v) = dist(u) + w; ) ) // Step 3: detect negative cycle // if value changes then we have a negative cycle in the graph // and we cannot find the shortest distances for (int j = 0; j < E; ++j) ( int u = graph.edge(j).s; int v = graph.edge(j).d; int w = graph.edge(j).w; if (dist(u) != Integer.MAX_VALUE && dist(u) + w < dist(v)) ( System.out.println("CreateGraph contains negative w cycle"); return; ) ) // No negative w cycle found! // Print the distance and predecessor array printSolution(dist, V); ) // Print the solution void printSolution(int dist(), int V) ( System.out.println("Vertex Distance from Source"); for (int i = 0; i 1 graph.edge(0).s = 0; graph.edge(0).d = 1; graph.edge(0).w = 5; // edge 0 --> 2 graph.edge(1).s = 0; graph.edge(1).d = 2; graph.edge(1).w = 4; // edge 1 --> 3 graph.edge(2).s = 1; graph.edge(2).d = 3; graph.edge(2).w = 3; // edge 2 --> 1 graph.edge(3).s = 2; graph.edge(3).d = 1; graph.edge(3).w = 6; // edge 3 --> 2 graph.edge(4).s = 3; graph.edge(4).d = 2; graph.edge(4).w = 2; graph.BellmanFord(graph, 0); // 0 is the source vertex ) )
 // Bellman Ford Algorithm in C #include #include #define INFINITY 99999 //struct for the edges of the graph struct Edge ( int u; //start vertex of the edge int v; //end vertex of the edge int w; //weight of the edge (u,v) ); //Graph - it consists of edges struct Graph ( int V; //total number of vertices in the graph int E; //total number of edges in the graph struct Edge *edge; //array of edges ); void bellmanford(struct Graph *g, int source); void display(int arr(), int size); int main(void) ( //create graph struct Graph *g = (struct Graph *)malloc(sizeof(struct Graph)); g->V = 4; //total vertices g->E = 5; //total edges //array of edges for graph g->edge = (struct Edge *)malloc(g->E * sizeof(struct Edge)); //------- adding the edges of the graph /* edge(u, v) where u = start vertex of the edge (u,v) v = end vertex of the edge (u,v) w is the weight of the edge (u,v) */ //edge 0 --> 1 g->edge(0).u = 0; g->edge(0).v = 1; g->edge(0).w = 5; //edge 0 --> 2 g->edge(1).u = 0; g->edge(1).v = 2; g->edge(1).w = 4; //edge 1 --> 3 g->edge(2).u = 1; g->edge(2).v = 3; g->edge(2).w = 3; //edge 2 --> 1 g->edge(3).u = 2; g->edge(3).v = 1; g->edge(3).w = 6; //edge 3 --> 2 g->edge(4).u = 3; g->edge(4).v = 2; g->edge(4).w = 2; bellmanford(g, 0); //0 is the source vertex return 0; ) void bellmanford(struct Graph *g, int source) ( //variables int i, j, u, v, w; //total vertex in the graph g int tV = g->V; //total edge in the graph g int tE = g->E; //distance array //size equal to the number of vertices of the graph g int d(tV); //predecessor array //size equal to the number of vertices of the graph g int p(tV); //step 1: fill the distance array and predecessor array for (i = 0; i < tV; i++) ( d(i) = INFINITY; p(i) = 0; ) //mark the source vertex d(source) = 0; //step 2: relax edges |V| - 1 times for (i = 1; i <= tV - 1; i++) ( for (j = 0; j edge(j).u; v = g->edge(j).v; w = g->edge(j).w; if (d(u) != INFINITY && d(v)> d(u) + w) ( d(v) = d(u) + w; p(v) = u; ) ) ) //step 3: detect negative cycle //if value changes then we have a negative cycle in the graph //and we cannot find the shortest distances for (i = 0; i edge(i).u; v = g->edge(i).v; w = g->edge(i).w; if (d(u) != INFINITY && d(v)> d(u) + w) ( printf("Negative weight cycle detected!"); return; ) ) //No negative weight cycle found! //print the distance and predecessor array printf("Distance array: "); display(d, tV); printf("Predecessor array: "); display(p, tV); ) void display(int arr(), int size) ( int i; for (i = 0; i < size; i++) ( printf("%d ", arr(i)); ) printf(""); )
 // Bellman Ford Algorithm in C++ #include // Struct for the edges of the graph struct Edge ( int u; //start vertex of the edge int v; //end vertex of the edge int w; //w of the edge (u,v) ); // Graph - it consists of edges struct Graph ( int V; // Total number of vertices in the graph int E; // Total number of edges in the graph struct Edge* edge; // Array of edges ); // Creates a graph with V vertices and E edges struct Graph* createGraph(int V, int E) ( struct Graph* graph = new Graph; graph->V = V; // Total Vertices graph->E = E; // Total edges // Array of edges for graph graph->edge = new Edge(E); return graph; ) // Printing the solution void printArr(int arr(), int size) ( int i; for (i = 0; i V; int E = graph->E; int dist(V); // Step 1: fill the distance array and predecessor array for (int i = 0; i < V; i++) dist(i) = INT_MAX; // Mark the source vertex dist(u) = 0; // Step 2: relax edges |V| - 1 times for (int i = 1; i <= V - 1; i++) ( for (int j = 0; j edge(j).u; int v = graph->edge(j).v; int w = graph->edge(j).w; if (dist(u) != INT_MAX && dist(u) + w < dist(v)) dist(v) = dist(u) + w; ) ) // Step 3: detect negative cycle // if value changes then we have a negative cycle in the graph // and we cannot find the shortest distances for (int i = 0; i edge(i).u; int v = graph->edge(i).v; int w = graph->edge(i).w; if (dist(u) != INT_MAX && dist(u) + w 1 graph->edge(0).u = 0; graph->edge(0).v = 1; graph->edge(0).w = 5; //edge 0 --> 2 graph->edge(1).u = 0; graph->edge(1).v = 2; graph->edge(1).w = 4; //edge 1 --> 3 graph->edge(2).u = 1; graph->edge(2).v = 3; graph->edge(2).w = 3; //edge 2 --> 1 graph->edge(3).u = 2; graph->edge(3).v = 1; graph->edge(3).w = 6; //edge 3 --> 2 graph->edge(4).u = 3; graph->edge(4).v = 2; graph->edge(4).w = 2; BellmanFord(graph, 0); //0 is the source vertex return 0; )

बेलमैन फोर्ड की जटिलता

समय जटिलता

बेस्ट केस कॉम्प्लेक्सिटी O (E)
औसत केस जटिलता O (VE)
सबसे खराब मामला जटिलता O (VE)

अंतरिक्ष की जटिलता

और, अंतरिक्ष जटिलता है O(V)

बेलमैन फोर्ड के एल्गोरिथ्म अनुप्रयोग

  1. रूटिंग एल्गोरिदम में सबसे छोटे रास्तों की गणना के लिए
  2. सबसे छोटा रास्ता खोजने के लिए

दिलचस्प लेख...