-
-
Notifications
You must be signed in to change notification settings - Fork 47k
Greedy min vertex cover hacktoberfest #5241
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
cclauss
merged 13 commits into
TheAlgorithms:master
from
manueldilullo:greedy_min_vertex_cover_hacktoberfest
Oct 15, 2021
Merged
Changes from 12 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
a078715
added complete graph generator function
manueldilullo 6490327
added doctest, type hints, wikipedia explanation
manueldilullo be997e6
added return type hint for function complete_graph
manueldilullo 54ecb1e
added descriptive name for the parameter: n
manueldilullo 9ac4b64
random graph generator with doctest and type hints
manueldilullo 7ee379c
added Greedy min vertex algorithm
manueldilullo fe73419
pre-commit hook(s) made changes
manueldilullo 6725c3a
Delete complete_graph_generator.py
manueldilullo 9f72eb8
Delete random_graph_generator.py
manueldilullo 65fba36
fixed doctest
manueldilullo 9d08ba4
updated commit following highligths
manueldilullo 37e2bc0
fixed following pre-commit highlights
manueldilullo 708da59
modified variables names
manueldilullo 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,65 @@ | ||
""" | ||
* Author: Manuel Di Lullo (https://github.com/manueldilullo) | ||
* Description: Approximization algorithm for minimum vertex cover problem. | ||
Greedy Approach. Uses graphs represented with an adjacency list | ||
|
||
URL: https://mathworld.wolfram.com/MinimumVertexCover.html | ||
URL: https://cs.stackexchange.com/questions/129017/greedy-algorithm-for-vertex-cover | ||
""" | ||
|
||
import heapq | ||
|
||
|
||
def greedy_min_vertex_cover(graph: dict) -> set: | ||
""" | ||
Greedy APX Algorithm for min Vertex Cover | ||
@input: graph (graph stored in an adjacency list where each vertex | ||
is represented with an integer) | ||
@example: | ||
>>> graph = {0: [1, 3], 1: [0, 3], 2: [0, 3, 4], 3: [0, 1, 2], 4: [2, 3]} | ||
>>> greedy_min_vertex_cover(graph) | ||
{0, 1, 2, 4} | ||
""" | ||
# queue used to store nodes and their rank | ||
queue = [] | ||
|
||
# for each node and his adjacency list add them and the rank of the node to queue | ||
# using heapq module the queue will be filled like a Priority Queue | ||
# heapq works with a min priority queue, so I used -1*len(v) to build it | ||
for k, v in graph.items(): | ||
# O(log(n)) | ||
heapq.heappush(queue, [-1 * len(v), (k, v)]) | ||
|
||
# s = set of chosen vertices | ||
s = set() | ||
|
||
# while queue isn't empty and there are still edges | ||
# (queue[0][0] is the rank of the node with max rank) | ||
while queue and queue[0][0] != 0: | ||
# extract vertex with max rank from queue and add it to s | ||
argmax = heapq.heappop(queue)[1][0] | ||
s.add(argmax) | ||
|
||
# Remove all arcs adjacent to argmax | ||
for elem in queue: | ||
# if v haven't adjacent node, skip | ||
if elem[0] == 0: | ||
continue | ||
# if argmax is reachable from elem | ||
# remove argmax from elem's adjacent list and update his rank | ||
if argmax in elem[1][1]: | ||
index = elem[1][1].index(argmax) | ||
del elem[1][1][index] | ||
elem[0] += 1 | ||
# re-order the queue | ||
heapq.heapify(queue) | ||
return s | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() | ||
|
||
# graph = {0: [1, 3], 1: [0, 3], 2: [0, 3, 4], 3: [0, 1, 2], 4: [2, 3]} | ||
# print(f"Minimum vertex cover:\n{greedy_min_vertex_cover(graph)}") |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and make similar changes elsewhere,