-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimpleGraph.py
More file actions
74 lines (62 loc) · 2.64 KB
/
Copy pathsimpleGraph.py
File metadata and controls
74 lines (62 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import networkx as nx
from matplotlib import pyplot as plt
class SimpleGraph:
""" Représentation d'un Graphe par matrice d'adjacence :
edges : liste d'adjacence (Liste 2D d'entiers : 0 ou 1)
names : tableau associant à chaque numéro de noeud une chaîne de caractère représentant par exemple le nom du noeud.
vertices : entier représentant le nombre de noeuds du graphe
"""
def __init__(self, names: [str]):
''' Constructeur. Deux configuPeut-être appelé soit avec edges et names, soit avec None.
Paramètres :
names : tableau des noms des noeuds.
'''
self.names = names
self.vertices = len(names)
self.edges = []
for i in range(len(names)) :
self.edges.append([0]*len(names))
def getNeighbors(self,vertex: int):
''' retourne la liste d'entiers, correspondant au voisinage du noeud "vertex" en entrée
param int vertex : noeud donné
return list of int : liste de voisins du noeud "vertex"
'''
return self.edges[vertex]
def addNeighbor(self,source: int, destination: int):
''' crée un arc entre le noeud "source" et le noeud "destination"
Paramètres :
* int source : noeud source
* int destination : noeud destination
'''
self.edges[source][destination]=1
self.edges[destination][source]=1
def __str__(self):
''' retourne une chaîne de caractères décrivant le graphe.
return String: chaîne de caractères
'''
show="<graph vertices=\"" + str(self.vertices) + "\">\n"
for i in range(self.vertices):
for j in range(self.vertices):
if (self.edges[i][j]==1):
show = show + "\t<arc source=\"" + self.names[i] + "\" destination=\"" + self.names[j] + "\">\n"
return show
def print_nx(self):
''' Affichage graphique du graphe
'''
G=nx.Graph();
labels={}
for i in range(self.vertices):
labels[i] = self.names[i]
G=nx.relabel_nodes(G,labels)
for i in range(self.vertices):
for j in range(i,self.vertices):
if self.edges[i][j]>0 and i!=j:
G.add_edge(i,j)
G.add_edge(j,i)
nx.draw(G, labels=labels,with_labels=True)
plt.show()
'''
+---------------------------------------------------------------------+
| Question 2: Instancier et afficher le graphe de la figure 5. |
+---------------------------------------------------------------------+
'''