Question
in c++
The Bellman-Ford Algorithm In this assignment, you are asked to implement the Bellman-Ford Algorithm which solves the single-

Output 1 TRUE INFINITY Input 2 6 11 016 1 2 5 1 3-4 14 8 21-2 3 0 2 3 2 7 3 49 3 5 -14 4 0 7 5 2 5 Output 2 FALSE Note that e
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:-

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
struct Edge
{
   int src, dest, weight;
};
struct Graph
{
   int V, E;
   struct Edge* edge;
};
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)
{
   for (int i = 0; i < n; ++i)
   {
       if(dist[i] == INT_MAX)
           printf("INFINITY\n");
       else
           printf("%d\n", dist[i]);
   }
}
void BellmanFord(struct Graph* graph, int src)
{
   int V = graph->V;
   int E = graph->E;
   int dist[V];
   for (int i = 0; i < V; i++)
   dist[i] = INT_MAX;
   dist[src] = 0;
   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;
       }
   }
   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])
       {
           cout << "FALSE";
           return;
       }
   }
   cout << "TRUE\n";
   printArr(dist, V);
}
main()
{
   int V;
   int E;
   cin >> V >> E;
   int u, v, c;
   struct Graph* graph = createGraph(V, E);
   for(int i=0; i<E; i++)
   {
       cin >> u >> v >> c;
       graph->edge[i].src = u;
       graph->edge[i].dest = v;
       graph->edge[i].weight = c;
   }
   BellmanFord(graph, 0);
}

Code Screenshots:-

Outputs:-

Please UPVOTE thank you...!!!

Add a comment
Know the answer?
Add Answer to:
in c++ The Bellman-Ford Algorithm In this assignment, you are asked to implement the Bellman-Ford Algorithm...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please help me with this answer. Performance Comparison for Dijkstra Algorithm and Bellman-Ford Algorithm Problem Description...

    Please help me with this answer. Performance Comparison for Dijkstra Algorithm and Bellman-Ford Algorithm Problem Description The shortest path problem is one of most important problems in graph theory and computer science in general. Shortest path problem is one of typical optimization problems. Given a graph G = (V,E), the goal is to nd a minimum cost path from s → t, s,t ∈ V . This variant is called one-to-one shortest path problem. Other variants are one-to-all (compute shortest...

  • Problem definition: Give the program that implement Prim’s algorithm. Input: First line is N, denotes the...

    Problem definition: Give the program that implement Prim’s algorithm. Input: First line is N, denotes the amount of test case, then there are Ns graph data with an option number (determine whether output the selected edges or not). Each graph is undirected and connected, it is composed of V (the number of vertices, <= 1000), E (the number of edges, <=10000), then followed by Es edges which are denoted by pair of vertex and weight (e.g., 2 4 10 means...

  • Input a simple undirected weighted graph G with non-negative edge weights (represented by w), and a...

    Input a simple undirected weighted graph G with non-negative edge weights (represented by w), and a source node v of G. Output: TDB. D: a vector indexed by the vertices of G. Q: priority queue containing the vertices of G using D[] as key D[v]=0; for (all vertex ut-v) [D[u]-infinity:) while not Q. empty() 11 Q is not empty fu - Q.removein(); // retrieve a vertex of Q with min D value for (all vertex : adjacent to u such...

  • Hello, I'd like someone to help me create these, thanks! 1. Type Vertex Create and document...

    Hello, I'd like someone to help me create these, thanks! 1. Type Vertex Create and document type Vertex. Each vertex v has the following pieces of information. A pointer to a linked list of edges listing all edges that are incident on v. This list is called an adjacency list. A real number indicating v's shortest distance from the start vertex. This number is −1 if the distance is not yet known. A vertex number u. The shortest path from...

  • 310/6310 Quiz 3 Fall 2017 NAME 4. Using Bellman-Ford algorithm, find the shortest paths from the...

    310/6310 Quiz 3 Fall 2017 NAME 4. Using Bellman-Ford algorithm, find the shortest paths from the vertex 3 to all other vertices Path 3-> I: Path 3->2: Path 3.5 Path 3-6: Path 3-3: Path 3.>4: 3 5 2 3 4 How many key-value pairs will be generated in total by all mappers at every iteration of MapReduce implementation of the algorithm? Explain your answer NAME: Quiz3 CS4310

  • Run the Dijkstra’s algorithm on the directed graph of the following figure 24.6, using vertex t...

    Run the Dijkstra’s algorithm on the directed graph of the following figure 24.6, using vertex t as the source. In the style of Figure 24.6, show the d and ? values and the vertices in set S after each iteration of the while loop. 1 8 10 I 10 14 4 6 4 6 2 3 2 3 4 6 5 5 2 (a) (c) 1 10 13 4 6 (d) (e) Figure 24.6 The execution of Dijkstra's algorithm. The...

  • Question 3. Below is the result of the 1st and 2nd iteration of the Bellman-Ford single...

    Question 3. Below is the result of the 1st and 2nd iteration of the Bellman-Ford single source shortest path algorithm starting at node A A B C D E B 2 000 0-14 E 0000 DO (D Please note the above table does not contain the pi or previous node values. Please provide the changes to the tables that occure during the third iteration only for distance(shortest path estimation) when processing only the edges: edges (D,C), (B,C),(D,B), (B,D) (B,E) and...

  • a. You have 5 problems in this assignment. b. G++ compiler will be used to compile...

    a. You have 5 problems in this assignment. b. G++ compiler will be used to compile your source codes. c. Your program will be tested on Ubuntu 16.04. d. You are not allowed to use global variables in your implementation. e. Your program will get two arguments, input and output file names, from the command line: >> Your_Executable INPUT_FILE_NAME OUTPUT_FILE_NAME 1. Given a number ? , we initially have ?+1 different sets which are {0}, {1}, {2}, ... , {?}....

  • Please follow all the instructions and do all the parts (a-d)! Create a Java program which implem...

    Please follow all the instructions and do all the parts (a-d)! Create a Java program which implements Dijkstra’s shortest path algorithm according to the psueudocode below. Base the design on the weighted graph implementation used in Problem3.java (provided at the bottom). Your program should include the following features: a. A new method receives a starting vertex index and a target vertex index (e.g. 0 and 4). It computes the shortest distances to all vertexes using Dijkstra’s algorithm. The pseudocode is...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT