从包含邻域关系的字典创建无向未加权图
Creating undirected unweighted graph from dictionary containing neighborhood relationship
我有一个 Python 字典,如下所示:
{'Aitkin': ['Carlton', 'Cass', 'Crow Wing', 'Itasca',
'Kanabec', 'Mille Lacs', 'Pine', 'St. Louis'], 'Anoka':
['Chisago', 'Hennepin', 'Isanti', 'Ramsey', 'Sherburne',
'Washington'], 'Becker': ['Clay', 'Clearwater', 'Hubbard',
'Mahnomen', 'Norman', 'Otter Tail', 'Wadena'], 'Beltrami':
['Cass', 'Clearwater', 'Hubbard', 'Itasca', 'Koochiching',
'Lake of the Woods', 'Marshall', 'Pennington', 'Roseau'],
'Benton': ['Mille Lacs', 'Morrison', 'Sherburne', 'Stearns'], 'Big
Stone': ['Lac qui Parle', 'Stevens', 'Swift', 'Traverse'], 'Blue
Earth': ['Brown', 'Faribault', 'Le Sueur', 'Martin',
'Nicollet', 'Waseca', 'Watonwan'], 'Brown': ['Blue Earth',
'Cottonwood', 'Nicollet', 'Redwood', 'Renville', 'Watonwan'],
'Carlton': ['Aitkin', 'Pine', 'St. Louis'], 'Carver': ['Hennepin',
'McLeod', 'Scott', 'Sibley', 'Wright'], 'Cass': ['Aitkin',
'Beltrami', 'Crow Wing', 'Hubbard', 'Itasca', 'Morrison',
'Todd', 'Wadena'], 'Chippewa': ['Kandiyohi', 'Lac qui Parle',
'Renville', 'Swift', 'Yellow Medicine'], 'Chisago': ['Anoka',
'Isanti', 'Kanabec', 'Pine', 'Washington'], 'Clay': ['Becker',
'Norman', 'Otter Tail', 'Wilkin'], 'Clearwater': ['Becker',
'Beltrami', 'Hubbard', 'Mahnomen', 'Pennington', 'Polk'],
'Cook': ['Lake'], 'Cottonwood': ['Brown', 'Jackson', 'Murray',
'Nobles', 'Redwood', 'Watonwan'], 'Crow Wing': ['Aitkin', 'Cass',
'Mille Lacs', 'Morrison'], 'Dakota': ['Goodhue', 'Hennepin',
'Ramsey', 'Rice', 'Scott', 'Washington'], 'Dodge': ['Goodhue',
'Mower', 'Olmsted', 'Rice', 'Steele'], 'Douglas': ['Grant', 'Otter
Tail', 'Pope', 'Stearns', 'Stevens', 'Todd'], 'Faribault': ['Blue
Earth', 'Freeborn', 'Martin', 'Waseca'], 'Fillmore': ['Houston',
'Mower', 'Olmsted', 'Winona'], 'Freeborn': ['Faribault', 'Mower',
'Steele', 'Waseca'], 'Goodhue': ['Dakota', 'Dodge', 'Olmsted',
'Rice', 'Wabasha'], 'Grant': ['Douglas', 'Otter Tail', 'Pope',
'Stevens', 'Traverse', 'Wilkin'], 'Hennepin': ['Anoka', 'Carver',
'Dakota', 'Ramsey', 'Scott', 'Sherburne', 'Wright'],
'Houston': ['Fillmore', 'Winona'], 'Hubbard': ['Becker', 'Beltrami',
'Cass', 'Clearwater', 'Wadena'], 'Isanti': ['Anoka', 'Chisago',
'Kanabec', 'Mille Lacs', 'Pine', 'Sherburne'], 'Itasca': ['Aitkin',
'Beltrami', 'Cass', 'Koochiching', 'St. Louis'], 'Jackson':
['Cottonwood', 'Martin', 'Nobles', 'Watonwan'], 'Kanabec': ['Aitkin',
'Chisago', 'Isanti', 'Mille Lacs', 'Pine'], 'Kandiyohi': ['Chippewa',
'Meeker', 'Pope', 'Renville', 'Stearns', 'Swift'], 'Kittson':
['Marshall', 'Roseau'], 'Koochiching': ['Beltrami', 'Itasca', 'Lake
of the Woods', 'St. Louis'], 'Lac qui Parle': ['Big Stone',
'Chippewa', 'Swift', 'Yellow Medicine'], 'Lake': ['Cook', 'St.
Louis'], 'Lake of the Woods': ['Beltrami', 'Koochiching', 'Roseau'],
'Le Sueur': ['Blue Earth', 'Nicollet', 'Rice', 'Scott', 'Sibley',
'Waseca'], 'Lincoln': ['Lyon', 'Pipestone', 'Yellow Medicine'],
'Lyon': ['Lincoln', 'Murray', 'Pipestone', 'Redwood', 'Yellow
Medicine'], 'Mahnomen': ['Becker', 'Clearwater', 'Norman', 'Polk'],
'Marshall': ['Beltrami', 'Kittson', 'Pennington', 'Polk', 'Roseau'],
'Martin': ['Blue Earth', 'Faribault', 'Jackson', 'Watonwan'],
'McLeod': ['Carver', 'Meeker', 'Renville', 'Sibley', 'Wright'],
'Meeker': ['Kandiyohi', 'McLeod', 'Renville', 'Stearns', 'Wright'],
'Mille Lacs': ['Aitkin', 'Benton', 'Crow Wing', 'Isanti',
'Kanabec', 'Morrison', 'Sherburne'], 'Morrison': ['Benton',
'Cass', 'Crow Wing', 'Mille Lacs', 'Stearns', 'Todd'], 'Mower':
['Dodge', 'Fillmore', 'Freeborn', 'Olmsted', 'Steele'], 'Murray':
['Cottonwood', 'Lyon', 'Nobles', 'Pipestone', 'Redwood', 'Rock'],
'Nicollet': ['Blue Earth', 'Brown', 'Le Sueur', 'Renville', 'Sibley'],
'Nobles': ['Cottonwood', 'Jackson', 'Murray', 'Rock'], 'Norman':
['Becker', 'Clay', 'Mahnomen', 'Polk'], 'Olmsted': ['Dodge',
'Fillmore', 'Goodhue', 'Mower', 'Wabasha', 'Winona'], 'Otter Tail':
['Becker', 'Clay', 'Douglas', 'Grant', 'Wadena', 'Wilkin'],
'Pennington': ['Beltrami', 'Clearwater', 'Marshall', 'Polk', 'Red
Lake'], 'Pine': ['Aitkin', 'Carlton', 'Chisago', 'Isanti',
'Kanabec'], 'Pipestone': ['Lincoln', 'Lyon', 'Murray', 'Rock'],
'Polk': ['Clearwater', 'Mahnomen', 'Marshall', 'Norman',
'Pennington', 'Red Lake'], 'Pope': ['Douglas', 'Grant',
'Kandiyohi', 'Stearns', 'Stevens', 'Swift'], 'Ramsey': ['Anoka',
'Dakota', 'Hennepin', 'Washington'], 'Red Lake': ['Pennington',
'Polk'], 'Redwood': ['Brown', 'Cottonwood', 'Lyon', 'Murray',
'Renville', 'Yellow Medicine'], 'Renville': ['Brown', 'Chippewa',
'Kandiyohi', 'McLeod', 'Meeker', 'Nicollet', 'Redwood',
'Sibley', 'Yellow Medicine'], 'Rice': ['Dakota', 'Dodge',
'Goodhue', 'Le Sueur', 'Scott', 'Steele', 'Waseca'], 'Rock':
['Murray', 'Nobles', 'Pipestone'], 'Roseau': ['Beltrami', 'Kittson',
'Lake of the Woods', 'Marshall'], 'Scott': ['Carver', 'Dakota',
'Hennepin', 'Le Sueur', 'Rice', 'Sibley'], 'Sherburne': ['Anoka',
'Benton', 'Hennepin', 'Isanti', 'Mille Lacs', 'Stearns',
'Wright'], 'Sibley': ['Carver', 'Le Sueur', 'McLeod', 'Nicollet',
'Renville', 'Scott'], 'St. Louis': ['Aitkin', 'Carlton', 'Itasca',
'Koochiching', 'Lake'], 'Stearns': ['Benton', 'Douglas',
'Kandiyohi', 'Meeker', 'Morrison', 'Pope', 'Sherburne',
'Todd', 'Wright'], 'Steele': ['Dodge', 'Freeborn', 'Mower', 'Rice',
'Waseca'], 'Stevens': ['Big Stone', 'Douglas', 'Grant', 'Pope',
'Swift', 'Traverse'], 'Swift': ['Big Stone', 'Chippewa',
'Kandiyohi', 'Lac qui Parle', 'Pope', 'Stevens'], 'Todd':
['Cass', 'Douglas', 'Morrison', 'Otter Tail', 'Stearns', 'Wadena'],
'Traverse': ['Big Stone', 'Grant', 'Stevens', 'Wilkin'], 'Wabasha':
['Goodhue', 'Olmsted', 'Winona'], 'Wadena': ['Becker', 'Cass',
'Hubbard', 'Otter Tail', 'Todd'], 'Waseca': ['Blue Earth',
'Faribault', 'Freeborn', 'Le Sueur', 'Rice', 'Steele'],
'Washington': ['Anoka', 'Chisago', 'Dakota', 'Ramsey'], 'Watonwan':
['Blue Earth', 'Brown', 'Cottonwood', 'Jackson', 'Martin'], 'Wilkin':
['Clay', 'Grant', 'Otter Tail', 'Traverse'], 'Winona': ['Fillmore',
'Houston', 'Olmsted', 'Wabasha'], 'Wright': ['Carver', 'Hennepin',
'McLeod', 'Meeker', 'Sherburne', 'Stearns'], 'Yellow Medicine':
['Chippewa', 'Lac qui Parle', 'Lincoln', 'Lyon', 'Redwood',
'Renville']}
字典中的键代表节点,而值(列表)代表键的邻居节点。这是一个无向无权图。
在networkx
或一些网络分析或图形相关的库中是否有一些函数可以实现这个?
您可以使用 nx.Graph
构造函数——如果它们没有作为键出现在原始字典中(data
代表原始问题中的字典),它将添加额外的节点:
import networkx as nx
graph = nx.Graph(data)
我有一个 Python 字典,如下所示:
{'Aitkin': ['Carlton', 'Cass', 'Crow Wing', 'Itasca',
'Kanabec', 'Mille Lacs', 'Pine', 'St. Louis'], 'Anoka': ['Chisago', 'Hennepin', 'Isanti', 'Ramsey', 'Sherburne',
'Washington'], 'Becker': ['Clay', 'Clearwater', 'Hubbard',
'Mahnomen', 'Norman', 'Otter Tail', 'Wadena'], 'Beltrami': ['Cass', 'Clearwater', 'Hubbard', 'Itasca', 'Koochiching',
'Lake of the Woods', 'Marshall', 'Pennington', 'Roseau'], 'Benton': ['Mille Lacs', 'Morrison', 'Sherburne', 'Stearns'], 'Big Stone': ['Lac qui Parle', 'Stevens', 'Swift', 'Traverse'], 'Blue Earth': ['Brown', 'Faribault', 'Le Sueur', 'Martin',
'Nicollet', 'Waseca', 'Watonwan'], 'Brown': ['Blue Earth',
'Cottonwood', 'Nicollet', 'Redwood', 'Renville', 'Watonwan'], 'Carlton': ['Aitkin', 'Pine', 'St. Louis'], 'Carver': ['Hennepin', 'McLeod', 'Scott', 'Sibley', 'Wright'], 'Cass': ['Aitkin',
'Beltrami', 'Crow Wing', 'Hubbard', 'Itasca', 'Morrison',
'Todd', 'Wadena'], 'Chippewa': ['Kandiyohi', 'Lac qui Parle',
'Renville', 'Swift', 'Yellow Medicine'], 'Chisago': ['Anoka', 'Isanti', 'Kanabec', 'Pine', 'Washington'], 'Clay': ['Becker', 'Norman', 'Otter Tail', 'Wilkin'], 'Clearwater': ['Becker',
'Beltrami', 'Hubbard', 'Mahnomen', 'Pennington', 'Polk'], 'Cook': ['Lake'], 'Cottonwood': ['Brown', 'Jackson', 'Murray', 'Nobles', 'Redwood', 'Watonwan'], 'Crow Wing': ['Aitkin', 'Cass', 'Mille Lacs', 'Morrison'], 'Dakota': ['Goodhue', 'Hennepin', 'Ramsey', 'Rice', 'Scott', 'Washington'], 'Dodge': ['Goodhue', 'Mower', 'Olmsted', 'Rice', 'Steele'], 'Douglas': ['Grant', 'Otter Tail', 'Pope', 'Stearns', 'Stevens', 'Todd'], 'Faribault': ['Blue Earth', 'Freeborn', 'Martin', 'Waseca'], 'Fillmore': ['Houston', 'Mower', 'Olmsted', 'Winona'], 'Freeborn': ['Faribault', 'Mower', 'Steele', 'Waseca'], 'Goodhue': ['Dakota', 'Dodge', 'Olmsted', 'Rice', 'Wabasha'], 'Grant': ['Douglas', 'Otter Tail', 'Pope', 'Stevens', 'Traverse', 'Wilkin'], 'Hennepin': ['Anoka', 'Carver',
'Dakota', 'Ramsey', 'Scott', 'Sherburne', 'Wright'], 'Houston': ['Fillmore', 'Winona'], 'Hubbard': ['Becker', 'Beltrami', 'Cass', 'Clearwater', 'Wadena'], 'Isanti': ['Anoka', 'Chisago', 'Kanabec', 'Mille Lacs', 'Pine', 'Sherburne'], 'Itasca': ['Aitkin', 'Beltrami', 'Cass', 'Koochiching', 'St. Louis'], 'Jackson': ['Cottonwood', 'Martin', 'Nobles', 'Watonwan'], 'Kanabec': ['Aitkin', 'Chisago', 'Isanti', 'Mille Lacs', 'Pine'], 'Kandiyohi': ['Chippewa', 'Meeker', 'Pope', 'Renville', 'Stearns', 'Swift'], 'Kittson': ['Marshall', 'Roseau'], 'Koochiching': ['Beltrami', 'Itasca', 'Lake of the Woods', 'St. Louis'], 'Lac qui Parle': ['Big Stone', 'Chippewa', 'Swift', 'Yellow Medicine'], 'Lake': ['Cook', 'St. Louis'], 'Lake of the Woods': ['Beltrami', 'Koochiching', 'Roseau'], 'Le Sueur': ['Blue Earth', 'Nicollet', 'Rice', 'Scott', 'Sibley', 'Waseca'], 'Lincoln': ['Lyon', 'Pipestone', 'Yellow Medicine'], 'Lyon': ['Lincoln', 'Murray', 'Pipestone', 'Redwood', 'Yellow Medicine'], 'Mahnomen': ['Becker', 'Clearwater', 'Norman', 'Polk'], 'Marshall': ['Beltrami', 'Kittson', 'Pennington', 'Polk', 'Roseau'], 'Martin': ['Blue Earth', 'Faribault', 'Jackson', 'Watonwan'], 'McLeod': ['Carver', 'Meeker', 'Renville', 'Sibley', 'Wright'], 'Meeker': ['Kandiyohi', 'McLeod', 'Renville', 'Stearns', 'Wright'], 'Mille Lacs': ['Aitkin', 'Benton', 'Crow Wing', 'Isanti',
'Kanabec', 'Morrison', 'Sherburne'], 'Morrison': ['Benton', 'Cass', 'Crow Wing', 'Mille Lacs', 'Stearns', 'Todd'], 'Mower': ['Dodge', 'Fillmore', 'Freeborn', 'Olmsted', 'Steele'], 'Murray': ['Cottonwood', 'Lyon', 'Nobles', 'Pipestone', 'Redwood', 'Rock'], 'Nicollet': ['Blue Earth', 'Brown', 'Le Sueur', 'Renville', 'Sibley'], 'Nobles': ['Cottonwood', 'Jackson', 'Murray', 'Rock'], 'Norman': ['Becker', 'Clay', 'Mahnomen', 'Polk'], 'Olmsted': ['Dodge', 'Fillmore', 'Goodhue', 'Mower', 'Wabasha', 'Winona'], 'Otter Tail': ['Becker', 'Clay', 'Douglas', 'Grant', 'Wadena', 'Wilkin'], 'Pennington': ['Beltrami', 'Clearwater', 'Marshall', 'Polk', 'Red Lake'], 'Pine': ['Aitkin', 'Carlton', 'Chisago', 'Isanti', 'Kanabec'], 'Pipestone': ['Lincoln', 'Lyon', 'Murray', 'Rock'], 'Polk': ['Clearwater', 'Mahnomen', 'Marshall', 'Norman',
'Pennington', 'Red Lake'], 'Pope': ['Douglas', 'Grant', 'Kandiyohi', 'Stearns', 'Stevens', 'Swift'], 'Ramsey': ['Anoka', 'Dakota', 'Hennepin', 'Washington'], 'Red Lake': ['Pennington', 'Polk'], 'Redwood': ['Brown', 'Cottonwood', 'Lyon', 'Murray',
'Renville', 'Yellow Medicine'], 'Renville': ['Brown', 'Chippewa', 'Kandiyohi', 'McLeod', 'Meeker', 'Nicollet', 'Redwood',
'Sibley', 'Yellow Medicine'], 'Rice': ['Dakota', 'Dodge',
'Goodhue', 'Le Sueur', 'Scott', 'Steele', 'Waseca'], 'Rock': ['Murray', 'Nobles', 'Pipestone'], 'Roseau': ['Beltrami', 'Kittson', 'Lake of the Woods', 'Marshall'], 'Scott': ['Carver', 'Dakota', 'Hennepin', 'Le Sueur', 'Rice', 'Sibley'], 'Sherburne': ['Anoka',
'Benton', 'Hennepin', 'Isanti', 'Mille Lacs', 'Stearns',
'Wright'], 'Sibley': ['Carver', 'Le Sueur', 'McLeod', 'Nicollet', 'Renville', 'Scott'], 'St. Louis': ['Aitkin', 'Carlton', 'Itasca', 'Koochiching', 'Lake'], 'Stearns': ['Benton', 'Douglas',
'Kandiyohi', 'Meeker', 'Morrison', 'Pope', 'Sherburne',
'Todd', 'Wright'], 'Steele': ['Dodge', 'Freeborn', 'Mower', 'Rice', 'Waseca'], 'Stevens': ['Big Stone', 'Douglas', 'Grant', 'Pope', 'Swift', 'Traverse'], 'Swift': ['Big Stone', 'Chippewa',
'Kandiyohi', 'Lac qui Parle', 'Pope', 'Stevens'], 'Todd': ['Cass', 'Douglas', 'Morrison', 'Otter Tail', 'Stearns', 'Wadena'], 'Traverse': ['Big Stone', 'Grant', 'Stevens', 'Wilkin'], 'Wabasha': ['Goodhue', 'Olmsted', 'Winona'], 'Wadena': ['Becker', 'Cass', 'Hubbard', 'Otter Tail', 'Todd'], 'Waseca': ['Blue Earth',
'Faribault', 'Freeborn', 'Le Sueur', 'Rice', 'Steele'], 'Washington': ['Anoka', 'Chisago', 'Dakota', 'Ramsey'], 'Watonwan': ['Blue Earth', 'Brown', 'Cottonwood', 'Jackson', 'Martin'], 'Wilkin': ['Clay', 'Grant', 'Otter Tail', 'Traverse'], 'Winona': ['Fillmore', 'Houston', 'Olmsted', 'Wabasha'], 'Wright': ['Carver', 'Hennepin', 'McLeod', 'Meeker', 'Sherburne', 'Stearns'], 'Yellow Medicine': ['Chippewa', 'Lac qui Parle', 'Lincoln', 'Lyon', 'Redwood',
'Renville']}
字典中的键代表节点,而值(列表)代表键的邻居节点。这是一个无向无权图。
在networkx
或一些网络分析或图形相关的库中是否有一些函数可以实现这个?
您可以使用 nx.Graph
构造函数——如果它们没有作为键出现在原始字典中(data
代表原始问题中的字典),它将添加额外的节点:
import networkx as nx
graph = nx.Graph(data)