is.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #!/usr/bin/env python3
  2. import sys
  3. # utility class
  4. class Tree:
  5. def __init__(self, value):
  6. self.value = value
  7. self.children = list()
  8. # wrapper removing truth value from tuple
  9. def independent_set_in_tree(self):
  10. return self.independent_set()[1]
  11. # represents implementation of independent-set-in-a-forest
  12. # as presented in our lectures
  13. def independent_set(self):
  14. s = list()
  15. adjacent = False
  16. if self.children == list():
  17. s.append(self.value)
  18. return (False, s)
  19. for c in self.children:
  20. if c == None: continue
  21. cis = c.independent_set()
  22. if not cis[0]:
  23. adjacent = True
  24. [s.append(i) for i in cis[1]]
  25. if not adjacent:
  26. s.append(self.value)
  27. return (adjacent, s)
  28. def add(self, pair):
  29. if self.value == pair[0]:
  30. self.children.append(Tree(pair[1]))
  31. return
  32. [c.add(pair) for c in self.children]
  33. def __str__(self):
  34. return str(self.value) + ", (" + ", ".join(str(c) for c in self.children) + ")"
  35. # wraps remove, as node is potentially not contained in list
  36. def remove(nodes, node):
  37. try:
  38. nodes.remove(node)
  39. except ValueError:
  40. pass
  41. # original algorithm!
  42. def biggest_independent_sets(trees, w_i):
  43. # use independent-set-in-a-forest for all individual trees
  44. independent_sets = list()
  45. for tree in trees:
  46. independent_sets.append(tree.independent_set_in_tree())
  47. print("independent_sets:", independent_sets)
  48. onethree = 1 if independent_sets[0][-1] == w_i[0] else 0
  49. onethree += 1 if independent_sets[2][-1] == w_i[2] else 0
  50. twofour = 1 if independent_sets[1][-1] == w_i[1] else 0
  51. twofour += 1 if independent_sets[3][-1] == w_i[3] else 0
  52. print(onethree, twofour)
  53. biggest_total_is = list()
  54. # decide between w_1, ..., w_4
  55. if onethree >= twofour:
  56. remove(independent_sets[1], w_i[1])
  57. remove(independent_sets[3], w_i[3])
  58. else:
  59. remove(independent_sets[0], w_i[0])
  60. remove(independent_sets[2], w_i[2])
  61. # append
  62. for independent_set in independent_sets:
  63. [biggest_total_is.append(s) for s in independent_set]
  64. return biggest_total_is
  65. def main(lines):
  66. # parsing
  67. T = list()
  68. for line in lines.split("\n"):
  69. if line == "": continue
  70. E_i = list()
  71. V_i = list()
  72. for pair in line.split(" "):
  73. P = list(map(int, pair.split(",")))
  74. E_i.append((P[0], P[1]))
  75. V_i.append(P[0])
  76. V_i.append(P[1])
  77. T.append(E_i)
  78. print("graph:", T)
  79. # init
  80. trees = list()
  81. w_i = list()
  82. for subtree in T:
  83. root = subtree[0][0]
  84. tree = Tree(root)
  85. w_i.append(root)
  86. for pair in subtree:
  87. tree.add(pair)
  88. print("tree:", tree)
  89. trees.append(tree)
  90. print(biggest_independent_sets(trees, w_i))
  91. if __name__ == '__main__':
  92. lines = sys.stdin.read()
  93. main(lines)