Skip to content

Commit fb28c62

Browse files
committed
Merge branch 'Algo/prims' of https://github.com/harshendram/DSA_Code into Algo/prims
2 parents 4922e14 + d182023 commit fb28c62

File tree

21 files changed

+1993
-49
lines changed

21 files changed

+1993
-49
lines changed

.github/scripts/validate-pr.sh

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,13 @@ while IFS= read -r file; do
5656
;;
5757
esac
5858

59-
# Check directory structure
59+
# Check directory structure - must be in Language/Category or Language/Category/Subcategory
6060
if [[ "$file" =~ \.(c|cpp|java|py|js|ts|go|rs|kt|swift|php|rb|cs|dart|scala)$ ]]; then
61-
if [[ ! "$dir" =~ (algorithms|data_structures|dynamic_programming|projects|graph|sorting|searching) ]]; then
62-
invalid_files+=("$file: Should be in proper subdirectory (algorithms/, data_structures/, etc.)")
61+
# Check if file is in a valid language directory with at least one subdirectory
62+
if [[ ! "$dir" =~ ^(C|CPP|Java|Python|JavaScript|TypeScript|Go|Rust|Kotlin|Swift|PHP|Ruby|CSharp|Dart|Scala)/ ]]; then
63+
invalid_files+=("$file: Must be in a language directory (C/, CPP/, Java/, Python/, etc.)")
64+
elif [[ ! "$dir" =~ (algorithms|data_structures|dynamic_programming|projects) ]]; then
65+
invalid_files+=("$file: Should be in proper category (algorithms/, data_structures/, dynamic_programming/, or projects/)")
6366
fi
6467
fi
6568
done <<< "$changed_files"

.github/workflows/pr-validation.yml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,53 @@ jobs:
7676
if [[ ! "$dir" =~ (algorithms|data_structures|dynamic_programming|projects)/ ]]; then
7777
invalid_files+=("$file: Should be in algorithms/, data_structures/, dynamic_programming/, or projects/ subdirectory")
7878
fi
79+
80+
;;
81+
*.java)
82+
# Should use PascalCase for Java classes
83+
if [[ ! "$filename" =~ ^[A-Z][a-zA-Z0-9]*\.java$ ]]; then
84+
invalid_files+=("$file: Java files should use PascalCase (e.g., BinarySearch.java, MaxRectangle.java)")
85+
fi
86+
;;
87+
*.js|*.ts|*.go|*.rs|*.kt|*.swift|*.php|*.rb|*.cs|*.dart|*.scala)
88+
# Allow flexible naming for other languages
89+
if [[ ! "$filename" =~ ^[a-zA-Z0-9_]+\.[a-z]+$ ]]; then
90+
invalid_files+=("$file: Use alphanumeric characters and underscores only")
91+
fi
92+
;;
93+
esac
94+
95+
# Check directory structure - must contain a valid category
96+
if [[ "$file" =~ \.(c|cpp|java|py|js|ts|go|rs|kt|swift|php|rb|cs|dart|scala)$ ]]; then
97+
if [[ ! "$dir" =~ (algorithms|data_structures|dynamic_programming|projects) ]]; then
98+
invalid_files+=("$file: Should be in algorithms/, data_structures/, dynamic_programming/, or projects/ subdirectory")
99+
fi
100+
fi
101+
done
102+
103+
if [ ${#invalid_files[@]} -gt 0 ]; then
104+
echo "❌ Found naming/structure violations:"
105+
printf '%s\n' "${invalid_files[@]}"
106+
exit 1
107+
else
108+
echo "✅ All files follow naming conventions!"
109+
fi
110+
111+
- name: 📝 Check for required documentation
112+
if: steps.changed-files.outputs.any_changed == 'true'
113+
run: |
114+
echo "📝 Checking for documentation in code files..."
115+
116+
missing_docs=()
117+
118+
for file in ${{ steps.changed-files.outputs.all_changed_files }}; do
119+
if [[ "$file" =~ \.(c|cpp|java|py|js|ts|go|rs|kt|swift|php|rb|cs|dart|scala)$ ]]; then
120+
echo "Checking documentation in: $file"
121+
122+
# Check for basic documentation elements
123+
if ! grep -q -i "algorithm\|description\|complexity" "$file" 2>/dev/null; then
124+
missing_docs+=("$file: Missing algorithm description or complexity analysis")
125+
79126
fi
80127
done
81128

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+
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <climits>
4+
using namespace std;
5+
6+
// Function to find minimum number of attempts needed
7+
int eggDrop(int eggs, int floors) {
8+
vector<vector<int>> dp(eggs + 1, vector<int>(floors + 1, 0));
9+
10+
// Base cases
11+
for (int i = 1; i <= eggs; i++)
12+
dp[i][0] = 0, dp[i][1] = 1;
13+
for (int j = 1; j <= floors; j++)
14+
dp[1][j] = j;
15+
16+
// Fill the rest using DP
17+
for (int e = 2; e <= eggs; e++) {
18+
for (int f = 2; f <= floors; f++) {
19+
dp[e][f] = INT_MAX;
20+
for (int k = 1; k <= f; k++) {
21+
int attempts = 1 + max(dp[e - 1][k - 1], dp[e][f - k]);
22+
dp[e][f] = min(dp[e][f], attempts);
23+
}
24+
}
25+
}
26+
27+
return dp[eggs][floors];
28+
}
29+
30+
int main() {
31+
int eggs, floors;
32+
cout << "Enter number of eggs: ";
33+
cin >> eggs;
34+
cout << "Enter number of floors: ";
35+
cin >> floors;
36+
37+
int minAttempts = eggDrop(eggs, floors);
38+
cout << "Minimum number of attempts needed: " << minAttempts << endl;
39+
40+
return 0;
41+
}

0 commit comments

Comments
 (0)