networkx
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseNetworkX Graph Analysis
NetworkX 图分析
Python library for creating, analyzing, and visualizing networks and graphs.
一款用于创建、分析和可视化网络与图的Python库。
When to Use
适用场景
- Social network analysis
- Knowledge graphs and ontologies
- Shortest path problems
- Community detection
- Citation/reference networks
- Biological networks (protein interactions)
- 社交网络分析
- 知识图谱与本体
- 最短路径问题
- 社区检测
- 引用/参考文献网络
- 生物网络(蛋白质相互作用)
Graph Types
图类型
| Type | Edges | Multiple Edges |
|---|---|---|
| Undirected | No |
| Directed | No |
| Undirected | Yes |
| Directed | Yes |
| 类型 | 边类型 | 允许多条边 |
|---|---|---|
| 无向 | 否 |
| 有向 | 否 |
| 无向 | 是 |
| 有向 | 是 |
Key Algorithms
核心算法
Centrality Measures
中心性指标
| Measure | What It Finds | Use Case |
|---|---|---|
| Degree | Most connections | Popular nodes |
| Betweenness | Bridge nodes | Information flow |
| Closeness | Fastest reach | Efficient spreaders |
| PageRank | Importance | Web pages, citations |
| Eigenvector | Influential connections | Who knows important people |
| 指标 | 作用 | 适用场景 |
|---|---|---|
| Degree | 找出连接数最多的节点 | 热门节点识别 |
| Betweenness | 找出桥接节点 | 信息流分析 |
| Closeness | 找出最易触达的节点 | 高效传播者识别 |
| PageRank | 评估节点重要性 | 网页排名、引用分析 |
| Eigenvector | 找出连接重要节点的节点 | 影响力节点识别 |
Path Algorithms
路径算法
| Algorithm | Purpose |
|---|---|
| Shortest path | Minimum hops |
| Weighted shortest | Minimum cost |
| All pairs shortest | Full distance matrix |
| Dijkstra | Efficient weighted paths |
| 算法 | 用途 |
|---|---|
| Shortest path | 寻找最少跳数路径 |
| Weighted shortest | 寻找最低成本路径 |
| All pairs shortest | 生成全节点距离矩阵 |
| Dijkstra | 高效寻找带权路径 |
Community Detection
社区检测
| Method | Approach |
|---|---|
| Louvain | Modularity optimization |
| Greedy modularity | Hierarchical merging |
| Label propagation | Fast, scalable |
| 方法 | 实现思路 |
|---|---|
| Louvain | 模块度优化 |
| Greedy modularity | 层次化合并 |
| Label propagation | 快速、可扩展 |
Graph Generators
图生成器
| Generator | Model |
|---|---|
| Erdős-Rényi | Random edges |
| Barabási-Albert | Preferential attachment (scale-free) |
| Watts-Strogatz | Small-world |
| Complete | All connected |
| 生成器 | 模型 |
|---|---|
| Erdős-Rényi | 随机边模型 |
| Barabási-Albert | 偏好依附模型(无标度) |
| Watts-Strogatz | 小世界模型 |
| Complete | 全连接模型 |
Layout Algorithms
布局算法
| Layout | Best For |
|---|---|
| Spring | General purpose |
| Circular | Regular structure |
| Kamada-Kawai | Aesthetics |
| Spectral | Clustered graphs |
| 布局 | 最佳适用场景 |
|---|---|
| Spring | 通用场景 |
| Circular | 规则结构 |
| Kamada-Kawai | 美观性优先 |
| Spectral | 聚类图 |
I/O Formats
输入输出格式
| Format | Preserves Attributes | Human Readable |
|---|---|---|
| GraphML | Yes | Yes (XML) |
| Edge list | No | Yes |
| JSON | Yes | Yes |
| Pandas | Yes | Via DataFrame |
| 格式 | 保留属性 | 人类可读性 |
|---|---|---|
| GraphML | 是 | 是(XML格式) |
| Edge list | 否 | 是 |
| JSON | 是 | 是 |
| Pandas | 是 | 通过DataFrame实现 |
Performance Considerations
性能考量
| Scale | Approach |
|---|---|
| < 10K nodes | Any algorithm |
| 10K - 100K | Use approximate algorithms |
| > 100K | Consider graph-tool or igraph |
Key concept: NetworkX is pure Python - great for prototyping, may need alternatives for production scale.
| 规模 | 处理方案 |
|---|---|
| < 10K 节点 | 任意算法均可 |
| 10K - 100K 节点 | 使用近似算法 |
| > 100K 节点 | 考虑使用graph-tool或igraph |
核心概念:NetworkX是纯Python实现的库,非常适合原型开发,但生产环境大规模场景可能需要替代方案。
Best Practices
最佳实践
- Set random seeds for reproducibility
- Choose correct graph type upfront
- Use pandas integration for data exchange
- Consider memory for large graphs
- 设置随机种子以保证结果可复现
- 提前选择正确的图类型
- 使用pandas集成进行数据交换
- 处理大图时注意内存限制