Golang program to find the minimum number of edges in a weighted directed graph that connects all nodes


In this article, we will write Go language programs to find the minimum no. of edges in a weighted directed graph that connects all nodes. We will use Prim’s algorithm to perform this operation. It is a greedy algorithm which is used to find the minimum spanning tree for a 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)

The go language's make function is used to build an array or map; it takes as parameters the kind of variable to be formed, along with its size and capacity.

func len(v Type) int

The length of any argument may be determined using the len() method. It requires a single parameter—the data-type variable whose length we want to determine—and returns an integer value that represents the variable's length.

Algorithm

  • Step 1 − Create a struct Edge to represent a graph edge. Each edge comprises source and target vertices, as well as the edge's weight.

  • Step 2 − Create a function called minEdgesInGraph that accepts the edges of the graph and the number of vertices as input and returns the smallest number of edges needed to link all nodes.

  • Step 3 − Create a 2D distances matrix with maximum distances between all vertices except the diagonal members, which are set to 0. The distances between vertices are represented by this matrix.

  • Step 4 − Iterate through the input edges, updating the distances matrix as needed. Set each edge's weight to the distance between the source and target vertices.

  • Step 5 − Use the Floyd-Warshall method to discover the shortest distances between all pairs of vertices in the distances matrix. Consider intermediate vertices while updating the distance vector.

  • Step 6 − In the distances matrix, find the greatest distance.

  • Step 7 − Count the number of edges in the graph that have the greatest distance between them. This number reflects the bare minimum of edges needed to link all nodes in the network.

  • Step 8 − As an input, provide the graph's edges and the number of vertices to the minEdges function, and save the result in the minEdges variable.

  • Step 9 − Print the shortest number of edges needed to link all nodes in the network.

Example

In this Example, we will write a Go language program to find minimum no. of edges in a weighted directed graph using prim's algorithm which helps to find minimum spanning tree.

package main

import (
	"fmt"
	"math"
)
type Edge struct {
	source, target, weight int
}
func minEdgesInGraph(edges []Edge, numVertices int) int {
	distances := make([][]int, numVertices)
	for i := range distances {
		distances[i] = make([]int, numVertices)
		for j := range distances[i] {
			if i != j {
				distances[i][j] = math.MaxInt32
			}
		}
	}

	for _, edge := range edges {
		distances[edge.source][edge.target] = edge.weight
	}

	for k := 0; k < numVertices; k++ {
		for i := 0; i < numVertices; i++ {
			for j := 0; j < numVertices; j++ {
				if distances[i][k]+distances[k][j] < distances[i][j] {
					distances[i][j] = distances[i][k] + distances[k][j]
				}
			}
		}
	}

	maxDistance := 0
	for i := 0; i < numVertices; i++ {
		for j := 0; j < numVertices; j++ {
			if distances[i][j] > maxDistance {
				maxDistance = distances[i][j]
			}
		}
	}

	count := 0
	for i := 0; i < numVertices; i++ {
		for j := 0; j < numVertices; j++ {
			if distances[i][j] == maxDistance {
				count++
			}
		}
	}

	return count
}
func main() {
	edges := []Edge{
		{0, 1, 4},
		{0, 2, 3},
		{1, 2, 1},
		{1, 3, 2},
		{2, 3, 4},
		{2, 4, 2},
		{3, 4, 3},
		{3, 5, 1},
		{4, 5, 5},
	}

	numVertices := 6
	minEdges := minEdgesInGraph(edges, numVertices)
	fmt.Println("Minimum number of edges in the graph:", minEdges)
}

Output

Minimum number of edges in the graph : 15

Conclusion

In this article we have compiled and executed the program of finding the minimum number of edges in a weighted directed graph that connects all nodes using prim's algorithm.

Updated on: 06-Jul-2023

83 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements