def gen_graph(self):
    ''' a generator that yield graphs in order
    '''
    while True:
      try:
        line = self.fd.next()
        nVertices, nEdges = map(int, line.split())
        if g_TryFlowNetwork:
          g = FlowNetwork()
          g.add_vertices(xrange(nVertices))
          for i in xrange(nEdges):
            u,v,w = map(int, self.fd.next().split())
            if u < v:
              g.add_edge(u, v, w)
            else:
              g.add_edge(v, u, w)
          g.src, g.sink = 0, nVertices-1
        else:
          g = UndirectedGraph()
          g.add_vertices(xrange(nVertices))
          for i in xrange(nEdges):
            u,v,w = map(int, self.fd.next().split())
            g.add_edge(u, v, w)          
        yield g
      except StopIteration:
        break
 a generator that yield graphs in order
    
recipes/Python/576907_Minimum_Cut_Solver/recipe-576907.py