Reputation: 467
I added the nodes by passing a dictionary of objects to add_nodes_from
function.
Then I specified the edges by passing a list to add_edges_from
function.
When, edges are added they create duplicate nodes instead of using the ones already added before.
import networkx as nx
import matplotlib.pyplot as plt
from Employee import Employee
G = nx.DiGraph()
employees = {
"John": Employee("John"),
"Mathews": Employee("Mathews"),
"Joseph": Employee("Joseph"),
"Lana": Employee("Lana"),
"Debrah": Employee("Debrah"),
"Greg": Employee("Greg"),
"Bob": Employee("Bob"),
"Mary": Employee("Mary"),
}
connections = [
(employees.get("John"), employees.get("Debrah")),
(employees.get("John"), employees.get("Mary")),
(employees.get("Mary"), employees.get("Greg")),
(employees.get("Mary"), employees.get("Lana")),
(employees.get("Mary"), employees.get("Debrah")),
(employees.get("Mathews"), employees.get("Joseph")),
(employees.get("Mathews"), employees.get("Debrah")),
(employees.get("Mathews"), employees.get("Mary")),
(employees.get("Lana"), employees.get("Debrah")),
(employees.get("Greg"), employees.get("Bob")),
]
G.add_nodes_from(employees)
G.add_edges_from(connections)
print(G.nodes)
Output
['John', 'Mathews', 'Joseph', 'Lana', 'Debrah', 'Greg', 'Bob', 'Mary', John, Debrah, Mary, Greg, Lana, Mathews, Joseph, Bob]
Upvotes: 2
Views: 245
Reputation: 363566
G.add_nodes_from(employees)
This is adding nodes using the keys of the dict (strings)
G.add_edges_from(connections)
This is adding edges using the values of the dict (employees)
Upvotes: 2