logo

Bellman Ford -algoritmi

Bellman ford -algoritmi on yhden lähteen lyhimmän polun algoritmi. Tätä algoritmia käytetään lyhimmän etäisyyden löytämiseen yhdestä kärjestä painotetun graafin kaikkiin muihin pisteisiin. Lyhimmän polun löytämiseen käytetään useita muita algoritmeja, kuten Dijkstra-algoritmi jne. Jos painotettu kaavio sisältää negatiiviset painoarvot, Dijkstra-algoritmi ei vahvista, tuottaako se oikean vastauksen vai ei. Toisin kuin Dijkstra-algoritmi, bellman ford -algoritmi takaa oikean vastauksen, vaikka painotettu kaavio sisältää negatiiviset painoarvot.

Tämän algoritmin sääntö

 We will go on relaxing all the edges (n - 1) times where, n = number of vertices 

Harkitse alla olevaa kaaviota:

Bellman-Ford -algoritmi

Kuten voimme havaita yllä olevasta kaaviosta, että jotkut painot ovat negatiivisia. Yllä oleva kaavio sisältää 6 kärkeä, joten jatketaan rentoutumista 5 pisteeseen asti. Täällä rentoudumme kaikki reunat 5 kertaa. Silmukka iteroidaan 5 kertaa oikean vastauksen saamiseksi. Jos silmukkaa iteroidaan enemmän kuin 5 kertaa, niin myös vastaus on sama, eli pisteiden välisessä etäisyydessä ei olisi muutosta.

Rentoutuminen tarkoittaa:

