import matplotlib.pyplot as plt
import networkx as nx
import seaborn as sns
sns.set()
%matplotlib inline
import warnings
import matplotlib.cbook
warnings.filterwarnings("ignore",category=matplotlib.cbook.mplDeprecation)
G = nx.karate_club_graph()
nx.draw(G)
degree(G[, nbunch, weight])
Returns a degree view of single node or of nbunch of nodes.degree_histogram(G)
Returns a list of the frequency of each degree value.density(G)
Returns the density of a graph.info(G[, n])
Print short summary of information for the graph G or the node n.create_empty_copy(G[, with_data])
Returns a copy of the graph G with all of the edges removed.is_directed(G)
Return True if graph is directed.to_directed(graph)
Returns a directed view of the graph graph.to_undirected(graph)
Returns an undirected view of the graph graph.is_empty(G)
Returns True if G has no edges.add_star(G_to_add_to, nodes_for_star, **attr)
Add a star to Graph G_to_add_to.add_path(G_to_add_to, nodes_for_path, **attr)
Add a path to the Graph G_to_add_to.add_cycle(G_to_add_to, nodes_for_cycle, **attr)
Add a cycle to the Graph G_to_add_to.subgraph(G, nbunch)
Returns the subgraph induced on nodes in nbunch.induced_subgraph(G, nbunch)
Returns a SubGraph view of G showing only nodes in nbunch.restricted_view(G, nodes, edges)
Returns a view of G with hidden nodes and edges.reverse_view(digraph)
Provide a reverse view of the digraph with edges reversed.edge_subgraph(G, edges)
Returns a view of the subgraph induced by the specified edges.nx.degree(G)
DegreeView({0: 16, 1: 9, 2: 10, 3: 6, 4: 3, 5: 4, 6: 4, 7: 4, 8: 5, 9: 2, 10: 3, 11: 1, 12: 2, 13: 5, 14: 2, 15: 2, 16: 2, 17: 2, 18: 2, 19: 3, 20: 2, 21: 2, 22: 2, 23: 5, 24: 3, 25: 3, 26: 2, 27: 4, 28: 3, 29: 4, 30: 4, 31: 6, 32: 12, 33: 17})
nx.degree_histogram(G)
[0, 1, 11, 6, 6, 3, 2, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1]
nx.density(G)
0.13903743315508021
nx.info(G)
"Name: Zachary's Karate Club\nType: Graph\nNumber of nodes: 34\nNumber of edges: 78\nAverage degree: 4.5882"
DG = nx.to_directed(G)
nx.draw(DG)
nx.is_directed(G)
False
nx.is_directed(DG)
True
nodes(G)
Returns an iterator over the graph nodes.number_of_nodes(G)
Returns the number of nodes in the graph.neighbors(G, n)
Returns a list of nodes connected to node n.all_neighbors(graph, node)
Returns all of the neighbors of a node in the graph.non_neighbors(graph, node)
Returns the non-neighbors of the node in the graph.common_neighbors(G, u, v)
Returns the common neighbors of two nodes in a graph.nx.nodes(G),G.nodes()
(NodeView((0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33)), NodeView((0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33)))
nx.number_of_nodes(G), G.number_of_nodes()
(34, 34)
[n for n in nx.neighbors(G,1)],[n for n in G.neighbors(1)]
([0, 2, 3, 7, 13, 17, 19, 21, 30], [0, 2, 3, 7, 13, 17, 19, 21, 30])
for node in G.nodes():
print(node,"|",[n for n in G.neighbors(node)])
0 | [1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12, 13, 17, 19, 21, 31] 1 | [0, 2, 3, 7, 13, 17, 19, 21, 30] 2 | [0, 1, 3, 7, 8, 9, 13, 27, 28, 32] 3 | [0, 1, 2, 7, 12, 13] 4 | [0, 6, 10] 5 | [0, 6, 10, 16] 6 | [0, 4, 5, 16] 7 | [0, 1, 2, 3] 8 | [0, 2, 30, 32, 33] 9 | [2, 33] 10 | [0, 4, 5] 11 | [0] 12 | [0, 3] 13 | [0, 1, 2, 3, 33] 14 | [32, 33] 15 | [32, 33] 16 | [5, 6] 17 | [0, 1] 18 | [32, 33] 19 | [0, 1, 33] 20 | [32, 33] 21 | [0, 1] 22 | [32, 33] 23 | [25, 27, 29, 32, 33] 24 | [25, 27, 31] 25 | [23, 24, 31] 26 | [29, 33] 27 | [2, 23, 24, 33] 28 | [2, 31, 33] 29 | [23, 26, 32, 33] 30 | [1, 8, 32, 33] 31 | [0, 24, 25, 28, 32, 33] 32 | [2, 8, 14, 15, 18, 20, 22, 23, 29, 30, 31, 33] 33 | [8, 9, 13, 14, 15, 18, 19, 20, 22, 23, 26, 27, 28, 29, 30, 31, 32]
[n for n in nx.all_neighbors(G,33)]
[8, 9, 13, 14, 15, 18, 19, 20, 22, 23, 26, 27, 28, 29, 30, 31, 32]
[n for n in nx.common_neighbors(G, 32, 33)]
[8, 14, 15, 18, 20, 22, 23, 29, 30, 31]
edges(G[, nbunch])
Returns an edge view of edges incident to nodes in nbunch.number_of_edges(G)
Returns the number of edges in the graph.density(G)
Returns the density of a graph.non_edges(graph)
Returns the non-existent edges in the graph.nx.edges(G), G.edges()
(EdgeView([(0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (0, 7), (0, 8), (0, 10), (0, 11), (0, 12), (0, 13), (0, 17), (0, 19), (0, 21), (0, 31), (1, 2), (1, 3), (1, 7), (1, 13), (1, 17), (1, 19), (1, 21), (1, 30), (2, 3), (2, 7), (2, 8), (2, 9), (2, 13), (2, 27), (2, 28), (2, 32), (3, 7), (3, 12), (3, 13), (4, 6), (4, 10), (5, 6), (5, 10), (5, 16), (6, 16), (8, 30), (8, 32), (8, 33), (9, 33), (13, 33), (14, 32), (14, 33), (15, 32), (15, 33), (18, 32), (18, 33), (19, 33), (20, 32), (20, 33), (22, 32), (22, 33), (23, 25), (23, 27), (23, 29), (23, 32), (23, 33), (24, 25), (24, 27), (24, 31), (25, 31), (26, 29), (26, 33), (27, 33), (28, 31), (28, 33), (29, 32), (29, 33), (30, 32), (30, 33), (31, 32), (31, 33), (32, 33)]), EdgeView([(0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (0, 7), (0, 8), (0, 10), (0, 11), (0, 12), (0, 13), (0, 17), (0, 19), (0, 21), (0, 31), (1, 2), (1, 3), (1, 7), (1, 13), (1, 17), (1, 19), (1, 21), (1, 30), (2, 3), (2, 7), (2, 8), (2, 9), (2, 13), (2, 27), (2, 28), (2, 32), (3, 7), (3, 12), (3, 13), (4, 6), (4, 10), (5, 6), (5, 10), (5, 16), (6, 16), (8, 30), (8, 32), (8, 33), (9, 33), (13, 33), (14, 32), (14, 33), (15, 32), (15, 33), (18, 32), (18, 33), (19, 33), (20, 32), (20, 33), (22, 32), (22, 33), (23, 25), (23, 27), (23, 29), (23, 32), (23, 33), (24, 25), (24, 27), (24, 31), (25, 31), (26, 29), (26, 33), (27, 33), (28, 31), (28, 33), (29, 32), (29, 33), (30, 32), (30, 33), (31, 32), (31, 33), (32, 33)]))
nx.edges(G, 33)
EdgeDataView([(33, 8), (33, 9), (33, 13), (33, 14), (33, 15), (33, 18), (33, 19), (33, 20), (33, 22), (33, 23), (33, 26), (33, 27), (33, 28), (33, 29), (33, 30), (33, 31), (33, 32)])
for node in G.nodes():
print(node,"|",nx.edges(G,node))
0 | [(0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (0, 7), (0, 8), (0, 10), (0, 11), (0, 12), (0, 13), (0, 17), (0, 19), (0, 21), (0, 31)] 1 | [(1, 0), (1, 2), (1, 3), (1, 7), (1, 13), (1, 17), (1, 19), (1, 21), (1, 30)] 2 | [(2, 0), (2, 1), (2, 3), (2, 7), (2, 8), (2, 9), (2, 13), (2, 27), (2, 28), (2, 32)] 3 | [(3, 0), (3, 1), (3, 2), (3, 7), (3, 12), (3, 13)] 4 | [(4, 0), (4, 6), (4, 10)] 5 | [(5, 0), (5, 6), (5, 10), (5, 16)] 6 | [(6, 0), (6, 4), (6, 5), (6, 16)] 7 | [(7, 0), (7, 1), (7, 2), (7, 3)] 8 | [(8, 0), (8, 2), (8, 30), (8, 32), (8, 33)] 9 | [(9, 2), (9, 33)] 10 | [(10, 0), (10, 4), (10, 5)] 11 | [(11, 0)] 12 | [(12, 0), (12, 3)] 13 | [(13, 0), (13, 1), (13, 2), (13, 3), (13, 33)] 14 | [(14, 32), (14, 33)] 15 | [(15, 32), (15, 33)] 16 | [(16, 5), (16, 6)] 17 | [(17, 0), (17, 1)] 18 | [(18, 32), (18, 33)] 19 | [(19, 0), (19, 1), (19, 33)] 20 | [(20, 32), (20, 33)] 21 | [(21, 0), (21, 1)] 22 | [(22, 32), (22, 33)] 23 | [(23, 25), (23, 27), (23, 29), (23, 32), (23, 33)] 24 | [(24, 25), (24, 27), (24, 31)] 25 | [(25, 23), (25, 24), (25, 31)] 26 | [(26, 29), (26, 33)] 27 | [(27, 2), (27, 23), (27, 24), (27, 33)] 28 | [(28, 2), (28, 31), (28, 33)] 29 | [(29, 23), (29, 26), (29, 32), (29, 33)] 30 | [(30, 1), (30, 8), (30, 32), (30, 33)] 31 | [(31, 0), (31, 24), (31, 25), (31, 28), (31, 32), (31, 33)] 32 | [(32, 2), (32, 8), (32, 14), (32, 15), (32, 18), (32, 20), (32, 22), (32, 23), (32, 29), (32, 30), (32, 31), (32, 33)] 33 | [(33, 8), (33, 9), (33, 13), (33, 14), (33, 15), (33, 18), (33, 19), (33, 20), (33, 22), (33, 23), (33, 26), (33, 27), (33, 28), (33, 29), (33, 30), (33, 31), (33, 32)]
nx.number_of_edges(G),G.number_of_edges()
(78, 78)
nx.density(G)
0.13903743315508021
is_weighted(G[, edge, weight])
Returns True if G has weighted edges.is_negatively_weighted(G[, edge, weight])
Returns True if G has negatively weighted edges.set_node_attributes(G, values[, name])
Sets node attributes from a given value or dictionary of values.get_node_attributes(G, name)
Get node attributes from graphset_edge_attributes(G, values[, name])
Sets edge attributes from a given value or dictionary of values.get_edge_attributes(G, name)
Get edge attributes from graphnx.is_weighted(G)
False
nx.is_negatively_weighted(G)
False
nx.get_node_attributes(G, 33)
{}
nx.get_edge_attributes(G,33)
{}