跳到主要内容
Ctrl+K
NetworkX 3.4.2 documentation - Home
  • 安装
  • 教程
  • 后端
  • 参考
  • 图库
  • 开发者
  • 版本发布
    • 指南
  • 主页
  • GitHub
  • 安装
  • 教程
  • 后端
  • 参考
  • 图库
  • 开发者
  • 版本发布
  • 指南
  • 主页
  • GitHub

章节导航

  • 基础
  • 绘图
  • 3D 绘图
  • Graphviz 布局
  • Graphviz 绘图
  • 图
  • 算法
    • 束搜索
    • 介数中心性
    • 块模型
    • 回路
    • 环检测
    • 戴维斯俱乐部
    • 去密度化
    • 使用 Girvan-Newman 进行社区检测
    • 贪心着色
    • 通过谱图分割进行图像分割
    • 迭代动力系统
    • Krackhardt 中心性
    • 最近公共祖先
    • 最大独立集
    • 并行介数
    • 逆 Cuthill–McKee
    • 寻找最短路径
    • SNAP 图摘要
    • 子图
  • 外部库
  • 地理空间
  • 子类
  • 图库
  • 算法
  • 束搜索

注意

转到末尾下载完整示例代码。

束搜索#

具有动态束宽的束搜索。

渐进式加宽束搜索重复执行束搜索,每次增加束宽,直到找到目标节点。

import math

import matplotlib.pyplot as plt
import networkx as nx


def progressive_widening_search(G, source, value, condition, initial_width=1):
    """Progressive widening beam search to find a node.

    The progressive widening beam search involves a repeated beam
    search, starting with a small beam width then extending to
    progressively larger beam widths if the target node is not
    found. This implementation simply returns the first node found that
    matches the termination condition.

    `G` is a NetworkX graph.

    `source` is a node in the graph. The search for the node of interest
    begins here and extends only to those nodes in the (weakly)
    connected component of this node.

    `value` is a function that returns a real number indicating how good
    a potential neighbor node is when deciding which neighbor nodes to
    enqueue in the breadth-first search. Only the best nodes within the
    current beam width will be enqueued at each step.

    `condition` is the termination condition for the search. This is a
    function that takes a node as input and return a Boolean indicating
    whether the node is the target. If no node matches the termination
    condition, this function raises :exc:`NodeNotFound`.

    `initial_width` is the starting beam width for the beam search (the
    default is one). If no node matching the `condition` is found with
    this beam width, the beam search is restarted from the `source` node
    with a beam width that is twice as large (so the beam width
    increases exponentially). The search terminates after the beam width
    exceeds the number of nodes in the graph.

    """
    # Check for the special case in which the source node satisfies the
    # termination condition.
    if condition(source):
        return source
    # The largest possible value of `i` in this range yields a width at
    # least the number of nodes in the graph, so the final invocation of
    # `bfs_beam_edges` is equivalent to a plain old breadth-first
    # search. Therefore, all nodes will eventually be visited.
    log_m = math.ceil(math.log2(len(G)))
    for i in range(log_m):
        width = initial_width * pow(2, i)
        # Since we are always starting from the same source node, this
        # search may visit the same nodes many times (depending on the
        # implementation of the `value` function).
        for u, v in nx.bfs_beam_edges(G, source, value, width):
            if condition(v):
                return v
    # At this point, since all nodes have been visited, we know that
    # none of the nodes satisfied the termination condition.
    raise nx.NodeNotFound("no node satisfied the termination condition")

搜索高中心性的节点。#

我们生成一个随机图,计算每个节点的中心性,然后执行渐进式加宽搜索以找到一个高中心性的节点。

# Set a seed for random number generation so the example is reproducible
seed = 89

G = nx.gnp_random_graph(100, 0.5, seed=seed)
centrality = nx.eigenvector_centrality(G)
avg_centrality = sum(centrality.values()) / len(G)


def has_high_centrality(v):
    return centrality[v] >= avg_centrality


source = 0
value = centrality.get
condition = has_high_centrality

found_node = progressive_widening_search(G, source, value, condition)
c = centrality[found_node]
print(f"found node {found_node} with centrality {c}")


# Draw graph
pos = nx.spring_layout(G, seed=seed)
options = {
    "node_color": "blue",
    "node_size": 20,
    "edge_color": "grey",
    "linewidths": 0,
    "width": 0.1,
}
nx.draw(G, pos, **options)
# Draw node with high centrality as large and red
nx.draw_networkx_nodes(G, pos, nodelist=[found_node], node_size=100, node_color="r")
plt.show()
plot beam search
found node 73 with centrality 0.12598283530728402

脚本总运行时间: (0 分 0.186 秒)

下载 Jupyter notebook: plot_beam_search.ipynb

下载 Python 源代码: plot_beam_search.py

下载 压缩包: plot_beam_search.zip

图库由 Sphinx-Gallery 生成

在本页面上
  • 搜索高中心性的节点。

© 版权所有 2004-2024, NetworkX 开发者。

使用 Sphinx 8.1.3 创建。

使用 PyData Sphinx Theme 0.15.4 构建。