kali linux terminaali
 If (d(u) + c(u , v) <d(v)) d(v)="d(u)" + c(u , v) < pre> <p>To find the shortest path of the above graph, the first step is note down all the edges which are given below:</p> <p>(A, B), (A, C), (A, D), (B, E), (C, E), (D, C), (D, F), (E, F), (C, B)</p> <p>Let&apos;s consider the source vertex as &apos;A&apos;; therefore, the distance value at vertex A is 0 and the distance value at all the other vertices as infinity shown as below:</p> <img src="//techcodeview.com/img/daa-tutorial/85/bellman-ford-algorithm-2.webp" alt="Bellman-Ford Algorithm"> <p>Since the graph has six vertices so it will have five iterations.</p> <p> <strong>First iteration</strong> </p> <p>Consider the edge (A, B). Denote vertex &apos;A&apos; as &apos;u&apos; and vertex &apos;B&apos; as &apos;v&apos;. Now use the relaxing formula:</p> <p>d(u) = 0</p> <p>d(v) = &#x221E;</p> <p>c(u , v) = 6</p> <p>Since (0 + 6) is less than &#x221E;, so update</p> <pre> d(v) = d(u) + c(u , v) </pre> <p>d(v) = 0 + 6 = 6</p> <p>Therefore, the distance of vertex B is 6.</p> <p>Consider the edge (A, C). Denote vertex &apos;A&apos; as &apos;u&apos; and vertex &apos;C&apos; as &apos;v&apos;. Now use the relaxing formula:</p> <p>d(u) = 0</p> <p>d(v) = &#x221E;</p> <p>c(u , v) = 4</p> <p>Since (0 + 4) is less than &#x221E;, so update</p> <pre> d(v) = d(u) + c(u , v) </pre> <p>d(v) = 0 + 4 = 4</p> <p>Therefore, the distance of vertex C is 4.</p> <p>Consider the edge (A, D). Denote vertex &apos;A&apos; as &apos;u&apos; and vertex &apos;D&apos; as &apos;v&apos;. Now use the relaxing formula:</p> <p>d(u) = 0</p> <p>d(v) = &#x221E;</p> <p>c(u , v) = 5</p> <p>Since (0 + 5) is less than &#x221E;, so update</p> <pre> d(v) = d(u) + c(u , v) </pre> <p>d(v) = 0 + 5 = 5</p> <p>Therefore, the distance of vertex D is 5.</p> <p>Consider the edge (B, E). Denote vertex &apos;B&apos; as &apos;u&apos; and vertex &apos;E&apos; as &apos;v&apos;. Now use the relaxing formula:</p> <p>d(u) = 6</p> <p>d(v) = &#x221E;</p> <p>c(u , v) = -1</p> <p>Since (6 - 1) is less than &#x221E;, so update</p> <pre> d(v) = d(u) + c(u , v) </pre> <p>d(v) = 6 - 1= 5</p> <p>Therefore, the distance of vertex E is 5.</p> <p>Consider the edge (C, E). Denote vertex &apos;C&apos; as &apos;u&apos; and vertex &apos;E&apos; as &apos;v&apos;. Now use the relaxing formula:</p> <p>d(u) = 4</p> <p>d(v) = 5</p> <p>c(u , v) = 3</p> <p>Since (4 + 3) is greater than 5, so there will be no updation. The value at vertex E is 5.</p> <p>Consider the edge (D, C). Denote vertex &apos;D&apos; as &apos;u&apos; and vertex &apos;C&apos; as &apos;v&apos;. Now use the relaxing formula:</p> <p>d(u) = 5</p> <p>d(v) = 4</p> <p>c(u , v) = -2</p> <p>Since (5 -2) is less than 4, so update</p> <pre> d(v) = d(u) + c(u , v) </pre> <p>d(v) = 5 - 2 = 3</p> <p>Therefore, the distance of vertex C is 3.</p> <p>Consider the edge (D, F). Denote vertex &apos;D&apos; as &apos;u&apos; and vertex &apos;F&apos; as &apos;v&apos;. Now use the relaxing formula:</p> <p>d(u) = 5</p> <p>d(v) = &#x221E;</p> <p>c(u , v) = -1</p> <p>Since (5 -1) is less than &#x221E;, so update</p> <pre> d(v) = d(u) + c(u , v) </pre> <p>d(v) = 5 - 1 = 4</p> <p>Therefore, the distance of vertex F is 4.</p> <p>Consider the edge (E, F). Denote vertex &apos;E&apos; as &apos;u&apos; and vertex &apos;F&apos; as &apos;v&apos;. Now use the relaxing formula:</p> <p>d(u) = 5</p> <p>d(v) = &#x221E;</p> <p>c(u , v) = 3</p> <p>Since (5 + 3) is greater than 4, so there would be no updation on the distance value of vertex F.</p> <p>Consider the edge (C, B). Denote vertex &apos;C&apos; as &apos;u&apos; and vertex &apos;B&apos; as &apos;v&apos;. Now use the relaxing formula:</p> <p>d(u) = 3</p> <p>d(v) = 6</p> <p>c(u , v) = -2</p> <p>Since (3 - 2) is less than 6, so update</p> <pre> d(v) = d(u) + c(u , v) </pre> <p>d(v) = 3 - 2 = 1</p> <p>Therefore, the distance of vertex B is 1.</p> <p>Now the first iteration is completed. We move to the second iteration.</p> <p> <strong>Second iteration:</strong> </p> <p>In the second iteration, we again check all the edges. The first edge is (A, B). Since (0 + 6) is greater than 1 so there would be no updation in the vertex B.</p> <p>The next edge is (A, C). Since (0 + 4) is greater than 3 so there would be no updation in the vertex C.</p> <p>The next edge is (A, D). Since (0 + 5) equals to 5 so there would be no updation in the vertex D.</p> <p>The next edge is (B, E). Since (1 - 1) equals to 0 which is less than 5 so update:</p> <p>d(v) = d(u) + c(u, v)</p> <p>d(E) = d(B) +c(B , E)</p> <p>= 1 - 1 = 0</p> <p>The next edge is (C, E). Since (3 + 3) equals to 6 which is greater than 5 so there would be no updation in the vertex E.</p> <p>The next edge is (D, C). Since (5 - 2) equals to 3 so there would be no updation in the vertex C.</p> <p>The next edge is (D, F). Since (5 - 1) equals to 4 so there would be no updation in the vertex F.</p> <p>The next edge is (E, F). Since (5 + 3) equals to 8 which is greater than 4 so there would be no updation in the vertex F.</p> <p>The next edge is (C, B). Since (3 - 2) equals to 1` so there would be no updation in the vertex B.</p> <img src="//techcodeview.com/img/daa-tutorial/85/bellman-ford-algorithm-3.webp" alt="Bellman-Ford Algorithm"> <p> <strong>Third iteration</strong> </p> <p>We will perform the same steps as we did in the previous iterations. We will observe that there will be no updation in the distance of vertices.</p> <pre> The following are the distances of vertices: A: 0 B: 1 C: 3 D: 5 E: 0 F: 3 </pre> <p> <strong>Time Complexity</strong> </p> <p>The time complexity of Bellman ford algorithm would be O(E|V| - 1).</p> <pre> function bellmanFord(G, S) for each vertex V in G distance[V] <- 0 infinite previous[v] <- null distance[s] for each vertex v in g edge (u,v) tempdistance distance[u] + edge_weight(u, v) if < distance[v] u distance[v} error: negative cycle exists return distance[], previous[] pre> <p> <strong>Drawbacks of Bellman ford algorithm</strong> </p> <ul> <li>The bellman ford algorithm does not produce a correct answer if the sum of the edges of a cycle is negative. Let&apos;s understand this property through an example. Consider the below graph. <br> <img src="//techcodeview.com/img/daa-tutorial/85/bellman-ford-algorithm-4.webp" alt="Bellman-Ford Algorithm"></li> <li>In the above graph, we consider vertex 1 as the source vertex and provides 0 value to it. We provide infinity value to other vertices shown as below: <br> <img src="//techcodeview.com/img/daa-tutorial/85/bellman-ford-algorithm-5.webp" alt="Bellman-Ford Algorithm"> <br>Edges can be written as: <br> (1, 3), (1, 2), (2, 4), (3, 2), (4, 3) </li> </ul> <p> <strong>First iteration</strong> </p> <p> <strong>Consider the edge (1, 3). Denote vertex &apos;1&apos; as &apos;u&apos; and vertex &apos;3&apos; as &apos;v&apos;. Now use the relaxing formula:</strong> </p> <p>d(u) = 0</p> <p>d(v) = &#x221E;</p> <p>c(u , v) = 5</p> <p>Since (0 + 5) is less than &#x221E;, so update</p> <pre> d(v) = d(u) + c(u , v) </pre> <p>d(v) = 0 + 5 = 5</p> <p>Therefore, the distance of vertex 3 is 5.</p> <p> <strong>Consider the edge (1, 2). Denote vertex &apos;1&apos; as &apos;u&apos; and vertex &apos;2&apos; as &apos;v&apos;. Now use the relaxing formula:</strong> </p> <p>d(u) = 0</p> <p>d(v) = &#x221E;</p> <p>c(u , v) = 4</p> <p>Since (0 + 4) is less than &#x221E;, so update</p> <pre> d(v) = d(u) + c(u , v) </pre> <p>d(v) = 0 + 4 = 4</p> <p>Therefore, the distance of vertex 2 is 4.</p> <p> <strong>Consider the edge (3, 2). Denote vertex &apos;3&apos; as &apos;u&apos; and vertex &apos;2&apos; as &apos;v&apos;. Now use the relaxing formula:</strong> </p> <p>d(u) = 5</p> <p>d(v) = 4</p> <p>c(u , v) = 7</p> <p>Since (5 + 7) is greater than 4, so there would be no updation in the vertex 2.</p> <p> <strong>Consider the edge (2, 4). Denote vertex &apos;2&apos; as &apos;u&apos; and vertex &apos;4&apos; as &apos;v&apos;. Now use the relaxing formula:</strong> </p> <p>d(u) = 4</p> <p>d(v) = &#x221E;</p> <p>c(u , v) = 7</p> <p>Since (4 + 7) equals to 11 which is less than &#x221E;, so update</p> <pre> d(v) = d(u) + c(u , v) </pre> <p>d(v) = 4 + 7 = 11</p> <p>Therefore, the distance of vertex 4 is 11.</p> <p> <strong>Consider the edge (4, 3). Denote vertex &apos;4&apos; as &apos;u&apos; and vertex &apos;3&apos; as &apos;v&apos;. Now use the relaxing formula:</strong> </p> <p>d(u) = 11</p> <p>d(v) = 5</p> <p>c(u , v) = -15</p> <p>Since (11 - 15) equals to -4 which is less than 5, so update</p> <pre> d(v) = d(u) + c(u , v) </pre> <p>d(v) = 11 - 15 = -4</p> <p>Therefore, the distance of vertex 3 is -4.</p> <p>Now we move to the second iteration.</p> <p> <strong>Second iteration</strong> </p> <p>Now, again we will check all the edges. The first edge is (1, 3). Since (0 + 5) equals to 5 which is greater than -4 so there would be no updation in the vertex 3.</p> <p>The next edge is (1, 2). Since (0 + 4) equals to 4 so there would be no updation in the vertex 2.</p> <p>The next edge is (3, 2). Since (-4 + 7) equals to 3 which is less than 4 so update:</p> <p>d(v) = d(u) + c(u, v)</p> <p>d(2) = d(3) +c(3, 2)</p> <p>= -4 + 7 = 3</p> <p>Therefore, the value at vertex 2 is 3.</p> <p>The next edge is (2, 4). Since ( 3+7) equals to 10 which is less than 11 so update</p> <p>d(v) = d(u) + c(u, v)</p> <p>d(4) = d(2) +c(2, 4)</p> <p>= 3 + 7 = 10</p> <p>Therefore, the value at vertex 4 is 10.</p> <p>The next edge is (4, 3). Since (10 - 15) equals to -5 which is less than -4 so update:</p> <p>d(v) = d(u) + c(u, v)</p> <p>d(3) = d(4) +c(4, 3)</p> <p>= 10 - 15 = -5</p> <p>Therefore, the value at vertex 3 is -5.</p> <p>Now we move to the third iteration.</p> <p> <strong>Third iteration</strong> </p> <p>Now again we will check all the edges. The first edge is (1, 3). Since (0 + 5) equals to 5 which is greater than -5 so there would be no updation in the vertex 3.</p> <p>The next edge is (1, 2). Since (0 + 4) equals to 4 which is greater than 3 so there would be no updation in the vertex 2.</p> <p>The next edge is (3, 2). Since (-5 + 7) equals to 2 which is less than 3 so update:</p> <p>d(v) = d(u) + c(u, v)</p> <p>d(2) = d(3) +c(3, 2)</p> <p>= -5 + 7 = 2</p> <p>Therefore, the value at vertex 2 is 2.</p> <p>The next edge is (2, 4). Since (2 + 7) equals to 9 which is less than 10 so update:</p> <p>d(v) = d(u) + c(u, v)</p> <p>d(4) = d(2) +c(2, 4)</p> <p>= 2 + 7 = 9</p> <p>Therefore, the value at vertex 4 is 9.</p> <p>The next edge is (4, 3). Since (9 - 15) equals to -6 which is less than -5 so update:</p> <p>d(v) = d(u) + c(u, v)</p> <p>d(3) = d(4) +c(4, 3)</p> <p>= 9 - 15 = -6</p> <p>Therefore, the value at vertex 3 is -6.</p> <img src="//techcodeview.com/img/daa-tutorial/85/bellman-ford-algorithm-6.webp" alt="Bellman-Ford Algorithm"> <p>Since the graph contains 4 vertices, so according to the bellman ford algorithm, there would be only 3 iterations. If we try to perform 4<sup>th</sup> iteration on the graph, the distance of the vertices from the given vertex should not change. If the distance varies, it means that the bellman ford algorithm is not providing the correct answer.</p> <p> <strong>4<sup>th</sup> iteration</strong> </p> <p>The first edge is (1, 3). Since (0 +5) equals to 5 which is greater than -6 so there would be no change in the vertex 3.</p> <p>The next edge is (1, 2). Since (0 + 4) is greater than 2 so there would be no updation.</p> <p>The next edge is (3, 2). Since (-6 + 7) equals to 1 which is less than 3 so update:</p> <p>d(v) = d(u) + c(u, v)</p> <p>d(2) = d(3) +c(3, 2)</p> <p>= -6 + 7 = 1</p> <p>In this case, the value of the vertex is updated. So, we conclude that the bellman ford algorithm does not work when the graph contains the negative weight cycle.</p> <p>Therefore, the value at vertex 2 is 1.</p> <hr></-></pre></d(v))>

