Minimum Weight Cycle in a Graph
Last Updated : 13 May, 2025
Given an undirected, weighted graph with V vertices numbered from 0 to V-1, and E edges represented as a 2D array edges[][]
, where each element edges[i] = [u, v, w]
denotes an edge between nodes u
and v
with weight w
, and all edge weights are positive integers, your task is to find the minimum weight cycle in the graph.
Note: A cycle in a graph is a path that starts and ends at the same vertex without repeating any edges or vertices (except the starting/ending vertex).
The minimum weight cycle is the one among all possible cycles that has the smallest total sum of edge weights
Examples:
Input: V = 5, edges = [[0, 1, 2], [1, 2, 2], [1, 3, 1], [1, 4, 1], [0, 4, 3], [2, 3, 4]]
Output: 6
Explaination:
The minimum-weighted cycle is 0 → 1 → 4 → 0
with a total weight of 6
(2 + 1 + 3)
[Naive Approach] Find all cycle weight
Find all cycles in the graph using DFS, and while exploring each cycle, keep track of its total weight. Update and maintain the minimum weight found across all such cycles.
C++ //Driver Code Starts #include <algorithm> #include <climits> #include <iostream> #include <vector> using namespace std; // Construct the adjacency list vector<vector<vector<int>>> constructAdj(int V, vector<vector<int>> &edges) { vector<vector<vector<int>>> adj(V); for (auto &edge : edges) { int u = edge[0], v = edge[1], w = edge[2]; adj[u].push_back({v, w}); adj[v].push_back({u, w}); } return adj; } //Driver Code Ends int minCycle; // DFS to explore cycles and track their weights void dfs(int u, int parent, vector<vector<vector<int>>> &adj, vector<bool> &visited, vector<int> &path,vector<int> &weights, int currWeight){ visited[u] = true; path.push_back(u); for (auto &edge : adj[u]){ int v = edge[0]; int w = edge[1]; // avoid going back to the parent if (v == parent) continue; if (!visited[v]){ weights.push_back(w); dfs(v, u, adj, visited, path, weights, currWeight + w); weights.pop_back(); } else{ // Found a cycle auto it = find(path.begin(), path.end(), v); if (it != path.end()){ int cycleWeight = 0; int idx = it - path.begin(); for (int i = idx; i < weights.size(); i++){ cycleWeight += weights[i]; } // add the closing edge cycleWeight += w; minCycle = min(minCycle, cycleWeight); } } } path.pop_back(); visited[u] = false; } int findMinCycle(int V, vector<vector<int>> &edges) { vector<vector<vector<int>>> adj = constructAdj(V, edges); minCycle = INT_MAX; vector<bool> visited(V, false); vector<int> path, weights; for (int i = 0; i < V; i++) { dfs(i, -1, adj, visited, path, weights, 0); } return minCycle; } //Driver Code Starts int main() { int V = 5; vector<vector<int>> edges = {{0, 1, 2}, {1, 2, 2}, {1, 3, 1}, {1, 4, 1}, {0, 4, 3}, {2, 3, 4}}; int result = findMinCycle(V, edges); cout << result << endl; return 0; } //Driver Code Ends
Java //Driver Code Starts import java.util.*; public class Solution { // Construct the adjacency list static ArrayList<ArrayList<int[]>> constructAdj(int V, int[][] edges) { ArrayList<ArrayList<int[]>> adj = new ArrayList<>(); for (int i = 0; i < V; i++) adj.add(new ArrayList<>()); for (int[] edge : edges) { int u = edge[0], v = edge[1], w = edge[2]; adj.get(u).add(new int[]{v, w}); adj.get(v).add(new int[]{u, w}); } return adj; //Driver Code Ends } static int minCycle; // DFS to explore cycles and track their weights static void dfs(int u, int parent,ArrayList<ArrayList<int[]>> adj, boolean[] visited, ArrayList<Integer> path, ArrayList<Integer> weights,int currWeight) { visited[u] = true; path.add(u); for (int[] edge : adj.get(u)) { int v = edge[0]; int w = edge[1]; // avoid going back to the parent if (v == parent) continue; if (!visited[v]) { weights.add(w); dfs(v, u, adj, visited, path, weights, currWeight + w); weights.remove(weights.size() - 1); } else { // Found a cycle int idx = path.indexOf(v); if (idx != -1) { int cycleWeight = 0; for (int i = idx; i < weights.size(); i++) { cycleWeight += weights.get(i); } // add the closing edge cycleWeight += w; minCycle = Math.min(minCycle, cycleWeight); } } } path.remove(path.size() - 1); visited[u] = false; } static int findMinCycle(int V, int[][] edges) { ArrayList<ArrayList<int[]>> adj = constructAdj(V, edges); minCycle = Integer.MAX_VALUE; boolean[] visited = new boolean[V]; ArrayList<Integer> path = new ArrayList<>(); ArrayList<Integer> weights = new ArrayList<>(); for (int i = 0; i < V; i++) { dfs(i, -1, adj, visited, path, weights, 0); } return minCycle; } //Driver Code Starts public static void main(String[] args) { int V = 5; int[][] edges = { {0, 1, 2}, {1, 2, 2}, {1, 3, 1}, {1, 4, 1}, {0, 4, 3}, {2, 3, 4} }; int result = findMinCycle(V, edges); System.out.println(result); } } //Driver Code Ends
Python #Driver Code Starts import sys # Construct the adjacency list def constructAdj(V, edges): adj = [[] for _ in range(V)] for edge in edges: u, v, w = edge adj[u].append([v, w]) adj[v].append([u, w]) return adj #Driver Code Ends minCycle = sys.maxsize # DFS to explore cycles and track their weights def dfs(u, parent, adj, visited, path, weights, currWeight): global minCycle visited[u] = True path.append(u) for edge in adj[u]: v = edge[0] w = edge[1] # avoid going back to the parent if v == parent: continue if not visited[v]: weights.append(w) dfs(v, u, adj, visited, path, weights, currWeight + w) weights.pop() else: # Found a cycle if v in path: idx = path.index(v) cycleWeight = sum(weights[idx:]) + w # add the closing edge minCycle = min(minCycle, cycleWeight) path.pop() visited[u] = False def findMinCycle(V, edges): global minCycle adj = constructAdj(V, edges) minCycle = sys.maxsize visited = [False] * V path = [] weights = [] for i in range(V): dfs(i, -1, adj, visited, path, weights, 0) return minCycle #Driver Code Starts # Main V = 5 edges = [[0, 1, 2], [1, 2, 2], [1, 3, 1], [1, 4, 1], [0, 4, 3], [2, 3, 4]] result = findMinCycle(V, edges) print(result) #Driver Code Ends
C# //Driver Code Starts using System; using System.Collections.Generic; class GfG { // Construct the adjacency list static List<List<int[]>> constructAdj(int V, int[,] edges) { var adj = new List<List<int[]>>(); for (int i = 0; i < V; i++) adj.Add(new List<int[]>()); for (int i = 0; i < edges.GetLength(0); i++) { int u = edges[i, 0], v = edges[i, 1], w = edges[i, 2]; adj[u].Add(new int[] { v, w }); adj[v].Add(new int[] { u, w }); } return adj; } //Driver Code Ends static int minCycle; // DFS to explore cycles and track their weights static void dfs(int u, int parent, List<List<int[]>> adj, bool[] visited, List<int> path, List<int> weights, int currWeight) { visited[u] = true; path.Add(u); foreach (var edge in adj[u]) { int v = edge[0]; int w = edge[1]; // avoid going back to the parent if (v == parent) continue; if (!visited[v]) { weights.Add(w); dfs(v, u, adj, visited, path, weights, currWeight + w); weights.RemoveAt(weights.Count - 1); } else { // Found a cycle int idx = path.IndexOf(v); if (idx != -1) { int cycleWeight = 0; for (int i = idx; i < weights.Count; i++) { cycleWeight += weights[i]; } // add the closing edge cycleWeight += w; minCycle = Math.Min(minCycle, cycleWeight); } } } path.RemoveAt(path.Count - 1); visited[u] = false; } static int findMinCycle(int V, int[,] edges) { var adj = constructAdj(V, edges); minCycle = int.MaxValue; var visited = new bool[V]; var path = new List<int>(); var weights = new List<int>(); for (int i = 0; i < V; i++) { dfs(i, -1, adj, visited, path, weights, 0); } return minCycle; } //Driver Code Starts public static void Main() { int V = 5; int[,] edges = { {0, 1, 2}, {1, 2, 2}, {1, 3, 1}, {1, 4, 1}, {0, 4, 3}, {2, 3, 4} }; int result = findMinCycle(V, edges); Console.WriteLine(result); } } //Driver Code Ends
JavaScript //Driver Code Starts // Construct the adjacency list function constructAdj(V, edges) { const adj = Array.from({ length: V }, () => []); for (const edge of edges) { const [u, v, w] = edge; adj[u].push([v, w]); adj[v].push([u, w]); } return adj; } //Driver Code Ends let minCycle = Number.MAX_SAFE_INTEGER; // DFS to explore cycles and track their weights function dfs(u, parent, adj, visited, path, weights, currWeight) { visited[u] = true; path.push(u); for (const edge of adj[u]) { const v = edge[0]; const w = edge[1]; // avoid going back to the parent if (v === parent) continue; if (!visited[v]) { weights.push(w); dfs(v, u, adj, visited, path, weights, currWeight + w); weights.pop(); } else { // Found a cycle const idx = path.indexOf(v); if (idx !== -1) { let cycleWeight = 0; for (let i = idx; i < weights.length; i++) { cycleWeight += weights[i]; } // add the closing edge cycleWeight += w; minCycle = Math.min(minCycle, cycleWeight); } } } path.pop(); visited[u] = false; } function findMinCycle(V, edges) { const adj = constructAdj(V, edges); minCycle = Number.MAX_SAFE_INTEGER; const visited = Array(V).fill(false); const path = []; const weights = []; for (let i = 0; i < V; i++) { dfs(i, -1, adj, visited, path, weights, 0); } return minCycle; } //Driver Code Starts // Main const V = 5; const edges = [[0, 1, 2], [1, 2, 2], [1, 3, 1], [1, 4, 1], [0, 4, 3], [2, 3, 4]]; const result = findMinCycle(V, edges); console.log(result); //Driver Code Ends
Time Complexity: O(2V)The time complexity is exponential in the worst case (e.g. O(2V)) due to the large number of simple cycles, but in typical cases, especially for sparse graphs, it may behave closer to O(V×(V + E))
Space Complexity: O(V+E), as dominated by the storage for the graph (adjacency list) and the DFS auxiliary structures (visited array, path, weights, and recursion stack).
[Expected Approach] : Using Dijkstra's algorithm - O(E * (V + E) log V) Time and O(V+E) Space
To find the shortest cycle in the graph, we iterate over each edge (u,v,w)and temporarily remove it. The idea is that any edge might be part of the minimum weight cycle, so we check if a cycle can still be formed without it.
We then use Dijkstra's algorithm (or any shortest path algorithm) to find the shortest path from u to v while excluding the removed edge. If such a path exists, adding the removed edge back completes a cycle. The total weight of this cycle is the sum of the shortest path and the weight of the removed edge.
By repeating this process for every edge, we ensure that all possible cycles are considered, and we avoid redundant checks. Among all valid cycles found, we track and return the one with the minimum total weight. This method is both efficient and systematic for identifying the shortest cycle in a weighted, undirected graph.
Step by step Implementation:
- First, construct the adjacency list representation of the graph based on the provided edges.
- Iterate through each edge in the graph and temporarily exclude it during the calculations.
- For each excluded edge, use Dijkstra's algorithm to compute the shortest path between the source and destination nodes.
- After calculating the shortest distance, if it's not infinity, it means a path exists between the source and destination even without the excluded edge, forming a potential cycle.
- Repeat the process for all edges and track the minimum cycle weight by adding the excluded edge's weight to the shortest path found
Illustration:
C++ //Driver Code Starts #include <bits/stdc++.h> using namespace std; // Construct adjacency list vector<vector<vector<int>>> constructadj(int V, vector<vector<int>>& edges) { vector<vector<vector<int>>> adj(V); for (auto& edge : edges) { int u = edge[0], v = edge[1], w = edge[2]; adj[u].push_back({v, w}); adj[v].push_back({u, w}); } return adj; } //Driver Code Ends // find sortest path between src and dest. int shortestPath(int V, vector<vector<vector<int>>>& adj, int src, int dest){ vector<int> dist(V, INT_MAX); dist[src] = 0; // priority queue priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> pq; pq.push({0, src}); while (!pq.empty()) { pair<int, int> top = pq.top(); pq.pop(); int d = top.first, u = top.second; if (d > dist[u]) continue; for (auto& neighbor : adj[u]) { int v = neighbor[0]; int w = neighbor[1]; // Skip the ignored edge if ((u == src && v == dest) || (u == dest && v == src)) continue; if (dist[v] > dist[u] + w) { dist[v] = dist[u] + w; pq.push({dist[v], v}); } } } return dist[dest]; } // Function to find the minimum weight cycle in the graph int findMinCycle(int V, vector<vector<int>>& edges) { vector<vector<vector<int>>> adj = constructadj(V, edges); int minCycle = INT_MAX; for (const auto& edge : edges) { int u = edge[0]; int v = edge[1]; int w = edge[2]; int dist = shortestPath(V, adj, u, v); if (dist != INT_MAX) { minCycle = min(minCycle, dist + w); } } return minCycle; } //Driver Code Starts // Driver code int main() { int V = 5; vector<vector<int>> edges = { {0, 1, 2}, {1, 2, 2}, {1, 3, 1}, {1, 4, 1}, {0, 4, 3}, {2, 3, 4} }; cout << findMinCycle(V, edges) << endl; return 0; } //Driver Code Ends
Java //Driver Code Starts import java.util.*; public class MinimumCycle { // Construct adjacency list public static ArrayList<ArrayList<int[]>> constructAdj(int V, int[][] edges) { ArrayList<ArrayList<int[]>> adj = new ArrayList<>(); for (int i = 0; i < V; i++) adj.add(new ArrayList<>()); for (int[] edge : edges) { int u = edge[0], v = edge[1], w = edge[2]; adj.get(u).add(new int[]{v, w}); adj.get(v).add(new int[]{u, w}); } return adj; } //Driver Code Ends // shortest path from src to dest public static int shortestPath(int V, ArrayList<ArrayList<int[]>> adj, int src, int dest) { int[] dist = new int[V]; Arrays.fill(dist, Integer.MAX_VALUE); PriorityQueue<int[]> pq = new PriorityQueue<> (Comparator.comparingInt(a -> a[0])); dist[src] = 0; pq.offer(new int[]{0, src}); while (!pq.isEmpty()) { int[] current = pq.poll(); int d = current[0]; int u = current[1]; if (d > dist[u]) continue; for (int[] neighbor : adj.get(u)) { int v = neighbor[0]; int w = neighbor[1]; // direct edge being removed to check for a cycle if ((u == src && v == dest) || (u == dest && v == src)) continue; if (dist[v] > dist[u] + w) { dist[v] = dist[u] + w; pq.offer(new int[]{dist[v], v}); } } } return dist[dest]; } // Function to find the minimum weight cycle in the graph public static int findMinCycle(int V, int[][] edges) { ArrayList<ArrayList<int[]>> adj = constructAdj(V, edges); int minCycle = Integer.MAX_VALUE; for (int[] edge : edges) { int u = edge[0], v = edge[1], w = edge[2]; int dist = shortestPath(V, adj, u, v); if (dist != Integer.MAX_VALUE) { minCycle = Math.min(minCycle, dist + w); } } return minCycle; } //Driver Code Starts public static void main(String[] args) { int V = 5; // Graph edges represented as {u, v, weight} int[][] edges = { {0, 1, 2}, {1, 2, 2}, {1, 3, 1}, {1, 4, 1}, {0, 4, 3}, {2, 3, 4} }; // Output the minimum weight cycle System.out.println(findMinCycle(V, edges)); } } //Driver Code Ends
Python #Driver Code Starts import heapq # Construct adjacency list using list of lists def constructadj(V, edges): adj = [[] for _ in range(V)] for edge in edges: u, v, w = edge adj[u].append((v, w)) adj[v].append((u, w)) return adj #Driver Code Ends # find shortest path from src to dest # while ignoring the edge between source and destination def shortestPath(V, adj, src, dest): # Initialize distance array with infinity dist = [float('inf')] * V dist[src] = 0 # Priority queue to store (distance, vertex) pq = [(0, src)] while pq: d, u = heapq.heappop(pq) # If we already found a better path, skip this one if d > dist[u]: continue # Traverse neighbors for v, w in adj[u]: # ignored edge from source and destination if (u == src and v == dest) or (u == dest and v == src): continue # Relaxation step if dist[v] > dist[u] + w: dist[v] = dist[u] + w heapq.heappush(pq, (dist[v], v)) return dist[dest] # Function to find the minimum weight cycle in the graph def findMinCycle(V, edges): # Build the adjacency list once adj = constructadj(V, edges) minCycle = float('inf') # Try removing each edge one by one for edge in edges: u, v, w = edge dist = shortestPath(V, adj, u, v) # If such a path exists, it forms a cycle with (u, v) if dist != float('inf'): minCycle = min(minCycle, dist + w) # If no cycle found, return -1 return minCycle #Driver Code Starts # Driver code if __name__ == "__main__": V = 5 # Graph edges represented as [u, v, weight] edges = [[0, 1, 2], [1, 2, 2], [1, 3, 1], [1, 4, 1], [0, 4, 3], [2, 3, 4]] print(findMinCycle(V, edges)) #Driver Code Ends
C# //Driver Code Starts using System; using System.Collections.Generic; class GfG { // Custom Min-Heap class MinHeap { private List<(int dist, int node)> heap = new List<(int, int)>(); private void Swap(int i, int j) { var temp = heap[i]; heap[i] = heap[j]; heap[j] = temp; } private void HeapifyUp(int i) { while (i > 0) { int parent = (i - 1) / 2; if (heap[i].dist >= heap[parent].dist) break; Swap(i, parent); i = parent; } } private void HeapifyDown(int i) { int n = heap.Count; while (true) { int left = 2 * i + 1; int right = 2 * i + 2; int smallest = i; if (left < n && heap[left].dist < heap[smallest].dist) smallest = left; if (right < n && heap[right].dist < heap[smallest].dist) smallest = right; if (smallest == i) break; Swap(i, smallest); i = smallest; } } public void Push(int dist, int node) { heap.Add((dist, node)); HeapifyUp(heap.Count - 1); } public (int dist, int node) Pop(){ if (heap.Count == 0) throw new InvalidOperationException("Heap is empty"); var result = heap[0]; heap[0] = heap[heap.Count - 1]; heap.RemoveAt(heap.Count - 1); HeapifyDown(0); return result; } public bool IsEmpty() { return heap.Count == 0; } } // Construct adjacency list static List<List<int[]>> constructadj(int V, int[,] edges) { var adj = new List<List<int[]>>(); for (int i = 0; i < V; i++) adj.Add(new List<int[]>()); int E = edges.GetLength(0); for (int i = 0; i < E; i++) { int u = edges[i, 0], v = edges[i, 1], w = edges[i, 2]; adj[u].Add(new int[] { v, w }); adj[v].Add(new int[] { u, w }); } //Driver Code Ends return adj; } // find shortest path from src to dest // while ignoring the edge between source and destination static int shortestPath(int V, List<List<int[]>> adj, int src, int dest) { int[] dist = new int[V]; for (int i = 0; i < V; i++) dist[i] = int.MaxValue; var pq = new MinHeap(); dist[src] = 0; pq.Push(0, src); while (!pq.IsEmpty()) { var (d, u) = pq.Pop(); if (d > dist[u]) continue; foreach (var edge in adj[u]) { int v = edge[0], w = edge[1]; // Skip the direct edge from src to dest if ((u == src && v == dest) || (u == dest && v == src)) continue; if (dist[v] > dist[u] + w) { dist[v] = dist[u] + w; pq.Push(dist[v], v); } } } return dist[dest]; } // Find the minimum cycle weight static int findMinCycle(int V, int[,] edges) { var adj = constructadj(V, edges); int minCycle = int.MaxValue; int E = edges.GetLength(0); for (int i = 0; i < E; i++) { int u = edges[i, 0], v = edges[i, 1], w = edges[i, 2]; int dist = shortestPath(V, adj, u, v); if (dist != int.MaxValue) { minCycle = Math.Min(minCycle, dist + w); } } return minCycle; //Driver Code Starts } static void Main() { int V = 9; int[,] edges = new int[,] { {0, 1, 2}, {1, 2, 2}, {1, 3, 1}, {1, 4, 1}, {0, 4, 3}, {2, 3, 4} }; Console.WriteLine(findMinCycle(V, edges)); } } //Driver Code Ends
JavaScript //Driver Code Starts // Min-Heap based Priority Queue class MinHeap { constructor() { this.heap = []; } push([dist, node]) { this.heap.push([dist, node]); this._bubbleUp(this.heap.length - 1); } pop() { const min = this.heap[0]; const end = this.heap.pop(); if (this.heap.length > 0) { this.heap[0] = end; this._sinkDown(0); } return min; } _bubbleUp(index) { const element = this.heap[index]; while (index > 0) { const parentIdx = Math.floor((index - 1) / 2); const parent = this.heap[parentIdx]; if (element[0] >= parent[0]) break; this.heap[index] = parent; this.heap[parentIdx] = element; index = parentIdx; } } _sinkDown(index) { const length = this.heap.length; const element = this.heap[index]; while (true) { let leftIdx = 2 * index + 1; let rightIdx = 2 * index + 2; let swap = null; if (leftIdx < length) { if (this.heap[leftIdx][0] < element[0]) { swap = leftIdx; } } if (rightIdx < length) { if ( (swap === null && this.heap[rightIdx][0] < element[0]) || (swap !== null && this.heap[rightIdx][0] < this.heap[leftIdx][0]) ) { swap = rightIdx; } } if (swap === null) break; this.heap[index] = this.heap[swap]; this.heap[swap] = element; index = swap; } } isEmpty() { return this.heap.length === 0; } } // Construct adjacency list function constructadj(V, edges) { let adj = Array.from({ length: V }, () => []); for (let [u, v, w] of edges) { adj[u].push([v, w]); adj[v].push([u, w]); } return adj; } //Driver Code Ends // find shortest path from src to dest // while ignoring the edge between source and destination function shortestPath(V, adj, src, dest) { let dist = new Array(V).fill(Infinity); dist[src] = 0; let pq = new MinHeap(); pq.push([0, src]); while (!pq.isEmpty()) { let [d, u] = pq.pop(); if (d > dist[u]) continue; for (let [v, w] of adj[u]) { // ignore direct connection between src and dest if ((u === src && v === dest) || (u === dest && v === src)) continue; if (dist[v] > dist[u] + w) { dist[v] = dist[u] + w; pq.push([dist[v], v]); } } } return dist[dest]; } // Find minimum cycle in the graph function findMinCycle(V, edges) { let adj = constructadj(V, edges); let minCycle = Infinity; for (let [u, v, w] of edges) { let dist = shortestPath(V, adj, u, v); if (dist !== Infinity) { minCycle = Math.min(minCycle, dist + w); } } return minCycle; } //Driver Code Starts // Driver code let V = 5; let edges = [[0, 1, 2], [1, 2, 2], [1, 3, 1], [1, 4, 1], [0, 4, 3], [2, 3, 4]]; console.log(findMinCycle(V, edges)); //Driver Code Ends
Time Complexity: O(E * (V + E) log V) for iterating over each edge and running Dijkstra's algorithm, which involves creating a new adjacency list and recalculating shortest paths multiple times.
Space Complexity: O(V + E) for the adjacency list, temporary edge storage, and Dijkstra's algorithm data structures like the distance array and priority queue.
Similar Reads
Graph Algorithms Graph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Introduction to Graph Data Structure Graph Data Structure is a non-linear data structure consisting of vertices and edges. It is useful in fields such as social network analysis, recommendation systems, and computer networks. In the field of sports data science, graph data structure can be used to analyze and understand the dynamics of
15+ min read
Graph and its representations A Graph is a non-linear data structure consisting of vertices and edges. The vertices are sometimes also referred to as nodes and the edges are lines or arcs that connect any two nodes in the graph. More formally a Graph is composed of a set of vertices( V ) and a set of edges( E ). The graph is den
12 min read
Types of Graphs with Examples A graph is a mathematical structure that represents relationships between objects by connecting a set of points. It is used to establish a pairwise relationship between elements in a given set. graphs are widely used in discrete mathematics, computer science, and network theory to represent relation
9 min read
Basic Properties of a Graph A Graph is a non-linear data structure consisting of nodes and edges. The nodes are sometimes also referred to as vertices and the edges are lines or arcs that connect any two nodes in the graph. The basic properties of a graph include: Vertices (nodes): The points where edges meet in a graph are kn
4 min read
Applications, Advantages and Disadvantages of Graph Graph is a non-linear data structure that contains nodes (vertices) and edges. A graph is a collection of set of vertices and edges (formed by connecting two vertices). A graph is defined as G = {V, E} where V is the set of vertices and E is the set of edges. Graphs can be used to model a wide varie
7 min read
Transpose graph Transpose of a directed graph G is another directed graph on the same set of vertices with all of the edges reversed compared to the orientation of the corresponding edges in G. That is, if G contains an edge (u, v) then the converse/transpose/reverse of G contains an edge (v, u) and vice versa. Giv
9 min read
Difference Between Graph and Tree Graphs and trees are two fundamental data structures used in computer science to represent relationships between objects. While they share some similarities, they also have distinct differences that make them suitable for different applications. Difference Between Graph and Tree What is Graph?A grap
2 min read
BFS and DFS on Graph
Breadth First Search or BFS for a GraphGiven a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta
15+ min read
Depth First Search or DFS for a GraphIn Depth First Search (or DFS) for a graph, we traverse all adjacent vertices one by one. When we traverse an adjacent vertex, we completely finish the traversal of all vertices reachable through that adjacent vertex. This is similar to a tree, where we first completely traverse the left subtree and
13 min read
Applications, Advantages and Disadvantages of Depth First Search (DFS)Depth First Search is a widely used algorithm for traversing a graph. Here we have discussed some applications, advantages, and disadvantages of the algorithm. Applications of Depth First Search:1. Detecting cycle in a graph: A graph has a cycle if and only if we see a back edge during DFS. So we ca
4 min read
Applications, Advantages and Disadvantages of Breadth First Search (BFS)We have earlier discussed Breadth First Traversal Algorithm for Graphs. Here in this article, we will see the applications, advantages, and disadvantages of the Breadth First Search. Applications of Breadth First Search: 1. Shortest Path and Minimum Spanning Tree for unweighted graph: In an unweight
4 min read
Iterative Depth First Traversal of GraphGiven a directed Graph, the task is to perform Depth First Search of the given graph.Note: Start DFS from node 0, and traverse the nodes in the same order as adjacency list.Note : There can be multiple DFS traversals of a graph according to the order in which we pick adjacent vertices. Here we pick
10 min read
BFS for Disconnected GraphIn the previous post, BFS only with a particular vertex is performed i.e. it is assumed that all vertices are reachable from the starting vertex. But in the case of a disconnected graph or any vertex that is unreachable from all vertex, the previous implementation will not give the desired output, s
14 min read
Transitive Closure of a Graph using DFSGiven a directed graph, find out if a vertex v is reachable from another vertex u for all vertex pairs (u, v) in the given graph. Here reachable means that there is a path from vertex u to v. The reach-ability matrix is called transitive closure of a graph. For example, consider below graph: GraphTr
8 min read
Difference between BFS and DFSBreadth-First Search (BFS) and Depth-First Search (DFS) are two fundamental algorithms used for traversing or searching graphs and trees. This article covers the basic difference between Breadth-First Search and Depth-First Search.Difference between BFS and DFSParametersBFSDFSStands forBFS stands fo
2 min read
Cycle in a Graph
Detect Cycle in a Directed GraphGiven the number of vertices V and a list of directed edges, determine whether the graph contains a cycle or not.Examples: Input: V = 4, edges[][] = [[0, 1], [0, 2], [1, 2], [2, 0], [2, 3]]Cycle: 0 â 2 â 0 Output: trueExplanation: The diagram clearly shows a cycle 0 â 2 â 0 Input: V = 4, edges[][] =
15+ min read
Detect cycle in an undirected graphGiven an undirected graph, the task is to check if there is a cycle in the given graph.Examples:Input: V = 4, edges[][]= [[0, 1], [0, 2], [1, 2], [2, 3]]Undirected Graph with 4 vertices and 4 edgesOutput: trueExplanation: The diagram clearly shows a cycle 0 â 2 â 1 â 0Input: V = 4, edges[][] = [[0,
8 min read
Detect Cycle in a directed graph using colorsGiven a directed graph represented by the number of vertices V and a list of directed edges, determine whether the graph contains a cycle.Your task is to implement a function that accepts V (number of vertices) and edges (an array of directed edges where each edge is a pair [u, v]), and returns true
9 min read
Detect a negative cycle in a Graph | (Bellman Ford)Given a directed weighted graph, your task is to find whether the given graph contains any negative cycles that are reachable from the source vertex (e.g., node 0).Note: A negative-weight cycle is a cycle in a graph whose edges sum to a negative value.Example:Input: V = 4, edges[][] = [[0, 3, 6], [1
15+ min read
Cycles of length n in an undirected and connected graphGiven an undirected and connected graph and a number n, count the total number of simple cycles of length n in the graph. A simple cycle of length n is defined as a cycle that contains exactly n vertices and n edges. Note that for an undirected graph, each cycle should only be counted once, regardle
10 min read
Detecting negative cycle using Floyd WarshallWe are given a directed graph. We need compute whether the graph has negative cycle or not. A negative cycle is one in which the overall sum of the cycle comes negative. Negative weights are found in various applications of graphs. For example, instead of paying cost for a path, we may get some adva
12 min read
Clone a Directed Acyclic GraphA directed acyclic graph (DAG) is a graph which doesn't contain a cycle and has directed edges. We are given a DAG, we need to clone it, i.e., create another graph that has copy of its vertices and edges connecting them. Examples: Input : 0 - - - > 1 - - - -> 4 | / \ ^ | / \ | | / \ | | / \ |
12 min read