-
Notifications
You must be signed in to change notification settings - Fork 2
/
CoursMagistral_4-5.py
1514 lines (1024 loc) · 50.2 KB
/
CoursMagistral_4-5.py
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# coding: utf-8
# # Table of Contents
# <p><div class="lev1 toc-item"><a href="#ALGO1-:-Introduction-à-l'algorithmique" data-toc-modified-id="ALGO1-:-Introduction-à-l'algorithmique-1"><span class="toc-item-num">1 </span><a href="https://perso.crans.org/besson/teach/info1_algo1_2019/" target="_blank">ALGO1 : Introduction à l'algorithmique</a></a></div><div class="lev1 toc-item"><a href="#Cours-Magistral-4-&-5" data-toc-modified-id="Cours-Magistral-4-&-5-2"><span class="toc-item-num">2 </span>Cours Magistral 4 & 5</a></div><div class="lev2 toc-item"><a href="#Type-abstrait-des-$\alpha$-graphes" data-toc-modified-id="Type-abstrait-des-$\alpha$-graphes-21"><span class="toc-item-num">2.1 </span>Type abstrait des <span class="MathJax_Preview" style="color: inherit;"></span><span class="MathJax" id="MathJax-Element-457-Frame" tabindex="0" style="position: relative;" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>&#x03B1;</mi></math>" role="presentation"><nobr aria-hidden="true"><span class="math" id="MathJax-Span-3266" style="width: 0.668em; display: inline-block;"><span style="display: inline-block; position: relative; width: 0.549em; height: 0px; font-size: 116%;"><span style="position: absolute; clip: rect(1.91em, 1000.55em, 2.596em, -1000em); top: -2.469em; left: 0em;"><span class="mrow" id="MathJax-Span-3267"><span class="mi" id="MathJax-Span-3268" style="font-family: STIXMathJax_Normal; font-style: italic;">𝛼<span style="display: inline-block; overflow: hidden; height: 1px; width: 0.005em;"></span></span></span><span style="display: inline-block; width: 0px; height: 2.469em;"></span></span></span><span style="display: inline-block; overflow: hidden; vertical-align: -0.057em; border-left: 0px solid; width: 0px; height: 0.614em;"></span></span></nobr><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>α</mi></math></span></span><script type="math/tex" id="MathJax-Element-457">\alpha</script> graphes</a></div><div class="lev3 toc-item"><a href="#Un-premier-exemple-de-graphe" data-toc-modified-id="Un-premier-exemple-de-graphe-211"><span class="toc-item-num">2.1.1 </span>Un premier exemple de graphe</a></div><div class="lev3 toc-item"><a href="#Graphe-aléatoire-de-taille-$n$" data-toc-modified-id="Graphe-aléatoire-de-taille-$n$-212"><span class="toc-item-num">2.1.2 </span>Graphe aléatoire de taille <span class="MathJax_Preview" style="color: inherit;"></span><span class="MathJax" id="MathJax-Element-386-Frame" tabindex="0" style="position: relative;" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>n</mi></math>" role="presentation"><nobr aria-hidden="true"><span class="math" id="MathJax-Span-2762" style="width: 0.614em; display: inline-block;"><span style="display: inline-block; position: relative; width: 0.518em; height: 0px; font-size: 118%;"><span style="position: absolute; clip: rect(1.866em, 1000.49em, 2.597em, -1000em); top: -2.448em; left: 0em;"><span class="mrow" id="MathJax-Span-2763"><span class="mi" id="MathJax-Span-2764" style="font-family: STIXMathJax_Normal; font-style: italic;">𝑛</span></span><span style="display: inline-block; width: 0px; height: 2.448em;"></span></span></span><span style="display: inline-block; overflow: hidden; vertical-align: -0.065em; border-left: 0px solid; width: 0px; height: 0.641em;"></span></span></nobr><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>n</mi></math></span></span><script type="math/tex" id="MathJax-Element-386">n</script></a></div><div class="lev2 toc-item"><a href="#Trois-différentes-implémentations" data-toc-modified-id="Trois-différentes-implémentations-22"><span class="toc-item-num">2.2 </span>Trois différentes implémentations</a></div><div class="lev3 toc-item"><a href="#Graphes-par-matrice-d'adjacence" data-toc-modified-id="Graphes-par-matrice-d'adjacence-221"><span class="toc-item-num">2.2.1 </span>Graphes par matrice d'adjacence</a></div><div class="lev3 toc-item"><a href="#Graphes-par-listes-d'adjacence" data-toc-modified-id="Graphes-par-listes-d'adjacence-222"><span class="toc-item-num">2.2.2 </span>Graphes par listes d'adjacence</a></div><div class="lev3 toc-item"><a href="#Graphes-par-liste-d'arêtes" data-toc-modified-id="Graphes-par-liste-d'arêtes-223"><span class="toc-item-num">2.2.3 </span>Graphes par liste d'arêtes</a></div><div class="lev2 toc-item"><a href="#Test-numérique-des-complexités-des-différentes-opérations" data-toc-modified-id="Test-numérique-des-complexités-des-différentes-opérations-23"><span class="toc-item-num">2.3 </span>Test numérique des complexités des différentes opérations</a></div><div class="lev3 toc-item"><a href="#Un-exemple" data-toc-modified-id="Un-exemple-231"><span class="toc-item-num">2.3.1 </span>Un exemple</a></div><div class="lev3 toc-item"><a href="#Tests-pour-différentes-tailles-de-graphes" data-toc-modified-id="Tests-pour-différentes-tailles-de-graphes-232"><span class="toc-item-num">2.3.2 </span>Tests pour différentes tailles de graphes</a></div><div class="lev3 toc-item"><a href="#Afficher-ces-mesures-de-temps-de-complexités" data-toc-modified-id="Afficher-ces-mesures-de-temps-de-complexités-233"><span class="toc-item-num">2.3.3 </span>Afficher ces mesures de temps de complexités</a></div><div class="lev2 toc-item"><a href="#Parcours-en-profondeur" data-toc-modified-id="Parcours-en-profondeur-24"><span class="toc-item-num">2.4 </span>Parcours en profondeur</a></div><div class="lev3 toc-item"><a href="#Version-récursive-:-vue-en-cours" data-toc-modified-id="Version-récursive-:-vue-en-cours-241"><span class="toc-item-num">2.4.1 </span>Version récursive : vue en cours</a></div><div class="lev3 toc-item"><a href="#Version-itérative-:-pas-vue-en-cours,-avec-une-pile" data-toc-modified-id="Version-itérative-:-pas-vue-en-cours,-avec-une-pile-242"><span class="toc-item-num">2.4.2 </span>Version itérative : pas vue en cours, avec une pile</a></div><div class="lev2 toc-item"><a href="#Application-:-composantes-connexes-d'un-graphe-non-orienté" data-toc-modified-id="Application-:-composantes-connexes-d'un-graphe-non-orienté-25"><span class="toc-item-num">2.5 </span>Application : composantes connexes d'un graphe non orienté</a></div><div class="lev2 toc-item"><a href="#Application-:-trouver-un-cycle-dans-un-graphe-non-orienté" data-toc-modified-id="Application-:-trouver-un-cycle-dans-un-graphe-non-orienté-26"><span class="toc-item-num">2.6 </span>Application : trouver un cycle dans un graphe non orienté</a></div><div class="lev2 toc-item"><a href="#Parcours-en-largeur" data-toc-modified-id="Parcours-en-largeur-27"><span class="toc-item-num">2.7 </span>Parcours en largeur</a></div><div class="lev3 toc-item"><a href="#Un-exemple-:" data-toc-modified-id="Un-exemple-:-271"><span class="toc-item-num">2.7.1 </span>Un exemple :</a></div><div class="lev3 toc-item"><a href="#Distance-des-plus-courts-chemins-avec-un-parcours-en-largeur" data-toc-modified-id="Distance-des-plus-courts-chemins-avec-un-parcours-en-largeur-272"><span class="toc-item-num">2.7.2 </span>Distance des plus courts chemins avec un parcours en largeur</a></div><div class="lev2 toc-item"><a href="#Algorithme-de-Dijkstra" data-toc-modified-id="Algorithme-de-Dijkstra-28"><span class="toc-item-num">2.8 </span>Algorithme de Dijkstra</a></div><div class="lev3 toc-item"><a href="#Algorithme-de-Dijkstra-naïf" data-toc-modified-id="Algorithme-de-Dijkstra-naïf-281"><span class="toc-item-num">2.8.1 </span>Algorithme de Dijkstra naïf</a></div><div class="lev3 toc-item"><a href="#File-de-priorité-min-:-implémentation-maison" data-toc-modified-id="File-de-priorité-min-:-implémentation-maison-282"><span class="toc-item-num">2.8.2 </span>File de priorité min : implémentation maison</a></div><div class="lev3 toc-item"><a href="#Algorithme-de-Dijkstra" data-toc-modified-id="Algorithme-de-Dijkstra-283"><span class="toc-item-num">2.8.3 </span>Algorithme de Dijkstra</a></div><div class="lev2 toc-item"><a href="#Algorithme-A*" data-toc-modified-id="Algorithme-A*-29"><span class="toc-item-num">2.9 </span>Algorithme A*</a></div><div class="lev2 toc-item"><a href="#Conclusion" data-toc-modified-id="Conclusion-210"><span class="toc-item-num">2.10 </span>Conclusion</a></div>
# # [ALGO1 : Introduction à l'algorithmique](https://perso.crans.org/besson/teach/info1_algo1_2019/)
#
# - [Page du cours](https://perso.crans.org/besson/teach/info1_algo1_2019/) : https://perso.crans.org/besson/teach/info1_algo1_2019/
# - Magistère d'Informatique de Rennes - ENS Rennes - Année 2019/2020
# - Intervenants :
# + Cours : [Lilian Besson](https://perso.crans.org/besson/)
# + Travaux dirigés : [Raphaël Truffet](http://perso.eleves.ens-rennes.fr/people/Raphael.Truffet/)
# - Références :
# + [Open Data Structures](http://opendatastructures.org/ods-python.pdf)
# # Cours Magistral 4 & 5
#
# - Ce cours traite de graphes.
# - On donne le type abstrait des graphes, et plusieurs implémentations de la même structure de données (plusieurs classes).
#
# - CM4 : On implémente le parcours en profondeur, qu'on illustre sur quelques exemples.
# - CM5 : On implémente le parcours en largeur, qu'on illustre sur quelques exemples.
# ----
# ## Type abstrait des $\alpha$ graphes
#
# On se donne un type $\alpha$ pour les sommets, et on va en fait se restreindre à $\alpha=$ `int`, et les sommets seront $\{0,\dots,n-1\}$ où $n = |S|$ pour des graphes $G = (S, A)$.
#
# On va écrire une classe qui implémente des opérations "plus haut niveau", en fonction des opérations bas niveau.
#
# Pour l'afficher, on va utiliser la librarie [networkx](https://networkx.github.io/documentation/stable/tutorial.html#drawing-graphs)
# In[140]:
import networkx as nx
# In[170]:
class BaseGraph():
def out_degree(self, vertex):
return len(self.succ(vertex))
def in_degree(self, vertex):
return len(self.pred(vertex))
def degree(self, vertex):
return len(self.neighbors(vertex))
def is_vertex(self, vertex):
""" Test presence of a vertex."""
return vertex in self.vertexes
@property
def vertexes(self):
""" List of vertexes."""
return list(range(self.nb_vertexes))
def is_neighbor(self, u, v):
""" Test neighborhood."""
return u in self.neighbors(v)
@property
def edges(self):
""" Set of edges (pairs), in O(|A|) if well implemented."""
return {(u, v) for u in self.vertexes for v in self.neighbors(u)}
@property
def nb_edges(self):
return len(self.edges)
def draw(self):
G = nx.DiGraph() if self.oriented else nx.Graph()
G.add_nodes_from(self.vertexes)
G.add_edges_from(self.edges)
return nx.draw_kamada_kawai(G, with_labels=True, font_weight='bold')
# ### Un premier exemple de graphe
#
# On va travailler avec le graphe exemple suivant, qu'il soit orienté ou non :
#
# | Orienté | Non orienté |
# |---------|-------------|
# | <img width="75%" src="figures/CM4_ExampleDiGraph.png"> | <img width="75%" src="figures/CM4_ExampleGraph.png?"> |
# In[183]:
def defaultGraph(GraphClass, oriented=True):
print(f"Creating empty graph with class {GraphClass}...")
graph = GraphClass(oriented=oriented)
n = 7
for i in range(n):
print(f"Adding vertex {i}...")
graph.add_vertex(i)
for edge in [(0, 1), (0, 2), (1, 3), (1, 4), (2, 5), (2, 6)]:
print(f"Adding edge {edge}...")
graph.add_edge(*edge)
return graph
# In[184]:
plt.figure()
defaultGraph(AdjMatrixGraph, oriented=True).draw()
plt.figure()
defaultGraph(AdjMatrixGraph, oriented=False).draw()
# On va tester les différentes implémentations avec la petite fonction suivante, qui vérifie que l'on peut accéder à toute l'information contenu dans le graphe.
# In[143]:
def test_defaultGraph(GraphClass):
for oriented in [True, False]:
graph = defaultGraph(GraphClass, oriented=oriented)
print(f"Graph:")
print(graph)
print(f"Number of vertexes: {graph.nb_vertexes}")
print(f"Is the graph oriented? {graph.oriented}")
print(f"Number of edges: {graph.nb_edges}")
print(f"List of vertexes: {graph.vertexes}")
print(f"List of edges: {graph.edges}")
for i in graph.vertexes:
print(f" List of neighbors of {i}: {graph.neighbors(i)} (degree {graph.degree(i)})")
print(f" List of succ of {i}: {graph.succ(i)} (out degree {graph.out_degree(i)})")
print(f" List of pred of {i}: {graph.pred(i)} (in degree {graph.in_degree(i)})")
# ### Graphe aléatoire de taille $n$
#
# On va étudier un graphe aléatoire suivant un modèle très simple : on fixe $n$ le nombre de sommets, et ensuite chaque arête $(i, j)$ est ajoutée dans le graphe avec une probabilité $p\in(0,1)$ fixée ([graphe d'Erdös-Rényi](https://en.wikipedia.org/wiki/Erd%C5%91s-R%C3%A9nyi_model)).
# In[144]:
import random
# In[145]:
def with_probability(p):
return random.random() <= p
# In[146]:
def randomGraph(GraphClass, n=10, probability=0.1, oriented=True):
graph = GraphClass(oriented=oriented)
for i in range(n):
graph.add_vertex(i)
for i in range(n):
for j in range(n):
if with_probability(probability):
graph.add_edge(i, j)
return graph
# In[147]:
print(randomGraph(AdjMatrixGraph, 10, 0.1, oriented=True))
# In[189]:
plt.figure(figsize=(4, 3))
randomGraph(AdjMatrixGraph, 10, 0.1, oriented=True).draw()
# In[190]:
plt.figure(figsize=(4, 3))
randomGraph(AdjMatrixGraph, 10, 0.5, oriented=True).draw()
# In[191]:
plt.figure(figsize=(4, 3))
randomGraph(AdjMatrixGraph, 10, 0.5, oriented=False).draw()
# In[192]:
plt.figure(figsize=(4, 3))
randomGraph(AdjMatrixGraph, 10, 0.9, oriented=True).draw()
# ----
# ## Trois différentes implémentations
# ### Graphes par matrice d'adjacence
# In[173]:
import numpy as np
# In[174]:
class AdjMatrixGraph(BaseGraph):
def __init__(self, oriented=True, n=0):
""" Takes O(n^2) time and space."""
self.oriented = oriented
self.nb_vertexes = n
self._matrix = np.zeros((n, n), dtype=bool)
def __str__(self):
return str(self._matrix)
def is_vertex(self, vertex):
""" Test presence of a vertex."""
return 0 <= vertex < self.nb_vertexes
def add_vertex(self, m):
""" Worst case is O(m^2) to extend the matrix. Best case is O(1) if nothing to do."""
if not self.is_vertex(m):
n = self.nb_vertexes
assert 0 <= m and n <= m
self.nb_vertexes = m + 1
old_matrix = self._matrix[:,:]
# extend the matrix
self._matrix = np.zeros((m + 1, m + 1), dtype=bool)
# copy the old matrix
self._matrix[:n, :n] = old_matrix
def add_edge(self, i, j):
""" O(1) time."""
assert 0 <= i < self.nb_vertexes and 0 <= j < self.nb_vertexes
self._matrix[i, j] = True
if not self.oriented:
self._matrix[j, i] = True
def remove_vertex(self, m):
""" TODO do it yourself, it's not hard!"""
raise NotImplementedError
def remove_edge(self, i, j):
""" O(1) time."""
assert 0 <= i < self.nb_vertexes and 0 <= j < self.nb_vertexes
self._matrix[i, j] = False
if not self.oriented:
self._matrix[j, i] = False
def merge_vertexes(self, i, j):
""" TODO do it yourself, it's not hard!"""
raise NotImplementedError
def pred(self, i):
assert 0 <= i < self.nb_vertexes
return [j for j in self.vertexes if self.is_neighbor(j, i)]
def is_pred(self, u, v):
""" O(1) time."""
return self._matrix[v, u]
def succ(self, i):
assert 0 <= i < self.nb_vertexes
return [j for j in self.vertexes if self.is_neighbor(i, j)]
def is_succ(self, u, v):
""" O(1) time."""
return self._matrix[u, v]
def neighbors(self, i):
assert 0 <= i < self.nb_vertexes
if self.oriented:
return self.succ(i)
else:
return [j for j in self.vertexes if self.is_neighbor(i, j) or self.is_neighbor(j, i)]
def is_neighbor(self, u, v):
""" O(1) time."""
if self.oriented:
return self._matrix[u, v]
else:
return self._matrix[u, v] or self._matrix[v, u]
# Testons cette première implémentation :
# In[175]:
defaultGraph(AdjMatrixGraph)
# In[176]:
test_defaultGraph(AdjMatrixGraph)
# ### Graphes par listes d'adjacence
# In[177]:
class AdjListsGraph(BaseGraph):
def __init__(self, oriented=True, n=0):
""" Takes O(n) time and space to allocate the empty lists."""
self.oriented = oriented
self.nb_vertexes = n
self._lists = [ [] for i in range(n) ]
def __str__(self):
return str(self._lists)
def is_vertex(self, vertex):
""" Test presence of a vertex."""
return 0 <= vertex < self.nb_vertexes
def add_vertex(self, m):
""" Worst case is O(m^2) to extend the matrix. Best case is O(1) if nothing to do."""
if not self.is_vertex(m):
n = self.nb_vertexes
assert 0 <= m and n <= m
self.nb_vertexes = m + 1
self._lists = [ [] if i >= n else self._lists[i] for i in range(m + 1) ]
def add_edge(self, i, j):
""" O(1) time: append j in head of list of neighbors of i."""
assert 0 <= i < self.nb_vertexes and 0 <= j < self.nb_vertexes
if not self.is_neighbor(i, j):
self._lists[i].append(j)
if not self.oriented:
if not self.is_neighbor(j, i):
self._lists[j].append(i)
def remove_vertex(self, m):
""" TODO do it yourself, it's not hard!"""
raise NotImplementedError
def remove_edge(self, i, j):
""" O(1) time."""
assert 0 <= i < self.nb_vertexes and 0 <= j < self.nb_vertexes
self._lists[i].remove(j)
if not self.oriented:
self._lists[j].remove(i)
def merge_vertexes(self, i, j):
""" TODO do it yourself, it's not hard!"""
raise NotImplementedError
def pred(self, i):
""" Not trivial, has to check all lists, in O(|S|*|A|)."""
assert 0 <= i < self.nb_vertexes
return [j for j in self.vertexes if self.is_pred(j, i)]
def is_pred(self, u, v):
""" O(|S|) time in worst case."""
return u in self._lists[v]
def succ(self, i):
""" Create a new list, to be sure that we don't modify the underlying self._lists. In O(deg(i))."""
assert 0 <= i < self.nb_vertexes
return list(self._lists[i])
def is_succ(self, u, v):
""" O(|S|) time in worst case."""
return v in self._lists[u]
def neighbors(self, i):
assert 0 <= i < self.nb_vertexes
if self.oriented:
return list(self.succ(i))
else:
return [j for j in self.vertexes if self.is_neighbor(i, j)]
def is_neighbor(self, u, v):
""" O(|S|) time in worst case."""
if self.oriented:
return v in self._lists[u]
else:
return v in self._lists[u] or u in self._lists[v]
# Testons cette première implémentation :
# In[178]:
defaultGraph(AdjListsGraph)
# In[179]:
test_defaultGraph(AdjListsGraph)
# ### Graphes par liste d'arêtes
# In[180]:
class EdgesListGraph(BaseGraph):
def __init__(self, oriented=True, n=0):
""" Takes O(n) time and space to allocate the empty lists."""
self.oriented = oriented
self.nb_vertexes = n
self._edges = set()
def __str__(self):
return str(self._edges)
def is_vertex(self, vertex):
""" Test presence of a vertex."""
return 0 <= vertex < self.nb_vertexes
def add_vertex(self, m):
""" Worst case is O(m^2) to extend the matrix. Best case is O(1) if nothing to do."""
if not self.is_vertex(m):
n = self.nb_vertexes
assert 0 <= m and n <= m
self.nb_vertexes = m + 1
def add_edge(self, i, j):
""" O(1) time: append j in head of list of neighbors of i."""
assert 0 <= i < self.nb_vertexes and 0 <= j < self.nb_vertexes
if not self.is_neighbor(i, j):
self._edges.add((i, j))
if not self.oriented:
if not self.is_neighbor(j, i):
self._edges.add((j, i))
def remove_vertex(self, m):
""" TODO do it yourself, it's not hard!"""
raise NotImplementedError
def remove_edge(self, i, j):
""" O(1) time."""
assert 0 <= i < self.nb_vertexes and 0 <= j < self.nb_vertexes
self._edges.remove((i, j))
if not self.oriented:
self._edges.remove((j, i))
def merge_vertexes(self, i, j):
""" TODO do it yourself, it's not hard!"""
raise NotImplementedError
def pred(self, i):
assert 0 <= i < self.nb_vertexes
return [j for j in self.vertexes if self.is_pred(j, i)]
def is_pred(self, u, v):
""" O(|S|) time in worst case."""
return (v, u) in self._edges
def succ(self, i):
assert 0 <= i < self.nb_vertexes
return [j for j in self.vertexes if self.is_succ(i, j)]
def is_succ(self, u, v):
""" O(|S|) time in worst case."""
return (u, v) in self._edges
def neighbors(self, i):
assert 0 <= i < self.nb_vertexes
return [j for j in self.vertexes if self.is_neighbor(j, i)]
def is_neighbor(self, u, v):
""" O(|S|) time in worst case."""
if self.oriented:
return (u, v) in self._edges
else:
return (u, v) in self._edges or (v, u) in self._edges
# Testons cette première implémentation :
# In[181]:
defaultGraph(EdgesListGraph)
# In[182]:
test_defaultGraph(EdgesListGraph)
# ----
# ## Test numérique des complexités des différentes opérations
#
# On rappelle qu'on devrait obtenir les résultats suivants, avec $n=|S|$ et $m=|A|$, que l'on va valider expérimentalement.
#
# | Opérations | Matrice d'adjacence | Listes d'adjacence | Liste d'arêtes |
# |:-----------|---------------------|--------------------|----------------|
# | Création (vide) | temps et mémoire $O(n^2)$ si vide | temps et mémoire $O(n)$ si vide | temps et mémoire $O(1)$ si vide |
# | Ajoute un sommet $u$ | $O(n^2)$ (recopie) | $O(1)$ | $O(1)$ |
# | Retire un sommet $u$ | $O(n^2)$ (recopie) | $O(d(u))$ si orienté, $O(n+m)$ sinon | $O(n)$ (suppression des arêtes) |
# | Ajoute un arc $(u,v)$ | $O(1)$ | $O(d(u))$ si orienté, $O(d(u)+d(v))$ sinon | $O(1)$ (si liste d'arêtes) ou $O(1)$ en amorti (si ensemble d'arêtes) |
# | Retire un arc $(u,v)$ | $O(1)$ | $O(d(u))$ si orienté, $O(d(u)+d(v))$ sinon | $O(n)$ (si liste d'arêtes) ou $O(1)$ en amorti (si ensemble d'arêtes) |
# | Liste des sommets | $O(n)$ | $O(n)$ | $O(n)$ |
# | Liste des arcs | $O(n^2)$ tout parcourir | $O(n)$ parcourir les $n$ listes de tailles $d(u)$, et $\sum_u d(u) = n$ | $O(1)$ (si liste d'arêtes) ou $O(n)$ (si ensemble d'arêtes) |
# | Liste des voisins du nœud $u$ | $O(n)$ | $O(d(u))$ ($O(1)$ si on ne crée pas de nouvelle liste) | $O(n)$ |
# | Degré du nœud $u$ | $O(n)$ | $O(n)$ | $O(n)$ |
# | Liste des voisins sortant du nœud $u$ | $O(n)$ | $O(n)$ | $O(n)$ |
# | Degré sortant du nœud $u$ | $O(n)$ | $O(n)$ | $O(n)$ |
# | Liste des voisins entrant du nœud $u$ | $O(n)$ | $O(n)$ | $O(n)$ |
# | Degré entrant du nœud $u$ | $O(n)$ | $O(n)$ | $O(n)$ |
#
# In[62]:
try:
from tqdm import tqdm_notebook
except ImportError:
def tqdm_notebook(iterator, *args, **kwargs):
return iterator
# In[64]:
def random_vertex(n):
return random.randint(0, n+1)
def random_edge(n):
return (random_vertex(n), random_vertex(n))
# ### Un exemple
# In[116]:
probability = 0.5
n = 1000
graph = randomGraph(AdjMatrixGraph, n=n, probability=probability)
get_ipython().run_line_magic('timeit', 'graph.is_vertex(random_vertex(n))')
get_ipython().run_line_magic('timeit', 'graph.add_vertex(n + 5)')
get_ipython().run_line_magic('timeit', 'graph.add_edge(*random_edge(n))')
get_ipython().run_line_magic('timeit', 'graph.remove_edge(*random_edge(n))')
get_ipython().run_line_magic('timeit', 'graph.pred(random_vertex(n))')
get_ipython().run_line_magic('timeit', 'graph.is_pred(*random_edge(n))')
get_ipython().run_line_magic('timeit', 'graph.succ(random_vertex(n))')
get_ipython().run_line_magic('timeit', 'graph.is_succ(*random_edge(n))')
get_ipython().run_line_magic('timeit', 'graph.neighbors(random_vertex(n))')
get_ipython().run_line_magic('timeit', 'graph.is_neighbor(*random_edge(n))')
# Sans voir l'évolution en fonction de $n$, difficile de conclure quoi que ce soit de ces premières expériences…
# ### Tests pour différentes tailles de graphes
#
# On va stocker les temps de calculs dans une petite structure de la forme suivante, qui permettra d'afficher directement des courbes (le traitement est fait plus bas).
# In[80]:
times = {
# clé sur n
"100": {
# clé sur p
r"|A| \simeq |S|": {
"AdjMatrixGraph": {
"operation1": 0.12,
# ...,
"operationN": 0.12,
},
"AdjListsGraph": {
"operation1": 0.12,
# ...,
"operationN": 0.12,
},
"EdgesListGraph": {
"operation1": 0.12,
# ...,
"operationN": 0.12,
},
},
},
}
# In[117]:
import timeit
# In[118]:
times = {}
for n in tqdm_notebook([100, 200, 400, 800, 1000, 1500, 2000, 2500, 3000, 3500, 4000, 4500, 5000], desc="n"):
number = 20 if n <= 200 else 5 # nb of repetitions of each operations
# print(f"\n\n For graphs with {n} vertexes:")
times[n] = {}
for probability, pname in tqdm_notebook([
(1.0/n, r"|A| \simeq |S|"), # p = 1/n => |A| ~= |S|
(1.0/np.sqrt(n), r"|A| \simeq |S|^{3/2}"), # p = 1/sqrt(n) => |A| ~= |S|^(3/2)
(0.1, r"|A| \simeq 0.1 |S|^2"), # p = 0.1 => |A| ~= 0.1 |S|^2
], desc="proba"):
times[n][pname] = {}
# print(f"\n and link probability of {probability}:")
for GraphClass in [AdjMatrixGraph, AdjListsGraph, EdgesListGraph]:
# print(f"\n for class {GraphClass}...")
graph = randomGraph(GraphClass, n=n, probability=probability)
the_times = {}
the_times["randomGraph"] = timeit.timeit(
"randomGraph(GraphClass, n=n, probability=probability)",
globals=globals(), number=number,
)
# print("Time to create a new graph:", the_times["randomGraph"])
the_times["is_vertex"] = timeit.timeit(
"graph.is_vertex(random_vertex(n))",
globals=globals(), number=number,
)
# print("Time to test presence of a vertex:", the_times["is_vertex"])
the_times["add_vertex"] = timeit.timeit(
"graph.add_vertex(n + 2)",
globals=globals(), number=number,
)
# print("Time to add the next vertex n + 2:", the_times["add_vertex"])
the_times["pred"] = timeit.timeit(
"graph.pred(random_vertex(n))",
globals=globals(), number=number,
)
# print("Time to compute pred:", the_times["pred"])
the_times["is_pred"] = timeit.timeit(
"graph.is_pred(*random_edge(n))",
globals=globals(), number=number,
)
# print("Time to test pred:", the_times["is_pred"])
the_times["succ"] = timeit.timeit(
"graph.succ(random_vertex(n))",
globals=globals(), number=number,
)
# print("Time to compute succ:", the_times["succ"])
the_times["is_succ"] = timeit.timeit(
"graph.is_succ(*random_edge(n))",
globals=globals(), number=number,
)
# print("Time to test succ:", the_times["is_succ"])
the_times["neighbors"] = timeit.timeit(
"graph.neighbors(random_vertex(n))",
globals=globals(), number=number,
)
# print("Time to compute neighbors:", the_times["neighbors"])
the_times["is_neighbor"] = timeit.timeit(
"graph.is_neighbor(*random_edge(n))",
globals=globals(), number=number,
)
# print("Time to test neighbors:", the_times["is_neighbor"])
the_times["add_edge"] = timeit.timeit(
"graph.add_edge(*random_edge(n))",
globals=globals(), number=number,
)
# print("Time to add an edge:", the_times["add_edge"])
times[n][pname][str(GraphClass)] = the_times
# ### Afficher ces mesures de temps de complexités
# In[103]:
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(context="notebook", style="whitegrid", palette="hls", font="sans-serif", font_scale=1.1)
import matplotlib as mpl
mpl.rcParams['figure.figsize'] = (10, 7)
mpl.rcParams['figure.dpi'] = 120
# Il faut écrire une fonction qui va extraire les données de ce `times`, et les afficher.
#
# - J'ai choisi d'afficher une courbe différente pour chaque valeur de $p$, et de structure de données,
# - Et sur chaque courbe, il y aura $n$ le nombre de sommets du graphe en abscisse, le temps (en milli secondes) en ordonnées, et des courbes pour chaque opérations.
# In[193]:
def plotComplexitiesOfOperations(times):
values_n = list(times.keys())
values_p = list(times[values_n[0]].keys())
values_class = list(times[values_n[0]][values_p[0]].keys())
values_opname = list(times[values_n[0]][values_p[0]][values_class[0]].keys())
for class_name in values_class:
for pname in values_p:
data = {
n: times[n][pname][class_name]
for n in values_n
}
fig = plt.figure()
for opname in values_opname:
plt.semilogy(values_n, [ 1e6 * data[n][opname] for n in values_n ],
label=opname,
marker='o', lw=3, ms=12, alpha=0.7,
)
plt.xlabel("Values of $n$")
plt.ylabel("Measured time of operations (in milli-seconds)")
name = class_name.replace("<class '__main__.", "").replace("'>", "")
plt.title(f"For graphs with class {name}, and $p={pname}$")
plt.legend()
plt.show()
return fig
# On vérifie cela :
# In[194]:
_ = plotComplexitiesOfOperations(times)
# Il faut écrire une fonction qui va extraire les données de ce `times`, et les afficher.
#
# - J'ai choisi d'afficher une courbe différente pour chaque valeur de $p$, et de structure de données,
# - Et sur chaque courbe, il y aura $n$ le nombre de sommets du graphe en abscisse, le temps (en milli secondes) en ordonnées, et des courbes pour chaque opérations.
# In[195]:
def plotComplexitiesOfOperations2(times):
values_n = list(times.keys())
values_p = list(times[values_n[0]].keys())
values_class = list(times[values_n[0]][values_p[0]].keys())
values_opname = list(times[values_n[0]][values_p[0]][values_class[0]].keys())
for opname in values_opname:
for pname in values_p:
fig = plt.figure()
for class_name in values_class:
name = class_name.replace("<class '__main__.", "").replace("'>", "")
plt.plot(values_n, [ times[n][pname][class_name][opname] for n in values_n ],
label=name, marker='o',
lw=3, ms=12, alpha=0.8)
plt.xlabel("Values of $n$")
plt.ylabel("Measured time of operations (in seconds)")
plt.title(f"For the operation {opname}, and ${pname}$")
plt.legend()
plt.show()
return fig
# On vérifie cela :
# In[196]:
_ = plotComplexitiesOfOperations2(times)
# ----
# ## Parcours en profondeur
#
# On va simplement implémenter l'algorithme donné en cours, avec deux fonctions génériques `post_visit` et `pre_visit`.
# ### Version récursive : vue en cours
# Un exemple :
# In[219]:
random.seed(12)
graph = randomGraph(AdjMatrixGraph, 10, 0.05, oriented=False)
# In[220]:
plt.figure(figsize=(4, 3))
graph.draw()
# In[221]:
dfs_recursive(graph, 0)
# Ici on a pu vérifier que sur cet exemple de graphe, $0, 4, 5, 9, 6$ sont dans la même composante connexe.
# ### Version itérative : pas vue en cours, avec une pile
# In[228]:
def pre_visit(u):
print(f"Previsit of u = {u}")
# In[229]:
def post_visit(u):
print(f"Postvisit of u = {u}")
# In[230]:
def dfs_iterative(graph, start, seen=None):
""" DFS, detect connected component, iterative implementation.
- graph: directed graph (any of the class defined above)
- node: from where start graph exploration
- seen (bool array): will be set true for the connected component containing node.
- Complexity: O(|S|+|A|).
"""
if seen is None:
seen = [False for _ in range(graph.nb_vertexes)]
seen[start] = True
to_visit_next = [start]
while to_visit_next: # while stack is not empty
node = to_visit_next.pop() # head of the stack / tête de la pile, O(1)
pre_visit(node)
for neighbor in graph.neighbors(node):
if not seen[neighbor]:
seen[neighbor] = True
to_visit_next.append(neighbor) # add to the stack, O(1)
post_visit(node) # /!\ not the same order as for the recursive function!
return seen
# Un exemple :
# In[231]:
random.seed(12)
graph = randomGraph(AdjMatrixGraph, 10, 0.05, oriented=False)
# In[234]:
plt.figure(figsize=(4, 3))
graph.draw()
# In[235]:
dfs_iterative(graph, 0)
# Ici on a pu vérifier que sur cet exemple de graphe, $0, 4, 5, 9, 6$ sont dans la même composante connexe.
# ----
# ## Application : composantes connexes d'un graphe non orienté
# In[261]:
def find_connected_components(graph):
""" Find all the connected components of a graph.
- graph: undirected graph (any of the class defined above)
- returns: list of vertices in a cycle, or None
- Complexity: O(|S|+|A|).
"""
n = graph.nb_vertexes
seen = [False for _ in range(n)]
# all nodes start by having their unique connected components
representants = [i for i in range(n)] # maps i to a representant of its connected components
start = -1
while not all(seen):
start += 1
if seen[start]:
continue
seen[start] = True
to_visit_next = [start]
while to_visit_next: # while stack is not empty
node = to_visit_next.pop() # head of the stack / tête de la pile, O(1)
for neighbor in graph.neighbors(node):
if not seen[neighbor]:
seen[neighbor] = True
representants[neighbor] = start
to_visit_next.append(neighbor) # add to the stack, O(1)
# now we can build the list of set of all connected components
list_of_connected_components = [
{ i for i in range(n) if representants[i] == representant }
for representant in set(representants)
]
return list_of_connected_components, representants
# Un exemple :
# In[262]:
random.seed(12)
graph = randomGraph(AdjMatrixGraph, 10, 0.05, oriented=False)
# In[263]:
plt.figure(figsize=(4, 3))
graph.draw()
# In[264]:
find_connected_components(graph)
# Ici on a pu vérifier que sur cet exemple de graphe, $0, 4, 5, 9, 6$ sont dans la même composante connexe, que $1$ et $8$ sont isolés, et que $2, 3, 7$ sont dans une dernière composante connexe.
# ----
# ## Application : trouver un cycle dans un graphe non orienté
# In[269]:
def find_cycle(graph):
""" Find a cycle in an undirected graph
- graph: undirected graph (any of the class defined above)
- returns: list of vertices in a cycle, or None
- Complexity: O(|S|+|A|).
"""
n = graph.nb_vertexes
prec = [None] * n # ancestor marks for visited vertices
for u in range(n):
if prec[u] is None: # unvisited vertex
to_visit_next = [u] # start new DFS
prec[u] = u # mark root (not necessary for this algorithm)
while to_visit_next:
u = to_visit_next.pop()
for v in graph.neighbors(u): # for all neighbors
if v != prec[u]: # except arcs to father in DFS tree
if prec[v] is not None:
cycle = [v, u] # cycle found, (u,v) back edge
while u not in (prec[v], prec[u]): # directed
u = prec[u] # climb up the tree
cycle.append(u)
return cycle
else:
prec[v] = u # v is new vertex in tree
to_visit_next.append(v)
# Un exemple :
# In[270]:
random.seed(12)
graph = randomGraph(AdjMatrixGraph, 10, 0.05, oriented=False)
# In[271]:
plt.figure(figsize=(4, 3))
graph.draw()
# In[272]:
find_cycle(graph)
# Ici on a pu vérifier que sur cet exemple de graphe, $[6, 9, 0]$ est bien un cycle.
# In[282]:
random.seed(123)
graph = randomGraph(AdjMatrixGraph, 5, 0.05, oriented=False)
# In[283]:
plt.figure(figsize=(4, 3))
graph.draw()
# In[284]: