0% found this document useful (0 votes)
59 views5 pages

Vinodbellmanfoldprog 3

1. The document describes using the Bellman-Ford algorithm to find the shortest path between any two nodes in a directed graph that may contain negative edges. 2. It provides pseudocode to implement the Bellman-Ford algorithm, which works by relaxing all edges multiple times to accurately propagate minimum distances throughout the graph. 3. For a sample graph, the algorithm correctly finds the shortest path between nodes 1 and 6 and outputs the path and distance.

Uploaded by

Vinod Mogadala
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views5 pages

Vinodbellmanfoldprog 3

1. The document describes using the Bellman-Ford algorithm to find the shortest path between any two nodes in a directed graph that may contain negative edges. 2. It provides pseudocode to implement the Bellman-Ford algorithm, which works by relaxing all edges multiple times to accurately propagate minimum distances throughout the graph. 3. For a sample graph, the algorithm correctly finds the shortest path between nodes 1 and 6 and outputs the path and distance.

Uploaded by

Vinod Mogadala
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

DESIGN AND SIMULATION LAB1

M.VINOD KUMAR (11EC64Q01) DOS: 02/09/11

PROBLEM STATEMENT: Write a C program to find shortest path between any two
nodes in the directed graph with negative edges by using BELLMAN -FORD ALOGRITM. The cost of the shortest path and the path traced is to be displayed as output.

DESIGN: Bellman- ford algorithm solves the shortest path problem for directed graphs
with negative edges. BellmanFord is in its basic structure very similar to Dijkstras algorithm, but instead of greedily selecting the minimum-weight node not yet processed to relax, it simply relaxes all the edges, and does this no of nodes minus one times in the graph. The repetitions allow minimum distances to accurately propagate throughout the graph, since in the absence of negative cycles the shortest path can only visit each node at most once. 1. We must assign the source node a distance of zero and all other nodes a distance of infinity. 2. If v is a node adjacent to u and w maps nodes to their weights, d is the distance map records the length of the shortest path to each node seen , so far , p is a predecessor map which records the parent of each node. For i= 1 to n-1; If w (u, v) +d (u) < d (v) d (v) = w (u, v) +d (u); p (v) = u; 3. For detecting negative cycle we have to repeat the one more time and we to check w (u, v) +d (u) < d (v) , if this condition satisfies then the graph contains negative weight cycle.

IMPLEMENTATION:
Get the no of nodes and distance between them in the network. Ask the user to enter the source and destination node. Initially the loop executed no of nodes -1 ,while executing it calculates the distance from source node to each and every node in the network. Next it executes the loop one more time to check whether there is any negative loop or not And finally it displays the distance between the source and destination and prints the path traversed.

DISCUSSIONS:

1. Bellman-ford algorithm gives shortest path from source node to destination node only if no negative weight cycle is reachable from source node. 2. If a graph contains only positive weights, then to find shortest path Dijkstras algorithm provides a more efficient alternative.

RESULTS: For the network shown below the shortest path between the source node 1
and destination node 6 is found.
2

4 2
1

2 -4
6

3
3

2 3
5

CODE:
#include <stdio.h> #define INFINITY 9999 #define MAX 100 int main(void) { int i,j,N,B,node1,node2,dist,src,dest,worknode,min,minnode,x,k,d; int distmat[MAX][MAX]; struct nodefmt { int srcdist; int prevnode; }; struct nodefmt node[MAX];

for(i=0;i<MAX;i++) { node[i].srcdist=INFINITY; node[i].prevnode=MAX+1; for(j=0;j<MAX;j++) { distmat[i][j]=INFINITY; } } printf("\nHow many nodes are there?"); scanf("%d",&N); printf("\nNumber of Branches ?"); scanf("%d",&B); printf("\nEnter the distance between the nodes "); for(i=0;i<B;i++) { printf("\n( Node1 Node2 Distance) for branch %d\t" , i+1); scanf("%d%d%d",&node1,&node2,&dist); distmat[node1-1][node2-1]=dist; } printf("\nEnter source node and destination node\t"); scanf("%d%d",&src,&dest); node[src-1].srcdist=0; node[src-1].prevnode=src-1; for(k=0;k<N-1;k++) { for(i=0;i<N;i++) { d=node[i].srcdist; for(j=0;j<N;j++) { if(i==j) continue; else if(node[j].srcdist>(d+distmat[i][j])) { node[j].srcdist=d+distmat[i][j]; node[j].prevnode=i; } } }

} for ( i = 0; i <=N; i++ ) { d=node[i].srcdist; for(j=0;j<N;j++) { if(i==j) continue; else if(node[j].srcdist>(d+distmat[i][j])) { printf("graph contains negative cycle"); return 1; } } } j=dest-1; printf("\nThe Shortest Path Is \n"); printf("%d",dest); for(i=0;j!=src-1;i++) { printf(" - %d",((node[j].prevnode)+1)); j=node[j].prevnode; } printf("\n\nShortest Distance is %d",node[dest-1].srcdist); getchar(); return 0; } How many nodes are there? 6 Number of Branches? 8 Enter the distance between the nodes Node1 Node2 Distance for branch 1 1 3 3 Node1 Node2 Distance for branch 2 1 2 4 Node1 Node2 Distance for branch 3 3 5 3 Node1 Node2 Distance for branch 4 2 4 4 Node1 Node2 Distance for branch 5 4 6 2 Node1 Node2 Distance for branch 6 1 4 2 Node1 Node2 Distance for branch 7 5 6 2 Node1 Node2 Distance for branch 8 2 5 -4 Enter source node and destination node 1 6 The Shortest Path Is 6 5-2 1. Shortest Distance is 2

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy