Networkx study notes(0)

  


Networkx Class


Type


Self-loops allowed


Parallel edges allowed


Graph


undirected


Yes


No


DiGraph


directed


Yes


No


MultiGraph


undirected


Yes


Yes


MultiDiGraph


directed


Yes


Yes

  

  There are two methods to grow the Graph :

import networkx as nx
import matplotlib.pyplot as plt

G = nx.Graph()
G.add_node(1, features = 10)
G.add_node(1, features = 20)
G.add_node(2, features = ‘bobo‘)

nodes = [(3, {‘features‘ : ‘aoao‘}), (4, {‘features‘ : 9})]
G.add_nodes_from(nodes)

G.nodes[1][‘feature‘] = ‘nice‘
print(G.nodes)
print(G.nodes(data = True))
#result
#[1, 2, 3, 4]
#[(1, {‘features‘: ‘nice‘}), (2, {‘features‘: ‘bobo‘}), (3, {‘features‘: ‘aoao‘}), (4, {‘features‘:9})]
G.add_edge(0, 1, distance = 100)
G.add_edge(0, 1, distance = 200)

edges = [(1, 3, {‘distance‘: 142}), (2, 3, {‘distance‘: 19})]
G.add_edges_from(edges)

G.edges[0, 1][‘distance‘] = 999
G[0][1][‘distance‘] = 1999

print(G.edges)
print(G.edges(data = True))
#result
#[(1, 0), (1, 3), (2, 3)]
#[(1, 0, {‘distance‘: 1999}), (1, 3, {‘distance‘: 142}), (2, 3, {‘distance‘: 19})]

  the other useful methods to report something :(neighbors and adj are not useful almostly,because we could access the Graph index directly)

for n in G.neighbors(1):
    print(n)
for n in G[1]:
    print(n)
for nbr, datadict in G.adj[2].items():
    print(nbr, datadict)
for nbr, datadict in G[2].items():
    print(nbr, datadict)
print(G.has_node(0))
print(G.has_node(5))
print(G.has_edge(0,1))
print(G.has_edge(2,1))
#0
#3
#0
#3
#3 {‘distance‘: 19}
#3 {‘distance‘: 19}
#True
#False
#True
#False

  how to use the data of the attributes of nodes and edges :

for node_index, node_feature in G.nodes(data=True):
    print(node_index, node_feature)
for receiver, sender, features in G.edges(data=True):
    print(receiver, sender, features)
1 {‘features‘: ‘nice‘}
2 {‘features‘: ‘bobo‘}
3 {‘features‘: ‘aoao‘}
4 {‘features‘: 9}
0 {}
1 0 {‘distance‘: 1999}
1 3 {‘distance‘: 142}
2 3 {‘distance‘: 19}

  there are two methods to get a directed graph like G, they have the same nodes,name ,but with each edge (u, v, data) replaced by two directed edges (u, v, data) and (v, u, data), the difference between of them is that ‘.to_directed()‘ is a deep copy , nx.DiGraph  is a shallow copy         

diG = G.to_directed()
for receiver, sender, features in diG.edges(data=True):
    print(receiver, sender, features)
diG = nx.DiGraph(G)
1 0 {‘distance‘: 1999}
1 3 {‘distance‘: 142}
2 3 {‘distance‘: 19}
3 1 {‘distance‘: 142}
3 2 {‘distance‘: 19}
0 1 {‘distance‘: 1999}

原文地址:https://www.cnblogs.com/bobo-aoao/p/11881228.html

时间: 2024-08-02 15:44:24

Networkx study notes(0)的相关文章

Machine Learning Algorithms Study Notes(2)--Supervised Learning

Machine Learning Algorithms Study Notes 高雪松 @雪松Cedro Microsoft MVP 2    Supervised Learning    3 2.1    Perceptron Learning Algorithm (PLA)    3 2.1.1    PLA -- "知错能改"演算法    4 2.2    Linear Regression    6 2.2.1    线性回归模型    6 2.2.2    最小二乘法( le

My study notes —— 初始24种设计模式

摘要 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了可重用代码.让代码更容易被他人理解.保证代码可靠性. 毫无疑问,设计模式于己于他人于系统都是多赢的:设计模式使代码编制真正工程化:设计模式是软件工程的基石脉络,如同大厦的结构一样. 目录 设计概念 设计原则 四要素 基本模式 创建型 结构型 行为型 正文 设计概念 Change:让代码富有弹性,能够应对需求的变化: DRY:消除冗余,让代码更精炼: KISS:让代码

[2016-03-16]How can I take better study notes?

A:Dad,how can I take better study notes? B:Okay,look.Divide your pate into two columns. A:Like this? B:No.the one on the right should be larger. A:Let me do it agian.Is this better? B:Yes.Write down all the ideas on the right side of the page. A:What

Machine Learning Algorithms Study Notes(3)--Learning Theory

Machine Learning Algorithms Study Notes 高雪松 @雪松Cedro Microsoft MVP 本系列文章是Andrew Ng 在斯坦福的机器学习课程 CS 229 的学习笔记. Machine Learning Algorithms Study Notes 系列文章介绍 3 Learning Theory 3.1 Regularization and model selection 模型选择问题:对于一个学习问题,可以有多种模型选择.比如要拟合一组样本点,

Machine Learning Algorithms Study Notes(1)--Introduction

Machine Learning Algorithms Study Notes 高雪松 @雪松Cedro Microsoft MVP 目 录 1    Introduction    1 1.1    What is Machine Learning    1 1.2    学习心得和笔记的框架    1 2    Supervised Learning    3 2.1    Perceptron Learning Algorithm (PLA)    3 2.1.1    PLA -- "知

[Python Study Notes]CS架构远程访问获取信息--Client端v1.0

更新内容: 1.添加entry栏默认ip和port口 2.修正退出功能 3.添加退出自动关闭窗口功能 4.优化cpu显示为固定保留两位小数 ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' >>文件: ps_client.py >>作者: liu yang >>邮箱: [email protected] '''''''''

[Python Study Notes]CS架构远程访问获取信息--SERVER端v2.0

更新内容: 1.增加内存信息获取 2.增加电池信息获取 3.增加磁盘信息获取 4.重新布局窗体 5.增加窗体名称 6.增加连接成功之前,不可按压 ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' >>文件: ps_server.py >>作者: liu yang >>邮箱: [email protected] >&g

[Python Study Notes]CS架构远程访问获取信息--Client端v2.0

更新内容: 1.增加内存信息获取 2.增加电池信息获取 3.增加磁盘信息获取 4.重新布局窗体 5.增加窗体名称 6.增加连接成功之前,不可按压 效果图: ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' >>文件: ps_client.py >>作者: liu yang >>邮箱: [email protected] &

[Python Study Notes]批量将ppt转换为pdf v1,0

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' >>文件: ppt2pdf.py >>作者: liu yang >>邮箱: [email protected] >>博客: www.cnblogs.com/liu66blog '''''''''''''''''''''''''''''''''''''''''''