comp-think/2021-2022

Lecture "Organising information: graphs", exercise 2

essepuntato opened this issue ยท 10 comments

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.

from networkx import MultiDiGraph

actor_film=MultiDiGraph()

actor_film.add_node("Brad Pitt")
actor_film.add_node("Catherine Zeta-Jones")
actor_film.add_node("Eva Green")
actor_film.add_node("George Clooney")
actor_film.add_node("Helena Bonham-Carter")
actor_film.add_node("Johnny Depp")
actor_film.add_node("Dark Shadows")
actor_film.add_node("Fight Club")
actor_film.add_node("Ocean's Twelve")

actor_film.add_edge("Ocean's Twelve", "Brad Pitt")
actor_film.add_edge("Ocean's Twelve", "Catherine Zeta-Jones")
actor_film.add_edge("Ocean's Twelve", "George Clooney")
actor_film.add_edge("Fight Club", "Brad Pitt")
actor_film.add_edge("Fight Club", "Helena Bonham-Carter")
actor_film.add_edge("Dark Shadows", "Eva Green")
actor_film.add_edge("Dark Shadows", "Jhonny Depp")
actor_film.add_edge("Dark Shadows", "Helena Bonham-Carter")

print(actor_film.nodes())
print(actor_film.edges())

Screenshot 2021-12-15 at 16 25 23

from networkx import MultiDiGraph

movies_graph = MultiDiGraph()

movies_graph.add_node("Brad Pitt")
movies_graph.add_node("Eva Green")
movies_graph.add_node("George Clooney")
movies_graph.add_node("Catherine Zeta-Jones")
movies_graph.add_node("Johnny Depp")
movies_graph.add_node("Helena Bonham Carter")

movies_graph.add_edge("Brad Pitt","Fight Club")
movies_graph.add_edge("Brad Pitt","Ocean's Twelve")
movies_graph.add_edge("Eva Green","Dark Shadows")
movies_graph.add_edge("George Clooney","Ocean's Twelve")
movies_graph.add_edge("Catherine Zeta-Jones","Ocean's Twelve")
movies_graph.add_edge("Johnny Depp","Dark Shadows")
movies_graph.add_edge("Helena Bonham Carter","Fight Club")
movies_graph.add_edge("Helena Bonham Carter","Dark Shadows")
from networkx import MultiDiGraph

MoviesCast_DirGraph = MultiDiGraph()

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

print(MoviesCast_DirGraph.nodes())
print(MoviesCast_DirGraph.edges())
from networkx import DiGraph

movies = DiGraph()

movies.add_edge("Ocean's Twelve", "Brad Pitt")
movies.add_edge("Ocean's Twelve", "George Clooney")
movies.add_edge("Ocean's Twelve", "Catherine Zeta-Jones")

movies.add_edge("Fight Club", "Brad Pitt")
movies.add_edge("Fight Club", "Helena Bonham Carter")

movies.add_edge("Dark Shadows", "Johnny Depp")
movies.add_edge("Dark Shadows", "Eva Green")
movies.add_edge("Dark Shadows", "Helena Bonham Carter")

print(movies.nodes())
print(movies.edges())

If I checked correctly, the graph should contain the following information

Actor Starring in
Bread Pitt Ocean's Twelve, Fight Club
Eva Green Dark Shadows
George Clooney Ocean's Twelve
Catherine Zeta-Jones Ocean's Twelve
Johnny Depp Dark Shadows
Helena Bonham Carter Fight Club, Dark Shadows

The graph translating this schema is:

from networkx import DiGraph

movieGraph = DiGraph()

#Actors
movieGraph.add_node("Bread Pitt")
movieGraph.add_node("Eva Green")
movieGraph.add_node("George Clooney")
movieGraph.add_node("Catherine Zeta-Jones")
movieGraph.add_node("Johnny Depp")
movieGraph.add_node("Helena Bonham Carter")

#Movies
movieGraph.add_node("Ocean's Twelve")
movieGraph.add_node("Fight Club")
movieGraph.add_node("Dark Shadows")

#Edges
movieGraph.add_edge("Bread Pitt", "Ocean's Twelve")
movieGraph.add_edge("Bread Pitt", "Fight Club")
movieGraph.add_edge("Eva Green", "Dark Shadows")
movieGraph.add_edge("George Clooney", "Ocean's Twelve")
movieGraph.add_edge("Catherine Zeta-Jones", "Ocean's Twelve")
movieGraph.add_edge("Johnny Depp", "Dark Shadows")
movieGraph.add_edge("Helena Bonham Carter", "Fight Club")
movieGraph.add_edge("Helena Bonham Carter", "Dark Shadows")

Schermata 2021-12-19 alle 19 44 23

image

from networkx import Graph

my_graph = DiGraph()

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

my_graph.add_edge(1, 7)
my_graph.add_edge(3, 7)
my_graph.add_edge(4, 7)
my_graph.add_edge(1, 8)
my_graph.add_edge(6, 8)
my_graph.add_edge(5, 9)
my_graph.add_edge(6, 9)
my_graph.add_edge(2, 9)