d(v) = 0 + 6 = 6

Siksi kärjen B etäisyys on 6.

Harkitse reunaa (A, C). Merkitse kärki 'A' muodossa 'u' ja kärki 'C' 'v':ksi. Käytä nyt rentouttavaa kaavaa:

d(u) = 0

d(v) = ∞

c(u , v) = 4

Koska (0 + 4) on pienempi kuin ∞, joten päivitä

 d(v) = d(u) + c(u , v) 

d(v) = 0 + 4 = 4

Siksi kärjen C etäisyys on 4.

Harkitse reunaa (A, D). Merkitse kärki 'A' muodossa 'u' ja kärki 'D' 'v':ksi. Käytä nyt rentouttavaa kaavaa:

d(u) = 0

d(v) = ∞

c(u , v) = 5

Koska (0 + 5) on pienempi kuin ∞, joten päivitä

 d(v) = d(u) + c(u , v) 

d(v) = 0 + 5 = 5

Siksi kärjen D etäisyys on 5.

Harkitse reunaa (B, E). Merkitse kärki 'B' muodossa 'u' ja kärki 'E' 'v':ksi. Käytä nyt rentouttavaa kaavaa:

d(u) = 6

d(v) = ∞

c(u, v) = -1

Koska (6 - 1) on pienempi kuin ∞, joten päivitä

 d(v) = d(u) + c(u , v) 

