terraproof 0.0.0-stage → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +356 -2
- package/bin/terraproof.js +4 -0
- package/dist/router/apply.js +147 -0
- package/dist/router/classify.js +111 -0
- package/dist/router/explain.js +9 -0
- package/dist/router/index.js +272 -0
- package/dist/router/resolve.js +239 -0
- package/dist/router/score.js +60 -0
- package/dist/router/types.js +1 -0
- package/dist/router/validate.js +81 -0
- package/instructions/engineering.md +14 -0
- package/instructions/security.md +14 -0
- package/instructions/simplicity.md +13 -0
- package/package.json +65 -4
- package/routing/DOMAIN_ROUTING.md +161 -0
- package/routing/ROUTER_DESIGN.md +173 -0
- package/routing/SKILL_MANIFEST.md +86 -0
- package/routing/checks.json +121 -0
- package/routing/collections.json +47 -0
- package/routing/registry.json +2211 -0
- package/routing/rules.json +64 -0
- package/skills/accuracy-assessment/SKILL.md +213 -0
- package/skills/accuracy-assessment/UPSTREAM.md +7 -0
- package/skills/accuracy-assessment/references/stratified-estimator.md +127 -0
- package/skills/accuracy-assessment/skill.json +103 -0
- package/skills/brainstorming/SKILL.md +250 -0
- package/skills/brainstorming/UPSTREAM.md +29 -0
- package/skills/brainstorming/scripts/frame-template.html +213 -0
- package/skills/brainstorming/scripts/helper.js +167 -0
- package/skills/brainstorming/scripts/server.cjs +723 -0
- package/skills/brainstorming/scripts/start-server.sh +209 -0
- package/skills/brainstorming/scripts/stop-server.sh +120 -0
- package/skills/brainstorming/skill.json +73 -0
- package/skills/brainstorming/spec-document-reviewer-prompt.md +49 -0
- package/skills/brainstorming/visual-companion.md +299 -0
- package/skills/crs-and-units/SKILL.md +200 -0
- package/skills/crs-and-units/UPSTREAM.md +7 -0
- package/skills/crs-and-units/skill.json +74 -0
- package/skills/design-postgis-tables/SKILL.md +494 -0
- package/skills/design-postgis-tables/UPSTREAM.md +21 -0
- package/skills/design-postgis-tables/skill.json +90 -0
- package/skills/gdal/SKILL.md +125 -0
- package/skills/gdal/UPSTREAM.md +22 -0
- package/skills/gdal/references/gdal-recipes.md +103 -0
- package/skills/gdal/skill.json +66 -0
- package/skills/geomaster/README.md +105 -0
- package/skills/geomaster/SKILL.md +366 -0
- package/skills/geomaster/UPSTREAM.md +21 -0
- package/skills/geomaster/references/advanced-gis.md +376 -0
- package/skills/geomaster/references/big-data.md +363 -0
- package/skills/geomaster/references/code-examples.md +531 -0
- package/skills/geomaster/references/coordinate-systems.md +364 -0
- package/skills/geomaster/references/core-libraries.md +273 -0
- package/skills/geomaster/references/data-sources.md +330 -0
- package/skills/geomaster/references/gis-software.md +369 -0
- package/skills/geomaster/references/industry-applications.md +420 -0
- package/skills/geomaster/references/machine-learning.md +462 -0
- package/skills/geomaster/references/programming-languages.md +456 -0
- package/skills/geomaster/references/remote-sensing.md +370 -0
- package/skills/geomaster/references/scientific-domains.md +416 -0
- package/skills/geomaster/references/specialized-topics.md +428 -0
- package/skills/geomaster/references/troubleshooting.md +439 -0
- package/skills/geomaster/skill.json +157 -0
- package/skills/geopandas/SKILL.md +250 -0
- package/skills/geopandas/UPSTREAM.md +22 -0
- package/skills/geopandas/references/crs-management.md +231 -0
- package/skills/geopandas/references/data-io.md +323 -0
- package/skills/geopandas/references/data-structures.md +207 -0
- package/skills/geopandas/references/geometric-operations.md +262 -0
- package/skills/geopandas/references/spatial-analysis.md +294 -0
- package/skills/geopandas/references/visualization.md +230 -0
- package/skills/geopandas/scripts/_common.py +605 -0
- package/skills/geopandas/scripts/crs_reprojection_plan.py +210 -0
- package/skills/geopandas/scripts/export_plan.py +305 -0
- package/skills/geopandas/scripts/geometry_validity_report.py +227 -0
- package/skills/geopandas/scripts/sensitive_coordinates_checklist.py +230 -0
- package/skills/geopandas/scripts/spatial_join_audit.py +368 -0
- package/skills/geopandas/scripts/vector_inventory.py +140 -0
- package/skills/geopandas/skill.json +116 -0
- package/skills/geoparquet-validation/SKILL.md +125 -0
- package/skills/geoparquet-validation/UPSTREAM.md +24 -0
- package/skills/geoparquet-validation/references/distribution-best-practices.md +118 -0
- package/skills/geoparquet-validation/references/gpio-commands.md +264 -0
- package/skills/geoparquet-validation/references/tool-comparison.md +155 -0
- package/skills/geoparquet-validation/skill.json +71 -0
- package/skills/geospatial-data-discovery/SKILL.md +49 -0
- package/skills/geospatial-data-discovery/UPSTREAM.md +15 -0
- package/skills/geospatial-data-discovery/skill.json +51 -0
- package/skills/geospatial-frontend/SKILL.md +86 -0
- package/skills/geospatial-frontend/UPSTREAM.md +23 -0
- package/skills/geospatial-frontend/references/design-system.md +283 -0
- package/skills/geospatial-frontend/references/user-preferences.md +96 -0
- package/skills/geospatial-frontend/skill.json +87 -0
- package/skills/geozarr/SKILL.md +244 -0
- package/skills/geozarr/UPSTREAM.md +21 -0
- package/skills/geozarr/skill.json +97 -0
- package/skills/networkx/SKILL.md +440 -0
- package/skills/networkx/UPSTREAM.md +22 -0
- package/skills/networkx/references/algorithms.md +384 -0
- package/skills/networkx/references/generators.md +385 -0
- package/skills/networkx/references/graph-basics.md +284 -0
- package/skills/networkx/references/io.md +457 -0
- package/skills/networkx/references/visualization.md +531 -0
- package/skills/networkx/skill.json +93 -0
- package/skills/raster-processing/SKILL.md +222 -0
- package/skills/raster-processing/UPSTREAM.md +7 -0
- package/skills/raster-processing/skill.json +81 -0
- package/skills/sar-basics/SKILL.md +202 -0
- package/skills/sar-basics/UPSTREAM.md +7 -0
- package/skills/sar-basics/references/calibration-lut.md +98 -0
- package/skills/sar-basics/skill.json +97 -0
- package/skills/satellite-preprocessing/SKILL.md +241 -0
- package/skills/satellite-preprocessing/UPSTREAM.md +7 -0
- package/skills/satellite-preprocessing/skill.json +84 -0
- package/skills/scientific-visualization/SKILL.md +285 -0
- package/skills/scientific-visualization/UPSTREAM.md +25 -0
- package/skills/scientific-visualization/assets/color_palettes.py +263 -0
- package/skills/scientific-visualization/assets/nature.mplstyle +68 -0
- package/skills/scientific-visualization/assets/presentation.mplstyle +68 -0
- package/skills/scientific-visualization/assets/publication.mplstyle +77 -0
- package/skills/scientific-visualization/assets/publisher_profiles.json +269 -0
- package/skills/scientific-visualization/references/color_palettes.md +227 -0
- package/skills/scientific-visualization/references/journal_requirements.md +169 -0
- package/skills/scientific-visualization/references/matplotlib_examples.md +336 -0
- package/skills/scientific-visualization/references/publication_guidelines.md +196 -0
- package/skills/scientific-visualization/references/sources.md +76 -0
- package/skills/scientific-visualization/scripts/_common.py +136 -0
- package/skills/scientific-visualization/scripts/export_plan.py +493 -0
- package/skills/scientific-visualization/scripts/figure_export.py +642 -0
- package/skills/scientific-visualization/scripts/image_metadata.py +731 -0
- package/skills/scientific-visualization/scripts/palette_audit.py +327 -0
- package/skills/scientific-visualization/scripts/style_presets.py +501 -0
- package/skills/scientific-visualization/scripts/style_preview.py +232 -0
- package/skills/scientific-visualization/skill.json +95 -0
- package/skills/spectral-indices/SKILL.md +235 -0
- package/skills/spectral-indices/UPSTREAM.md +7 -0
- package/skills/spectral-indices/skill.json +88 -0
- package/skills/stac-and-cloud-access/SKILL.md +209 -0
- package/skills/stac-and-cloud-access/UPSTREAM.md +7 -0
- package/skills/stac-and-cloud-access/references/run-manifest.md +94 -0
- package/skills/stac-and-cloud-access/skill.json +98 -0
- package/skills/statistical-analysis/SKILL.md +446 -0
- package/skills/statistical-analysis/UPSTREAM.md +24 -0
- package/skills/statistical-analysis/references/assumptions_and_diagnostics.md +379 -0
- package/skills/statistical-analysis/references/bayesian_statistics.md +686 -0
- package/skills/statistical-analysis/references/effect_sizes_and_power.md +649 -0
- package/skills/statistical-analysis/references/reporting_standards.md +482 -0
- package/skills/statistical-analysis/references/test_selection_guide.md +129 -0
- package/skills/statistical-analysis/scripts/assumption_checks.py +652 -0
- package/skills/statistical-analysis/skill.json +127 -0
- package/skills/systematic-debugging/SKILL.md +283 -0
- package/skills/systematic-debugging/UPSTREAM.md +28 -0
- package/skills/systematic-debugging/condition-based-waiting-example.ts +158 -0
- package/skills/systematic-debugging/condition-based-waiting.md +115 -0
- package/skills/systematic-debugging/defense-in-depth.md +122 -0
- package/skills/systematic-debugging/find-polluter.sh +72 -0
- package/skills/systematic-debugging/root-cause-tracing.md +169 -0
- package/skills/systematic-debugging/skill.json +88 -0
- package/skills/time-series-and-change/SKILL.md +217 -0
- package/skills/time-series-and-change/UPSTREAM.md +7 -0
- package/skills/time-series-and-change/references/change-methods.md +42 -0
- package/skills/time-series-and-change/skill.json +116 -0
- package/skills/vector-quality/SKILL.md +204 -0
- package/skills/vector-quality/UPSTREAM.md +7 -0
- package/skills/vector-quality/skill.json +82 -0
- package/skills/verification/SKILL.md +120 -0
- package/skills/verification/UPSTREAM.md +31 -0
- package/skills/verification/skill.json +67 -0
- package/skills/writing-plans/SKILL.md +171 -0
- package/skills/writing-plans/UPSTREAM.md +23 -0
- package/skills/writing-plans/plan-document-reviewer-prompt.md +49 -0
- package/skills/writing-plans/skill.json +80 -0
- package/sources/SOURCES.md +374 -0
- package/third_party/isaaccorley-geospatial-skills/LICENSE +176 -0
- package/third_party/isaaccorley-geospatial-skills/NOTICE.md +17 -0
- package/third_party/kdense-scientific-agent-skills/LICENSE +21 -0
- package/third_party/kdense-scientific-agent-skills/NOTICE.md +19 -0
- package/third_party/superpowers/LICENSE +21 -0
- package/third_party/superpowers/NOTICE.md +17 -0
- package/third_party/timescale-pg-aiguide/LICENSE +176 -0
- package/third_party/timescale-pg-aiguide/NOTICE.md +14 -0
|
@@ -0,0 +1,385 @@
|
|
|
1
|
+
# NetworkX Graph Generators
|
|
2
|
+
|
|
3
|
+
## Classic Graphs
|
|
4
|
+
|
|
5
|
+
### Complete Graphs
|
|
6
|
+
```python
|
|
7
|
+
# Complete graph (all nodes connected to all others)
|
|
8
|
+
G = nx.complete_graph(n=10)
|
|
9
|
+
|
|
10
|
+
# Complete bipartite graph
|
|
11
|
+
G = nx.complete_bipartite_graph(n1=5, n2=7)
|
|
12
|
+
|
|
13
|
+
# Complete multipartite graph
|
|
14
|
+
G = nx.complete_multipartite_graph(3, 4, 5) # Three partitions
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
### Cycle and Path Graphs
|
|
18
|
+
```python
|
|
19
|
+
# Cycle graph (nodes arranged in circle)
|
|
20
|
+
G = nx.cycle_graph(n=20)
|
|
21
|
+
|
|
22
|
+
# Path graph (linear chain)
|
|
23
|
+
G = nx.path_graph(n=15)
|
|
24
|
+
|
|
25
|
+
# Circular ladder graph
|
|
26
|
+
G = nx.circular_ladder_graph(n=10)
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
### Regular Graphs
|
|
30
|
+
```python
|
|
31
|
+
# Empty graph (no edges)
|
|
32
|
+
G = nx.empty_graph(n=10)
|
|
33
|
+
|
|
34
|
+
# Null graph (no nodes)
|
|
35
|
+
G = nx.null_graph()
|
|
36
|
+
|
|
37
|
+
# Star graph (one central node connected to all others)
|
|
38
|
+
G = nx.star_graph(n=19) # Creates 20-node star
|
|
39
|
+
|
|
40
|
+
# Wheel graph (cycle with central hub)
|
|
41
|
+
G = nx.wheel_graph(n=10)
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### Special Named Graphs
|
|
45
|
+
```python
|
|
46
|
+
# Bull graph
|
|
47
|
+
G = nx.bull_graph()
|
|
48
|
+
|
|
49
|
+
# Chvatal graph
|
|
50
|
+
G = nx.chvatal_graph()
|
|
51
|
+
|
|
52
|
+
# Cubical graph
|
|
53
|
+
G = nx.cubical_graph()
|
|
54
|
+
|
|
55
|
+
# Diamond graph
|
|
56
|
+
G = nx.diamond_graph()
|
|
57
|
+
|
|
58
|
+
# Dodecahedral graph
|
|
59
|
+
G = nx.dodecahedral_graph()
|
|
60
|
+
|
|
61
|
+
# Heawood graph
|
|
62
|
+
G = nx.heawood_graph()
|
|
63
|
+
|
|
64
|
+
# House graph
|
|
65
|
+
G = nx.house_graph()
|
|
66
|
+
|
|
67
|
+
# Petersen graph
|
|
68
|
+
G = nx.petersen_graph()
|
|
69
|
+
|
|
70
|
+
# Karate club graph (classic social network)
|
|
71
|
+
G = nx.karate_club_graph()
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## Random Graphs
|
|
75
|
+
|
|
76
|
+
### Erdős-Rényi Graphs
|
|
77
|
+
```python
|
|
78
|
+
# G(n, p) model: n nodes, edge probability p
|
|
79
|
+
G = nx.erdos_renyi_graph(n=100, p=0.1, seed=42)
|
|
80
|
+
|
|
81
|
+
# G(n, m) model: n nodes, exactly m edges
|
|
82
|
+
G = nx.gnm_random_graph(n=100, m=500, seed=42)
|
|
83
|
+
|
|
84
|
+
# Fast version (for large sparse graphs)
|
|
85
|
+
G = nx.fast_gnp_random_graph(n=10000, p=0.0001, seed=42)
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### Watts-Strogatz Small-World
|
|
89
|
+
```python
|
|
90
|
+
# Small-world network with rewiring
|
|
91
|
+
# n nodes, k nearest neighbors, rewiring probability p
|
|
92
|
+
G = nx.watts_strogatz_graph(n=100, k=6, p=0.1, seed=42)
|
|
93
|
+
|
|
94
|
+
# Connected version (guarantees connectivity)
|
|
95
|
+
G = nx.connected_watts_strogatz_graph(n=100, k=6, p=0.1, tries=100, seed=42)
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### Barabási-Albert Preferential Attachment
|
|
99
|
+
```python
|
|
100
|
+
# Scale-free network (power-law degree distribution)
|
|
101
|
+
# n nodes, m edges to attach from new node
|
|
102
|
+
G = nx.barabasi_albert_graph(n=100, m=3, seed=42)
|
|
103
|
+
|
|
104
|
+
# Extended version with parameters
|
|
105
|
+
G = nx.extended_barabasi_albert_graph(n=100, m=3, p=0.5, q=0.2, seed=42)
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### Power Law Degree Sequence
|
|
109
|
+
```python
|
|
110
|
+
# Power law cluster graph
|
|
111
|
+
G = nx.powerlaw_cluster_graph(n=100, m=3, p=0.1, seed=42)
|
|
112
|
+
|
|
113
|
+
# Random power law tree
|
|
114
|
+
G = nx.random_powerlaw_tree(n=100, gamma=3, seed=42, tries=1000)
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
### Configuration Model
|
|
118
|
+
```python
|
|
119
|
+
# Graph with specified degree sequence
|
|
120
|
+
degree_sequence = [3, 3, 3, 3, 2, 2, 2, 1, 1, 1]
|
|
121
|
+
G = nx.configuration_model(degree_sequence, seed=42)
|
|
122
|
+
|
|
123
|
+
# Remove self-loops and parallel edges
|
|
124
|
+
G = nx.Graph(G)
|
|
125
|
+
G.remove_edges_from(nx.selfloop_edges(G))
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
### Random Geometric Graphs
|
|
129
|
+
```python
|
|
130
|
+
# Nodes in unit square, edges if distance < radius
|
|
131
|
+
G = nx.random_geometric_graph(n=100, radius=0.2, seed=42)
|
|
132
|
+
|
|
133
|
+
# With positions
|
|
134
|
+
pos = nx.get_node_attributes(G, 'pos')
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
### Random Regular Graphs
|
|
138
|
+
```python
|
|
139
|
+
# Every node has exactly d neighbors
|
|
140
|
+
G = nx.random_regular_graph(d=3, n=100, seed=42)
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
### Stochastic Block Model
|
|
144
|
+
```python
|
|
145
|
+
# Community structure model
|
|
146
|
+
sizes = [50, 50, 50] # Three communities
|
|
147
|
+
probs = [[0.25, 0.05, 0.02], # Within and between community probabilities
|
|
148
|
+
[0.05, 0.35, 0.07],
|
|
149
|
+
[0.02, 0.07, 0.40]]
|
|
150
|
+
G = nx.stochastic_block_model(sizes, probs, seed=42)
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
## Lattice and Grid Graphs
|
|
154
|
+
|
|
155
|
+
### Grid Graphs
|
|
156
|
+
```python
|
|
157
|
+
# 2D grid
|
|
158
|
+
G = nx.grid_2d_graph(m=5, n=7) # 5x7 grid
|
|
159
|
+
|
|
160
|
+
# 3D grid
|
|
161
|
+
G = nx.grid_graph(dim=[5, 7, 3]) # 5x7x3 grid
|
|
162
|
+
|
|
163
|
+
# Hexagonal lattice
|
|
164
|
+
G = nx.hexagonal_lattice_graph(m=5, n=7)
|
|
165
|
+
|
|
166
|
+
# Triangular lattice
|
|
167
|
+
G = nx.triangular_lattice_graph(m=5, n=7)
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
### Hypercube
|
|
171
|
+
```python
|
|
172
|
+
# n-dimensional hypercube
|
|
173
|
+
G = nx.hypercube_graph(n=4)
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
## Tree Graphs
|
|
177
|
+
|
|
178
|
+
### Random Trees
|
|
179
|
+
```python
|
|
180
|
+
# Random labeled tree with n nodes, sampled uniformly over labeled trees
|
|
181
|
+
# (nx.random_tree was removed in NetworkX 3.4)
|
|
182
|
+
G = nx.random_labeled_tree(100, seed=42)
|
|
183
|
+
|
|
184
|
+
# Sample uniformly over isomorphism classes instead
|
|
185
|
+
G = nx.random_unlabeled_tree(100, seed=42)
|
|
186
|
+
|
|
187
|
+
# Rooted variants
|
|
188
|
+
G = nx.random_labeled_rooted_tree(100, seed=42)
|
|
189
|
+
|
|
190
|
+
# Prefix tree (tries)
|
|
191
|
+
G = nx.prefix_tree([[0, 1, 2], [0, 1, 3], [0, 4]])
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
### Balanced Trees
|
|
195
|
+
```python
|
|
196
|
+
# Balanced r-ary tree of height h
|
|
197
|
+
G = nx.balanced_tree(r=2, h=5) # Binary tree, height 5
|
|
198
|
+
|
|
199
|
+
# Full r-ary tree with n nodes
|
|
200
|
+
G = nx.full_rary_tree(r=3, n=100) # Ternary tree
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
### Barbell and Lollipop Graphs
|
|
204
|
+
```python
|
|
205
|
+
# Two complete graphs connected by path
|
|
206
|
+
G = nx.barbell_graph(m1=5, m2=3) # Two K_5 graphs with 3-node path
|
|
207
|
+
|
|
208
|
+
# Complete graph connected to path
|
|
209
|
+
G = nx.lollipop_graph(m=7, n=5) # K_7 with 5-node path
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
## Social Network Models
|
|
213
|
+
|
|
214
|
+
### Karate Club
|
|
215
|
+
```python
|
|
216
|
+
# Zachary's karate club (classic social network)
|
|
217
|
+
G = nx.karate_club_graph()
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
### Davis Southern Women
|
|
221
|
+
```python
|
|
222
|
+
# Bipartite social network
|
|
223
|
+
G = nx.davis_southern_women_graph()
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
### Florentine Families
|
|
227
|
+
```python
|
|
228
|
+
# Historical marriage and business networks
|
|
229
|
+
G = nx.florentine_families_graph()
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
### Les Misérables
|
|
233
|
+
```python
|
|
234
|
+
# Character co-occurrence network
|
|
235
|
+
G = nx.les_miserables_graph()
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
## Directed Graph Generators
|
|
239
|
+
|
|
240
|
+
### Random Directed Graphs
|
|
241
|
+
```python
|
|
242
|
+
# Directed Erdős-Rényi
|
|
243
|
+
G = nx.gnp_random_graph(n=100, p=0.1, directed=True, seed=42)
|
|
244
|
+
|
|
245
|
+
# Scale-free directed
|
|
246
|
+
G = nx.scale_free_graph(n=100, seed=42)
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
### DAG (Directed Acyclic Graph)
|
|
250
|
+
```python
|
|
251
|
+
# Random DAG
|
|
252
|
+
G = nx.gnp_random_graph(n=20, p=0.2, directed=True, seed=42)
|
|
253
|
+
G = nx.DiGraph([(u, v) for (u, v) in G.edges() if u < v]) # Remove backward edges
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
### Tournament Graphs
|
|
257
|
+
```python
|
|
258
|
+
# Random tournament (complete directed graph); lives in the tournament module
|
|
259
|
+
G = nx.tournament.random_tournament(n=10, seed=42)
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
## Duplication-Divergence Models
|
|
263
|
+
|
|
264
|
+
### Duplication Divergence Graph
|
|
265
|
+
```python
|
|
266
|
+
# Biological network model (protein interaction networks)
|
|
267
|
+
G = nx.duplication_divergence_graph(n=100, p=0.5, seed=42)
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
## Degree Sequence Generators
|
|
271
|
+
|
|
272
|
+
### Valid Degree Sequences
|
|
273
|
+
```python
|
|
274
|
+
# Check if degree sequence is valid (graphical)
|
|
275
|
+
sequence = [3, 3, 3, 3, 2, 2, 2, 1, 1, 1]
|
|
276
|
+
is_valid = nx.is_graphical(sequence)
|
|
277
|
+
|
|
278
|
+
# For directed graphs
|
|
279
|
+
in_sequence = [2, 2, 2, 1, 1]
|
|
280
|
+
out_sequence = [2, 2, 1, 2, 1]
|
|
281
|
+
is_valid = nx.is_digraphical(in_sequence, out_sequence)
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
### Creating from Degree Sequence
|
|
285
|
+
```python
|
|
286
|
+
# Havel-Hakimi algorithm
|
|
287
|
+
G = nx.havel_hakimi_graph(degree_sequence)
|
|
288
|
+
|
|
289
|
+
# Configuration model (allows multi-edges/self-loops)
|
|
290
|
+
G = nx.configuration_model(degree_sequence)
|
|
291
|
+
|
|
292
|
+
# Directed configuration model
|
|
293
|
+
G = nx.directed_configuration_model(in_degree_sequence, out_degree_sequence)
|
|
294
|
+
```
|
|
295
|
+
|
|
296
|
+
## Bipartite Graphs
|
|
297
|
+
|
|
298
|
+
### Random Bipartite
|
|
299
|
+
```python
|
|
300
|
+
# Random bipartite with two node sets
|
|
301
|
+
G = nx.bipartite.random_graph(n=50, m=30, p=0.1, seed=42)
|
|
302
|
+
|
|
303
|
+
# Configuration model for bipartite
|
|
304
|
+
G = nx.bipartite.configuration_model(deg1=[3, 3, 2], deg2=[2, 2, 2, 2], seed=42)
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
### Bipartite Generators
|
|
308
|
+
```python
|
|
309
|
+
# Complete bipartite
|
|
310
|
+
G = nx.complete_bipartite_graph(n1=5, n2=7)
|
|
311
|
+
|
|
312
|
+
# Gnmk random bipartite (n, m nodes, k edges)
|
|
313
|
+
G = nx.bipartite.gnmk_random_graph(n=10, m=8, k=20, seed=42)
|
|
314
|
+
```
|
|
315
|
+
|
|
316
|
+
## Operators on Graphs
|
|
317
|
+
|
|
318
|
+
### Graph Operations
|
|
319
|
+
```python
|
|
320
|
+
# Union
|
|
321
|
+
G = nx.union(G1, G2)
|
|
322
|
+
|
|
323
|
+
# Disjoint union
|
|
324
|
+
G = nx.disjoint_union(G1, G2)
|
|
325
|
+
|
|
326
|
+
# Compose (overlay)
|
|
327
|
+
G = nx.compose(G1, G2)
|
|
328
|
+
|
|
329
|
+
# Complement
|
|
330
|
+
G = nx.complement(G1)
|
|
331
|
+
|
|
332
|
+
# Cartesian product
|
|
333
|
+
G = nx.cartesian_product(G1, G2)
|
|
334
|
+
|
|
335
|
+
# Tensor (Kronecker) product
|
|
336
|
+
G = nx.tensor_product(G1, G2)
|
|
337
|
+
|
|
338
|
+
# Strong product
|
|
339
|
+
G = nx.strong_product(G1, G2)
|
|
340
|
+
```
|
|
341
|
+
|
|
342
|
+
## Customization and Seeding
|
|
343
|
+
|
|
344
|
+
### Setting Random Seed
|
|
345
|
+
Always set seed for reproducible graphs:
|
|
346
|
+
```python
|
|
347
|
+
G = nx.erdos_renyi_graph(n=100, p=0.1, seed=42)
|
|
348
|
+
```
|
|
349
|
+
|
|
350
|
+
### Converting Graph Types
|
|
351
|
+
```python
|
|
352
|
+
# Convert to specific type
|
|
353
|
+
G_directed = G.to_directed()
|
|
354
|
+
G_undirected = G.to_undirected()
|
|
355
|
+
G_multi = nx.MultiGraph(G)
|
|
356
|
+
```
|
|
357
|
+
|
|
358
|
+
## Performance Considerations
|
|
359
|
+
|
|
360
|
+
### Fast Generators
|
|
361
|
+
For large graphs, use optimized generators:
|
|
362
|
+
```python
|
|
363
|
+
# Fast ER graph (sparse)
|
|
364
|
+
G = nx.fast_gnp_random_graph(n=10000, p=0.0001, seed=42)
|
|
365
|
+
```
|
|
366
|
+
|
|
367
|
+
### Memory Efficiency
|
|
368
|
+
Some generators create graphs incrementally to save memory. For very large graphs, consider:
|
|
369
|
+
- Using sparse representations
|
|
370
|
+
- Generating subgraphs as needed
|
|
371
|
+
- Working with adjacency lists or edge lists instead of full graphs
|
|
372
|
+
|
|
373
|
+
## Validation and Properties
|
|
374
|
+
|
|
375
|
+
### Checking Generated Graphs
|
|
376
|
+
```python
|
|
377
|
+
# Verify properties
|
|
378
|
+
print(f"Nodes: {G.number_of_nodes()}")
|
|
379
|
+
print(f"Edges: {G.number_of_edges()}")
|
|
380
|
+
print(f"Density: {nx.density(G)}")
|
|
381
|
+
print(f"Connected: {nx.is_connected(G)}")
|
|
382
|
+
|
|
383
|
+
# Degree distribution
|
|
384
|
+
degree_sequence = sorted([d for n, d in G.degree()], reverse=True)
|
|
385
|
+
```
|
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
# NetworkX Graph Basics
|
|
2
|
+
|
|
3
|
+
## Graph Types
|
|
4
|
+
|
|
5
|
+
NetworkX supports four main graph classes:
|
|
6
|
+
|
|
7
|
+
### Graph (Undirected)
|
|
8
|
+
```python
|
|
9
|
+
import networkx as nx
|
|
10
|
+
G = nx.Graph()
|
|
11
|
+
```
|
|
12
|
+
- Undirected graphs with single edges between nodes
|
|
13
|
+
- No parallel edges allowed
|
|
14
|
+
- Edges are bidirectional
|
|
15
|
+
|
|
16
|
+
### DiGraph (Directed)
|
|
17
|
+
```python
|
|
18
|
+
G = nx.DiGraph()
|
|
19
|
+
```
|
|
20
|
+
- Directed graphs with one-way connections
|
|
21
|
+
- Edge direction matters: (u, v) ≠ (v, u)
|
|
22
|
+
- Used for modeling directed relationships
|
|
23
|
+
|
|
24
|
+
### MultiGraph (Undirected Multi-edge)
|
|
25
|
+
```python
|
|
26
|
+
G = nx.MultiGraph()
|
|
27
|
+
```
|
|
28
|
+
- Allows multiple edges between same node pairs
|
|
29
|
+
- Useful for modeling multiple relationships
|
|
30
|
+
|
|
31
|
+
### MultiDiGraph (Directed Multi-edge)
|
|
32
|
+
```python
|
|
33
|
+
G = nx.MultiDiGraph()
|
|
34
|
+
```
|
|
35
|
+
- Directed graph with multiple edges between nodes
|
|
36
|
+
- Combines features of DiGraph and MultiGraph
|
|
37
|
+
|
|
38
|
+
## Creating and Adding Nodes
|
|
39
|
+
|
|
40
|
+
### Single Node Addition
|
|
41
|
+
```python
|
|
42
|
+
G.add_node(1)
|
|
43
|
+
G.add_node("protein_A")
|
|
44
|
+
G.add_node((x, y)) # Nodes can be any hashable type
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### Bulk Node Addition
|
|
48
|
+
```python
|
|
49
|
+
G.add_nodes_from([2, 3, 4])
|
|
50
|
+
G.add_nodes_from(range(100, 110))
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### Nodes with Attributes
|
|
54
|
+
```python
|
|
55
|
+
G.add_node(1, time='5pm', color='red')
|
|
56
|
+
G.add_nodes_from([
|
|
57
|
+
(4, {"color": "red"}),
|
|
58
|
+
(5, {"color": "blue", "weight": 1.5})
|
|
59
|
+
])
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### Important Node Properties
|
|
63
|
+
- Nodes can be any hashable Python object: strings, tuples, numbers, custom objects
|
|
64
|
+
- Node attributes stored as key-value pairs
|
|
65
|
+
- Use meaningful node identifiers for clarity
|
|
66
|
+
|
|
67
|
+
## Creating and Adding Edges
|
|
68
|
+
|
|
69
|
+
### Single Edge Addition
|
|
70
|
+
```python
|
|
71
|
+
G.add_edge(1, 2)
|
|
72
|
+
G.add_edge('gene_A', 'gene_B')
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### Bulk Edge Addition
|
|
76
|
+
```python
|
|
77
|
+
G.add_edges_from([(1, 2), (1, 3), (2, 4)])
|
|
78
|
+
G.add_edges_from(edge_list)
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### Edges with Attributes
|
|
82
|
+
```python
|
|
83
|
+
G.add_edge(1, 2, weight=4.7, relation='interacts')
|
|
84
|
+
G.add_edges_from([
|
|
85
|
+
(1, 2, {'weight': 4.7}),
|
|
86
|
+
(2, 3, {'weight': 8.2, 'color': 'blue'})
|
|
87
|
+
])
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### Adding from Edge List with Attributes
|
|
91
|
+
```python
|
|
92
|
+
# From pandas DataFrame
|
|
93
|
+
import pandas as pd
|
|
94
|
+
df = pd.DataFrame({'source': [1, 2], 'target': [2, 3], 'weight': [4.7, 8.2]})
|
|
95
|
+
G = nx.from_pandas_edgelist(df, 'source', 'target', edge_attr='weight')
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
## Examining Graph Structure
|
|
99
|
+
|
|
100
|
+
### Basic Properties
|
|
101
|
+
```python
|
|
102
|
+
# Get collections
|
|
103
|
+
G.nodes # NodeView of all nodes
|
|
104
|
+
G.edges # EdgeView of all edges
|
|
105
|
+
G.adj # AdjacencyView for neighbor relationships
|
|
106
|
+
|
|
107
|
+
# Count elements
|
|
108
|
+
G.number_of_nodes() # Total node count
|
|
109
|
+
G.number_of_edges() # Total edge count
|
|
110
|
+
len(G) # Number of nodes (shorthand)
|
|
111
|
+
|
|
112
|
+
# Degree information
|
|
113
|
+
G.degree() # DegreeView of all node degrees
|
|
114
|
+
G.degree(1) # Degree of specific node
|
|
115
|
+
list(G.degree()) # List of (node, degree) pairs
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
### Checking Existence
|
|
119
|
+
```python
|
|
120
|
+
# Check if node exists
|
|
121
|
+
1 in G # Returns True/False
|
|
122
|
+
G.has_node(1)
|
|
123
|
+
|
|
124
|
+
# Check if edge exists
|
|
125
|
+
G.has_edge(1, 2)
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
### Accessing Neighbors
|
|
129
|
+
```python
|
|
130
|
+
# Get neighbors of node 1
|
|
131
|
+
list(G.neighbors(1))
|
|
132
|
+
list(G[1]) # Dictionary-like access
|
|
133
|
+
|
|
134
|
+
# For directed graphs
|
|
135
|
+
list(G.predecessors(1)) # Incoming edges
|
|
136
|
+
list(G.successors(1)) # Outgoing edges
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
### Iterating Over Elements
|
|
140
|
+
```python
|
|
141
|
+
# Iterate over nodes
|
|
142
|
+
for node in G.nodes:
|
|
143
|
+
print(node, G.nodes[node]) # Access node attributes
|
|
144
|
+
|
|
145
|
+
# Iterate over edges
|
|
146
|
+
for u, v in G.edges:
|
|
147
|
+
print(u, v, G[u][v]) # Access edge attributes
|
|
148
|
+
|
|
149
|
+
# Iterate with attributes
|
|
150
|
+
for node, attrs in G.nodes(data=True):
|
|
151
|
+
print(node, attrs)
|
|
152
|
+
|
|
153
|
+
for u, v, attrs in G.edges(data=True):
|
|
154
|
+
print(u, v, attrs)
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
## Modifying Graphs
|
|
158
|
+
|
|
159
|
+
### Removing Elements
|
|
160
|
+
```python
|
|
161
|
+
# Remove single node (also removes incident edges)
|
|
162
|
+
G.remove_node(1)
|
|
163
|
+
|
|
164
|
+
# Remove multiple nodes
|
|
165
|
+
G.remove_nodes_from([1, 2, 3])
|
|
166
|
+
|
|
167
|
+
# Remove edges
|
|
168
|
+
G.remove_edge(1, 2)
|
|
169
|
+
G.remove_edges_from([(1, 2), (2, 3)])
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
### Clearing Graph
|
|
173
|
+
```python
|
|
174
|
+
G.clear() # Remove all nodes and edges
|
|
175
|
+
G.clear_edges() # Remove only edges, keep nodes
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
## Attributes and Metadata
|
|
179
|
+
|
|
180
|
+
### Graph-Level Attributes
|
|
181
|
+
```python
|
|
182
|
+
G.graph['name'] = 'Social Network'
|
|
183
|
+
G.graph['date'] = '2025-01-15'
|
|
184
|
+
print(G.graph)
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
### Node Attributes
|
|
188
|
+
```python
|
|
189
|
+
# Set at creation
|
|
190
|
+
G.add_node(1, time='5pm', weight=0.5)
|
|
191
|
+
|
|
192
|
+
# Set after creation
|
|
193
|
+
G.nodes[1]['time'] = '6pm'
|
|
194
|
+
nx.set_node_attributes(G, {1: 'red', 2: 'blue'}, 'color')
|
|
195
|
+
|
|
196
|
+
# Get attributes
|
|
197
|
+
G.nodes[1]
|
|
198
|
+
G.nodes[1]['time']
|
|
199
|
+
nx.get_node_attributes(G, 'color')
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
### Edge Attributes
|
|
203
|
+
```python
|
|
204
|
+
# Set at creation
|
|
205
|
+
G.add_edge(1, 2, weight=4.7, color='red')
|
|
206
|
+
|
|
207
|
+
# Set after creation
|
|
208
|
+
G[1][2]['weight'] = 5.0
|
|
209
|
+
nx.set_edge_attributes(G, {(1, 2): 10.5}, 'weight')
|
|
210
|
+
|
|
211
|
+
# Get attributes
|
|
212
|
+
G[1][2]
|
|
213
|
+
G[1][2]['weight']
|
|
214
|
+
G.edges[1, 2]
|
|
215
|
+
nx.get_edge_attributes(G, 'weight')
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
## Subgraphs and Views
|
|
219
|
+
|
|
220
|
+
### Subgraph Creation
|
|
221
|
+
```python
|
|
222
|
+
# Create subgraph from node list
|
|
223
|
+
nodes_subset = [1, 2, 3, 4]
|
|
224
|
+
H = G.subgraph(nodes_subset) # Returns view (references original)
|
|
225
|
+
|
|
226
|
+
# Create independent copy
|
|
227
|
+
H = G.subgraph(nodes_subset).copy()
|
|
228
|
+
|
|
229
|
+
# Edge-induced subgraph
|
|
230
|
+
edge_subset = [(1, 2), (2, 3)]
|
|
231
|
+
H = G.edge_subgraph(edge_subset)
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
### Graph Views
|
|
235
|
+
```python
|
|
236
|
+
# Reverse view (for directed graphs)
|
|
237
|
+
G_reversed = G.reverse()
|
|
238
|
+
|
|
239
|
+
# Convert between directed/undirected
|
|
240
|
+
G_undirected = G.to_undirected()
|
|
241
|
+
G_directed = G.to_directed()
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
## Graph Information and Diagnostics
|
|
245
|
+
|
|
246
|
+
### Basic Information
|
|
247
|
+
```python
|
|
248
|
+
print(G) # Summary string, e.g. "Graph with 5 nodes and 4 edges"
|
|
249
|
+
# (nx.info was removed in NetworkX 3.0)
|
|
250
|
+
|
|
251
|
+
# Density (ratio of actual edges to possible edges)
|
|
252
|
+
nx.density(G)
|
|
253
|
+
|
|
254
|
+
# Check if graph is directed
|
|
255
|
+
G.is_directed()
|
|
256
|
+
|
|
257
|
+
# Check if graph is multigraph
|
|
258
|
+
G.is_multigraph()
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
### Connectivity Checks
|
|
262
|
+
```python
|
|
263
|
+
# For undirected graphs
|
|
264
|
+
nx.is_connected(G)
|
|
265
|
+
nx.number_connected_components(G)
|
|
266
|
+
|
|
267
|
+
# For directed graphs
|
|
268
|
+
nx.is_strongly_connected(G)
|
|
269
|
+
nx.is_weakly_connected(G)
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
## Important Considerations
|
|
273
|
+
|
|
274
|
+
### Floating Point Precision
|
|
275
|
+
Once graphs contain floating point numbers, all results are inherently approximate due to precision limitations. Small arithmetic errors can affect algorithm outcomes, particularly in minimum/maximum computations.
|
|
276
|
+
|
|
277
|
+
### Memory Considerations
|
|
278
|
+
Each time a script starts, graph data must be loaded into memory. For large datasets, this can cause performance issues. Consider:
|
|
279
|
+
- Using efficient data formats (pickle for Python objects)
|
|
280
|
+
- Loading only necessary subgraphs
|
|
281
|
+
- Using graph databases for very large networks
|
|
282
|
+
|
|
283
|
+
### Node and Edge Removal Behavior
|
|
284
|
+
When a node is removed, all edges incident with that node are automatically removed as well.
|