-
-
Notifications
You must be signed in to change notification settings - Fork 47.1k
Matching min vertex cover #5326
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 6 commits into
TheAlgorithms:master
from
manueldilullo:matching_min_vertex_cover
Oct 15, 2021
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0e80ea5
matching algorithm for min vertex cover problem
manueldilullo b04e615
fixed hint on row 37
manueldilullo 97ba99f
changed variable names
manueldilullo 014c4cd
provided doctest for get_edges function
manueldilullo 5c48603
Removed dict.keys() iteration
manueldilullo 1e7c9bc
Update matching_min_vertex_cover.py
cclauss 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,62 @@ | ||
""" | ||
* Author: Manuel Di Lullo (https://github.com/manueldilullo) | ||
* Description: Approximization algorithm for minimum vertex cover problem. | ||
Matching Approach. Uses graphs represented with an adjacency list | ||
|
||
URL: https://mathworld.wolfram.com/MinimumVertexCover.html | ||
URL: https://www.princeton.edu/~aaa/Public/Teaching/ORF523/ORF523_Lec6.pdf | ||
""" | ||
|
||
|
||
def matching_min_vertex_cover(graph: dict) -> set: | ||
""" | ||
APX Algorithm for min Vertex Cover using Matching Approach | ||
@input: graph (graph stored in an adjacency list where each vertex | ||
is represented as an integer) | ||
@example: | ||
>>> graph = {0: [1, 3], 1: [0, 3], 2: [0, 3, 4], 3: [0, 1, 2], 4: [2, 3]} | ||
>>> matching_min_vertex_cover(graph) | ||
{0, 1, 2, 4} | ||
""" | ||
# chosen_vertices = set of chosen vertices | ||
chosen_vertices = set() | ||
# edges = list of graph's edges | ||
edges = get_edges(graph) | ||
|
||
# While there are still elements in edges list, take an arbitrary edge | ||
# (from_node, to_node) and add his extremity to chosen_vertices and then | ||
# remove all arcs adjacent to the from_node and to_node | ||
while edges: | ||
from_node, to_node = edges.pop() | ||
chosen_vertices.add(from_node) | ||
chosen_vertices.add(to_node) | ||
for edge in edges.copy(): | ||
if from_node in edge or to_node in edge: | ||
edges.discard(edge) | ||
return chosen_vertices | ||
|
||
|
||
def get_edges(graph: dict) -> set: | ||
""" | ||
Return a set of couples that represents all of the edges. | ||
@input: graph (graph stored in an adjacency list where each vertex is | ||
represented as an integer) | ||
@example: | ||
>>> graph = {0: [1, 3], 1: [0, 3], 2: [0, 3], 3: [0, 1, 2]} | ||
>>> get_edges(graph) | ||
{(0, 1), (3, 1), (0, 3), (2, 0), (3, 0), (2, 3), (1, 0), (3, 2), (1, 3)} | ||
""" | ||
edges = set() | ||
for from_node, to_nodes in graph.items(): | ||
for to_node in to_nodes: | ||
edges.add((from_node, to_node)) | ||
return edges | ||
|
||
|
||
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"Matching vertex cover:\n{matching_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.
Uh oh!
There was an error while loading. Please reload this page.