title: Using combinatios and unions in Python
body: <p><strong>This is a homework code, I will need help understanding why the code doesn't run currently and what I can do to fix it</strong></p>
  
  <p>The homework problem is opening a file of some numbers. The first number will represent a room number, room[0], and the second number represents a room number, room[1], that is adjacent to the first room; this continues on until the code has iterated though the whole text document. It will then produce a adjacency matrix. This is done with a class. I have this part complete, and it works like it should. </p>
  
  <p>The next part is finding the minimum number of "TA's" are needed to cover the rooms, and what rooms to place the TA's in so that through the connected rooms a TA can be in every room. Basically greedy algorithm. </p>
  
  <p>Here is a visual example of the out come of the test file I will share:</p>
  
  <pre><code>    TAs needed: 2
      TAs assigned to rooms: 2, 5
      Adjacency Matrix
      1: 2
      2: 1 3
      3: 2 4
      4: 3 5
      5: 4 6
      6: 5
  </code></pre>
  
  <p>What this means, there are 6 study rooms and they connect to the rooms that are listed next to them. The minimum number of TAs required to service all 6 rooms is 2, and to make sure a TA can be in every room one needs to be placed in room 2 and the other in room 5. </p>
  
  <p>Naturally, the part I need help with is finding the minimum number of TAs and which rooms to put them in. I know what I need to do, for the most part, but I don't know how to get there. Here is my code:</p>
  
  <pre><code>import itertools
  
  class Matrix(object):
      '''
      Class to create a matrix of adjacent rooms. 
      '''
  
      def __init__(self):  # no modification is needed for this method, but you may modify it if you wish to
          '''
          Create and initialize your class attributes.
          '''
          self._matrix = []
          self._rooms = 0
  
      def read_file(self,fp):  #fp is a file pointer
          '''
          Function reads the given file and returns a matrix
          '''
  
          self._rooms = int(fp.readline())
          for i in range(self._rooms + 1):
              self._matrix.append(set())
          for line in fp:
              line = line.split()
              x = int(line[0])
              y = int(line[1])
              self._matrix[x].add(y)
              self._matrix[y].add(x)
  
          return self._matrix
  
  
      def __str__(self):
          '''
          '''
          s = ''
          for i,L in enumerate(self._matrix):
              if i &gt; 0:
                 s += "{}: {}\n".format( i,' '.join(str(l) for l in L) ) 
          return  "Adjancency Matrix: \n{}".format(s) 
  
  
      def __repr__(self):  # no modification need of this method
          '''
          Call __str__() to return a string for displaying in the shell
          '''
          return self.__str__()  
  
      def adjacent(self,index):
          '''
          Return the set of connecting rooms to room specified by index
          '''
          return self._matrix[index]
  
      def rooms(self):
          '''Return the number of rooms
          '''
          return self._rooms
  
  
  def open_file():
  
      ''' Function to open a file, if the file does not exsist 
      it will return an error message and ask the user to input the file name.
      '''
      while True:
          prompt = input("\nEnter the name of the file: ")
          try:
              fp = open(prompt,'r')
              return fp
              #where fp is the file pointer that will be used throughout the code
          except FileNotFoundError:
              print("\nFile not found, please try again. ")
  
  
  def main():
      '''
      Main function determines the number of TAs needed to cover each
      room
      '''
      TAs=0
      enough = True
      list_of_rooms = set()
      fp = open_file()
      matrix = Matrix()
      file = matrix.read_file(fp)
      x = set()
      while True:
          TAs +=1
          if enough != False:
              for room in file:
                  list_of_rooms=(room)
  
                  for tup_set in itertools.combinations(list_of_rooms, TAs):
                      for i in tup_set:
                          x.update(matrix.adjacent(i).union(matrix.adjacent(TAs)))
                          if x == set():
                              enough = True
                          else:
                              print("TAs Needed: {}".format(TAs))
                              print( "TAs assigned to rooms: {}".format(x))
                              enough = False
          elif enough == False:
              break                 
  
      print(matrix.__str__())
  
  
  if __name__ == "__main__":
      main() 
  </code></pre>
  
  <p>The file to open is:</p>
  
  <pre><code>6
  1 2
  2 3
  3 4
  4 5
  5 6
  </code></pre>
  
  <p>The top most single number of the file indicates the total number of rooms.</p>
  
  <p>I know that using itertools.combinations will give me the total number of combinations of rooms the number of TAs can be in. I know that I can use union to find which rooms to put the TAs in, but I can't seem to 1: determine the number of TAs correctly and 2: correctly print the rooms to put them in. </p>
  
  <p>I need some help in understanding how to complete this problem. I apologize for the length of the question, I could not think of a way to shorten it. </p>