Open essepuntato opened 4 years ago
I looked for a quick and easy way to represent the graph and see if it was correct. I found this: https://www.geeksforgeeks.org/python-visualize-graphs-generated-in-networkx-using-matplotlib/. However, I didn't understand how to add attributes to the image.
I also noticed that if you create a connection between two nodes without having created the nodes first, they are created automatically, thus saving you many steps. Is this approach correct @essepuntato?
import networkx as nx
import matplotlib.pyplot as plt
tim_berners_lee_network = nx.MultiGraph()
tim_berners_lee_network.add_edge("Tim Berners Lee", "Tom Heath", weight=18)
tim_berners_lee_network.add_edge("Tim Berners Lee", "Christian Bizer", weight=18)
tim_berners_lee_network.add_edge("Tim Berners Lee", "Sören Auer", weight=10)
tim_berners_lee_network.add_edge("Tim Berners Lee", "Lalana Kagal", weight=9)
tim_berners_lee_network.add_edge("Tim Berners Lee", "Daniel J. Weitzner", weight=8)
print(tim_berners_lee_network.nodes())
# ['Tim Berners Lee', 'Tom Heath', 'Christian Bizer', 'Sören Auer', 'Lalana Kagal', 'Daniel J. Weitzner']
print(tim_berners_lee_network.edges(data=True))
# [('Tim Berners Lee', 'Tom Heath', {'weight': 18}), ('Tim Berners Lee', 'Christian Bizer', {'weight': 18}), ('Tim Berners Lee', 'Sören Auer', {'weight': 10}), ('Tim Berners Lee', 'Lalana Kagal', {'weight': 9}), ('Tim Berners Lee', 'Daniel J. Weitzner', {'weight': 8})]
nx.draw(tim_berners_lee_network, with_labels = True)
plt.savefig("filename.png")
from networkx import Graph
mygraph = Graph()
mygraph.add_node("Tom Heath")
mygraph.add_node("Christian Bizer")
mygraph.add_node("Sören Auer")
mygraph.add_node("Lalana Kagal")
mygraph.add_node("Daniel J. Weitzner")
mygraph.add_node("Tim Berners Lee")
mygraph.add_edge("Tim Berners Lee", "Tom Heath", weight=18)
mygraph.add_edge("Tim Berners Lee", "Christian Bizer", weight=18)
mygraph.add_edge("Tim Berners Lee", "Sören Auer", weight=10)
mygraph.add_edge("Tim Berners Lee", "Lalana Kagal", weight=9)
mygraph.add_edge("Tim Berners Lee", "Daniel J. Weitzner", weight=8)
print(mygraph.nodes())
#['Tom Heath', 'Christian Bizer', 'Sören Auer', 'Lalana Kagal', 'Daniel J. Weitzner', 'Tim Berners Lee']
print(mygraph.edges(data=True))
#[('Tom Heath', 'Tim Berners Lee', {'weight': 18}), ('Christian Bizer', 'Tim Berners Lee', {'weight': 18}), ('Sören Auer', 'Tim Berners Lee', {'weight': 10}), ('Lalana Kagal', 'Tim Berners Lee', {'weight': 9}), ('Daniel J. Weitzner', 'Tim Berners Lee', {'weight': 8})]
print(mygraph.adj["Tim Berners Lee"])
#{'Tom Heath': {'weight': 18}, 'Christian Bizer': {'weight': 18}, 'Sören Auer': {'weight': 10}, 'Lalana Kagal': {'weight': 9}, 'Daniel J. Weitzner': {'weight': 8}}
Consider the list of co-authors of Tim Berners-Lee as illustrated in the right box at http://dblp.uni-trier.de/pers/hd/b/Berners=Lee:Tim. Build an undirected graph that contains Tim Berners Lee as the central node, and that links to five nodes representing his top-five co-authors. Also, specify the weight of each edge as an attribute, where the value of the weight is the number of bibliographic resources (articles, proceedings, etc.) Tim Berners-Lee has co-authored with the person linked by that edge.