- Network Science with Python and NetworkX Quick Start Guide
- Edward L. Platt
- 707字
- 2025-04-04 14:31:13
The Graph class – undirected networks
In NetworkX, the Graph class is used to represent undirected networks and analyze their structure. The previous chapter showed how to create a network from scratch by adding nodes and edges. This section will instead use one of the ready-made networks available in NetworkX: Zachary's karate club (Zachary, 1977).
This network represents the friendships (edges) between members (nodes) of a karate club studied between 1970 and 1972. This particular karate club has long been of interest to sociologists and network scientists, because it eventually split into two different clubs after a disagreement between the instructor and the club president (this might explain why there aren't any famous studies of conflict resolution clubs). In the original study, Zachary used the network structure to predict which members would join which of the two clubs with near-perfect accuracy! Specifically, he used the minimum-cut algorithm discussed in Chapter 7, In-Between – Communities.
The karate club network can be created and visualized using the karate_club_graph() function given as follows:
G = nx.karate_club_graph()
karate_pos = nx.spring_layout(G, k=0.3)
nx.draw_networkx(G, karate_pos)
The preceding code stores the karate club network in G. The visualization layout is then pre-calculated using spring_layout() and stored in karate_pos, which will allow us to reuse the layout throughout the chapter. Different layout methods are discussed in detail in Chapter 11, Visualization, but for now, all you need to know is that spring_layout() tries to place nodes closer together if they are connected by an edge. Finally, the call to draw_networkx() creates the following visualization:

The Graph class offers many ways to interact with nodes and edges. The nodes and edges in a Graph class can be accessed using its nodes and edges attributes. These attributes are iterables and can be used to iterate over nodes and edges, or converted to a list of node IDs and edges, demonstrated as follows:
list(G.nodes)
[0, 1, 2, ...]
list(G.edges)
[(0, 1), (0, 2), (0, 3), ...]
Another simple way to interact with a network is to check whether a particular node is present. In Zachary's paper, the node with the 0 ID was identified as the club instructor, Mr. Hi (a pseudonym). It's easy to confirm that Mr. Hi's node is part of the network using the Python in operator, or the Graph class's has_node() method, given as follows:
mr_hi = 0
mr_hi in G
True
G.has_node(mr_hi)
True
These statements evaluate to True because G contains a node with an ID matching the given value. If the given value doesn't match any of the node IDs, the statement evaluates to False as follows:
wild_goose = 1337
wild_goose in G
False
G.has_node(wild_goose)
False
Now that you know that Mr. Hi is part of the network, you can examine his friendships. Each edge connected to Mr. Hi's node represents one of his friendships. The nodes on the other end of these edges represent his friends. In general, the set of nodes that are connected to a particular node by an edge are called that node's neighbors and can be found using the neighbors() method of the Graph class. The neighbors() method returns an iterator, which is handy for most uses, but if you just want to see the neighbors, you can use the list() constructor:
list(G.neighbors(mr_hi))
[1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12, 13, 17, 19, 21, 31]
So, Mr. Hi has 16 friends, represented by the preceding node IDs. You can test whether a given edge exists using either the Python in operator or the Graph class's has_edge() method. If you just want to know whether Mr. Hi is friends with a particular club member (say node ID 1), you can use the following code:
member_id = 1
(mr_hi, member_id) in G.edges
True
G.has_edge(mr_hi, member_id)
True
The president of the karate club, nicknamed John A., is represented by ID 33. The following code checks whether he and Mr. Hi are friends:
john_a = 33
(mr_hi, john_a) in G.edges
False
G.has_edge(mr_hi, john_a)
False
The previous result tells us that the club president and the instructor were not friends. Perhaps that played a role in the demise of the club...