comp-think / 2019-2020

The GitHub repository containing all the material related to the Computational Thinking and Programming course of the Digital Humanities and Digital Knowledge degree at the University of Bologna (a.a. 2019/2020).
Other
12 stars 3 forks source link

Lecture "Organising information: graphs", exercise 1 #36

Open essepuntato opened 4 years ago

essepuntato commented 4 years ago

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.

morinigiu commented 4 years ago
Schermata 2019-12-09 alle 14 22 40
giuliamanganelli commented 4 years ago

Screen Shot 2019-12-09 at 14 20 12

arcangelo7 commented 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")

filename

FrancescoFernicola commented 4 years ago
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}}