Skip to content

Category Archives: Graph

Given a graph and a source vertex in the graph, find the shortest paths from the source to all vertices in the given graph. Examples:… Read More
We recommend reading the following two posts as a prerequisite to this post. Greedy Algorithms | Set 5 (Prim’s Minimum Spanning Tree (MST))  Graph and… Read More
Introduction to Prim’s algorithm: We have discussed Kruskal’s algorithm for Minimum Spanning Tree. Like Kruskal’s algorithm, Prim’s algorithm is also a Greedy algorithm. Prim’s algorithm always starts… Read More
A graph is a data structure that consists of the following two components: 1. A finite set of vertices also called as nodes. 2. A finite set… Read More
What is a Spanning Tree? A Spanning tree is a subset to a connected graph G, where all the edges are connected, i.e, one can… Read More
In the previous post, we introduced union find algorithm and used it to detect cycle in a graph. We used following union() and find() operations… Read More
A disjoint-set data structure is defined as a data structure that keeps track of a set of elements partitioned into a number of disjoint (non-overlapping)… Read More
Given a binary 2D matrix, find the number of islands. A group of connected 1s forms an island. For example, the below matrix contains 5… Read More
The Floyd Warshall Algorithm is for solving all pairs shortest path problems. The problem is to find the shortest distances between every pair of vertices… Read More
Hamiltonian Path in an undirected graph is a path that visits each vertex exactly once. A Hamiltonian cycle (or Hamiltonian circuit) is a Hamiltonian Path… Read More
Given an undirected graph and a number m, determine if the graph can be colored with at most m colors such that no two adjacent… Read More
Given a Directed Graph and two vertices in it, check whether there is a path from the first given vertex to second. Example:  Consider the following… Read More
Given the root of a Directed graph, The task is to check whether the graph contains a cycle if yes then return true, return false… Read More
Breadth-First Traversal (or Search) for a graph is similar to Breadth-First Traversal of a tree (See method 2 of this post).  The only catch here… Read More
Depth First Traversal (or Search) for a graph is similar to Depth First Traversal of a tree. The only catch here is, that, unlike trees,… Read More

Start Your Coding Journey Now!