comp-think / 2023-2024

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. 2023/2024).
14 stars 0 forks source link

Lecture "Organising information: graphs", exercise 2 #40

Open essepuntato opened 6 months ago

essepuntato commented 6 months ago

Create a directed graph that relates the actors Brad Pitt, Eva Green, George Clooney, Catherine Zeta-Jones, Johnny Depp, and Helena Bonham Carter to the following movies: Ocean's Twelve, Fight Club, Dark Shadows.

frammenti commented 6 months ago
import networkx as nx

G = nx.DiGraph()

G.add_nodes_from([
    "Ocean's Twelve", "Fight Club", "Dark Shadows",
    "Brad Pitt", "Eva Green", "George Clooney",
    "Catherine Zeta-Jones", "Johnny Depp", "Helena Bonham Carter",
    ])
G.add_edges_from([
    ("George Clooney", "Ocean's Twelve"), ("Brad Pitt", "Ocean's Twelve"), ("Catherine Zeta-Jones", "Ocean's Twelve"),
    ("Brad Pitt", "Fight Club"), ("Helena Bonham Carter", "Fight Club"),
    ("Johnny Depp", "Dark Shadows"), ("Eva Green", "Dark Shadows"), ("Helena Bonham Carter", "Dark Shadows"),
    ])

print(G.reverse().adj["Ocean's Twelve"])
# {'Brad Pitt': {}, 'George Clooney': {}, 'Catherine Zeta-Jones': {}}
print(G.reverse().adj["Fight Club"])
# {'Brad Pitt': {}, 'Helena Bonham Carter': {}}
print(G.reverse().adj["Dark Shadows"])
# {'Eva Green': {}, 'Johnny Depp': {}, 'Helena Bonham Carter': {}}

Figure_2

Liber-R commented 6 months ago

Screenshot 2023-12-18 130146

Chiaramartina commented 6 months ago
Schermata 2023-12-18 alle 17 54 19
simocasaz commented 6 months ago
from networkx import MultiDiGraph

my_graph = MultiDiGraph()

my_graph.add_node(1, type = "movie", title = "Ocean's Twelve")
my_graph.add_node(2, type = "movie", title = "Fight Club")
my_graph.add_node(3, type = "movie", title = "Dark Shadows")
my_graph.add_node(4, type = "actor", name = "Brad", surname = "Pitt")
my_graph.add_node(5, type = "actor", name = "Eva", surname = "Green")
my_graph.add_node(6, type = "actor", name = "George", surname = "Clooney")
my_graph.add_node(7, type = "actor", name = "Catherine", surname = "Zeta-Jones")
my_graph.add_node(8, type = "actor", name = "Johnny", surname = "Depp")
my_graph.add_node(9, type = "actor", name = "Helena", surname = "Bonham Carter")

my_graph.add_edge(4, 1, relation = "starred in")
my_graph.add_edge(1, 4, relation = "has in the cast")
my_graph.add_edge(4, 2, relation = "starred in")
my_graph.add_edge(2, 4, relation = "has in the cast")
my_graph.add_edge(5, 3, relation = "starred in")
my_graph.add_edge(3, 5, relation = "has in the cast")
my_graph.add_edge(6, 1, relation = "starred in")
my_graph.add_edge(1, 6, relation = "has in the cast")
my_graph.add_edge(7, 1, relation = "starred in")
my_graph.add_edge(1, 7, relation = "has in the cast")
my_graph.add_edge(8, 3, relation = "starred in")
my_graph.add_edge(3, 8, relation = "has in the cast")
my_graph.add_edge(9, 2, relation = "starred in")
my_graph.add_edge(2, 9, relation = "has in the cast")
my_graph.add_edge(9, 3, relation = "starred in")
my_graph.add_edge(3, 9, relation = "has in the cast")
qwindici commented 6 months ago
from networkx import DiGraph

films = {
    "Ocean's Twelve": ["Catherine Zeta-Jones", "George Clooney", "Brad Pitt"],
    "Fight Club": ["Brad Pitt", "Helena Bonham Carter"],
    "Dark Shadows": ["Johnny Depp", "Helena Bonham Carter", "Eva Green"],
}

starred_in = DiGraph()

for film in films:
    starred_in.add_node(film)
    for actor in films[film]:
        starred_in.add_node(actor)
        starred_in.add_edge(film, actor, relationship='starring')