Kruskal’s Algorithm in Kotlin

Kruskal’s Algorithm is a greedy algorithm used to find the minimum spanning tree of a weighted undirected graph. It works by sorting the edges of the graph in increasing order of weight and adding them to the tree one by one, provided that the edge does not create a cycle in the tree. The algorithm … Read more

Prim’s Algorithm in Kotlin MST

Prim’s Algorithm is a greedy algorithm used to find the minimum spanning tree of a weighted undirected graph. It starts by selecting a random vertex from the graph and repeatedly adding the closest vertex that has not yet been added to the tree. The algorithm maintains a priority queue of edges with their weights and … Read more

DFS in Kotlin. Depth First Search

DFS stands for Depth-First Search. It is a graph traversal algorithm that starts at a given source vertex and explores as far as possible along each branch before backtracking. In other words, it explores the vertices of a graph in depth-first order until there are no more vertices left to explore. To implement DFS, we … Read more

BFS in Kotlin. Breath First Search

In this implementation, we define a Graph class that stores the graph as an adjacency list. The addEdge method adds an undirected edge between two vertices. The bfs method performs BFS starting from the given startVertex. We maintain a visited array to keep track of which vertices we have visited, and a queue to store … Read more

Floyd-Warshall Algorithm in Kotlin

The Floyd-Warshall algorithm (also known as Warshall’s algorithm) is an algorithm used to find the shortest paths in a weighted graph with positive or negative edge weights. The algorithm was developed by Robert Floyd and Stephen Warshall in 1962. Here’s how the algorithm works: The floydWarshall function takes a 2D array graph representing the adjacency … Read more

Dijkstra Algorithm in Kotlin

Dijkstra’s algorithm is a popular algorithm used in computer science to find the shortest path between two nodes in a weighted graph. The algorithm was invented by Edsger W. Dijkstra in 1956. Here’s how the algorithm works: Dijkstra’s algorithm is commonly used in applications such as routing protocols in computer networks, finding shortest paths in … Read more

Detect Cycle in Directed Graph in Kotlin

Detecting cycles in directed graphs is a bit more complicated than in undirected graphs because there are two types of edges – forward and backward – that need to be considered. One way to detect cycles in a directed graph is to use depth-first search (DFS) and maintain a stack of visited vertices. To detect … Read more