d(v) = 6 - 1 = 5

Siksi kärjen E etäisyys on 5.

Tarkastellaan reunaa (C, E). Merkitse kärki 'C' muodossa 'u' ja kärki 'E' 'v':ksi. Käytä nyt rentouttavaa kaavaa:

d(u) = 4

d(v) = 5

c(u , v) = 3

Koska (4 + 3) on suurempi kuin 5, päivitystä ei tehdä. Arvo kärjessä E on 5.

Tarkastellaan reunaa (D, C). Merkitse kärki 'D' muodossa 'u' ja kärki 'C' 'v':ksi. Käytä nyt rentouttavaa kaavaa:

d(u) = 5

d(v) = 4

c(u, v) = -2

Koska (5 -2) on pienempi kuin 4, joten päivitä

 d(v) = d(u) + c(u , v) 

d(v) = 5 - 2 = 3

Siksi kärjen C etäisyys on 3.

Harkitse reunaa (D, F). Merkitse kärki 'D':ksi 'u' ja kärki 'F':ksi 'v'. Käytä nyt rentouttavaa kaavaa:

d(u) = 5

d(v) = ∞

c(u, v) = -1

Koska (5 -1) on pienempi kuin ∞, joten päivitä

 d(v) = d(u) + c(u , v) 

d(v) = 5 - 1 = 4

