digraph 操作节点和边的添加修改删除查找方法以及查找前驱后

2025-03-13  本文已影响0人  hehehehe

在 NetworkX 的 DiGraph(有向图)中,可以通过以下方法操作节点和边,以及查找前驱和后继节点。以下是详细的添加、修改、删除和查找方法:


一、操作节点的方法

1. 添加节点

使用 add_node 方法添加单个节点,可以同时设置属性:

import networkx as nx

# 创建有向图
graph = nx.DiGraph()

# 添加单个节点并设置属性
graph.add_node(1, color="red", size=10)
graph.add_node(2, color="blue", size=20)

# 批量添加节点
graph.add_nodes_from([3, 4, 5])

# 查看所有节点及其属性
print(graph.nodes(data=True))
# 输出: [(1, {'color': 'red', 'size': 10}), (2, {'color': 'blue', 'size': 20}), (3, {}), (4, {}), (5, {})]

2. 修改节点属性

可以直接通过 graph.nodes[node][attribute] 修改节点属性:

# 修改节点属性
graph.nodes[1]["color"] = "green"
graph.nodes[1]["size"] = 15

print(graph.nodes[1])
# 输出: {'color': 'green', 'size': 15}

3. 删除节点

使用 remove_node 方法删除单个节点:

# 删除节点
graph.remove_node(5)

print(graph.nodes(data=True))
# 输出: [(1, {'color': 'green', 'size': 15}), (2, {'color': 'blue', 'size': 20}), (3, {}), (4, {})]

可以批量删除节点:

graph.remove_nodes_from([3, 4])
print(graph.nodes(data=True))
# 输出: [(1, {'color': 'green', 'size': 15}), (2, {'color': 'blue', 'size': 20})]

4. 查找节点

# 获取所有节点
print(list(graph.nodes))  # 输出: [1, 2]

# 判断节点是否存在
print(graph.has_node(1))  # 输出: True
print(graph.has_node(5))  # 输出: False

二、操作边的方法

1. 添加边

使用 add_edge 方法添加单条边,可以同时设置属性:

# 添加单条边
graph.add_edge(1, 2, weight=5, label="A")

# 批量添加边
graph.add_edges_from([(2, 3), (3, 4, {"weight": 10}), (4, 1)])

# 查看所有边及其属性
print(graph.edges(data=True))
# 输出: [(1, 2, {'weight': 5, 'label': 'A'}), (2, 3, {}), (3, 4, {'weight': 10}), (4, 1, {})]

2. 修改边属性

可以直接通过 graph.edges[edge][attribute] 修改边属性:

# 修改边属性
graph.edges[1, 2]["weight"] = 15
graph.edges[1, 2]["label"] = "B"

print(graph.edges[1, 2])
# 输出: {'weight': 15, 'label': 'B'}

3. 删除边

使用 remove_edge 方法删除单条边:

# 删除单条边
graph.remove_edge(1, 2)

print(graph.edges(data=True))
# 输出: [(2, 3, {}), (3, 4, {'weight': 10}), (4, 1, {})]

可以批量删除边:

graph.remove_edges_from([(2, 3), (4, 1)])
print(graph.edges(data=True))
# 输出: [(3, 4, {'weight': 10})]

4. 查找边

# 获取所有边
print(list(graph.edges))  # 输出: [(3, 4)]

# 判断边是否存在
print(graph.has_edge(3, 4))  # 输出: True
print(graph.has_edge(1, 2))  # 输出: False

三、查找前驱和后继的方法

在有向图中:

1. 查找某节点的前驱

使用 graph.predecessors(node) 查找某节点的所有前驱节点:

graph.add_edge(1, 2)
graph.add_edge(3, 2)
graph.add_edge(4, 2)

print(list(graph.predecessors(2)))
# 输出: [1, 3, 4]

2. 查找某节点的后继

使用 graph.successors(node) 查找某节点的所有后继节点:

graph.add_edge(2, 5)
graph.add_edge(2, 6)

print(list(graph.successors(2)))
# 输出: [5, 6]

四、其他相关方法

1. 入度和出度

使用 graph.in_degree(node)graph.out_degree(node) 获取入度和出度:

print(graph.in_degree(2))  # 输出: 3 (节点 2 的前驱有 1, 3, 4)
print(graph.out_degree(2))  # 输出: 2 (节点 2 的后继有 5, 6)

2. 获取邻接关系

# 后继节点
print(graph[2])  # 输出: {5: {}, 6: {}}

# 前驱节点
print(graph.pred[2])  # 输出: {1: {}, 3: {}, 4: {}}

3. 查找入度和出度的节点

使用 graph.in_edges(node)graph.out_edges(node) 获取与某节点有关的入边和出边:

print(graph.in_edges(2))  # 输出: [(1, 2), (3, 2), (4, 2)] (指向节点 2 的边)
print(graph.out_edges(2))  # 输出: [(2, 5), (2, 6)] (从节点 2 出发的边)

五、总结

节点操作方法

边操作方法

前驱和后继方法

上一篇 下一篇

猜你喜欢

热点阅读