@@ -68,7 +68,9 @@ There are four basic tasks for interacting with networks
68
68
- Understanding how to use Matplotlib to visualize and plot results
69
69
- Describe basic analysis to be done on networks
70
70
71
- * Working with Nodes and Objects
71
+ * Creating Networks From Scratch and Manipulation
72
+
73
+ ** Working with Nodes and Objects
72
74
73
75
| Method Command | Description |
74
76
|--------------------------------+-----------------------------------------------|
@@ -77,24 +79,44 @@ There are four basic tasks for interacting with networks
77
79
| ~nx.all_neighbors(G, node)~ | Return all neighbors of node in graph |
78
80
| ~nx.non_neighbors(G, node)~ | Return non-neighbors of node in graph |
79
81
| ~nx.common_neighbors(G, u, v)~ | Return common neighbors of two nodes in graph |
82
+ | ~G.add_node(node)~ | Add node to graph object, G |
83
+ | ~G.add_nodes_from(array)~ | Add array of nodes to graph object, G |
80
84
81
85
#+BEGIN_SRC python
82
86
# Create (empty) graph with no nodes and no edges
83
- G = nx.Graph()
84
- #+END_SRC
87
+ DG = nx.DiGraph()
85
88
86
- * Creating Networks From Scratch and Manipulation
89
+ # Add one node
90
+ G.add_node("Michael")
91
+
92
+ # Add multiple nodes
93
+ G.add_nodes_from(["Andy", "Jim", "Angela"])
94
+
95
+ # Look at nodes are in our object
96
+ G.nodes()
97
+ #+END_SRC
87
98
88
99
** Working with Edges and Connections
89
100
90
- | Method Command | Description |
91
- |-------------------------+------------------------------------|
92
- | ~nx.edges(G)~ | Return edge view of edges |
93
- | ~nx.number_of_edges(G)~ | Return number of edges in graph |
94
- | ~nx.non_edges(G)~ | Return non-existent edges in graph |
101
+ | Method Command | Description |
102
+ |----------------------------+------------------------------------|
103
+ | ~nx.edges(G)~ | Return edge view of edges |
104
+ | ~nx.number_of_edges(G)~ | Return number of edges in graph |
105
+ | ~nx.non_edges(G)~ | Return non-existent edges in graph |
106
+ | ~G.add_edge(node1, node2)~ | Add edge between node1 and node2 |
107
+
108
+ #+BEGIN_SRC python
109
+ # Add edges and connections between nodes
110
+ DG.add_edge("Michael", "Jim")
111
+
112
+ # Or add a group of edge conections all at once
113
+ DG.add_edges_from([("Michael", "Angela"), ("Michael", "Andy"), ("Angela", "Andy")])
114
+ #+END_SRC
95
115
96
116
** Node and Edge Attributes
97
117
118
+ Nodes and edges can not only just exist, but can also take on values or *attributes*.
119
+
98
120
| Type | Method Command | Description |
99
121
|------+-------------------------------------+----------------------------------------------------|
100
122
| Node | ~nx.set_node_attributes(G, values)~ | Set node attributes from given value or dictionary |
@@ -112,16 +134,16 @@ G = nx.Graph()
112
134
113
135
** Drawing Graphs in Different Ways
114
136
115
- There are lots of flexibility in drawing networks. You can find more
137
+ There is lots of flexibility in drawing networks. You can find more
116
138
[[https://networkx.github.io/documentation/networkx-1.10/reference/generated/networkx.drawing.nx_pylab.draw_networkx.html#networkx.drawing.nx_pylab.draw_networkx][here in the ~draw_networkx()~ reference documentation]].
117
139
118
140
*** Drawing Algorithms
119
141
120
142
While it is great to create and have a network in a data structure, it is also
121
143
important and interesting to explore these networks visually by plotting them
122
- in various way .
144
+ in various ways .
123
145
124
- | Method Command | Description |
146
+ | Method Command | Drawing Description |
125
147
|-----------------------+------------------------------------------------------------------------|
126
148
| ~nx.draw(G)~ | Draw graph with Matplotlib |
127
149
| ~nx.draw_random(G)~ | Draw graph with random layout |
@@ -140,12 +162,12 @@ method.
140
162
141
163
#+BEGIN_SRC python
142
164
# Use the *_layout() functions within the draw() method
143
- nx.draw(KG, pos=spring_layout(KG))
165
+ nx.draw(KG, pos=nx. spring_layout(KG))
144
166
#+END_SRC
145
167
146
168
*** Drawing Nodes
147
169
148
- There are a few paramters for the ~nx.draw()~ method that can manipulate node
170
+ There are a few parameters for the ~nx.draw()~ method that can manipulate node
149
171
properties.
150
172
151
173
| Paramter | Description |
@@ -166,7 +188,7 @@ properties.
166
188
167
189
Here are some parameters specific for modifying how edges are displayed.
168
190
169
- | Paramter | Description |
191
+ | Parameter | Description |
170
192
|--------------+-----------------------------------------------------------|
171
193
| ~arrows~ | For directed graphs, if ~True~ draw arrowheads |
172
194
| ~arrowstyle~ | For directed graphs, choose style of arrow |
@@ -219,8 +241,8 @@ Quick and basic metrics of a graph are the number of edges and nodes.
219
241
220
242
*** Degree of Nodes
221
243
222
- Remember, that there are two basic kinds of graphs: directed and undirected. In
223
- the directed graph, there are two different kinds of edges: in and out.
244
+ Remember, there are two basic kinds of graphs: directed and undirected. In the
245
+ directed graph, there are two different kinds of edges: in and out.
224
246
225
247
#+BEGIN_SRC python
226
248
# Find in-degree edges
@@ -260,11 +282,11 @@ Here are just a few.
260
282
|------------------------+------------------------------------------------------+--------------------------------|
261
283
| Degree centrality | Node is important based on degrees | ~nx.degree_centrality(G)~ |
262
284
| Eigenvector centrality | Node is important if linked by other important nodes | ~nx.eigenvector_centrality(G)~ |
263
- | Betweenness centrality | Node is important if within shortest paths in nodes | ~nx.betweeneess_centrality (G)~ |
285
+ | Betweenness centrality | Node is important if within shortest paths in nodes | ~nx.betweenness_centrality (G)~ |
264
286
265
287
#+BEGIN_SRC python
266
288
# Calculate eigenvector centrality
267
- nx.eigenvector_centrality(DG )
289
+ nx.eigenvector_centrality(KG )
268
290
#+END_SRC
269
291
270
292
** Basic Graph Algorithms
@@ -279,9 +301,9 @@ Given a network, **graph traversal** is the problem of searching through a
279
301
network for particular nodes or certain paths. A path is a sequence of edges
280
302
between two nodes.
281
303
282
- *Situation *: social networks like to recommend friends to you and one idea is to
283
- find friends close enough to your current social network, who else would you
284
- know?
304
+ *Problem *: Social networks like to recommend friends to you and one idea is to
305
+ find friends close enough to your current social network. So who else would you
306
+ potentially know?
285
307
286
308
#+BEGIN_SRC python
287
309
# Check a path can exist
@@ -302,7 +324,7 @@ Now we have the shortest path, we can then plot this.
302
324
303
325
*** Community Detection
304
326
305
- In a social network, you may be interested in tightly connected group of
327
+ In a social network, you may be interested in tightly connected groups of
306
328
friends. We can use a method called *community detection* or *module detection*
307
329
to answer this question.
308
330
@@ -361,7 +383,8 @@ Return the results as a dictionary.
361
383
362
384
*Bonus Challenge*: Create a function and script to use the random network from the
363
385
previous exercise, creates 10 networks of size 5, 10, 15 and use your just
364
- created function to return a dictionary with the average for each
386
+ created function to return a dictionary with the average number of neighbors
387
+ for each.
365
388
366
389
* Resources
367
390
0 commit comments