Found 1082 Articles for Go Programming

Golang program to find minimum spanning tree using dijkstra Algorithm

Akhil Sharma
Updated on 05-Apr-2023 15:29:19

410 Views

In this article we will write a go language program to find minimum spanning of tree. A minimum spanning tree (MST) is a tree that connects all the nodes in an undirected, weighted graph with the minimum possible edges. There are several Algorithms to find the minimum spanning tree of a graph like dijkastra Algorithm, prim's Algorithm and kruskal's Algorithm. What is Dijkistra Algorithm? Dijkstra's Algorithm is an Algorithm that finds the shortest path between a source vertex and all other vertices in a weighted graph with non-negative edge weights. It works by maintaining a set of visited vertices ... Read More

Golang program to find the last occurrence of a target element in a sorted slice

Akhil Sharma
Updated on 05-Apr-2023 15:28:23

125 Views

In this article, we will learn how to write a golang program to find the last occurrence of a target element in a sorted slice using linear and binary search approach. We will use two programs in this article. In the first program we will use the Linear search approach while in the second one we will use the binary search approach to implement the result. Using the Linear Search Approach The simplest method to find the last occurrence of a target element in a sorted slice is to perform a linear search. In this method, we iterate through the ... Read More

Go program to find the diameter of a graph using the Dijkstra Algorithm

Akhil Sharma
Updated on 05-Apr-2023 15:25:56

199 Views

In this article we will write a go language program to find the diameter of a graph. The diameter of a graph is the maximum distance between any two vertices in the graph. There are several Algorithms that can be used to find the diameter of a graph, including Dijkstra's Algorithm, Floyd-Warshall Algorithm, and Breadth-First Search Algorithm. Since, dijkastra Algorithm finds the shortest distance between source vertex and other vertices. We can also use it to find the largest distance by comparing the length of vertices recieved. Syntax func len(v Type) int The len() function is used to get ... Read More

Golang program to find the minimum number of coins needed to make a given amount of money

Akhil Sharma
Updated on 05-Apr-2023 15:25:18

202 Views

In this golang article we are going to find the minimum number of coins needed to make a given amount of money if a list of coins with different denominations is given. We can use the Greedy Algorithm for this problem. Algorithm Step 1 − First, we need to import the fmt package. Step 2 − Then create a function named minCoins() used to calculate the minimum number of coins. The function accepts two arguments one is the array of integers having number of coins and the amount that is to be formed. Step 3 − Inside this function ... Read More

Golang program to find maximum sum of a subarray with length k

Akhil Sharma
Updated on 05-Apr-2023 15:24:43

337 Views

In this article we are going to understand how to use methods naming brute-force, sliding window and prefix sum methods of golang to find the maximum sum of a subarray with length k. We will also discuss the Algorithm for each method and provide code Examples to demonstrate their implementation. Syntax func len(v Type) int The len() function is used to get the length of a any parameter. It takes one parameter as the data type variable whose length we wish to find and returns the integer value which is the length of the variable. Example 1 The first ... Read More

Golang program to traverse a circular linked list and print its elements

Akhil Sharma
Updated on 05-Apr-2023 15:24:05

234 Views

In this article we are going to understand how to creat a golang program to traverse a circular linked list and print it’s element using simple for and while loop. Circular linked lists are a type of data structure where the last element of the list is connected to the first element, forming a loop. Algorithm Step 1 − First, we need to import the fmt package. This struct contains a data variable to store data along with a pointer variable to store the address of the next node. Step 2 − Then create a function called Traverse() to ... Read More

Golang program to implement bucket sort

Akhil Sharma
Updated on 05-Apr-2023 15:23:15

272 Views

In this article we will learn to develop a go language program to implement the bucket sort through using custom sorting Algorithm. In Bucket sort we sort the unsorted array in different bucket every bucket contains a wide range of elements. The elements present in each bucket are then sorted by using a different sorting Algorithm, such as insertion sort or quicksort. Then the sorted buckets are merged back together. Algorithm Step 1 − First, we need to import the fmt package. Then create a function called bucketSort() which accepts the array to be sorted as an argument and ... Read More

Implement circular linked list in Golang

Akhil Sharma
Updated on 05-Apr-2023 15:21:58

474 Views

In this article we will learn how to write a go language program to implement circular linked list using struct and slice method. circular linked lists are created in such a way that each node of the linked list points to the next node and the last node points again to the starting node. Example 1 In this Example we will write a go language program to implement the circular linked lists by using a struct to store each node of the list. package main import "fmt" type Node struct { data int ... Read More

Implement Prims Algorithm in Golang

Akhil Sharma
Updated on 05-Apr-2023 15:20:55

247 Views

In this article we are going to learn how to use Binary Heap method and Priority queue method to implement prims Algorithm in golang. Prim's Algorithm is used to find the minimum spanning tree of a weighted undirected graph. Algorithm Step 1 − First, we need to import the fmt and heap package. Then create the required structs and functions and define properties to them. Step 2 − Further Initialize an empty visited set and a binary heap h with the minimum edge from the starting vertex s. Step 3 − Then create the main() function. Inside the function ... Read More

Implement Kruskals Algorithm in Golang

Akhil Sharma
Updated on 05-Apr-2023 15:20:06

257 Views

In this article we are going to understand how to develop a golang program to implement kruskals Algorithm with the help of union-find Algorithm and priority queue approach. Kruskal's Algorithm is used to find the minimum spanning tree of a graph. Algorithm Step 1 − First, we need to import the fmt and sort packages. Then create structures called Edge, graph and subset and assign properties to it. Step 2 − Then sort all the edges of the graph in non-decreasing order of their weight. Step 3 − Create a disjoint set data structure, where each set contains ... Read More

Advertisements