Siksi kärjen F etäisyys on 4.

Harkitse reunaa (E, F). Merkitse kärki 'E':ksi 'u' ja kärki 'F':ksi 'v'. Käytä nyt rentouttavaa kaavaa:

d(u) = 5

d(v) = ∞

c(u , v) = 3

Koska (5 + 3) on suurempi kuin 4, niin huippupisteen F etäisyysarvoa ei päivitetä.

Tarkastellaan reunaa (C, B). Merkitse kärki 'C' muodossa 'u' ja kärki 'B' 'v':ksi. Käytä nyt rentouttavaa kaavaa:

d(u) = 3

d(v) = 6

c(u, v) = -2

Koska (3 - 2) on pienempi kuin 6, joten päivitä

 d(v) = d(u) + c(u , v) 

d(v) = 3 - 2 = 1

Siksi kärjen B etäisyys on 1.

Nyt ensimmäinen iteraatio on valmis. Siirrymme toiseen iteraatioon.

Toinen iteraatio:

Toisessa iteraatiossa tarkistamme jälleen kaikki reunat. Ensimmäinen reuna on (A, B). Koska (0 + 6) on suurempi kuin 1, pisteessä B ei tapahtuisi päivitystä.

Seuraava reuna on (A, C). Koska (0 + 4) on suurempi kuin 3, pisteessä C ei tapahtuisi päivitystä.

Seuraava reuna on (A, D). Koska (0 + 5) on 5, joten pisteessä D ei tapahtuisi päivitystä.

Seuraava reuna on (B, E). Koska (1 - 1) on 0, joka on pienempi kuin 5, joten päivitä:

d(v) = d(u) + c(u, v)

d(E) = d(B) +c(B , E)

java do while esimerkki

= 1 - 1 = 0

Seuraava reuna on (C, E). Koska (3 + 3) on yhtä kuin 6, joka on suurempi kuin 5, niin pisteessä E ei tapahtuisi päivitystä.

Seuraava reuna on (D, C). Koska (5 - 2) on yhtä kuin 3, joten huippupisteessä C ei tapahtuisi päivitystä.

Seuraava reuna on (D, F). Koska (5 - 1) on yhtä kuin 4, joten huippupisteessä F ei tapahtuisi päivitystä.

Seuraava reuna on (E, F). Koska (5 + 3) on yhtä kuin 8, joka on suurempi kuin 4, niin huippupisteessä F ei tapahtuisi päivitystä.

Seuraava reuna on (C, B). Koska (3 - 2) on yhtä kuin 1`, huippupisteessä B ei tapahtuisi päivitystä.

Bellman-Ford -algoritmi

Kolmas iteraatio

Suoritamme samat vaiheet kuin edellisissä iteraatioissa. Huomaamme, että kärkipisteiden etäisyyttä ei päivitetä.

 The following are the distances of vertices: A: 0 B: 1 C: 3 D: 5 E: 0 F: 3 

Aika monimutkaisuus

Bellman ford -algoritmin aikamonimutkaisuus olisi O(E|V| - 1).

 function bellmanFord(G, S) for each vertex V in G distance[V] <- 0 infinite previous[v] <- null distance[s] for each vertex v in g edge (u,v) tempdistance distance[u] + edge_weight(u, v) if < distance[v] u distance[v} error: negative cycle exists return distance[], previous[] pre> <p> <strong>Drawbacks of Bellman ford algorithm</strong> </p> <ul> <li>The bellman ford algorithm does not produce a correct answer if the sum of the edges of a cycle is negative. Let&apos;s understand this property through an example. Consider the below graph. <br> <img src="//techcodeview.com/img/daa-tutorial/85/bellman-ford-algorithm-4.webp" alt="Bellman-Ford Algorithm"></li> <li>In the above graph, we consider vertex 1 as the source vertex and provides 0 value to it. We provide infinity value to other vertices shown as below: <br> <img src="//techcodeview.com/img/daa-tutorial/85/bellman-ford-algorithm-5.webp" alt="Bellman-Ford Algorithm"> <br>Edges can be written as: <br> (1, 3), (1, 2), (2, 4), (3, 2), (4, 3) </li> </ul> <p> <strong>First iteration</strong> </p> <p> <strong>Consider the edge (1, 3). Denote vertex &apos;1&apos; as &apos;u&apos; and vertex &apos;3&apos; as &apos;v&apos;. Now use the relaxing formula:</strong> </p> <p>d(u) = 0</p> <p>d(v) = &#x221E;</p> <p>c(u , v) = 5</p> <p>Since (0 + 5) is less than &#x221E;, so update</p> <pre> d(v) = d(u) + c(u , v) </pre> <p>d(v) = 0 + 5 = 5</p> <p>Therefore, the distance of vertex 3 is 5.</p> <p> <strong>Consider the edge (1, 2). Denote vertex &apos;1&apos; as &apos;u&apos; and vertex &apos;2&apos; as &apos;v&apos;. Now use the relaxing formula:</strong> </p> <p>d(u) = 0</p> <p>d(v) = &#x221E;</p> <p>c(u , v) = 4</p> <p>Since (0 + 4) is less than &#x221E;, so update</p> <pre> d(v) = d(u) + c(u , v) </pre> <p>d(v) = 0 + 4 = 4</p> <p>Therefore, the distance of vertex 2 is 4.</p> <p> <strong>Consider the edge (3, 2). Denote vertex &apos;3&apos; as &apos;u&apos; and vertex &apos;2&apos; as &apos;v&apos;. Now use the relaxing formula:</strong> </p> <p>d(u) = 5</p> <p>d(v) = 4</p> <p>c(u , v) = 7</p> <p>Since (5 + 7) is greater than 4, so there would be no updation in the vertex 2.</p> <p> <strong>Consider the edge (2, 4). Denote vertex &apos;2&apos; as &apos;u&apos; and vertex &apos;4&apos; as &apos;v&apos;. Now use the relaxing formula:</strong> </p> <p>d(u) = 4</p> <p>d(v) = &#x221E;</p> <p>c(u , v) = 7</p> <p>Since (4 + 7) equals to 11 which is less than &#x221E;, so update</p> <pre> d(v) = d(u) + c(u , v) </pre> <p>d(v) = 4 + 7 = 11</p> <p>Therefore, the distance of vertex 4 is 11.</p> <p> <strong>Consider the edge (4, 3). Denote vertex &apos;4&apos; as &apos;u&apos; and vertex &apos;3&apos; as &apos;v&apos;. Now use the relaxing formula:</strong> </p> <p>d(u) = 11</p> <p>d(v) = 5</p> <p>c(u , v) = -15</p> <p>Since (11 - 15) equals to -4 which is less than 5, so update</p> <pre> d(v) = d(u) + c(u , v) </pre> <p>d(v) = 11 - 15 = -4</p> <p>Therefore, the distance of vertex 3 is -4.</p> <p>Now we move to the second iteration.</p> <p> <strong>Second iteration</strong> </p> <p>Now, again we will check all the edges. The first edge is (1, 3). Since (0 + 5) equals to 5 which is greater than -4 so there would be no updation in the vertex 3.</p> <p>The next edge is (1, 2). Since (0 + 4) equals to 4 so there would be no updation in the vertex 2.</p> <p>The next edge is (3, 2). Since (-4 + 7) equals to 3 which is less than 4 so update:</p> <p>d(v) = d(u) + c(u, v)</p> <p>d(2) = d(3) +c(3, 2)</p> <p>= -4 + 7 = 3</p> <p>Therefore, the value at vertex 2 is 3.</p> <p>The next edge is (2, 4). Since ( 3+7) equals to 10 which is less than 11 so update</p> <p>d(v) = d(u) + c(u, v)</p> <p>d(4) = d(2) +c(2, 4)</p> <p>= 3 + 7 = 10</p> <p>Therefore, the value at vertex 4 is 10.</p> <p>The next edge is (4, 3). Since (10 - 15) equals to -5 which is less than -4 so update:</p> <p>d(v) = d(u) + c(u, v)</p> <p>d(3) = d(4) +c(4, 3)</p> <p>= 10 - 15 = -5</p> <p>Therefore, the value at vertex 3 is -5.</p> <p>Now we move to the third iteration.</p> <p> <strong>Third iteration</strong> </p> <p>Now again we will check all the edges. The first edge is (1, 3). Since (0 + 5) equals to 5 which is greater than -5 so there would be no updation in the vertex 3.</p> <p>The next edge is (1, 2). Since (0 + 4) equals to 4 which is greater than 3 so there would be no updation in the vertex 2.</p> <p>The next edge is (3, 2). Since (-5 + 7) equals to 2 which is less than 3 so update:</p> <p>d(v) = d(u) + c(u, v)</p> <p>d(2) = d(3) +c(3, 2)</p> <p>= -5 + 7 = 2</p> <p>Therefore, the value at vertex 2 is 2.</p> <p>The next edge is (2, 4). Since (2 + 7) equals to 9 which is less than 10 so update:</p> <p>d(v) = d(u) + c(u, v)</p> <p>d(4) = d(2) +c(2, 4)</p> <p>= 2 + 7 = 9</p> <p>Therefore, the value at vertex 4 is 9.</p> <p>The next edge is (4, 3). Since (9 - 15) equals to -6 which is less than -5 so update:</p> <p>d(v) = d(u) + c(u, v)</p> <p>d(3) = d(4) +c(4, 3)</p> <p>= 9 - 15 = -6</p> <p>Therefore, the value at vertex 3 is -6.</p> <img src="//techcodeview.com/img/daa-tutorial/85/bellman-ford-algorithm-6.webp" alt="Bellman-Ford Algorithm"> <p>Since the graph contains 4 vertices, so according to the bellman ford algorithm, there would be only 3 iterations. If we try to perform 4<sup>th</sup> iteration on the graph, the distance of the vertices from the given vertex should not change. If the distance varies, it means that the bellman ford algorithm is not providing the correct answer.</p> <p> <strong>4<sup>th</sup> iteration</strong> </p> <p>The first edge is (1, 3). Since (0 +5) equals to 5 which is greater than -6 so there would be no change in the vertex 3.</p> <p>The next edge is (1, 2). Since (0 + 4) is greater than 2 so there would be no updation.</p> <p>The next edge is (3, 2). Since (-6 + 7) equals to 1 which is less than 3 so update:</p> <p>d(v) = d(u) + c(u, v)</p> <p>d(2) = d(3) +c(3, 2)</p> <p>= -6 + 7 = 1</p> <p>In this case, the value of the vertex is updated. So, we conclude that the bellman ford algorithm does not work when the graph contains the negative weight cycle.</p> <p>Therefore, the value at vertex 2 is 1.</p> <hr></->

d(v) = 0 + 5 = 5

Siksi kärjen 3 etäisyys on 5.

Harkitse reunaa (1, 2). Merkitse kärki '1' muodossa 'u' ja kärki '2' 'v':ksi. Käytä nyt rentouttavaa kaavaa:

d(u) = 0

d(v) = ∞

muuntaa merkkijonon päivämäärä

c(u , v) = 4

Koska (0 + 4) on pienempi kuin ∞, joten päivitä

 d(v) = d(u) + c(u , v) 

d(v) = 0 + 4 = 4

Siksi kärjen 2 etäisyys on 4.

Harkitse reunaa (3, 2). Merkitse kärki '3' muodossa 'u' ja kärki '2' 'v':ksi. Käytä nyt rentouttavaa kaavaa:

d(u) = 5

d(v) = 4

c(u , v) = 7

Koska (5 + 7) on suurempi kuin 4, joten kärjessä 2 ei tapahtuisi päivitystä.

Harkitse reunaa (2, 4). Merkitse kärki '2' muodossa 'u' ja kärki '4' 'v':ksi. Käytä nyt rentouttavaa kaavaa:

d(u) = 4

d(v) = ∞

c(u , v) = 7

Koska (4 + 7) on 11, joka on pienempi kuin ∞, joten päivitä

 d(v) = d(u) + c(u , v) 

d(v) = 4 + 7 = 11

Siksi kärjen 4 etäisyys on 11.

Harkitse reunaa (4, 3). Merkitse kärki '4':ksi 'u' ja kärki '3':ksi 'v'. Käytä nyt rentouttavaa kaavaa:

d(u) = 11

d(v) = 5

c(u, v) = -15

Koska (11 - 15) on -4, joka on pienempi kuin 5, joten päivitä

 d(v) = d(u) + c(u , v) 

d(v) = 11 - 15 = -4

Siksi kärjen 3 etäisyys on -4.

Nyt siirrytään toiseen iteraatioon.

Toinen iteraatio

Nyt tarkistamme jälleen kaikki reunat. Ensimmäinen reuna on (1, 3). Koska (0 + 5) on yhtä kuin 5, joka on suurempi kuin -4, joten pisteessä 3 ei tapahtuisi päivitystä.

Seuraava reuna on (1, 2). Koska (0 + 4) on 4, joten kärjessä 2 ei tapahtuisi päivitystä.

Seuraava reuna on (3, 2). Koska (-4 + 7) on 3, joka on pienempi kuin 4, joten päivitä:

d(v) = d(u) + c(u, v)

d(2) = d(3) +c(3, 2)

= -4 + 7 = 3

Siksi arvo kärjessä 2 on 3.

Seuraava reuna on (2, 4). Koska ( 3+7) on 10, joka on pienempi kuin 11, joten päivitä

d(v) = d(u) + c(u, v)

d(4) = d(2) +c(2, 4)

= 3 + 7 = 10

Siksi arvo kärjessä 4 on 10.

ääretön silmukka

Seuraava reuna on (4, 3). Koska (10 - 15) on -5, joka on pienempi kuin -4, joten päivitä:

d(v) = d(u) + c(u, v)

d(3) = d(4) +c(4, 3)

= 10 - 15 = -5

Siksi arvo kärjessä 3 on -5.

Nyt siirrytään kolmanteen iteraatioon.

Kolmas iteraatio

Nyt tarkistetaan jälleen kaikki reunat. Ensimmäinen reuna on (1, 3). Koska (0 + 5) on yhtä kuin 5, joka on suurempi kuin -5, joten pisteessä 3 ei tapahtuisi päivitystä.

Seuraava reuna on (1, 2). Koska (0 + 4) on yhtä kuin 4, joka on suurempi kuin 3, joten kärjessä 2 ei tapahtuisi päivitystä.

Seuraava reuna on (3, 2). Koska (-5 + 7) on 2, joka on pienempi kuin 3, joten päivitä:

d(v) = d(u) + c(u, v)

d(2) = d(3) +c(3, 2)

= -5 + 7 = 2

Siksi arvo kärjessä 2 on 2.

Seuraava reuna on (2, 4). Koska (2 + 7) on 9, joka on pienempi kuin 10, joten päivitä:

d(v) = d(u) + c(u, v)

d(4) = d(2) +c(2, 4)

= 2 + 7 = 9

Siksi arvo kärjessä 4 on 9.

Seuraava reuna on (4, 3). Koska (9 - 15) on -6, joka on pienempi kuin -5, joten päivitä:

d(v) = d(u) + c(u, v)

d(3) = d(4) +c(4, 3)

= 9 - 15 = -6

Siksi arvo kärjessä 3 on -6.

Bellman-Ford -algoritmi

Koska graafi sisältää 4 kärkeä, bellman ford -algoritmin mukaan iteraatioita olisi vain 3. Jos yritämme suorittaa 4thiteraatio graafissa, pisteiden etäisyys annetusta kärjestä ei saisi muuttua. Jos etäisyys vaihtelee, se tarkoittaa, että bellman ford -algoritmi ei anna oikeaa vastausta.

4thiterointi

Ensimmäinen reuna on (1, 3). Koska (0 +5) on 5, joka on suurempi kuin -6, joten kärkipisteessä 3 ei olisi muutosta.

Seuraava reuna on (1, 2). Koska (0 + 4) on suurempi kuin 2, päivitystä ei tapahtuisi.

Seuraava reuna on (3, 2). Koska (-6 + 7) on yhtä kuin 1, joka on pienempi kuin 3, joten päivitä:

d(v) = d(u) + c(u, v)

d(2) = d(3) +c(3, 2)

= -6 + 7 = 1

Tässä tapauksessa huippupisteen arvo päivitetään. Joten päätämme, että bellman ford -algoritmi ei toimi, kun kaavio sisältää negatiivisen painosyklin.

Siksi arvo kärjessä 2 on 1.