50 KiB
50 KiB
In [2]:
import matplotlib.pyplot as plt
import networkx as nx
import numpy as np
import pydot
from networkx.drawing.nx_pydot import graphviz_layout
from networkx.drawing.nx_agraph import to_agraph
import matplotlib.image as mpimg
Traceback (most recent call last):
File "<basthon-input-2-787db96fd013>", line 4, in <module>
import pydot
ModuleNotFoundError: No module named 'pydot'
In [3]:
G = nx.DiGraph([("a","b"), ("a","c"), ("b","d"), ("c","d"),
("b","e"), ("d","e"), ("e","g"), ("e","f"),
("f","g"), ("g","h")])
G.nodes["a"]['pos'] = "0,10!"
G.nodes["b"]['pos'] = "10,20!"
G.nodes["c"]['pos'] = "10,0!"
G.nodes["d"]['pos'] = "20,10!"
G.nodes["e"]['pos'] = "30,20!"
G.nodes["f"]['pos'] = "50,20!"
G.nodes["g"]['pos'] = "40,10!"
G.nodes["h"]['pos'] = "30,0!"
pos = graphviz_layout(G)
plt.figure("cycle")
nx.draw_networkx_nodes(G, pos, node_color="cyan")
nx.draw_networkx_labels(G, pos)
nx.draw_networkx_edges(G, pos, edge_color='r', arrows=False)
plt.box(False)
plt.show()Traceback (most recent call last):
File "<basthon-input-3-3c616d50977d>", line 14, in <module>
pos = graphviz_layout(G)
^^^^^^^^^^^^^^^
NameError: name 'graphviz_layout' is not defined
In [4]:
G = {}
G['a'] = ['b','c']
G['b'] = ['a','d','e']
G['c'] = ['a','d']
G['d'] = ['b','c','e']
G['e'] = ['b','d','f','g']
G['f'] = ['e','g']
G['g'] = ['e','f','h']
G['h'] = ['g']In [5]:
A1=[[0, 1, 1, 0, 0, 0, 0, 0],
[1, 0, 0, 1, 1, 0, 0, 0],
[1, 0, 0, 1, 0, 0, 0, 0],
[0, 1, 1, 0, 1, 0, 0, 0],
[0, 1, 0, 1, 0, 1, 1, 0],
[0, 0, 0, 0, 1, 0, 1, 0],
[0, 0, 0, 0, 1, 1, 0, 1],
[0, 0, 0, 0, 0, 0, 1, 0]]
A1Out [5]:
[[0, 1, 1, 0, 0, 0, 0, 0], [1, 0, 0, 1, 1, 0, 0, 0], [1, 0, 0, 1, 0, 0, 0, 0], [0, 1, 1, 0, 1, 0, 0, 0], [0, 1, 0, 1, 0, 1, 1, 0], [0, 0, 0, 0, 1, 0, 1, 0], [0, 0, 0, 0, 1, 1, 0, 1], [0, 0, 0, 0, 0, 0, 1, 0]]
In [9]:
liste=['a','b','c','d','e','f','g','h']
n = len(liste)
A=[[0]*n for i in range(n)]
for i in range(n):
for j in range(n):
if liste[j] in G[liste[i]]:
A[i][j]=1
AOut [9]:
[[0, 1, 1, 0, 0, 0, 0, 0], [1, 0, 0, 1, 1, 0, 0, 0], [1, 0, 0, 1, 0, 0, 0, 0], [0, 1, 1, 0, 1, 0, 0, 0], [0, 1, 0, 1, 0, 1, 1, 0], [0, 0, 0, 0, 1, 0, 1, 0], [0, 0, 0, 0, 1, 1, 0, 1], [0, 0, 0, 0, 0, 0, 1, 0]]
In [14]:
print(G.keys()) # affiche les clésdict_keys(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'])
In [15]:
print(G.values()) # affiche les valeursdict_values([['b', 'c'], ['a', 'd', 'e'], ['a', 'd'], ['b', 'c', 'e'], ['b', 'd', 'f', 'g'], ['e', 'g'], ['e', 'f', 'h'], ['g']])
In [16]:
print(len(G)) # affiche le nombre de clés8
In [17]:
print(G['e']) # affiche la valeur de la clé ’e’['b', 'd', 'f', 'g']
In [18]:
# G.keys() et G.values() sont itérables
# affiche les valeurs du dictionnaire
for el in G.values():
print(el)['b', 'c'] ['a', 'd', 'e'] ['a', 'd'] ['b', 'c', 'e'] ['b', 'd', 'f', 'g'] ['e', 'g'] ['e', 'f', 'h'] ['g']
In [20]:
# affiche les clés et les valeurs des clés
for key in G.keys():
print(key,G[key])a ['b', 'c'] b ['a', 'd', 'e'] c ['a', 'd'] d ['b', 'c', 'e'] e ['b', 'd', 'f', 'g'] f ['e', 'g'] g ['e', 'f', 'h'] h ['g']
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [21]:
# On importe le module
import networkx as nxIn [22]:
# Création du graphe
g1 = nx.Graph()In [23]:
# On ajoute les sommets (appelés node)
g1.add_node('a')
g1.add_node('b')
g1.add_node('c')
g1.add_node('d')
g1.add_node('e')
g1.add_node('f')
g1.add_node('g')
g1.add_node('h')In [24]:
# On ajoute les arêtes (appelées edge)
g1.add_edge('a','b')
g1.add_edge('a','c')
g1.add_edge('b','d')
g1.add_edge('b','e')
g1.add_edge('c','d')
g1.add_edge('d','e')
g1.add_edge('e','g')
g1.add_edge('e','f')
g1.add_edge('g','f')
g1.add_edge('g','h')In [25]:
import matplotlib.pyplot as plt
nx.draw(g1, with_labels=True, font_weight='bold',
node_size=800, node_color='lightgrey')
plt.show()In [26]:
import networkx as nx
import matplotlib.pyplot as plt
#création du graphe à partir de listes
liste1 = ['a','b','c','d','e','f','g','h']
g2 = nx.Graph()
g2.add_nodes_from(liste1)
liste2=[('a','b'),('a','c'),('b','d'),('b','e'),
('c','d'),('d','e'),('e','g'),('e','f'),
('g','f'),('g','h')]
g2.add_edges_from(liste2)
nx.draw(g2, with_labels=True, font_weight='bold', node_size=800,node_color='lightgrey')
plt.show()In [33]:
B = nx.adjacency_matrix(g2)
print(B[(0,0)])
n=len(liste1)
A=[[0]*n for i in range(n)]
for i in range(n):
for j in range(n):
A[i][j] = B[(i,j)]
print(A)0.0 [[0.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0], [1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0], [1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0], [0.0, 1.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0], [0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 1.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0]]
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]: