Skip to content

Commit 05bb1ec

Browse files
Merge pull request #203 from rajni2002491/add-kadane-cpp
Kadane’s Algorithm (C++) with Description and Complexity
2 parents f091075 + bdeb2c4 commit 05bb1ec

File tree

2 files changed

+210
-0
lines changed

2 files changed

+210
-0
lines changed

C/algorithms/graph/dijkstra.c

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
/**
2+
* Dijkstra's Algorithm (Single-Source Shortest Path) - C Implementation
3+
*
4+
* Description:
5+
* Finds the shortest path distances from a source vertex to all other vertices
6+
* in a weighted directed graph with non-negative edge weights.
7+
*
8+
* Input format (from stdin):
9+
* - First line: V E (number of vertices and edges)
10+
* - Next E lines: u v w (edge from u to v with weight w)
11+
* - Last line: src (source vertex)
12+
* Vertices are 0-indexed in the range [0, V-1].
13+
*
14+
* Output:
15+
* - Prints one line per vertex: "dist[src->i] = D" where D is the distance
16+
* from src to i, or INF if unreachable.
17+
*
18+
* Time Complexity:
19+
* - Using adjacency list + simple O(V) min selection: O(V^2 + E)
20+
* (Good for small/medium graphs without extra dependencies.)
21+
* - With a binary heap priority queue, it can be improved to O((V + E) log V).
22+
*
23+
* Space Complexity:
24+
* - O(V + E) for the adjacency list and auxiliary arrays.
25+
*/
26+
27+
#include <stdio.h>
28+
#include <stdlib.h>
29+
#include <limits.h>
30+
31+
typedef struct Edge {
32+
int to;
33+
int weight;
34+
struct Edge *next;
35+
} Edge;
36+
37+
typedef struct {
38+
Edge **heads;
39+
int numVertices;
40+
} Graph;
41+
42+
static Edge *createEdge(int to, int weight) {
43+
Edge *edge = (Edge *)malloc(sizeof(Edge));
44+
if (!edge) {
45+
fprintf(stderr, "Memory allocation failed for Edge.\n");
46+
exit(EXIT_FAILURE);
47+
}
48+
edge->to = to;
49+
edge->weight = weight;
50+
edge->next = NULL;
51+
return edge;
52+
}
53+
54+
static Graph createGraph(int numVertices) {
55+
Graph graph;
56+
graph.numVertices = numVertices;
57+
graph.heads = (Edge **)calloc(numVertices, sizeof(Edge *));
58+
if (!graph.heads) {
59+
fprintf(stderr, "Memory allocation failed for graph heads.\n");
60+
exit(EXIT_FAILURE);
61+
}
62+
return graph;
63+
}
64+
65+
static void addEdge(Graph *graph, int from, int to, int weight) {
66+
Edge *edge = createEdge(to, weight);
67+
edge->next = graph->heads[from];
68+
graph->heads[from] = edge;
69+
}
70+
71+
static void freeGraph(Graph *graph) {
72+
for (int i = 0; i < graph->numVertices; i++) {
73+
Edge *curr = graph->heads[i];
74+
while (curr) {
75+
Edge *next = curr->next;
76+
free(curr);
77+
curr = next;
78+
}
79+
}
80+
free(graph->heads);
81+
}
82+
83+
static int extractMinUnvisited(int *dist, int *visited, int V) {
84+
int minIndex = -1;
85+
int minValue = INT_MAX;
86+
for (int i = 0; i < V; i++) {
87+
if (!visited[i] && dist[i] < minValue) {
88+
minValue = dist[i];
89+
minIndex = i;
90+
}
91+
}
92+
return minIndex;
93+
}
94+
95+
static void dijkstra(const Graph *graph, int source, int *dist) {
96+
int V = graph->numVertices;
97+
int *visited = (int *)calloc(V, sizeof(int));
98+
if (!visited) {
99+
fprintf(stderr, "Memory allocation failed for visited array.\n");
100+
exit(EXIT_FAILURE);
101+
}
102+
103+
for (int i = 0; i < V; i++) {
104+
dist[i] = INT_MAX;
105+
}
106+
dist[source] = 0;
107+
108+
for (int count = 0; count < V - 1; count++) {
109+
int u = extractMinUnvisited(dist, visited, V);
110+
if (u == -1) {
111+
break; // No reachable unvisited vertices remain
112+
}
113+
visited[u] = 1;
114+
115+
for (Edge *edge = graph->heads[u]; edge != NULL; edge = edge->next) {
116+
int v = edge->to;
117+
int w = edge->weight;
118+
if (!visited[v] && dist[u] != INT_MAX && dist[u] + w < dist[v]) {
119+
dist[v] = dist[u] + w;
120+
}
121+
}
122+
}
123+
124+
free(visited);
125+
}
126+
127+
static void printDistances(const int *dist, int V, int src) {
128+
for (int i = 0; i < V; i++) {
129+
if (dist[i] == INT_MAX) {
130+
printf("dist[%d->%d] = INF\n", src, i);
131+
} else {
132+
printf("dist[%d->%d] = %d\n", src, i, dist[i]);
133+
}
134+
}
135+
}
136+
137+
int main() {
138+
int V, E;
139+
if (scanf("%d %d", &V, &E) != 2) {
140+
fprintf(stderr, "Invalid input. Expected: V E\n");
141+
return 1;
142+
}
143+
144+
if (V <= 0) {
145+
fprintf(stderr, "Number of vertices must be positive.\n");
146+
return 1;
147+
}
148+
149+
Graph graph = createGraph(V);
150+
151+
for (int i = 0; i < E; i++) {
152+
int u, v, w;
153+
if (scanf("%d %d %d", &u, &v, &w) != 3) {
154+
fprintf(stderr, "Invalid edge input at line %d. Expected: u v w\n", i + 1);
155+
freeGraph(&graph);
156+
return 1;
157+
}
158+
if (u < 0 || u >= V || v < 0 || v >= V || w < 0) {
159+
fprintf(stderr, "Invalid edge values: u=%d v=%d w=%d\n", u, v, w);
160+
freeGraph(&graph);
161+
return 1;
162+
}
163+
addEdge(&graph, u, v, w);
164+
}
165+
166+
int src;
167+
if (scanf("%d", &src) != 1) {
168+
fprintf(stderr, "Invalid input. Expected source vertex.\n");
169+
freeGraph(&graph);
170+
return 1;
171+
}
172+
if (src < 0 || src >= V) {
173+
fprintf(stderr, "Source vertex out of range.\n");
174+
freeGraph(&graph);
175+
return 1;
176+
}
177+
178+
int *dist = (int *)malloc(V * sizeof(int));
179+
if (!dist) {
180+
fprintf(stderr, "Memory allocation failed for dist array.\n");
181+
freeGraph(&graph);
182+
return 1;
183+
}
184+
185+
dijkstra(&graph, src, dist);
186+
printDistances(dist, V, src);
187+
188+
free(dist);
189+
freeGraph(&graph);
190+
return 0;
191+
}
192+
193+

CPP/algorithms/dynamic_programming/kadane_Algorithm.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
/**
2+
* Kadane's Algorithm - Maximum Subarray Sum (C++)
3+
*
4+
* Description:
5+
* Finds the maximum possible sum of a contiguous subarray within a
6+
* one-dimensional array of integers. Handles arrays with all-negative
7+
* numbers as well. Provided are two variants:
8+
* - kadane: classic approach with an all-negative safeguard
9+
* - kadane_optimized: concise variant that naturally handles all-negative arrays
10+
*
11+
* Time Complexity:
12+
* - O(n), where n is the number of elements in the array (single pass)
13+
*
14+
* Space Complexity:
15+
* - O(1), constant extra space
16+
*/
17+
118
#include <vector>
219
#include <algorithm>
320
#include <climits>

0 commit comments

Comments
 (0)