Detect a negative cycle in a Graph | (Bellman Ford)
Last Updated :
19 May, 2025
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, 0, 4], [1, 2, 6], [3, 1, 2]]
Example 2Output: false
Algorithm to Find Negative Cycle in a Directed Weighted Graph Using Bellman-Ford:
- Initialize distance array dist[] for each vertex 'v' as dist[v] = INFINITY.
- Assume any vertex (let's say '0') as source and assign dist[source][/source] = 0.
- Relax all the edges(u,v,weight) N-1 times as per the below condition:
- dist[v] = minimum(dist[v], distance[u] + weight)
- Now, Relax all the edges one more time i.e. the Nth time and based on the below two cases we can detect the negative cycle:
- Case 1 (Negative cycle exists): For any edge(u, v, weight), if dist[u] + weight < dist[v]
- Case 2 (No Negative cycle) : case 1 fails for all the edges.
Working of Bellman-Ford Algorithm to Detect the Negative cycle in the graph:
Below is the implementation to detect Negative cycle in a graph:
C++
// A C++ program for Bellman-Ford's single source
// shortest path algorithm.
#include <bits/stdc++.h>
using namespace std;
// A structure to represent a weighted edge in graph
struct Edge {
int src, dest, weight;
};
// A structure to represent a connected, directed and
// weighted graph
struct Graph {
// V-> Number of vertices, E-> Number of edges
int V, E;
// Graph is represented as an array of edges.
struct Edge* edge;
};
// Creates a graph with V vertices and E edges
struct Graph* createGraph(int V, int E)
{
struct Graph* graph = new Graph;
graph->V = V;
graph->E = E;
graph->edge = new Edge[graph->E];
return graph;
}
// The main function that finds shortest distances
// from src to all other vertices using Bellman-
// Ford algorithm. The function also detects
// negative weight cycle
bool isNegCycleBellmanFord(struct Graph* graph, int src,
int dist[])
{
int V = graph->V;
int E = graph->E;
// Step 1: Initialize distances from src
// to all other vertices as INFINITE
for (int i = 0; i < V; i++)
dist[i] = INT_MAX;
dist[src] = 0;
// Step 2: Relax all edges |V| - 1 times.
// A simple shortest path from src to any
// other vertex can have at-most |V| - 1
// edges
for (int i = 1; i <= V - 1; i++) {
for (int j = 0; j < E; j++) {
int u = graph->edge[j].src;
int v = graph->edge[j].dest;
int weight = graph->edge[j].weight;
if (dist[u] != INT_MAX
&& dist[u] + weight < dist[v])
dist[v] = dist[u] + weight;
}
}
// Step 3: check for negative-weight cycles.
// The above step guarantees shortest distances
// if graph doesn't contain negative weight cycle.
// If we get a shorter path, then there
// is a cycle.
for (int i = 0; i < E; i++) {
int u = graph->edge[i].src;
int v = graph->edge[i].dest;
int weight = graph->edge[i].weight;
if (dist[u] != INT_MAX
&& dist[u] + weight < dist[v])
return true;
}
return false;
}
// Returns true if given graph has negative weight
// cycle.
bool isNegCycleDisconnected(struct Graph* graph)
{
int V = graph->V;
// To keep track of visited vertices to avoid
// recomputations.
bool visited[V];
memset(visited, 0, sizeof(visited));
// This array is filled by Bellman-Ford
int dist[V];
// Call Bellman-Ford for all those vertices
// that are not visited
for (int i = 0; i < V; i++) {
if (visited[i] == false) {
// If cycle found
if (isNegCycleBellmanFord(graph, i, dist))
return true;
// Mark all vertices that are visited
// in above call.
for (int i = 0; i < V; i++)
if (dist[i] != INT_MAX)
visited[i] = true;
}
}
return false;
}
// Driver Code
int main()
{
// Number of vertices in graph
int V = 5;
// Number of edges in graph
int E = 8;
// Let us create the graph given in above example
struct Graph* graph = createGraph(V, E);
graph->edge[0].src = 0;
graph->edge[0].dest = 1;
graph->edge[0].weight = -1;
graph->edge[1].src = 0;
graph->edge[1].dest = 2;
graph->edge[1].weight = 4;
graph->edge[2].src = 1;
graph->edge[2].dest = 2;
graph->edge[2].weight = 3;
graph->edge[3].src = 1;
graph->edge[3].dest = 3;
graph->edge[3].weight = 2;
graph->edge[4].src = 1;
graph->edge[4].dest = 4;
graph->edge[4].weight = 2;
graph->edge[5].src = 3;
graph->edge[5].dest = 2;
graph->edge[5].weight = 5;
graph->edge[6].src = 3;
graph->edge[6].dest = 1;
graph->edge[6].weight = 1;
graph->edge[7].src = 4;
graph->edge[7].dest = 3;
graph->edge[7].weight = -3;
if (isNegCycleDisconnected(graph))
cout << "Yes";
else
cout << "No";
return 0;
}
Java
// A Java program for Bellman-Ford's single source
// shortest path algorithm.
import java.util.*;
class GFG {
// A structure to represent a weighted
// edge in graph
static class Edge {
int src, dest, weight;
}
// A structure to represent a connected,
// directed and weighted graph
static class Graph {
// V-> Number of vertices,
// E-> Number of edges
int V, E;
// Graph is represented as
// an array of edges.
Edge edge[];
}
// Creates a graph with V vertices and E edges
static Graph createGraph(int V, int E)
{
Graph graph = new Graph();
graph.V = V;
graph.E = E;
graph.edge = new Edge[graph.E];
for (int i = 0; i < graph.E; i++) {
graph.edge[i] = new Edge();
}
return graph;
}
// The main function that finds shortest distances
// from src to all other vertices using Bellman-
// Ford algorithm. The function also detects
// negative weight cycle
static boolean
isNegCycleBellmanFord(Graph graph, int src, int dist[])
{
int V = graph.V;
int E = graph.E;
// Step 1: Initialize distances from src
// to all other vertices as INFINITE
for (int i = 0; i < V; i++)
dist[i] = Integer.MAX_VALUE;
dist[src] = 0;
// Step 2: Relax all edges |V| - 1 times.
// A simple shortest path from src to any
// other vertex can have at-most |V| - 1
// edges
for (int i = 1; i <= V - 1; i++) {
for (int j = 0; j < E; j++) {
int u = graph.edge[j].src;
int v = graph.edge[j].dest;
int weight = graph.edge[j].weight;
if (dist[u] != Integer.MAX_VALUE
&& dist[u] + weight < dist[v])
dist[v] = dist[u] + weight;
}
}
// Step 3: check for negative-weight cycles.
// The above step guarantees shortest distances
// if graph doesn't contain negative weight cycle.
// If we get a shorter path, then there
// is a cycle.
for (int i = 0; i < E; i++) {
int u = graph.edge[i].src;
int v = graph.edge[i].dest;
int weight = graph.edge[i].weight;
if (dist[u] != Integer.MAX_VALUE
&& dist[u] + weight < dist[v])
return true;
}
return false;
}
// Returns true if given graph has negative weight
// cycle.
static boolean isNegCycleDisconnected(Graph graph)
{
int V = graph.V;
// To keep track of visited vertices
// to avoid recomputations.
boolean visited[] = new boolean[V];
Arrays.fill(visited, false);
// This array is filled by Bellman-Ford
int dist[] = new int[V];
// Call Bellman-Ford for all those vertices
// that are not visited
for (int i = 0; i < V; i++) {
if (visited[i] == false) {
// If cycle found
if (isNegCycleBellmanFord(graph, i, dist))
return true;
// Mark all vertices that are visited
// in above call.
for (int j = 0; j < V; j++)
if (dist[j] != Integer.MAX_VALUE)
visited[j] = true;
}
}
return false;
}
// Driver Code
public static void main(String[] args)
{
int V = 5, E = 8;
Graph graph = createGraph(V, E);
// Add edge 0-1 (or A-B in above figure)
graph.edge[0].src = 0;
graph.edge[0].dest = 1;
graph.edge[0].weight = -1;
// Add edge 0-2 (or A-C in above figure)
graph.edge[1].src = 0;
graph.edge[1].dest = 2;
graph.edge[1].weight = 4;
// Add edge 1-2 (or B-C in above figure)
graph.edge[2].src = 1;
graph.edge[2].dest = 2;
graph.edge[2].weight = 3;
// Add edge 1-3 (or B-D in above figure)
graph.edge[3].src = 1;
graph.edge[3].dest = 3;
graph.edge[3].weight = 2;
// Add edge 1-4 (or A-E in above figure)
graph.edge[4].src = 1;
graph.edge[4].dest = 4;
graph.edge[4].weight = 2;
// Add edge 3-2 (or D-C in above figure)
graph.edge[5].src = 3;
graph.edge[5].dest = 2;
graph.edge[5].weight = 5;
// Add edge 3-1 (or D-B in above figure)
graph.edge[6].src = 3;
graph.edge[6].dest = 1;
graph.edge[6].weight = 1;
// Add edge 4-3 (or E-D in above figure)
graph.edge[7].src = 4;
graph.edge[7].dest = 3;
graph.edge[7].weight = -3;
if (isNegCycleDisconnected(graph))
System.out.println("Yes");
else
System.out.println("No");
}
}
// This code is contributed by adityapande88
Python
# A Python3 program for Bellman-Ford's single source
# shortest path algorithm.
# The main function that finds shortest distances
# from src to all other vertices using Bellman-
# Ford algorithm. The function also detects
# negative weight cycle
def isNegCycleBellmanFord(src, dist):
global graph, V, E
# Step 1: Initialize distances from src
# to all other vertices as INFINITE
for i in range(V):
dist[i] = 10**18
dist[src] = 0
# Step 2: Relax all edges |V| - 1 times.
# A simple shortest path from src to any
# other vertex can have at-most |V| - 1
# edges
for i in range(1, V):
for j in range(E):
u = graph[j][0]
v = graph[j][1]
weight = graph[j][2]
if (dist[u] != 10**18 and dist[u] + weight < dist[v]):
dist[v] = dist[u] + weight
# Step 3: check for negative-weight cycles.
# The above step guarantees shortest distances
# if graph doesn't contain negative weight cycle.
# If we get a shorter path, then there
# is a cycle.
for i in range(E):
u = graph[i][0]
v = graph[i][1]
weight = graph[i][2]
if (dist[u] != 10**18 and dist[u] + weight < dist[v]):
return True
return False
# Returns true if given graph has negative weight
# cycle.
def isNegCycleDisconnected():
global V, E, graph
# To keep track of visited vertices to avoid
# recomputations.
visited = [0]*V
# memset(visited, 0, sizeof(visited))
# This array is filled by Bellman-Ford
dist = [0]*V
# Call Bellman-Ford for all those vertices
# that are not visited
for i in range(V):
if (visited[i] == 0):
# If cycle found
if (isNegCycleBellmanFord(i, dist)):
return True
# Mark all vertices that are visited
# in above call.
for i in range(V):
if (dist[i] != 10**18):
visited[i] = True
return False
# Driver code
if __name__ == '__main__':
# /* Let us create the graph given in above example */
V = 5 # Number of vertices in graph
E = 8 # Number of edges in graph
graph = [[0, 0, 0] for i in range(8)]
# add edge 0-1 (or A-B in above figure)
graph[0][0] = 0
graph[0][1] = 1
graph[0][2] = -1
# add edge 0-2 (or A-C in above figure)
graph[1][0] = 0
graph[1][1] = 2
graph[1][2] = 4
# add edge 1-2 (or B-C in above figure)
graph[2][0] = 1
graph[2][1] = 2
graph[2][2] = 3
# add edge 1-3 (or B-D in above figure)
graph[3][0] = 1
graph[3][1] = 3
graph[3][2] = 2
# add edge 1-4 (or A-E in above figure)
graph[4][0] = 1
graph[4][1] = 4
graph[4][2] = 2
# add edge 3-2 (or D-C in above figure)
graph[5][0] = 3
graph[5][1] = 2
graph[5][2] = 5
# add edge 3-1 (or D-B in above figure)
graph[6][0] = 3
graph[6][1] = 1
graph[6][2] = 1
# add edge 4-3 (or E-D in above figure)
graph[7][0] = 4
graph[7][1] = 3
graph[7][2] = -3
if (isNegCycleDisconnected()):
print("Yes")
else:
print("No")
# This code is contributed by mohit kumar 29
C#
// A C# program for Bellman-Ford's single source
// shortest path algorithm.
using System;
using System.Collections.Generic;
public class GFG {
// A structure to represent a weighted
// edge in graph
public class Edge {
public int src, dest, weight;
}
// A structure to represent a connected,
// directed and weighted graph
public class Graph {
// V-> Number of vertices,
// E-> Number of edges
public int V, E;
// Graph is represented as
// an array of edges.
public Edge[] edge;
}
// Creates a graph with V vertices and E edges
static Graph createGraph(int V, int E)
{
Graph graph = new Graph();
graph.V = V;
graph.E = E;
graph.edge = new Edge[graph.E];
for (int i = 0; i < graph.E; i++) {
graph.edge[i] = new Edge();
}
return graph;
}
// The main function that finds shortest distances
// from src to all other vertices using Bellman-
// Ford algorithm. The function also detects
// negative weight cycle
static bool isNegCycleBellmanFord(Graph graph, int src,
int[] dist)
{
int V = graph.V;
int E = graph.E;
// Step 1: Initialize distances from src
// to all other vertices as INFINITE
for (int i = 0; i < V; i++)
dist[i] = int.MaxValue;
dist[src] = 0;
// Step 2: Relax all edges |V| - 1 times.
// A simple shortest path from src to any
// other vertex can have at-most |V| - 1
// edges
for (int i = 1; i <= V - 1; i++) {
for (int j = 0; j < E; j++) {
int u = graph.edge[j].src;
int v = graph.edge[j].dest;
int weight = graph.edge[j].weight;
if (dist[u] != int.MaxValue
&& dist[u] + weight < dist[v])
dist[v] = dist[u] + weight;
}
}
// Step 3: check for negative-weight cycles.
// The above step guarantees shortest distances
// if graph doesn't contain negative weight cycle.
// If we get a shorter path, then there
// is a cycle.
for (int i = 0; i < E; i++) {
int u = graph.edge[i].src;
int v = graph.edge[i].dest;
int weight = graph.edge[i].weight;
if (dist[u] != int.MaxValue
&& dist[u] + weight < dist[v])
return true;
}
return false;
}
// Returns true if given graph has negative weight
// cycle.
static bool isNegCycleDisconnected(Graph graph)
{
int V = graph.V;
// To keep track of visited vertices
// to avoid recomputations.
bool[] visited = new bool[V];
// This array is filled by Bellman-Ford
int[] dist = new int[V];
// Call Bellman-Ford for all those vertices
// that are not visited
for (int i = 0; i < V; i++) {
if (visited[i] == false) {
// If cycle found
if (isNegCycleBellmanFord(graph, i, dist))
return true;
// Mark all vertices that are visited
// in above call.
for (int j = 0; j < V; j++)
if (dist[j] != int.MaxValue)
visited[j] = true;
}
}
return false;
}
// Driver Code
public static void Main(String[] args)
{
int V = 5, E = 8;
Graph graph = createGraph(V, E);
// Add edge 0-1 (or A-B in above figure)
graph.edge[0].src = 0;
graph.edge[0].dest = 1;
graph.edge[0].weight = -1;
// Add edge 0-2 (or A-C in above figure)
graph.edge[1].src = 0;
graph.edge[1].dest = 2;
graph.edge[1].weight = 4;
// Add edge 1-2 (or B-C in above figure)
graph.edge[2].src = 1;
graph.edge[2].dest = 2;
graph.edge[2].weight = 3;
// Add edge 1-3 (or B-D in above figure)
graph.edge[3].src = 1;
graph.edge[3].dest = 3;
graph.edge[3].weight = 2;
// Add edge 1-4 (or A-E in above figure)
graph.edge[4].src = 1;
graph.edge[4].dest = 4;
graph.edge[4].weight = 2;
// Add edge 3-2 (or D-C in above figure)
graph.edge[5].src = 3;
graph.edge[5].dest = 2;
graph.edge[5].weight = 5;
// Add edge 3-1 (or D-B in above figure)
graph.edge[6].src = 3;
graph.edge[6].dest = 1;
graph.edge[6].weight = 1;
// Add edge 4-3 (or E-D in above figure)
graph.edge[7].src = 4;
graph.edge[7].dest = 3;
graph.edge[7].weight = -3;
if (isNegCycleDisconnected(graph))
Console.WriteLine("Yes");
else
Console.WriteLine("No");
}
}
// This code is contributed by aashish1995
JavaScript
<script>
// A Javascript program for Bellman-Ford's single source
// shortest path algorithm.
// A structure to represent a weighted
// edge in graph
class Edge
{
constructor()
{
let src, dest, weight;
}
}
// A structure to represent a connected,
// directed and weighted graph
class Graph
{
constructor()
{
// V-> Number of vertices,
// E-> Number of edges
let V, E;
// Graph is represented as
// an array of edges.
let edge=[];
}
}
// Creates a graph with V vertices and E edges
function createGraph(V,E)
{
let graph = new Graph();
graph.V = V;
graph.E = E;
graph.edge = new Array(graph.E);
for(let i = 0; i < graph.E; i++)
{
graph.edge[i] = new Edge();
}
return graph;
}
// The main function that finds shortest distances
// from src to all other vertices using Bellman-
// Ford algorithm. The function also detects
// negative weight cycle
function isNegCycleBellmanFord(graph,src,dist)
{
let V = graph.V;
let E = graph.E;
// Step 1: Initialize distances from src
// to all other vertices as INFINITE
for(let i = 0; i < V; i++)
dist[i] = Number.MAX_VALUE;
dist[src] = 0;
// Step 2: Relax all edges |V| - 1 times.
// A simple shortest path from src to any
// other vertex can have at-most |V| - 1
// edges
for(let i = 1; i <= V - 1; i++)
{
for(let j = 0; j < E; j++)
{
let u = graph.edge[j].src;
let v = graph.edge[j].dest;
let weight = graph.edge[j].weight;
if (dist[u] != Number.MAX_VALUE &&
dist[u] + weight < dist[v])
dist[v] = dist[u] + weight;
}
}
// Step 3: check for negative-weight cycles.
// The above step guarantees shortest distances
// if graph doesn't contain negative weight cycle.
// If we get a shorter path, then there
// is a cycle.
for(let i = 0; i < E; i++)
{
let u = graph.edge[i].src;
let v = graph.edge[i].dest;
let weight = graph.edge[i].weight;
if (dist[u] != Number.MAX_VALUE &&
dist[u] + weight < dist[v])
return true;
}
return false;
}
// Returns true if given graph has negative weight
// cycle.
function isNegCycleDisconnected(graph)
{
let V = graph.V;
// To keep track of visited vertices
// to avoid recomputations.
let visited = new Array(V);
for(let i=0;i<V;i++)
{
visited[i]=false;
}
// This array is filled by Bellman-Ford
let dist = new Array(V);
// Call Bellman-Ford for all those vertices
// that are not visited
for(let i = 0; i < V; i++)
{
if (visited[i] == false)
{
// If cycle found
if (isNegCycleBellmanFord(graph, i, dist))
return true;
// Mark all vertices that are visited
// in above call.
for(let j = 0; j < V; j++)
if (dist[j] != Number.MAX_VALUE)
visited[j] = true;
}
}
return false;
}
// Driver Code
let V = 5, E = 8;
let graph = createGraph(V, E);
// Add edge 0-1 (or A-B in above figure)
graph.edge[0].src = 0;
graph.edge[0].dest = 1;
graph.edge[0].weight = -1;
// Add edge 0-2 (or A-C in above figure)
graph.edge[1].src = 0;
graph.edge[1].dest = 2;
graph.edge[1].weight = 4;
// Add edge 1-2 (or B-C in above figure)
graph.edge[2].src = 1;
graph.edge[2].dest = 2;
graph.edge[2].weight = 3;
// Add edge 1-3 (or B-D in above figure)
graph.edge[3].src = 1;
graph.edge[3].dest = 3;
graph.edge[3].weight = 2;
// Add edge 1-4 (or A-E in above figure)
graph.edge[4].src = 1;
graph.edge[4].dest = 4;
graph.edge[4].weight = 2;
// Add edge 3-2 (or D-C in above figure)
graph.edge[5].src = 3;
graph.edge[5].dest = 2;
graph.edge[5].weight = 5;
// Add edge 3-1 (or D-B in above figure)
graph.edge[6].src = 3;
graph.edge[6].dest = 1;
graph.edge[6].weight = 1;
// Add edge 4-3 (or E-D in above figure)
graph.edge[7].src = 4;
graph.edge[7].dest = 3;
graph.edge[7].weight = -3;
if (isNegCycleDisconnected(graph))
document.write("Yes");
else
document.write("No");
// This code is contributed by patel2127
</script>
Time Complexity: O(V*E), , where V and E are the number of vertices in the graph and edges respectively.
Auxiliary Space: O(V), where V is the number of vertices in the graph.
Related articles:
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph 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
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem