code: def graph_to_dimacs(g, f):
      """
      Persists the supplied graph in valid dimacs format into the file.
  
      Parameters
      ----------
      g : `~medpy.graphcut.graph.Graph`
          A graph object to persist.
      f : file
          A file-like object.
      """
      # write comments
      f.write("c Created by medpy\n")
      f.write("c Oskar Maier, oskar.maier@googlemail.com\n")
      f.write("c\n")
  
      # write problem
      f.write("c problem line\n")
      f.write(
          "p max {} {}\n".format(
              g.get_node_count() + 2, len(g.get_edges())
          )
      )  # +2 as terminal nodes also count in dimacs format # no-nodes / no-edges
  
      # denote source and sink
      f.write("c source descriptor\n")
      f.write("n 1 s\n")
      f.write("c sink descriptor\n")
      f.write("n 2 t\n")
  
      # write terminal arcs (t-weights)
      f.write("c terminal arcs (t-weights)\n")
      for node, weight in list(g.get_tweights().items()):
          # Note: the nodes ids of the graph start from 1, but 1 and 2 are reserved for source and sink respectively, therefore add 2
          if not 0 == weight[0]:  # 0 weights are implicit
              f.write("a 1 {} {}\n".format(node + 2, weight[0]))
          if not 0 == weight[1]:  # 0 weights are implicit
              f.write("a {} 2 {}\n".format(node + 2, weight[1]))
  
      # write inter-node arcs (n-weights)
      f.write("c inter-node arcs (n-weights)\n")
      for edge, weight in list(g.get_nweights().items()):
          if not 0 == weight[0]:  # 0 weights are implicit
              f.write(
                  "a {} {} {}\n".format(
                      edge[0] + 2, edge[1] + 2, weight[0]
                  )
              )
          # reversed weights have to follow directly in the next line
          if not 0 == weight[1]:  # 0 weights are implicit
              f.write(
                  "a {} {} {}\n".format(
                      edge[1] + 2, edge[0] + 2, weight[1]
                  )
              )
  
      # end comment
      f.write("c end-of-file")
  
label_desc: Use image node_id 1 for calling a global function with example usage: graph_to_dimacs(g, f) without return types