Golang program to find the longest path in a weighted directed acyclic graph using Bellman-Ford algorithm


In this article, we will write a Go language program to find the longest path in a weighted directed acyclic graph using Bellman-ford-algorithm. Bellman-Ford-Algorithm is a popular algorithm used to find the longest path from source vertex to other vertices in a weighted directed graph.

Syntax

func range(variable)

Any data type may be iterated over using the range function. This may be used by first writing the range keyword, then the data type to which we wish to iterate.

func make ([] type, size, capacity)

When creating an array or map in the Go programming language, the make function receives three arguments: the kind of variable to be created, its size, and its capacity.

Algorithm

  • Step 1 − Create an Edge struct with three fields:- src, dest and weight of type int

  • Step 2 − In this step, create a find_longest_path function with edges, vertices and source as input parameters

  • Step 3 − This function returns an array that depicts the longest path distance from the source vertex to all other vertices

  • Step 4 − In this step, create an array by the name dist using make as built-in function to store the path distance

  • Step 5 − Initially, using a for loop, in each iteration set the elements of dist to math.MinInt32 and set the source vertex to 0. Then, use a nested loop to iterate vertices-1 times in the outer loop. In the inner loop, iterate over each edge in the edges array

  • Step 6 − For each edge, calculate the source vertex u, the destination vertex v, and the weight w. Then, check if the distance to the source vertex u is not math.MinInt32

  • Step 7 − Then see, if the sum of the distance to u and the weight w is greater than the current distance to v modify the distance to v as the sum of the distance to u and the weight w

  • Step 8 − The dist array will store the longest path distances from the source vertex to all other vertices

  • Step 9 − Finally, in the main, create the number of vertices and the list of edges

  • Step 10 − Here, create a source vertex variable for which you want to find the longest path distances. Then, call the find_longest_path function with edges, vertices and source as arguments. The result obtained will be stored in the longest_path variable

  • Step 11 − Finally, use a for loop to print the longest path distance for each vertex with vertex index and the distance and print the statement using the Println function from the fmt package.

Example

Bellman ford algorithm relaxes the edges of the graph repeatedly and update the distance values until the best longest path is found.

package main

import (
	"fmt"
	"math"
)
type Edge struct {
	src, dest, weight int
}

func find_longest_path(edges []Edge, vertices, source int) []int {
	dist := make([]int, vertices)
	for i := range dist {
		dist[i] = math.MinInt32
	}
	dist[source] = 0

	for i := 0; i < vertices-1; i++ {
		for _, edge := range edges {
			u := edge.src
			v := edge.dest
			w := edge.weight

			if dist[u] != math.MinInt32 && dist[u]+w > dist[v] {
				dist[v] = dist[u] + w
			}
		}
	}
	return dist
}

func main() {
	vertices := 6
	edges := []Edge{
		{0, 1, 5},
		{0, 2, 3},
		{1, 3, 6},
		{1, 2, 2},
		{2, 4, 4},
		{2, 5, 2},
		{2, 3, 7},
		{3, 5, 1},
		{3, 4, -1},
		{4, 5, -2},
	}

	source := 1
	longest_path := find_longest_path(edges, vertices, source)

	fmt.Println("Longest path distances from source vertex", source, "to all other vertices:")
	for I, dist := range longest_path {
		fmt.Println("Vertex", I, ":", dist)
	}
}

Output

Longest path distances from source vertex 1 to all other vertices:
Vertex 0 : -2147483648
Vertex 1 : 0
Vertex 2 : 2
Vertex 3 : 9
Vertex 4 : 8
Vertex 5 : 10

Conclusion

In this article we have explored a program to find the longest path in a weighted directed acyclic graph using Bellman-Ford algorithm. This is important to note that the time complexity of bellman ford algorithm is O(V*E), where E and V represent number of edges and number of vertices respectively.

Updated on: 06-Jul-2023

258 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements