Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
essepuntato opened this issue Dec 9, 2019 · 4 comments
Open

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

essepuntato opened this issue Dec 9, 2019 · 4 comments
Labels

Comments

@essepuntato
Copy link
Collaborator

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
Copy link

Schermata 2019-12-09 alle 14 22 40

@giuliamanganelli
Copy link

Screen Shot 2019-12-09 at 14 20 12

@arcangelo7
Copy link

arcangelo7 commented Dec 9, 2019

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
Copy link

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}}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants