0% found this document useful (0 votes)
52 views3 pages

Directed Graphs: Digraph Digraph - Out - Edges Digraph - in - Degree Digraph - Predecessors Digraph - Successors

1. The document discusses different types of graphs that NetworkX supports including directed graphs, multigraphs, and undirected graphs. 2. It provides examples of how to construct and analyze these different graph types using NetworkX including adding nodes and edges, calculating degree, finding connected components, and computing shortest paths. 3. The document also reviews how to generate graphs using classic constructions, stochastic models, or loading graphs from files as well as classic algorithms supported by NetworkX like shortest paths, connected components, and clustering.

Uploaded by

Kitty Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views3 pages

Directed Graphs: Digraph Digraph - Out - Edges Digraph - in - Degree Digraph - Predecessors Digraph - Successors

1. The document discusses different types of graphs that NetworkX supports including directed graphs, multigraphs, and undirected graphs. 2. It provides examples of how to construct and analyze these different graph types using NetworkX including adding nodes and edges, calculating degree, finding connected components, and computing shortest paths. 3. The document also reviews how to generate graphs using classic constructions, stochastic models, or loading graphs from files as well as classic algorithms supported by NetworkX like shortest paths, connected components, and clustering.

Uploaded by

Kitty Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Directed graphs

The  DiGraph  class provides additional properties specific to directed edges,


e.g., DiGraph.out_edges() ,  DiGraph.in_degree() ,  DiGraph.predecessors() ,  DiGraph.successors()  etc. To
allow algorithms to work with both classes easily, the directed versions of  neighbors()  is
equivalent to  successors()  while  degree  reports the sum of  in_degree  and  out_degree  even though
that may feel inconsistent at times.

>>>
>>> DG = nx.DiGraph()
>>> DG.add_weighted_edges_from([(1, 2, 0.5), (3, 1, 0.75)])
>>> DG.out_degree(1, weight='weight')
0.5
>>> DG.degree(1, weight='weight')
1.25
>>> list(DG.successors(1))
[2]
>>> list(DG.neighbors(1))
[2]
Some algorithms work only for directed graphs and others are not well defined for directed
graphs. Indeed the tendency to lump directed and undirected graphs together is dangerous. If
you want to treat a directed graph as undirected for some measurement you should probably
convert it using  Graph.to_undirected()  or with

>>>
>>> H = nx.Graph(G) # convert G to undirected graph

Multigraphs
NetworkX provides classes for graphs which allow multiple edges between any pair of nodes.
The  MultiGraph  and  MultiDiGraph  classes allow you to add the same edge twice, possibly with
different edge data. This can be powerful for some applications, but many algorithms are not
well defined on such graphs. Where results are well defined, e.g.,  MultiGraph.degree()  we
provide the function. Otherwise you should convert to a standard graph in a way that makes
the measurement well defined.

>>>
>>> MG = nx.MultiGraph()
>>> MG.add_weighted_edges_from([(1, 2, 0.5), (1, 2, 0.75), (2, 3, 0.5)])
>>> dict(MG.degree(weight='weight'))
{1: 1.25, 2: 1.75, 3: 0.5}
>>> GG = nx.Graph()
>>> for n, nbrs in MG.adjacency():
... for nbr, edict in nbrs.items():
... minvalue = min([d['weight'] for d in edict.values()])
... GG.add_edge(n, nbr, weight = minvalue)
...
>>> nx.shortest_path(GG, 1, 3)
[1, 2, 3]

Graph generators and graph operations


In addition to constructing graphs node-by-node or edge-by-edge, they can also be generated
by

1. Applying classic graph operations, such as:


2. subgraph(G, nbunch) - induced subgraph view of G on nodes in nbunch
3. union(G1,G2) - graph union
4. disjoint_union(G1,G2) - graph union assuming all nodes are different
5. cartesian_product(G1,G2) - return Cartesian product graph
6. compose(G1,G2) - combine graphs identifying nodes common to both
7. complement(G) - graph complement
8. create_empty_copy(G) - return an empty copy of the same graph class
9. to_undirected(G) - return an undirected representation of G
10. to_directed(G) - return a directed representation of G
11. Using a call to one of the classic small graphs, e.g.,
>>>
>>> petersen = nx.petersen_graph()
>>> tutte = nx.tutte_graph()
>>> maze = nx.sedgewick_maze_graph()
>>> tet = nx.tetrahedral_graph()
3. Using a (constructive) generator for a classic graph, e.g.,
>>>
>>> K_5 = nx.complete_graph(5)
>>> K_3_5 = nx.complete_bipartite_graph(3, 5)
>>> barbell = nx.barbell_graph(10, 10)
>>> lollipop = nx.lollipop_graph(10, 20)
4. Using a stochastic graph generator, e.g.,
>>>
>>> er = nx.erdos_renyi_graph(100, 0.15)
>>> ws = nx.watts_strogatz_graph(30, 3, 0.1)
>>> ba = nx.barabasi_albert_graph(100, 5)
>>> red = nx.random_lobster(100, 0.9, 0.9)
5. Reading a graph stored in a file using common graph formats, such as edge lists,
adjacency lists, GML, GraphML, pickle, LEDA and others.
>>>
>>> nx.write_gml(red, "path.to.file")
>>> mygraph = nx.read_gml("path.to.file")
For details on graph formats see Reading and writing graphs and for graph generator
functions see Graph generators

Analyzing graphs
The structure of  G  can be analyzed using various graph-theoretic functions such as:

>>>
>>> G = nx.Graph()
>>> G.add_edges_from([(1, 2), (1, 3)])
>>> G.add_node("spam") # adds node "spam"
>>> list(nx.connected_components(G))
[{1, 2, 3}, {'spam'}]
>>> sorted(d for n, d in G.degree())
[0, 1, 1, 2]
>>> nx.clustering(G)
{1: 0, 2: 0, 3: 0, 'spam': 0}
Some functions with large output iterate over (node, value) 2-tuples. These are easily stored
in a  dict  structure if you desire.

>>>
>>> sp = dict(nx.all_pairs_shortest_path(G))
>>> sp[3]
{3: [3], 1: [3, 1], 2: [3, 1, 2]}
See Algorithms for details on graph algorithms supported.

You might also like