-
-
Notifications
You must be signed in to change notification settings - Fork 632
Add Dijkstra in C #4322
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add Dijkstra in C #4322
Changes from 51 commits
Commits
Show all changes
56 commits
Select commit
Hold shift + click to select a range
efe02da
Add binary search in C
2Clutch d4c3e22
Merge branch 'TheRenegadeCoder:main' into master
2Clutch 7251534
Add code
2Clutch ea9c9ff
Handle errors and potential edge cases
2Clutch b428138
Clean Up + Refactor + Request Input from Command Line
2Clutch bca6b20
Follow documentation to the letter
2Clutch 6a1105c
for the win? (e.g. check that array is not empty)
2Clutch 7e838d0
Clean Up & Refactor
2Clutch 9f66116
Merge remote-tracking branch 'refs/remotes/origin/master'
2Clutch e6816cb
Clean Up & Refactor
2Clutch 7028097
Fix error message
2Clutch de08b32
Update error message handling
2Clutch 8b8d41b
Merge branch 'TheRenegadeCoder:main' into master
2Clutch 29511a4
Add Convex Hull
2Clutch ee12a88
Merge branch 'TheRenegadeCoder:main' into master
2Clutch 643eb70
Update test handling
2Clutch 8c310de
Merge branch 'TheRenegadeCoder:main' into master
2Clutch d0f8213
Clean Up & Refactor
2Clutch 51f982e
for the win?
2Clutch 5dfa5c5
Merge branch 'TheRenegadeCoder:main' into master
2Clutch fddbd64
Merge remote-tracking branch 'refs/remotes/origin/master'
2Clutch fe4f138
Fix input validation and output format for convex hull calculation + …
2Clutch 793dc0a
Add command line argument handling for x and y coordinates + Implemen…
2Clutch aa58d24
brand new implementation
2Clutch bd69cb4
Refactor error handling to eliminate code repetition for usage messages
2Clutch 54d6812
Use same error message everywhere
2Clutch db2fd2e
Handle negative numbers
2Clutch 6314765
Fix convex hull logic to correctly output hull points
2Clutch 1afd977
Merge branch 'main' into master
rzuckerm b67e744
Merge branch 'main' into master
rzuckerm 9f03c2b
Fix Convex Hull in C
6e64781
Merge branch 'TheRenegadeCoder:main' into master
2Clutch 13b1aab
Add job-sequencing.c
2Clutch fe0426a
Update solution
2Clutch 17073e3
Merge branch 'TheRenegadeCoder:main' into master
2Clutch 1d17029
Add fraction-math.c
2Clutch bca9bd6
Update solution
2Clutch b67b7a2
Merge branch 'TheRenegadeCoder:main' into master
2Clutch 8d08765
Add depth-first-search.c
2Clutch eb26486
Update solution
2Clutch 40cfc72
remove dependency on math library
2Clutch dfff909
improve input validation and error handling
2Clutch d5571d3
Merge branch 'main' into master
2Clutch 47167d8
bring back sqrt solution
2Clutch a48676d
add math library linking flag for sqrt support
2Clutch cc5ba65
adjust error handling & output format
2Clutch 7a280fa
Merge branch 'main' into master
rzuckerm 5243c9f
Merge branch 'main' into master
rzuckerm 9b57dc5
Merge branch 'TheRenegadeCoder:main' into master
2Clutch a29234e
add dijkstra.c
2Clutch 2f72523
update solution
2Clutch 5ce295d
fix input parsing
2Clutch f13d940
update input parsing
2Clutch f919d9a
fix algorithm implementation & input parsing
2Clutch 29de0db
fix edge case handling
2Clutch 94cf360
undo brain fart
2Clutch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
| #include <string.h> | ||
| #include <limits.h> | ||
| #include <stdbool.h> | ||
|
|
||
| #define MAX_NODES 100 | ||
|
|
||
| int dijkstra(int graph[MAX_NODES][MAX_NODES], int src, int dest, int n) { | ||
| int dist[MAX_NODES]; | ||
| bool sptSet[MAX_NODES]; | ||
|
|
||
| for (int i = 0; i < n; i++) { | ||
| dist[i] = INT_MAX; | ||
| sptSet[i] = false; | ||
| } | ||
|
|
||
| dist[src] = 0; | ||
|
|
||
| for (int count = 0; count < n - 1; count++) { | ||
| int min = INT_MAX, min_index; | ||
|
|
||
| for (int v = 0; v < n; v++) | ||
| if (sptSet[v] == false && dist[v] <= min) | ||
| min = dist[v], min_index = v; | ||
|
|
||
| int u = min_index; | ||
| sptSet[u] = true; | ||
|
|
||
| for (int v = 0; v < n; v++) | ||
| if (!sptSet[v] && graph[u][v] && dist[u] != INT_MAX | ||
| && dist[u] + graph[u][v] < dist[v]) | ||
| dist[v] = dist[u] + graph[u][v]; | ||
| } | ||
|
|
||
| return dist[dest] == INT_MAX ? -1 : dist[dest]; | ||
| } | ||
|
|
||
| int main(int argc, char *argv[]) { | ||
| if (argc != 4) { | ||
| printf("Usage: please provide three inputs: a serialized matrix, a source node and a destination node\n"); | ||
| return 1; | ||
| } | ||
|
|
||
| char *matrix = argv[1]; | ||
| int src = atoi(argv[2]); | ||
| int dest = atoi(argv[3]); | ||
|
|
||
| int n = 0; | ||
| for (int i = 0; matrix[i]; i++) | ||
| if (matrix[i] == ',') n++; | ||
| n = (int)sqrt(n + 1); | ||
|
|
||
| if (n * n != strlen(matrix) / 2 + 1) { | ||
| printf("Usage: please provide three inputs: a serialized matrix, a source node and a destination node\n"); | ||
| return 1; | ||
| } | ||
|
|
||
| if (src < 0 || dest < 0 || src >= n || dest >= n) { | ||
| printf("Usage: please provide three inputs: a serialized matrix, a source node and a destination node\n"); | ||
| return 1; | ||
| } | ||
|
|
||
| int graph[MAX_NODES][MAX_NODES] = {0}; | ||
| char *token = strtok(matrix, ","); | ||
| for (int i = 0; i < n; i++) { | ||
| for (int j = 0; j < n; j++) { | ||
| if (!token) { | ||
| printf("Usage: please provide three inputs: a serialized matrix, a source node and a destination node\n"); | ||
| return 1; | ||
| } | ||
| int weight = atoi(token); | ||
| if (weight < 0) { | ||
| printf("Usage: please provide three inputs: a serialized matrix, a source node and a destination node\n"); | ||
| return 1; | ||
| } | ||
| graph[i][j] = weight; | ||
| token = strtok(NULL, ","); | ||
| } | ||
| } | ||
|
|
||
| int result = dijkstra(graph, src, dest, n); | ||
| if (result == -1) { | ||
| printf("Usage: please provide three inputs: a serialized matrix, a source node and a destination node\n"); | ||
| } else { | ||
| printf("%d\n", result); | ||
| } | ||
|
|
||
| return 0; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.