comp-think / 2018-2019

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. 2018/2019).
Other
30 stars 8 forks source link

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

Open essepuntato opened 5 years ago

essepuntato commented 5 years ago

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.

SeverinJB commented 5 years ago
from networkx import MultiDiGraph

movie_graph = MultiDiGraph()

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

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

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

# Cast Fight Club
movie_graph.add_edge("Brad Pitt", "Fight Club")
movie_graph.add_edge("Helena Bonham Carter", "Fight Club")

# Cast Dark Shadows
movie_graph.add_edge("Johnny Depp", "Dark Shadows")
movie_graph.add_edge("Helena Bonham Carter", "Dark Shadows")
movie_graph.add_edge("Eva Green", "Dark Shadows")

print(movie_graph.nodes())
print(movie_graph.edges())
EleonoraPeruch commented 5 years ago
# Ocean's Twelve
# George Clooney
# Brad Pitt
# Catherine Zeta-Jones

# Fight Club
# Brad Pitt
# Helena Bonham Carter

# Dark Shadows
# Johnny Depp
# Helena Bonham Carter
# Eva Green

from networkx import DiGraph

# create a new graph
actor_and_movie = DiGraph() # I use DiGraph since there is only one edge between each two nodes

# create nodes for actors
actor_and_movie.add_node("Brad Pitt")
actor_and_movie.add_node("Eva Green")
actor_and_movie.add_node("George Clooney")
actor_and_movie.add_node("Catherine Zeta-Jones")
actor_and_movie.add_node("Johnny Depp")
actor_and_movie.add_node("Helena Bonham Carter")

# create nodes for movies
actor_and_movie.add_node("Ocean's Twelve")
actor_and_movie.add_node("Fight Club")
actor_and_movie.add_node("Dark Shadows")

# create edges between each actor and the movies in which he acted
actor_and_movie.add_edge("Brad Pitt", "Ocean's Twelve")
actor_and_movie.add_edge("Brad Pitt", "Fight Club")
actor_and_movie.add_edge("Eva Green", "Dark Shadows")
actor_and_movie.add_edge("George Clooney", "Ocean's Twelve")
actor_and_movie.add_edge("Catherine Zeta-Jones","Ocean's Twelve")
actor_and_movie.add_edge("Johnny Depp", "Dark Shadows")
actor_and_movie.add_edge("Helena Bonham Carter", "Fight Club")
actor_and_movie.add_edge("Helena Bonham Carter", "Dark Shadows")

print(actor_and_movie.nodes())
print(actor_and_movie.edges())

['Brad Pitt', 'Eva Green', 'George Clooney', 'Catherine Zeta-Jones', 'Johnny Depp', 'Helena Bonham Carter', "Ocean's Twelve", 'Fight Club', 'Dark Shadows']
[('Brad Pitt', "Ocean's Twelve"), ('Brad Pitt', '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'), ('Helena Bonham Carter', 'Dark Shadows')]
andreamust commented 5 years ago

from networkx import MultiDiGraph

my_cinema_graph = MultiDiGraph()

my_cinema_graph.add_node(1, name = "Brad", surname = "Pitt")
my_cinema_graph.add_node(2, name = "Eva", surname = "Green")
my_cinema_graph.add_node(3, name = "George", surname = "Clooney")
my_cinema_graph.add_node(4, name = "Catherine", surname = "Zeta-Jones")
my_cinema_graph.add_node(5, name = "Johnny", surname = "Depp")
my_cinema_graph.add_node(6, name = "Helena", surname = "Bohnam Carter")

my_cinema_graph.add_node('x', title = "Ocean's Twelve")
my_cinema_graph.add_node('y', title = "Fight Club")
my_cinema_graph.add_node('z', title = "Dark Shadows")

my_cinema_graph.add_edge(1, 'x')
my_cinema_graph.add_edge(1, 'y')
my_cinema_graph.add_edge(2, 'z')
my_cinema_graph.add_edge(3, 'x')
my_cinema_graph.add_edge(4, 'x')
my_cinema_graph.add_edge(5, 'z')
my_cinema_graph.add_edge(6, 'y')
my_cinema_graph.add_edge(6, 'z')

@essepuntato is there a way to print only the attributes without the node identifiers?

saraarmaroli commented 5 years ago

from networkx import DiGraph actor_movie = DiGraph()

actors nodes

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

movies nodes

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

edges

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

print(actor_movie.nodes()) print(actor_movie.edges()) ['Brad Pitt', 'Eva Green', 'George Clooney', 'Catherine Zeta Jones', 'Johnny Depp', 'Helena Bonham Carter', "Ocean's Twelve", 'Fight Club', 'Dark Shadows', 'Dark Shadow'] [('Brad Pitt', "Ocean's Twelve"), ('Brad Pitt', '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'), ('Helena Bonham Carter', 'Dark Shadow')]

MilenaCorbellini commented 5 years ago
from networkx import MultiDiGraph
movies_graph= MultiDiGraph()
#Actors
movies_graph.add_node("Brad Pit")
movies_graph.add_node("Eva Green")
movies_graph.add_node("George Clooney")
movies_graph.add_node("Chaterine Zeta-Jones")
movies_graph.add_node("Jonny Deep")
movies_graph.add_node("Helena Bonham Carter")
#Movies
movies_graph.add_node("Ocean's Twelve")
movies_graph.add_node("Fight Club")
movies_graph.add_node("Dark Shadows")

