在 python 中为随机图着色
Coloring a random graph in python
我在 python 中有一个实现贪婪着色的脚本,换句话说,它用四种颜色为图形着色。我想生成一个随机图,然后将其传递到上面 link 中找到的脚本中以对其进行着色。
这就是我生成随机图的方式(以字典的形式):
list = [1,2,3,4,5]
d = {i: sample([j for j in q if i != j], randrange(1, len(q) - 1))
for i in q}
所以我现在需要的是某种将创建的字典传递给贪婪着色脚本的函数,因为如果我在名为 greedycoloring[=18= 的脚本中的函数中输入图形 d ] 我收到以下错误:
Traceback (most recent call last):
File "/private/var/mobile/Containers/Shared/AppGroup/9244016A-6D10-4627-B39B-6D63D3F9D22C/Pythonista3/Documents/nuovaprova.py", line 88, in <module>
greedyColoring(d, 5)
File "/private/var/mobile/Containers/Shared/AppGroup/9244016A-6D10-4627-B39B-6D63D3F9D22C/Pythonista3/Documents/nuovaprova.py", line 33, in greedyColoring
if (result[i] != -1):
IndexError: list index out of range
从0开始list
q如下图:
q = [0, 1, 2, 3, 4]
d = {i: sample([j for j in q if i != j], randrange(1, len(q) - 1))
for i in q}
然后,您可以直接将字典 d
传递到脚本中:
greedyColoring(d, 5)
给定的脚本将图表示为 list of lists
其中列表的索引表示节点,其对应的值表示其相邻节点,因此您也可以将 d
转换为列表的列表作为 [node for node in d.values()]
然后传入函数为:
greedyColoring([node for node in d.values()], 5)
我在 python 中有一个实现贪婪着色的脚本,换句话说,它用四种颜色为图形着色。我想生成一个随机图,然后将其传递到上面 link 中找到的脚本中以对其进行着色。 这就是我生成随机图的方式(以字典的形式):
list = [1,2,3,4,5]
d = {i: sample([j for j in q if i != j], randrange(1, len(q) - 1))
for i in q}
所以我现在需要的是某种将创建的字典传递给贪婪着色脚本的函数,因为如果我在名为 greedycoloring[=18= 的脚本中的函数中输入图形 d ] 我收到以下错误:
Traceback (most recent call last):
File "/private/var/mobile/Containers/Shared/AppGroup/9244016A-6D10-4627-B39B-6D63D3F9D22C/Pythonista3/Documents/nuovaprova.py", line 88, in <module>
greedyColoring(d, 5)
File "/private/var/mobile/Containers/Shared/AppGroup/9244016A-6D10-4627-B39B-6D63D3F9D22C/Pythonista3/Documents/nuovaprova.py", line 33, in greedyColoring
if (result[i] != -1):
IndexError: list index out of range
从0开始list
q如下图:
q = [0, 1, 2, 3, 4]
d = {i: sample([j for j in q if i != j], randrange(1, len(q) - 1))
for i in q}
然后,您可以直接将字典 d
传递到脚本中:
greedyColoring(d, 5)
给定的脚本将图表示为 list of lists
其中列表的索引表示节点,其对应的值表示其相邻节点,因此您也可以将 d
转换为列表的列表作为 [node for node in d.values()]
然后传入函数为:
greedyColoring([node for node in d.values()], 5)