-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add graph_prim #63
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
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
89e7e77
fix: cosmetic details
ryukinix 6c72ca8
feat: add graph_prim declaration on graph.h
ryukinix 9260b36
feat: add utils/pair_hash.h
ryukinix b73960e
fixup! fix: cosmetic details
ryukinix 3bc3dd9
fix: graph_print not printing nodes
ryukinix def908d
feat: add graph_has_node
ryukinix 79fbc48
feat: add graph_prim implementation using priority queue
ryukinix 03a0546
fix: graph_prim memory leak
ryukinix 2871db8
fix: use undirected graphs for prim and kruskal
ryukinix ef01559
tests: add assert of kruskal and prim over edge cost sum
ryukinix 801e2c8
fix: free objects and generate .dot files for kruskal/prim
ryukinix bfc34f0
tests: fix newline missing
ryukinix 4bd9b72
fix: memory leak over graph_remove_duplicated_edges
ryukinix 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
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
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
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
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,46 @@ | ||
| #include "graph.h" | ||
| #include "../utils/pair_hash.h" | ||
| #include "../pqueue/pqueue.h" | ||
|
|
||
| void update_heap(Graph* g, PQueue* pq, Set* visited, int start) { | ||
| Set* neighbors = graph_get_neighbors(g, start); | ||
| Iterator *it = set_iterator_items(neighbors); | ||
| while (!iterator_done(it)) { | ||
| List *neighbor_weight = (List*) iterator_next(it); | ||
| if (!set_contains(visited, neighbor_weight->key)) { | ||
| int k = pair_hash(start, neighbor_weight->key); | ||
| int w = neighbor_weight->data; | ||
| pqueue_insert(pq, k, w); | ||
| } | ||
| } | ||
| set_free(neighbors); | ||
| iterator_free(it); | ||
| } | ||
|
|
||
| // WARNING: this implementation only return the Minimum Spanning Tree | ||
| // of conected component from <start> node. If some node is | ||
| // unreachable from <start> will be not included in the final tree. | ||
| Graph* graph_prim(Graph *g, int start) { | ||
| Graph* g_prim = graph_undirected_create(); | ||
| Set* visited = set_create(); | ||
| PQueue *pq = pqueue_create(MIN_PQUEUE); | ||
| update_heap(g, pq, visited, start); | ||
| set_add(visited, start); | ||
|
|
||
| while (!pqueue_is_empty(pq)) { | ||
| PQueueNode node = pqueue_extract(pq); | ||
| int u = unpair_hash_x(node.key); | ||
| int v = unpair_hash_y(node.key); | ||
| int w = node.value; | ||
| if (set_contains(visited, v)) { | ||
| continue; | ||
| } | ||
| graph_add_edge_with_weight(g_prim, u, v, w); | ||
| set_add(visited, v); | ||
| update_heap(g, pq, visited, v); | ||
| } | ||
| pqueue_free(pq); | ||
| set_free(visited); | ||
|
|
||
| return g_prim; | ||
| } |
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
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
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
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
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
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,31 @@ | ||
| #include <math.h> | ||
|
|
||
| /* @brief Pair function of Szudzik (2006). | ||
| * @details Useful to encapsulate two integers as a unique reversible integer hash. | ||
| * @return hashed integer. | ||
| * @see https://en.wikipedia.org/wiki/Pairing_function#Cantor_pairing_function | ||
| */ | ||
| static inline int pair_hash(int x, int y) { | ||
| if (x < y) { | ||
| return y * y + x; | ||
| } | ||
| return x * x + x + y; | ||
| } | ||
|
|
||
| static inline int unpair_hash_x(int z) { | ||
| int z_sqrt = floor(sqrt(z)); | ||
| int z_rest = z - z_sqrt * z_sqrt; | ||
| if (z_rest < z_sqrt) { | ||
| return z_rest; | ||
| } | ||
| return z_rest; | ||
| } | ||
|
|
||
| static inline int unpair_hash_y(int z) { | ||
| int z_sqrt = floor(sqrt(z)); | ||
| int z_rest = z - z_sqrt * z_sqrt; | ||
| if (z_rest < z_sqrt) { | ||
| return z_sqrt; | ||
| } | ||
| return z_rest - z_sqrt; | ||
| } | ||
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.