comp-think/2020-2021

Lecture "Organising information: graphs", exercise 2

Opened this issue · 14 comments

Create a directed graph which 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.

import matplotlib.pyplot as plt
import networkx as nx

kino_graph = nx.MultiDiGraph()

kino_graph.add_node("Ocean's Twelve")
kino_graph.add_node("Fight Club")
kino_graph.add_node("Dark Shadows")

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

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

color_map = []
for node in kino_graph:
    if node == "Ocean's Twelve" or node == "Fight Club" or node == "Dark Shadows":
        color_map.append('blue')
    else:
        color_map.append('green')


nx.draw_networkx(kino_graph, node_color=color_map, with_labels=True)
ax = plt.gca()
ax.margins(0.20)
plt.axis("off")
plt.show()

Figure_1

from networkx import DiGraph

films_actors_graph = DiGraph()

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

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

print(films_actors_graph.edges())

from networkx import DiGraph

# Actors:
A_BP = "Brad Pitt"
A_EG = "Eva Green"
A_GC = "George Clooney"
A_CZJ = "Catherine Zeta-Jones"
A_JD = "Johnny Depp"
A_HBC = "Helena Bonham Carter"

# Movies:
M_OT = "Ocean's Twelve"
M_FC = "Fight Club"      # Sorry for breaking the first two rules
M_DS = "Dark Shadows"

ActorsToMovies=DiGraph()

ActorsToMovies.add_edge(A_BP,M_OT)
ActorsToMovies.add_edge(A_BP,M_FC)
ActorsToMovies.add_edge(A_EG,M_DS)
ActorsToMovies.add_edge(A_GC,M_OT)
ActorsToMovies.add_edge(A_CZJ,M_OT)
ActorsToMovies.add_edge(A_JD,M_DS)
ActorsToMovies.add_edge(A_HBC,M_FC)
ActorsToMovies.add_edge(A_HBC,M_DS)

print(ActorsToMovies.nodes)
print(ActorsToMovies.edges)

from networkx import DiGraph

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

from networkx import DiGraph

actors_graph = DiGraph()

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

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

print(actors_graph.edges())

from networkx import DiGraph


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

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

from networkx import DiGraph
movies_graph = Graph()

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

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

from networkx import DiGraph

movies = DiGraph()

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

movies.add_node("Ocean's Twelve")
movies.add_node("Fight Club")
movies.add_node("Dark Shadows")

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

print(movies.edges())
from networkx import MultiDiGraph

my_graph=MultiDiGraph()

my_graph.add_node('Brad Pitt')
my_graph.add_node('Eva Green')
my_graph.add_node('George Clooney')
my_graph.add_node('Catherine Zeta Jones')
my_graph.add_node('Johnny Depp')
my_graph.add_node('Helena Bonham Carter')

my_graph.add_node('Oceans Twelve')
my_graph.add_node('Fight Club')
my_graph.add_node('Dark Shadows')

my_graph.add_edge('Brad Pitt','Oceans Twelve')
my_graph.add_edge('Brad Pitt','Fight Club')
my_graph.add_edge('George Clooney','Oceans Twelve')
my_graph.add_edge('Johnny Depp','Dark Shadows')
my_graph.add_edge('Helena Bonham Carter','Fight Club')
my_graph.add_edge('Helena Bonham Carter','Dark Shadows')

print(my_graph.adj)

import networkx as ntx

actors_movies = ntx.DiGraph()

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

actors_movies.add_node("Ocean's Twelve")
actors_movies.add_node("Fight Club")
actors_movies.add_node("Dark Shadows")

actors_movies.add_edge("Brad Pitt", "Ocean's Twelve")
actors_movies.add_edge("Brad Pitt", "Fight Club")
actors_movies.add_edge("Eva Green", "Dark Shadows")
actors_movies.add_edge("George Clooney", "Ocean's Twelve")
actors_movies.add_edge("Catherine Zeta-Jones", "Ocean's Twelve")
actors_movies.add_edge("Johnny Depp", "Dark Shadows")
actors_movies.add_edge("Helena Bonham Carter", "Fight Club")
actors_movies.add_edge("Helena Bonham Carter", "Dark Shadows")
BP = "Brad Pitt"
EG = "Eva Green"
GC = "George Clooney"
CZJ = "Catherine Zeta-Jones"
JD = "Johnny Depp"
HBC = "Helena Bonham Carter"
OT = "Ocean's Twelve"
FC = "Fight Club"
DS = "Dark Shadows"

film_graph = nx.MultiDiGraph()

film_graph.add_node(BP)
film_graph.add_node(EG)
film_graph.add_node(GC)
film_graph.add_node(CZJ)
film_graph.add_node(JD)
film_graph.add_node(HBC)
film_graph.add_edge(BP, OT)
film_graph.add_edge(CZJ, OT)
film_graph.add_edge(GC, OT)
film_graph.add_edge(BP, FC)
film_graph.add_edge(HBC, FC)
film_graph.add_edge(JD, DS)
film_graph.add_edge(HBC, DS)
film_graph.add_edge(EG, DS)

print(film_graph.edges())
nx.draw_circular(film_graph, with_labels=True)
plt.show()
#plt.savefig("film_graph.png")

from networkx import MultiDiGraph

actors_graph = MultiDiGraph()

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

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

act = DiGraph()
act.add_node("Brad Pitt", link="https://www.imdb.com/name/nm0000093/")
act.add_node("Eva Green", link="https://www.imdb.com/name/nm1200692/")
act.add_node("George Clooney", link="https://www.imdb.com/name/nm0000123/")
act.add_node("Helena Bonham Carter", link="https://www.imdb.com/name/nm0000307/")
act.add_node("Catherine Zeta-Jones", link="https://www.imdb.com/name/nm0001876/")
act.add_node("Johnny Depp", link="https://www.imdb.com/name/nm0000136/")

act.add_node("Ocean's Twelve", link="https://www.imdb.com/title/tt0349903/")
act.add_node("Fight Club", link="https://www.imdb.com/title/tt0137523/")
act.add_node("Dark Shadows", link="https://www.imdb.com/title/tt1077368/")


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

# actors: Brad Pitt, Eva Green, George Clooney, Catherine Zeta-Jones, Johnny Depp, and Helena Bonham Carter
# movies: Ocean's Twelve, Fight Club, Dark Shadows

from networkx import DiGraph

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

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