Return a string specifying the given graph in Dot language.
    
    @type  G: graph
    @param G: Graph.

    @type  weighted: boolean
    @param weighted: Whether edges should be labelled with their weight.
    
    @rtype:  string
    @return: String specifying the graph in Dot Language.

def write(G, weighted=False):
    """
    Return a string specifying the given graph in Dot language.
    
    @type  G: graph
    @param G: Graph.

    @type  weighted: boolean
    @param weighted: Whether edges should be labelled with their weight.
    
    @rtype:  string
    @return: String specifying the graph in Dot Language.
    """
    dotG = pydot.Dot()
    
    if not 'name' in dir(G):
        dotG.set_name('graphname')
    else:
        dotG.set_name(G.name)
    
    if (isinstance(G, graph)):
        dotG.set_type('graph')
        directed = False
    elif (isinstance(G, digraph)):
        dotG.set_type('digraph')
        directed = True
    elif (isinstance(G, hypergraph)):
        return write_hypergraph(G)
    else:
        raise InvalidGraphType("Expected graph or digraph, got %s" %  repr(G) )
    
    for node in G.nodes():
        attr_list = {}
        for attr in G.node_attributes(node):
            attr_list[str(attr[0])] = str(attr[1])
        
        newNode = pydot.Node(str(node), **attr_list)
        
        dotG.add_node(newNode)
        
    # Pydot doesn't work properly with the get_edge, so we use
    #  our own set to keep track of what's been added or not.
    seen_edges = set([])
    for edge_from, edge_to in G.edges():
        if (str(edge_from) + "-" + str(edge_to)) in seen_edges:
            continue

        if (not directed) and (str(edge_to) + "-" + str(edge_from)) in seen_edges:
            continue
        
        attr_list = {}
        for attr in G.edge_attributes((edge_from, edge_to)):
            attr_list[str(attr[0])] = str(attr[1])
        
        if str(G.edge_label((edge_from, edge_to))):
            attr_list['label'] = str(G.edge_label((edge_from, edge_to)))

        elif weighted:
            attr_list['label'] = str(G.edge_weight((edge_from, edge_to)))
        
        if weighted:
            attr_list['weight'] = str(G.edge_weight((edge_from, edge_to)))
        
        newEdge = pydot.Edge(str(edge_from), str(edge_to), **attr_list)
        
        dotG.add_edge(newEdge)
        
        seen_edges.add(str(edge_from) + "-" + str(edge_to))
        
    return dotG.to_string()