當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python NetworkX greedy_color用法及代碼示例

本文簡要介紹 networkx.algorithms.coloring.greedy_color 的用法。

用法:

greedy_color(G, strategy='largest_first', interchange=False)

使用各種貪心圖形著色策略為圖形著色。

嘗試使用盡可能少的顏色為圖形著色,其中節點的鄰居不能具有與節點本身相同的顏色。給定的策略決定了節點著色的順序。

這些策略在[1]中說明,smallest-last基於[2]。

參數

GNetworkX 圖
strategy字符串或函數(G,顏色)

提供著色策略的函數(或表示函數的字符串),通過按應著色的順序返回節點。 G 是圖形,colors 是當前分配顏色的字典,由節點鍵控。該函數必須返回 G 中所有節點的可迭代對象。

如果策略函數是迭代器生成器(即帶有 yield 語句的函數),請記住 colors 字典將在每個 yield 之後更新,因為該函數貪心地選擇顏色。

如果strategy 是字符串,則必須是以下之一,每一個代表一個內置策略函數。

  • 'largest_first'
  • 'random_sequential'
  • 'smallest_last'
  • 'independent_set'
  • 'connected_sequential_bfs'
  • 'connected_sequential_dfs'
  • 'connected_sequential'(上一個策略的別名)
  • 'saturation_largest_first'
  • 'DSATUR'(上一個策略的別名)
interchange: bool

如果設置為 True ,將使用 [3] 中說明的顏色交換算法。

請注意,saturation_largest_firstindependent_set 不適用於互換。此外,如果您將交換與自己的策略函數一起使用,則不能依賴 colors 參數中的值。

返回

具有表示節點的鍵和表示的值的字典
相應的著色。

拋出

NetworkXPointlessConcept

如果 strategysaturation_largest_firstindependent_set 並且 interchangeTrue

參考

1

Adrian Kosowski, and Krzysztof Manuszewski, Classical Coloring of Graphs, Graph Colorings, 2-19, 2004. ISBN 0-8218-3458-4.

2

David W. Matula, and Leland L. Beck, “Smallest-last ordering and clustering and graph coloring algorithms.” J. ACM 30, 3 (July 1983), 417-427. <https://doi.org/10.1145/2402.322385>

3

Maciej M. Sysło, Marsingh Deo, Janusz S. Kowalik, Discrete Optimization Algorithms with Pascal Programs, 415-424, 1983. ISBN 0-486-45353-7.

例子

>>> G = nx.cycle_graph(4)
>>> d = nx.coloring.greedy_color(G, strategy="largest_first")
>>> d in [{0: 0, 1: 1, 2: 0, 3: 1}, {0: 1, 1: 0, 2: 1, 3: 0}]
True

相關用法


注:本文由純淨天空篩選整理自networkx.org大神的英文原創作品 networkx.algorithms.coloring.greedy_color。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。