Comments (12)
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())
from 2018-2019.
# 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')]
from 2018-2019.
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?
from 2018-2019.
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')]
from 2018-2019.
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')]
from 2018-2019.
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')]
from 2018-2019.
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())
from 2018-2019.
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())
from 2018-2019.
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')
from 2018-2019.
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")
from 2018-2019.
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', {})]
from 2018-2019.
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.
from 2018-2019.
Related Issues (20)
- Lecture "Brute-force algorithms", exercise 3 HOT 13
- Lecture "Brute-force algorithms", exercise 4 HOT 16
- Lecture "Brute-force algorithms", exercise 5 HOT 12
- Lecture "Organising information: unordered structures", exercise 1 HOT 21
- Lecture "Organising information: unordered structures", exercise 2 HOT 19
- Lecture "Organising information: unordered structures", exercise 3 HOT 17
- Lecture "Recursion", exercise 1 HOT 18
- Lecture "Recursion", exercise 2 HOT 20
- Lecture "Divide and conquer algorithm", exercise 1 HOT 12
- Lecture "Divide and conquer algorithm", exercise 2 HOT 5
- Lecture "Dynamic programming algorithms", exercise 1 HOT 15
- Lecture "Dynamic programming algorithms", exercise 2 HOT 8
- Lecture "Organising information: trees", exercise 1 HOT 4
- Lecture "Organising information: trees", exercise 2 HOT 4
- Lecture "Organising information: graphs", exercise 1 HOT 14
- Christmas present game + bonus Python exercise
- Lecture "Backtracking algorithms", exercise 1 HOT 3
- Lecture "Greedy algorithms", exercise 1 HOT 2
- Question about an exercise from last year
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from 2018-2019.