movies_graph.add_edge("Brad Pit", "Ocean's Twelve", color = 'r')
movies_graph.add_edge("George Clooney", "Ocean's Twelve", color = 'r')
movies_graph.add_edge("Chaterine Zeta-Jones", "Ocean's Twelve", color = 'r')

movies_graph.add_edge("Brad Pit", "Fight Club", color = 'blue')
movies_graph.add_edge("Helena Bonham Carter", "Fight Club", color= 'blue')
lisasiurina commented 5 years ago
from networkx import DiGraph
movie = DiGraph()

#nodes for actors
movie.add_node("Brad Pitt")
movie.add_node("Eva Green")
movie.add_node("George Clooney")
movie.add_node("Catherine Zeta-Jones")
movie.add_node("Johnny Depp")
movie.add_node("Helena Bonham Carter")

#nodes for movies
movie.add_node("Ocean's Twelve")
movie.add_node("Fight Club")
movie.add_node("Dark Shadows")

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

print(movie.nodes())
print(movie.edges())

['Brad Pitt', 'Eva Green', 'George Clooney', 'Catherine Zeta-Jones', 'Johnny Depp', 'Helena Bonham Carter', "Ocean's Twelve", 'Fight Club', 'Dark Shadows'] [('Brad Pitt', "Ocean's Twelve"), ('Brad Pitt', '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'), ('Helena Bonham Carter', 'Dark Shadows')]

delfimpandiani commented 5 years ago
from networkx import MultiDiGraph

hollywood = MultiDiGraph()

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

print(hollywood.edges())

[('George Clooney', "Ocean's Twelve"), ('Brad Pitt', "Ocean's Twelve"), ('Brad Pitt', 'Fight Club'), ('Catherine Zeta-Jones', "Ocean's Twelve"), ('Helena Bonhan Carter', 'Fight Club'), ('Johny Depp', 'Dark Shadows'), ('Helena Bonham Carter', 'Dark Shadows'), ('Eva Green', 'Dark Shadows')]
mangiafrangette commented 5 years ago
from networkx import MultiDiGraph

actor_movie_graph = MultiDiGraph()

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

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

# adding edges
actor_movie_graph.add_edge("George Clooney", "Ocean's Twelve")
actor_movie_graph.add_edge("Brad Pitt", "Ocean's Twelve")
actor_movie_graph.add_edge("Catherine Zeta-Jones", "Ocean's Twelve")
actor_movie_graph.add_edge("Brad Pitt", "Fight Club")
actor_movie_graph.add_edge("Helena Bonham Carter", "Fight Club")
actor_movie_graph.add_edge("Brad Pitt", "Dark Shadows")
actor_movie_graph.add_edge("Helena Bonham Carter", "Dark Shadows")
actor_movie_graph.add_edge("Eva Green", "Dark Shadows")
bluebell94 commented 5 years ago

from networkx import MultiDiGraph movie_stars = MultiDiGraph() movie_stars.add_node("a",weight="Ocean's Twelve") movie_stars.add_node("b",weight="Fight Club") movie_stars.add_node("c",weight="Dark Shadows") movie_stars.add_node(1, weight="Brad Pitt") movie_stars.add_node(2, weight="George Clooney") movie_stars.add_node(3, weight="Eva Green") movie_stars.add_node(4, weight="Catherine Zeta-Jones") movie_stars.add_node(6, weight="Helena Bonham Carter")

movie_stars.add_edge(1,"a") movie_stars.add_edge(1,"b") movie_stars.add_edge(2,"a") movie_stars.add_edge(3,"c") movie_stars.add_edge(4,"a") movie_stars.add_edge(5,"c") movie_stars.add_edge(6,"b") movie_stars.add_edge(6,"c")

print(movie_stars.nodes(data=True)) print(movie_stars.edges(data=True))

[('a', {'weight': "Ocean's Twelve"}), ('b', {'weight': 'Fight Club'}), ('c', {'weight': 'Dark Shadows'}), (1, {'weight': 'Brad Pitt'}), (2, {'weight': 'George Clooney'}), (3, {'weight': 'Eva Green'}), (4, {'weight': 'Catherine Zeta-Jones'}), (6, {'weight': 'Helena Bonham Carter'}), (5, {})] [(1, 'a', {}), (1, 'b', {}), (2, 'a', {}), (3, 'c', {}), (4, 'a', {}), (6, 'b', {}), (6, 'c', {}), (5, 'c', {})]

beccadelbens commented 5 years ago
from networkx import MultiDiGraph

second_graph = MultiDiGraph

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

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

print(second_graph.nodes())
print(second_graph.edges())
dafnikitsiki commented 5 years ago

from networkx import MultiDiGraph

create a new graph

cast_graph = MultiDiGraph()

create nodes for movies

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

create nodes for actors

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

create edges between actors and movies

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

print(cast_graph.nodes()) print(cast_graph.edges())

essepuntato commented 5 years ago

Hi @andreamust

is there a way to print only the attributes without the node identifiers?

Not to my knowledge – of course, you can implement a function that does exactly that, if you want.

Another comment for @bluebell94: use appropriate attribute names for specifying values – e.g. I do not expect to have a name associated to the attribute weight, bur rather a number. If you want to specify the names as attributes, please look at the way @andreamust did.