NP-Complete: First working version
This commit is contained in:
38
NP-Complete/cs412_np_independent_set.py
Normal file
38
NP-Complete/cs412_np_independent_set.py
Normal file
@@ -0,0 +1,38 @@
|
||||
"""
|
||||
name: Nicholas Tamassia
|
||||
|
||||
Honor Code and Acknowledgments:
|
||||
|
||||
This work complies with the JMU Honor Code.
|
||||
|
||||
Comments here on your code and submission.
|
||||
"""
|
||||
|
||||
|
||||
def independent_set(proposed_set: set[int], graph: dict[int, set[int]]) -> bool:
|
||||
for vertex in proposed_set:
|
||||
for adjecent in graph[vertex]:
|
||||
if adjecent in proposed_set:
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
|
||||
# All modules for CS 412 must include a main method that allows it
|
||||
# to imported and invoked from other python scripts
|
||||
def main():
|
||||
n = int(input())
|
||||
|
||||
graph: dict[int, set[int]] = {}
|
||||
|
||||
for _ in range(n):
|
||||
vertecies = list(map(int, input().split()))
|
||||
graph[vertecies[0]] = set(vertecies[1:])
|
||||
|
||||
proposed_set = set(map(int, input().split()))
|
||||
|
||||
print("TRUE" if independent_set(proposed_set, graph) else "FALSE")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user