gen.py (4447B)
1 #!/usr/bin/env python 2 3 import networkx as nx 4 from networkx.algorithms.distance_measures import * 5 import matplotlib.pyplot as plt 6 from random import * 7 8 from sys import argv 9 from io import open 10 11 def random_simple_connected_graph(nodes, num_edges): 12 g = nx.Graph() 13 14 # random spanning tree 15 for i in range(1,len(nodes)): 16 g.add_edge(nodes[i], choice(nodes[:i])) 17 18 # additionnal edges 19 g.add_edges_from(choices(list(nx.non_edges(g)), k=num_edges-len(nodes))) 20 21 return g 22 23 def draw_graph_file(id, g): 24 plt.clf() 25 26 pos = nx.nx_agraph.graphviz_layout(g, prog="neato") 27 nx.draw(g, pos=pos, with_labels=True, font_size=36, node_size=2500, node_color="white", edge_color="black", width=2, linewidths=2) 28 #nx.draw_kamada_kawai(g, with_labels=True, font_size=36, node_size=2500, node_color="white", edge_color="black", width=2, linewidths=2) 29 30 ax = plt.gca() 31 ax.collections[0].set_edgecolor("#000000") 32 33 ax.margins(0.20) 34 35 #plt.text(-0.5,-1,"Diamètre : __ Rayon : __ Centre(s) : __________", va="top", ha="left") 36 #plt.show() 37 #plt.axis("off") 38 fname = ID+"g"+id+".png" 39 plt.savefig(fname) 40 return fname 41 42 def printboth(s, end='\n'): 43 print(s, file=out, end=end) 44 print(s, file=outcorr, end=end) 45 46 if len(argv)>1: 47 ID = argv[1] 48 else: 49 ID="0" 50 51 print(ID) 52 53 out = open(ID+".md", 'w') 54 outcorr = open(ID+"-corr.md", 'w') 55 56 printboth(""" 57 58 # Évaluation sur les graphes 59 60 Nom : 61 62 Prénom : 63 64 Classe : 65 """) 66 67 printboth("*(version générée N°%s)*"%ID) 68 69 # Exercice 1 70 71 nodes = ['Alice', 'Bob', 'Céline', 'Damien', 'Élodie'] 72 73 g = random_simple_connected_graph(nodes, 7) 74 75 printboth(""" 76 **Exercice 1** : Dessinez le graphe correspondant aux relations sociales suivantes : 77 """) 78 79 for edge in g.edges(): 80 printboth("- **{}** est ami{} avec **{}**".format(edge[0], 'e' if edge[0][-1]=='e' else '', edge[1])) 81 82 print(""" 83 \\ 84 85 \\ 86 87 \\ 88 89 \\ 90 """,file=out) 91 92 fname = draw_graph_file("ex1", g) 93 94 print("![](%s){ width=50%%}"%fname, file=outcorr) 95 96 # Exercice 2 97 98 printboth(""" 99 **Exercice 2** : Dans le graphe ci-dessous : 100 """) 101 102 g = random_simple_connected_graph("DEFGH", 8) 103 g.add_edge("A", choice("DFG")) 104 g.add_edge("B", choice("EH")) 105 g.add_edge("C", choice("DEFGH")) 106 107 printboth("- Donnez un chemin entre **A** et **B** : ", end="") 108 109 print("_______________", file=out) 110 print(nx.shortest_path(g, 'A', 'B'), file=outcorr) 111 112 printboth("- Quelle est la distance entre **A** et **C** (en nombre d'arêtes) ? ", end="") 113 print("___", file=out) 114 print(nx.shortest_path_length(g, 'A', 'C'), file=outcorr) 115 116 printboth("- Quelle est la distance entre **B** et **C** (en nombre d'arêtes) ? ", end="") 117 print("___", file=out) 118 print(nx.shortest_path_length(g, 'B', 'C'), file=outcorr) 119 120 fname = draw_graph_file("ex2", g) 121 122 print("\n![](%s){ width=70%% }"%fname, file=out) 123 124 print("\n![](%s){ width=50%% }"%fname, file=outcorr) 125 126 # Exercice 3 127 printboth(""" 128 **Exercice 3 (verso)** : Trouvez les diamètres, rayons, et centres des graphes suivants. 129 \pagebreak 130 """) 131 132 alpha = list("ABCDEFGHIJKLMNOP") 133 134 glist = [] 135 136 nodes = alpha[:5] 137 138 shuffle(nodes) 139 140 glist.append(random_simple_connected_graph(nodes, 5)) 141 glist.append(random_simple_connected_graph(nodes, 7)) 142 143 nodes = alpha[:6] 144 shuffle(nodes) 145 146 glist.append(random_simple_connected_graph(nodes, 8)) 147 glist.append(random_simple_connected_graph(nodes, 10)) 148 149 nodes = alpha[:7] 150 shuffle(nodes) 151 152 glist.append(random_simple_connected_graph(nodes, 11)) 153 glist.append(random_simple_connected_graph(nodes, 12)) 154 155 fname="" 156 157 for i in range(6): 158 oldfname = fname 159 fname = draw_graph_file(str(i), glist[i]) 160 161 if i%2==1: 162 printboth("| ![](%s) | ![](%s) |"%(oldfname, fname)) 163 printboth("|---|---|") 164 print("| Diamètre : ___ \\ \\ \\ Rayon : ___ Centre(s) : _____________ ", file=out, end="") 165 print("| Diamètre : ___ \\ \\ \\ Rayon : ___ Centre(s) : _____________ ", file=out, end="|\n") 166 print("| Diamètre :", diameter(glist[i-1]),"Rayon :", radius(glist[i-1]), "Centres :", sorted(center(glist[i-1])), file=outcorr, end=" ") 167 print("| Diamètre :", diameter(glist[i]),"Rayon :", radius(glist[i]), "Centres :", sorted(center(glist[i])), file=outcorr, end=" |\n") 168 169 # end = "" if i%2==0 else "|\n" 170 # printboth("| ![](%s)"%fname, end=end) 171 # print("| Diamètre : ___ Rayon : ___ Centre(s) : __________ ", file=out, end=end) 172 # print("| Diamètre : 1 Rayon : 1 Centre(s) : A,B,C ", file=outcorr, end=end) 173 printboth("") 174 175 176 177