An undirected graph class without multiple (parallel) edges.
Nodes can be arbitrary (hashable) objects.
Arbitrary data/labels/objects can be associated with edges. The default data is 1. See add_edge and add_edges_from methods for details. Many NetworkX routines developed for weighted graphs assume this data is a number. Feel free to put other objects on the edges, but be aware that these weighted graph algorithms may give unpredictable results if the graph isn’t a weighted graph.
Self loops are allowed.
Examples
Create an empty graph structure (a “null graph”) with no nodes and no edges.
>>> import networkx as nx
>>> G=nx.Graph()
G can be grown in several ways. By adding one node at a time:
>>> G.add_node(1)
by adding a list of nodes:
>>> G.add_nodes_from([2,3])
by using an iterator:
>>> G.add_nodes_from(xrange(100,110))
or by adding any container of nodes (a list, dict, set or even a file or the nodes from another graph).
>>> H=nx.path_graph(10)
>>> G.add_nodes_from(H)
Any hashable object (except None) can represent a node, e.g. a customized node object, or even another Graph.
>>> G.add_node(H)
G can also be grown by adding one edge at a time:
>>> G.add_edge(1, 2)
by adding a list of edges:
>>> G.add_edges_from([(1,2),(1,3)])
or by adding any collection of edges:
>>> G.add_edges_from(H.edges())
Nodes will be added as needed when you add edges and there are no complaints when adding existing nodes or edges.
The default edge data is the number 1. To add edge information with an edge, use a 3-tuple (u,v,d).
>>> G.add_edges_from([(1,2,'blue'),(2,3,3.1)])
>>> G.add_edges_from( [(3,4),(4,5)], data='red')
>>> G.add_edge( 1, 2, 4.7 )
Graph.add_node (n) | Add a single node n. |
Graph.add_nodes_from (nbunch) | Add nodes from nbunch. |
Graph.remove_node (n) | Remove node n. |
Graph.remove_nodes_from (nbunch) | Remove nodes specified in nbunch. |
Graph.add_edge (u, v[, data]) | Add an edge between u and v with optional data. |
Graph.add_edges_from (ebunch[, data]) | Add all the edges in ebunch. |
Graph.remove_edge (u, v) | Remove the edge between (u,v). |
Graph.remove_edges_from (ebunch) | Remove all edges specified in ebunch. |
Graph.add_star (nlist[, data]) | Add a star. |
Graph.add_path (nlist[, data]) | Add a path. |
Graph.add_cycle (nlist[, data]) | Add a cycle. |
Graph.clear () | Remove all nodes and edges. |
Graph.nodes () | Return a list of the nodes. |
Graph.nodes_iter () | Return an iterator for the nodes. |
Graph.__iter__ () | Iterate over the nodes. Use “for n in G”. |
Graph.edges ([nbunch, data]) | Return a list of edges. |
Graph.edges_iter ([nbunch, data]) | Return an iterator over the edges. |
Graph.get_edge (u, v[, default]) | Return the data associated with the edge (u,v). |
Graph.neighbors (n) | Return a list of the nodes connected to the node n. |
Graph.neighbors_iter (n) | Return an iterator over all neighbors of node n. |
Graph.__getitem__ (n) | Return the neighbors of node n. Use “G[n]”. |
Graph.adjacency_list () | Return an adjacency list as a Python list of lists |
Graph.adjacency_iter () | Return an iterator of (node, adjacency dict) tuples for all nodes. |
Graph.nbunch_iter ([nbunch]) | Return an iterator of nodes contained in nbunch that are also in the graph. |
Graph.has_node (n) | Return True if graph has node n. |
Graph.__contains__ (n) | Return True if n is a node, False otherwise. Use “n in G”. |
Graph.has_edge (u, v) | Return True if graph contains the edge (u,v), False otherwise. |
Graph.has_neighbor (u, v) | Return True if node u has neighbor v. |
Graph.nodes_with_selfloops () | Return a list of nodes with self loops. |
Graph.selfloop_edges ([data]) | Return a list of selfloop edges |
Graph.order () | Return the number of nodes. |
Graph.number_of_nodes () | Return the number of nodes. |
Graph.__len__ () | Return the number of nodes. Use “len(G)”. |
Graph.size ([weighted]) | Return the number of edges. |
Graph.number_of_edges ([u, v]) | Return the number of edges between two nodes. |
Graph.number_of_selfloops () | Return the number of selfloop edges (edge from a node to itself). |
Graph.degree ([nbunch, with_labels, ...]) | Return the degree of a node or nodes. |
Graph.degree_iter ([nbunch, weighted]) | Return an iterator for (node, degree). |
Graph.copy () | Return a copy of the graph. |
Graph.to_directed () | Return a directed representation of the graph. |
Graph.subgraph (nbunch[, copy]) | Return the subgraph induced on nodes in nbunch. |