Convert
=======

>>> from networkx import *
>>> from networkx.convert import *
>>> from networkx.operators import *
>>> from networkx.generators.classic import barbell_graph,cycle_graph

Simple Graphs
--------------

>>> G=barbell_graph(10,3)

Dict of dicts
~~~~~~~~~~~~~

>>> dod=to_dict_of_dicts(G)
>>> GG=from_dict_of_dicts(dod)
>>> sorted(G.nodes())==sorted(GG.nodes())
True
>>> sorted(G.edges())==sorted(GG.edges())
True
>>> GW=from_whatever(dod)
>>> sorted(G.nodes())==sorted(GW.nodes())
True
>>> sorted(G.edges())==sorted(GW.edges())
True
>>> GI=Graph(dod)
>>> sorted(G.nodes())==sorted(GI.nodes())
True
>>> sorted(G.edges())==sorted(GI.edges())
True


>>> G=barbell_graph(10,3)




With nodelist keyword
---------------------

>>> P4=path_graph(4)
>>> P3=path_graph(3)
>>> dod=to_dict_of_dicts(P4,nodelist=[0,1,2])
>>> Gdod=Graph(dod)
>>> sorted(Gdod.nodes())==sorted(P3.nodes())
True
>>> sorted(Gdod.edges())==sorted(P3.edges())
True


Dict of lists
~~~~~~~~~~~~~

>>> dol=to_dict_of_lists(G)
>>> GG=from_dict_of_lists(dol)
>>> sorted(G.nodes())==sorted(GG.nodes())
True
>>> sorted(G.edges())==sorted(GG.edges())
True
>>> GW=from_whatever(dol)
>>> sorted(G.nodes())==sorted(GW.nodes())
True
>>> sorted(G.edges())==sorted(GW.edges())
True
>>> GI=Graph(dol)
>>> sorted(G.nodes())==sorted(GI.nodes())
True
>>> sorted(G.edges())==sorted(GI.edges())
True



With nodelist keyword
---------------------

>>> P4=path_graph(4)
>>> P3=path_graph(3)
>>> dol=to_dict_of_lists(P4,nodelist=[0,1,2])
>>> Gdol=Graph(dol)
>>> sorted(Gdol.nodes())==sorted(P3.nodes())
True
>>> sorted(Gdol.edges())==sorted(P3.edges())
True


DiGraphs
--------

>>> G=cycle_graph(10)

Dict of dicts
~~~~~~~~~~~~~

>>> dod=to_dict_of_dicts(G)
>>> GG=from_dict_of_dicts(dod)
>>> sorted(G.nodes())==sorted(GG.nodes())
True
>>> sorted(G.edges())==sorted(GG.edges())
True
>>> GW=from_whatever(dod)
>>> sorted(G.nodes())==sorted(GW.nodes())
True
>>> sorted(G.edges())==sorted(GW.edges())
True
>>> GI=Graph(dod)
>>> sorted(G.nodes())==sorted(GI.nodes())
True
>>> sorted(G.edges())==sorted(GI.edges())
True


>>> G=cycle_graph(10,create_using=DiGraph())

Dict of lists
~~~~~~~~~~~~~

>>> dol=to_dict_of_lists(G)
>>> GG=from_dict_of_lists(dol,create_using=DiGraph())
>>> sorted(G.nodes())==sorted(GG.nodes())
True
>>> sorted(G.edges())==sorted(GG.edges())
True
>>> GW=from_whatever(dol,create_using=DiGraph())
>>> sorted(G.nodes())==sorted(GW.nodes())
True
>>> sorted(G.edges())==sorted(GW.edges())
True
>>> GI=DiGraph(dol)
>>> sorted(G.nodes())==sorted(GI.nodes())
True
>>> sorted(G.edges())==sorted(GI.edges())
True


Graph
------

>>> G=cycle_graph(10)
>>> e=G.edges()
>>> source=[u for u,v in e]
>>> dest=[v for u,v in e]
>>> ex=zip(source,dest,source)
>>> G=Graph()
>>> G.add_weighted_edges_from(ex)

Dict of dicts
~~~~~~~~~~~~~

>>> dod=to_dict_of_dicts(G)
>>> GG=from_dict_of_dicts(dod,create_using=Graph())
>>> sorted(G.nodes())==sorted(GG.nodes())
True
>>> sorted(G.edges())==sorted(GG.edges())
True
>>> GW=from_whatever(dod,create_using=Graph())
>>> sorted(G.nodes())==sorted(GW.nodes())
True
>>> sorted(G.edges())==sorted(GW.edges())
True
>>> GI=Graph(dod)
>>> sorted(G.nodes())==sorted(GI.nodes())
True
>>> sorted(G.edges())==sorted(GI.edges())
True


Dict of lists
~~~~~~~~~~~~~

>>> dol=to_dict_of_lists(G)
>>> GG=from_dict_of_lists(dol,create_using=Graph())

dict of lists throws away edge data so set it to none
>>> enone=[(u,v,{}) for (u,v,d) in G.edges(data=True)]
>>> sorted(G.nodes())==sorted(GG.nodes())
True
>>> enone==sorted(GG.edges(data=True))
True
>>> GW=from_whatever(dol,create_using=Graph())
>>> sorted(G.nodes())==sorted(GW.nodes())
True
>>> enone==sorted(GW.edges(data=True))
True
>>> GI=Graph(dol)
>>> sorted(G.nodes())==sorted(GI.nodes())
True
>>> enone==sorted(GI.edges(data=True))
True


with multiedges and self loops

>>> G=cycle_graph(10)
>>> e=G.edges()
>>> source,dest = zip(*e)
>>> ex=zip(source,dest,source)
>>> XG=Graph()
>>> XG.add_weighted_edges_from(ex)
>>> XGM=MultiGraph()
>>> XGM.add_weighted_edges_from(ex)
>>> XGM.add_edge(0,1,weight=2) # multiedge
>>> XGS=Graph()
>>> XGS.add_weighted_edges_from(ex)
>>> XGS.add_edge(0,0,weight=100) # self loop


Dict of dicts
~~~~~~~~~~~~~

with self loops, OK

>>> dod=to_dict_of_dicts(XGS)
>>> GG=from_dict_of_dicts(dod,create_using=Graph())
>>> sorted(XGS.nodes())==sorted(GG.nodes())
True
>>> sorted(XGS.edges())==sorted(GG.edges())
True
>>> GW=from_whatever(dod,create_using=Graph())
>>> sorted(XGS.nodes())==sorted(GW.nodes())
True
>>> sorted(XGS.edges())==sorted(GW.edges())
True
>>> GI=Graph(dod)
>>> sorted(XGS.nodes())==sorted(GI.nodes())
True
>>> sorted(XGS.edges())==sorted(GI.edges())
True


Dict of lists
~~~~~~~~~~~~~
with self loops, OK

>>> dol=to_dict_of_lists(XGS)
>>> GG=from_dict_of_lists(dol,create_using=Graph())

dict of lists throws away edge data so set it to none
>>> enone=[(u,v,{}) for (u,v,d) in XGS.edges(data=True)]
>>> sorted(XGS.nodes())==sorted(GG.nodes())
True
>>> enone==sorted(GG.edges(data=True))
True
>>> GW=from_whatever(dol,create_using=Graph())
>>> sorted(XGS.nodes())==sorted(GW.nodes())
True
>>> enone==sorted(GW.edges(data=True))
True
>>> GI=Graph(dol)
>>> sorted(XGS.nodes())==sorted(GI.nodes())
True
>>> enone==sorted(GI.edges(data=True))
True

Dict of dicts
~~~~~~~~~~~~~

with multiedges, OK

>>> dod=to_dict_of_dicts(XGM)
>>> GG=from_dict_of_dicts(dod,create_using=MultiGraph(),multigraph_input=True)
>>> sorted(XGM.nodes())==sorted(GG.nodes())
True
>>> sorted(XGM.edges())==sorted(GG.edges())
True
>>> GW=from_whatever(dod,create_using=MultiGraph(),multigraph_input=True)
>>> sorted(XGM.nodes())==sorted(GW.nodes())
True
>>> sorted(XGM.edges())==sorted(GW.edges())
True
>>> GI=MultiGraph(dod)  # convert can't tell whether to duplicate edges!
>>> sorted(XGM.nodes())==sorted(GI.nodes())
True
>>> sorted(XGM.edges())!=sorted(GI.edges())
True
>>> GE=from_dict_of_dicts(dod,create_using=MultiGraph(),multigraph_input=False)  
>>> sorted(XGM.nodes())==sorted(GE.nodes())
True
>>> sorted(XGM.edges())!=sorted(GE.edges())
True
>>> GI=MultiGraph(XGM)
>>> sorted(XGM.nodes())==sorted(GI.nodes())
True
>>> sorted(XGM.edges())==sorted(GI.edges())
True
>>> GM=MultiGraph(G)
>>> sorted(GM.nodes())==sorted(G.nodes())
True
>>> sorted(GM.edges())==sorted(G.edges())
True


Dict of lists
~~~~~~~~~~~~~
with multiedges, OK, but better write as DiGraph else you'll get double edges

>>> dol=to_dict_of_lists(G)
>>> GG=from_dict_of_lists(dol,create_using=MultiGraph())
>>> sorted(G.nodes())==sorted(GG.nodes())
True
>>> sorted(G.edges())==sorted(GG.edges())
True
>>> GW=from_whatever(dol,create_using=MultiGraph())
>>> sorted(G.nodes())==sorted(GW.nodes())
True
>>> sorted(G.edges())==sorted(GW.edges())
True
>>> GI=MultiGraph(dol)
>>> sorted(G.nodes())==sorted(GI.nodes())
True
>>> sorted(G.edges())==sorted(GI.edges())
True

Edgelists
~~~~~~~~~

>>> P=path_graph(4)

>>> e=[(0,1),(1,2),(2,3)]
>>> G=Graph(e)
>>> sorted(G.nodes())==sorted(P.nodes())
True
>>> sorted(G.edges())==sorted(P.edges())
True
>>> sorted(G.edges(data=True))==sorted(P.edges(data=True))
True

>>> e=[(0,1,{}),(1,2,{}),(2,3,{})]
>>> G=Graph(e)
>>> sorted(G.nodes())==sorted(P.nodes())
True
>>> sorted(G.edges())==sorted(P.edges())
True
>>> sorted(G.edges(data=True))==sorted(P.edges(data=True))
True


>>> e=((n,n+1) for n in range(3))
>>> G=Graph(e)
>>> sorted(G.nodes())==sorted(P.nodes())
True
>>> sorted(G.edges())==sorted(P.edges())
True
>>> sorted(G.edges(data=True))==sorted(P.edges(data=True))
True



