forked from MrEntity303/ST0853-ASD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Graph.java
1052 lines (1001 loc) · 46.7 KB
/
Graph.java
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
/**
*
*/
package it.unicam.cs.asdl2122.pt1;
import java.util.Set;
/**
* Classe astratta per un generico grafo i cui nodi sono etichettati con
* elementi della classe {@code L}. Le classi {@code GraphNode<L>} e
* {@code GraphEdge<L>} definiscono le operazioni generiche sui nodi e sugli
* archi.
*
* Il grafo può essere orientato o non orientato, la sottoclasse che estende
* questa classe astratta specifica questo aspetto. Tale informazione è
* disponibile tramite il metodo {@code isDirected()}.
*
* Le etichette dei nodi sono obbligatorie e uniche, cioè un nodo non può avere
* etichetta nulla e due nodi con la stessa etichetta sono lo stesso nodo.
*
* @author Luca Tesei
*
* @param <L>
* tipo delle etichette dei nodi del grafo
*
*/
public abstract class Graph<L> {
/**
* Restituisce il numero di nodi in questo grafo.
*
* @return il numero di nodi in questo grafo.
*/
public abstract int nodeCount();
/**
* Restituisce il numero di archi in questo grafo.
*
* @return il numero di archi in questo grafo.
*/
public abstract int edgeCount();
/**
* Restituisce la dimensione di questo grafo definita come il numero di nodi
* più il numero di archi.
*
* @return la dimensione di questo grafo definita come il numero dei nodi
* più il numero degli archi.
*/
public int size() {
return this.nodeCount() + this.edgeCount();
}
/**
* Determina se questo grafo è vuoto, cioè senza nodi e senza archi.
*
* @return se questo grafo è vuoto, false altrimenti.
*/
public boolean isEmpty() {
// se non ci sono nodi non ci possono essere neanche archi
return this.nodeCount() == 0;
}
/**
* Cancella tutti i nodi e gli archi di questo grafo portandolo a essere un
* grafo vuoto.
*/
public abstract void clear();
/**
* Determina se questo grafo è orientato oppure no.
*
* @return true se questo grafo è orientato, false altrimenti.
*/
public abstract boolean isDirected();
/**
* Aggiunge un nodo a questo grafo.
*
* @param node
* il nuovo nodo da aggiungere
* @return true se il nodo è stato aggiunto, false altrimenti cioè se il
* nodo è già presente
* @throws NullPointerException
* se il nodo passato è null
*/
public abstract boolean addNode(GraphNode<L> node);
/**
* Aggiunge un nodo a questo grafo etichettandolo con una etichetta data.
*
* @param label
* l'etichetta del nuovo nodo da aggiungere
* @return true se il nodo è stato aggiunto, false altrimenti cioè se il
* nodo è già presente
* @throws NullPointerException
* se l'etichetta passata è null
*/
public abstract boolean addNode(L label);
/**
* Rimuove un nodo da questo grafo. Tutti gli archi collegati al nodo
* vengono anch'essi eliminati.
*
* Questa operazione è opzionale.
*
* @param node
* il nodo da rimuovere
* @throws IllegalArgumentException
* se il nodo passato non esiste
* in questo grafo
* @throws NullPointerException
* se il nodo passato è null
*
* @throws UnsupportedOperationException
* se l'implementazione del grafo
* non supporta questa operazione
*/
public abstract void removeNode(GraphNode<L> node);
/**
* Rimuove il nodo etichettato con una etichetta data da questo grafo. Tutti
* gli archi collegati al nodo vengono anch'essi eliminati.
*
* Questa operazione è opzionale.
*
* @param label
* l'etichetta del nodo da rimuovere
* @throws IllegalArgumentException
* se l'etichetta data non esiste
* in nessun nodo di questo grafo
* @throws NullPointerException
* se l'etichetta passata è null
*
* @throws UnsupportedOperationException
* se l'implementazione del grafo
* non supporta questa operazione
*/
public abstract void removeNode(L label);
/**
* Rimuove il nodo con un certo indice da questo grafo. Tutti gli archi
* collegati al nodo vengono anch'essi eliminati.
*
* Questa operazione è opzionale.
*
* @param i
* l'indice del nodo da rimuovere
* @throws IndexOutOfBoundsException
* se l'indice passato non
* corrisponde a nessun nodo o è
* fuori dai limiti
* dell'intervallo
* <code>[0, this.nodeCount() - 1]</code>
*
* @throws UnsupportedOperationException
* se l'implementazione del grafo
* non supporta questa operazione
*/
public abstract void removeNode(int i);
/**
* Restituisce il nodo di questo grafo che risulta uguale al nodo passato.
*
* @param node
* il nodo da restituire
*
* @return il nodo di questo grafo che risulta uguale al nodo passato, null
* se tale nodo non esiste in questo grafo
*
* @throws NullPointerException
* se il nodo passato è nullo
*
*/
public abstract GraphNode<L> getNode(GraphNode<L> node);
/**
* Restituisce il nodo di questo grafo avente l'etichetta passata.
*
* @param label
* l'etichetta del nodo da restituire
*
* @return il nodo di questo grafo che ha l'etichetta uguale a
* {@code label}, null se tale nodo non esiste in questo grafo
*
* @throws NullPointerException
* se l'etichetta è nulla
*
*/
public abstract GraphNode<L> getNode(L label);
/**
* Restituisce il nodo attualmente associato a un certo indice
* nell'intervallo <code>[0, this.nodeCount() - 1]</code>. Questa
* funzionalità è tipicamente disponibile se il grafo è rappresentato con
* una matrice di adiacenza.
*
* Questa operazione è opzionale.
*
* @param i
* l'indice da ricercare
* @return il nodo correntemente associato all'indice i
*
* @throws IndexOutOfBoundsException
* se l'indice passato non
* corrisponde a nessun nodo o è
* fuori dai limiti
* dell'intervallo
* <code>[0, this.nodeCount() - 1]</code>
* @throws UnsupportedOperationException
* se questa operazione non è
* supportata dall'implementazione
* di questo grafo
*/
public abstract GraphNode<L> getNode(int i);
/**
* Restituisce l'indice unico attualmente associato in questo grafo a un
* nodo dato. L'indice sarà nell'intervallo
* <code>[0, this.nodeCount() - 1]</code>. Questa funzionalità è tipicamente
* disponibile se il grafo è rappresentato con una matrice di adiacenza.
* Tale indice può anche essere usato per identificare i nodi in strutture
* dati esterne come array o liste che contengono informazioni aggiuntive
* calcolate, ad esempio, da un algoritmo sul grafo.
*
* Questa operazione è opzionale.
*
* @param node
* il nodo di cui restituire l'indice
*
* @return Un indice unico nell'intervallo
* <code>[0, this.nodeCount() - 1]</code> attualmente associato al
* nodo {@code node}. L'indice può cambiare se il grafo viene
* modificato togliendo dei nodi
*
* @throws NullPointerException
* se il nodo passato è null
* @throws IllegalArgumentException
* se il nodo passato non esiste
* in questo grafo
*
* @throws UnsupportedOperationException
* se questa operazione non è
* supportata dall'implementazione
* di questo grafo
*/
public abstract int getNodeIndexOf(GraphNode<L> node);
/**
* Restituisce l'indice unico attualmente associato in questo grafo al nodo
* etichettato con l'etichetta data. L'indice sarà nell'intervallo
* <code>[0, this.nodeCount() - 1]</code>. Questa funzionalità è tipicamente
* disponibile se il grafo è rappresentato con una matrice di adiacenza.
* Tale indice può anche essere usato per identificare i nodi in strutture
* dati esterne come array o liste che contengono informazioni aggiuntive
* calcolate, ad esempio, da un algoritmo sul grafo.
*
* Questa operazione è opzionale.
*
* @param label
* l'etichetta del nodo di cui restituire l'indice
*
* @return Un indice unico nell'intervallo
* <code>[0, this.nodeCount() - 1]</code> attualmente associato al
* nodo con etichetta {@code label}. L'indice può cambiare se il
* grafo viene modificato togliendo dei nodi
*
* @throws NullPointerException
* se l'etichetta passata è null
* @throws IllegalArgumentException
* se un nodo con l'etichetta
* {@code label} non esiste in
* questo grafo
*
* @throws UnsupportedOperationException
* se questa operazione non è
* supportata dall'implementazione
* di questo grafo
*/
public abstract int getNodeIndexOf(L label);
/**
* Restituisce l'insieme dei nodi di questo grafo.
*
* @return l'insieme dei nodi di questo grafo, possibilmente vuoto.
*/
public abstract Set<GraphNode<L>> getNodes();
/**
* Aggiunge un arco a questo grafo.
*
* @param edge
* l'arco da inserire
* @return true se l'arco è stato inserito, false se un arco esattamente
* uguale già esiste
* @throws NullPointerException
* se l'arco passato è nullo
* @throws IllegalArgumentException
* se almeno uno dei due nodi
* specificati nell'arco non esiste
* @throws IllegalArgumentException
* se l'arco è orientato e questo grafo
* non è orientato o viceversa
*/
public abstract boolean addEdge(GraphEdge<L> edge);
/**
* Aggiunge un arco a questo grafo tra due nodi dati. Se il grafo è
* orientato l'arco sarà orientato, altrimenti sarà un arco non orientato.
* L'arco viene inserito non pesato.
*
* @param node1
* nodo sorgente del nuovo arco in caso di grafo orientato,
* altrimenti un nodo del nuovo arco
* @param node2
* nodo destinazione del nuovo arco in caso di grafo
* orientato, altrimenti un nodo del nuovo arco
* @return true se l'arco è stato inserito, false se un arco esattamente
* uguale già esiste
* @throws NullPointerException
* se almeno uno dei due nodi passati è
* nullo
* @throws IllegalArgumentException
* se almeno uno dei due nodi passati
* non esiste nel grafo
*
*/
public abstract boolean addEdge(GraphNode<L> node1, GraphNode<L> node2);
/**
* Aggiunge un arco pesato a questo grafo tra due nodi dati. Se il grafo è
* orientato l'arco sarà orientato, altrimenti sarà un arco non orientato.
*
* @param node1
* nodo sorgente del nuovo arco in caso di grafo
* orientato, altrimenti un nodo del nuovo arco
* @param node2
* nodo destinazione del nuovo arco in caso di grafo
* orientato, altrimenti un nodo del nuovo arco
* @param weight
* il peso da associare all'arco
* @return true se l'arco è stato inserito, false se un arco esattamente
* uguale già esiste
* @throws NullPointerException
* se almeno uno dei due nodi passati è
* nullo
* @throws IllegalArgumentException
* se almeno uno dei due nodi passati
* non esiste nel grafo
*/
public abstract boolean addWeightedEdge(GraphNode<L> node1,
GraphNode<L> node2, double weight);
/**
* Aggiunge un arco a questo grafo tra due nodi etichettati con le etichette
* date. Se il grafo è orientato l'arco sarà orientato, altrimenti sarà un
* arco non orientato. L'arco viene inserito non pesato.
*
* @param label1
* l'etichetta del nodo sorgente del nuovo arco in caso di
* grafo orientato, altrimenti l'etichetta di un nodo del
* nuovo arco
* @param label2
* l'etichetta del nodo destinazione del nuovo arco in
* caso di grafo orientato, altrimenti l'etichetta di un
* nodo del nuovo arco
* @return true se l'arco è stato inserito, false se un arco esattamente
* uguale già esiste
* @throws NullPointerException
* se una delle etichette passate è
* nulla
* @throws IllegalArgumentException
* se almeno una delle due etichette
* non esiste in nessun nodo del grafo
*/
public abstract boolean addEdge(L label1, L label2);
/**
* Aggiunge un arco pesato a questo grafo tra due nodi etichettati con le
* etichette date. Se il grafo è orientato l'arco sarà orientato, altrimenti
* sarà un arco non orientato.
*
* @param label1
* l'etichetta del nodo sorgente del nuovo arco in caso di
* grafo orientato, altrimenti l'etichetta di un nodo del
* nuovo arco
* @param label2
* l'etichetta del nodo destinazione del nuovo arco in
* caso di grafo orientato, altrimenti l'etichetta di un
* nodo del nuovo arco
* @param weight
* il peso da associare all'arco
* @return true se l'arco è stato inserito, false se un arco esattamente
* uguale già esiste
* @throws NullPointerException
* se una delle etichette passate è
* nulla
* @throws IllegalArgumentException
* se almeno una delle due etichette
* non esiste in nessun nodo del grafo
*/
public abstract boolean addWeightedEdge(L label1, L label2, double weight);
/**
* Aggiunge un arco a questo grafo tra due nodi di un indice dato. Se il grafo
* è orientato l'arco sarà orientato, altrimenti sarà un arco non orientato.
* L'arco viene inserito non pesato.
*
* Questa operazione è opzionale.
*
* @param i
* l'indice del nodo sorgente del nuovo arco in caso di grafo
* orientato, altrimenti l'indice di uno dei nodi del nuovo
* arco
* @param j
* l'indice del nodo destinazione del nuovo arco in caso di
* grafo orientato, altrimenti l'indice di uno dei nodi del
* nuovo arco
* @return true se l'arco è stato inserito, false se un arco esattamente
* uguale già esiste
* @throws IndexOutOfBoundsException
* se almeno uno degli indici
* passati non corrisponde a
* nessun nodo o è fuori dai
* limiti dell'intervallo
* <code>[0, this.nodeCount() - 1]</code>
* @throws UnsupportedOperationException
* se questa operazione non è
* supportata dall'implementazione
* di questo grafo
*/
public abstract boolean addEdge(int i, int j);
/**
* Aggiunge un arco pesato a questo grafo tra due nodi di un indice dato. Se il
* grafo è orientato l'arco sarà orientato, altrimenti sarà un arco non
* orientato.
*
* Questa operazione è opzionale.
*
* @param i
* l'indice del nodo sorgente del nuovo arco in caso di
* grafo orientato, altrimenti l'indice di uno dei nodi
* del nuovo arco
* @param j
* l'indice del nodo destinazione del nuovo arco in caso
* di grafo orientato, altrimenti l'indice di uno dei nodi
* del nuovo arco
* @param weight
* il peso da associare all'arco
* @return true se l'arco è stato inserito, false se un arco esattamente
* uguale già esiste
* @throws IndexOutOfBoundsException
* se almeno uno degli indici
* passati non corrisponde a
* nessun nodo o è fuori dai
* limiti dell'intervallo
* <code>[0, this.nodeCount() - 1]</code>
* @throws UnsupportedOperationException
* se questa operazione non è
* supportata dall'implementazione
* di questo grafo
*/
public abstract boolean addWeightedEdge(int i, int j, double weight);
/**
* Rimuove un arco da questo grafo.
*
* Questa operazione è opzionale.
*
* @param edge
* l'arco da rimuovere
*
* @throws IllegalArgumentException
* se l'arco non esiste in questo
* grafo
* @throws IllegalArgumentException
* se almeno uno dei due nodi
* specificati nell'arco non
* esiste
* @throws NullPointerException
* se l'arco passato è nullo
* @throws UnsupportedOperationException
* se l'implementazione del grafo
* non supporta questa operazione
*/
public abstract void removeEdge(GraphEdge<L> edge);
/**
* Rimuove l'arco che connette due nodi dati da questo grafo. Se il grafo è
* non orientato l'ordine dei due nodi passati come parametri non conta.
*
* Questa operazione è opzionale.
*
* @param node1
* il nodo sorgente dell'arco da rimuovere nel caso di
* grafo orientato, altrimenti uno dei due nodi dell'arco
* da rimuovere
* @param node2
* il nodo destinazione dell'arco da rimuovere nel caso di
* grafo orientato, altrimenti uno dei due nodi dell'arco
* da rimuovere
* @throws IllegalArgumentException
* se l'arco non esiste nel grafo
* @throws IllegalArgumentException
* se almeno uno dei due nodi
* passati non esiste
* @throws NullPointerException
* se almeno uno dei due nodi
* passati è nullo
* @throws UnsupportedOperationException
* se l'implementazione del grafo
* non supporta questa operazione
*/
public abstract void removeEdge(GraphNode<L> node1, GraphNode<L> node2);
/**
* Rimuove l'arco che connette i due nodi con etichette date da questo
* grafo. Se il grafo è non orientato l'ordine delle due etichette passate
* come parametri non conta.
*
* Questa operazione è opzionale.
*
* @param label1
* l'etichetta del nodo sorgente dell'arco da rimuovere
* nel caso di grafo orientato, altrimenti l'etichetta di
* uno dei due nodi dell'arco da rimuovere
* @param label2
* l'etichetta del nodo destinazione dell'arco da
* rimuovere nel caso di grafo orientato, altrimenti
* l'etichetta di uno dei due nodi dell'arco da rimuovere
* @throws IllegalArgumentException
* se l'arco non esiste nel grafo
* @throws IllegalArgumentException
* se almeno una delle due
* etichette passate non esiste
* nel grafo
* @throws NullPointerException
* se almeno una delle due
* etichette passate è nulla
* @throws UnsupportedOperationException
* se l'implementazione del grafo
* non supporta questa operazione
*/
public abstract void removeEdge(L label1, L label2);
/**
* Rimuove l'arco che connette i due nodi con indici dati da questo grafo.
* Se il grafo è non orientato l'ordine dei due indici passati come
* parametri non conta.
*
* Questa operazione è opzionale.
*
* @param i
* l'indice del nodo sorgente dell'arco da rimuovere nel caso
* di grafo orientato, altrimenti l'etichetta di uno dei due
* nodi dell'arco da rimuovere
* @param j
* l'indice del nodo destinazione dell'arco da rimuovere nel
* caso di grafo orientato, altrimenti l'etichetta di uno dei
* due nodi dell'arco da rimuovere
*
* @throws IllegalArgumentException
* se l'arco non esiste nel grafo
* @throws IndexOutOfBoundsException
* se almeno uno degli indici
* passati non corrisponde a
* nessun nodo o è fuori dai
* limiti dell'intervallo
* <code>[0, this.nodeCount() - 1]</code>
* @throws UnsupportedOperationException
* se questa operazione non è
* supportata dall'implementazione
* di questo grafo
*/
public abstract void removeEdge(int i, int j);
/**
* Restituisce, se esiste, un arco di questo grafo che è uguale all'arco
* dato. Se il grafo è non orientato l'arco restituito potrebbe avere i nodi
* in ordine diverso da quelli dell'arco dato.
*
* @param edge
* l'arco di cui ricercare l'arco uguale in questo grafo
* @return Un arco di questo grafo uguale all'arco dato oppure null se
* questo grafo non contiene nessun arco uguale a quello dato. Se il
* grafo non è orientato allora l'arco restituito può contenere i
* nodi dell'arco dato in qualsiasi ordine
* @throws NullPointerException
* l'arco passato è null
* @throws IllegalArgumentException
* se almeno uno dei due nodi dell'arco
* passato non esiste nel grafo
*/
public abstract GraphEdge<L> getEdge(GraphEdge<L> edge);
/**
* Restituisce, se esiste, l'arco che connette due nodi dati in questo
* grafo. In caso di grafo non orientato l'ordine dei due nodi è
* ininfluente, in caso di grafo orientato viene ricercato l'arco che
* connette il primo nodo specificato al secondo.
*
* @param node1
* il primo nodo
* @param node2
* il secondo nodo
* @return Un arco che connette node1 e node2 nel grafo oppure null se tale
* arco non esiste nel grafo. Se il grafo è orientato l'arco
* ricercato e restituito è quello che ha sorgente in node1 e
* destinazione in node2. Se il grafo non è orientato allora l'arco
* restituito può contenere node1 e node2 in qualsiasi ordine
* @throws NullPointerException
* se almeno uno dei due nodi passati è
* null
* @throws IllegalArgumentException
* se almeno uno dei due nodi passati
* non esiste nel grafo
*/
public abstract GraphEdge<L> getEdge(GraphNode<L> node1,
GraphNode<L> node2);
/**
* Restituisce, se esiste, l'arco che connette i due nodi che hanno le
* etichette date in questo grafo. In caso di grafo non orientato l'ordine
* delle due etichette è ininfluente, in caso di grafo orientato viene
* ricercato l'arco che connette il primo nodo con etichetta specificata al
* secondo.
*
* @param label1
* l'etichetta del primo nodo
* @param label2
* l'etichetta del secondo nodo
* @return Un arco che connette il nodo etichettato con label1 e il nodo
* etichettato con label2 nel grafo oppure null se tale arco non
* esiste nel grafo. Se il grafo è orientato l'arco ricercato e
* restituito è quello che ha sorgente nel nodo etichettato con
* label1 e destinazione nel nodo etichettato con label2. Se il
* grafo non è orientato allora l'arco restituito può contenere il
* nodo etichettato con label1 e il nodo etichettato con label2 in
* qualsiasi ordine
* @throws NullPointerException
* se almeno una delle due etichette
* passate è null
* @throws IllegalArgumentException
* se almeno una delle due etichette
* passate non è presente in nessun
* nodo di questo grafo
*/
public abstract GraphEdge<L> getEdge(L label1, L label2);
/**
* Restituisce, se esiste, l'arco che connette i due nodi con indici dati in
* questo grafo. In caso di grafo non orientato l'ordine dei due nodi è
* ininfluente, in caso di grafo orientato viene ricercato l'arco che
* connette il primo nodo specificato al secondo.
*
* Questa operazione è opzionale.
*
* @param i
* l'indice del primo nodo
* @param j
* l'indice del secondo nodo
* @return Un arco che connette il nodo all'indice i e il nodo all'indice j
* nel grafo oppure null se tale arco non esiste nel grafo. Se il
* grafo è orientato l'arco ricercato e restituito è quello che ha
* sorgente nel nodo di un indice i e destinazione nel nodo di un indice
* j. Se il grafo non è orientato allora l'arco restituito può
* contenere il nodo di un indice i e il nodo di un indice j in qualsiasi
* ordine
* @throws IndexOutOfBoundsException
* se almeno uno degli indici
* passati non corrisponde a
* nessun nodo o è fuori dai
* limiti dell'intervallo
* <code>[0, this.nodeCount() - 1]</code>
* @throws UnsupportedOperationException
* se questa operazione non è
* supportata dall'implementazione
* di questo grafo
*/
public abstract GraphEdge<L> getEdge(int i, int j);
/**
* Restituisce l'insieme di tutti i nodi adiacenti a un certo nodo. Se il
* grafo è orientato, i nodi restituiti sono solo quelli collegati da un
* arco uscente dal nodo passato.
*
* @param node
* il nodo di cui cercare i nodi adiacenti
* @return l'insieme di tutti i nodi adiacenti al nodo specificato
*
* @throws IllegalArgumentException
* se il nodo passato non esiste
* @throws NullPointerException
* se il nodo passato è nullo
*/
public abstract Set<GraphNode<L>> getAdjacentNodesOf(GraphNode<L> node);
/**
* Restituisce l'insieme di tutti i nodi adiacenti al nodo con etichetta
* data. Se il grafo è orientato, i nodi restituiti sono solo quelli
* collegati da un arco uscente dal nodo in questione.
*
* @param label
* l'etichetta del nodo di cui cercare i nodi adiacenti
* @return l'insieme di tutti i nodi adiacenti al nodo con etichetta data
*
* @throws IllegalArgumentException
* se l'etichetta passata non esiste in
* nessun nodo di questo grafo
* @throws NullPointerException
* se l'etichetta passata è null
*/
public abstract Set<GraphNode<L>> getAdjacentNodesOf(L label);
/**
* Restituisce l'insieme di tutti i nodi adiacenti al nodo con l'indice
* dato. Se il grafo è orientato, i nodi restituiti sono solo quelli
* collegati da un arco uscente dal nodo in questione.
*
* Questa operazione è opzionale.
*
* @param i
* l'indice del nodo di cui cercare i nodi adiacenti
* @return l'insieme di tutti i nodi adiacenti al nodo con indice dato
*
* @throws IndexOutOfBoundsException
* se l'indice passato non
* corrisponde a nessun nodo o è
* fuori dai limiti
* dell'intervallo
* <code>[0, this.nodeCount() - 1]</code>
* @throws UnsupportedOperationException
* se questa operazione non è
* supportata dall'implementazione
* di questo grafo
*/
public abstract Set<GraphNode<L>> getAdjacentNodesOf(int i);
/**
* Restituisce l'insieme di tutti i nodi collegati tramite un arco entrante
* in un certo nodo in un grafo orientato.
*
* @param node
* il nodo di cui cercare i nodi predecessori
* @return l'insieme di tutti i nodi collegati tramite un arco entrante al
* nodo specificato
* @throws UnsupportedOperationException
* se il grafo su cui il metodo è
* chiamato non è orientato
* @throws IllegalArgumentException
* se il nodo passato non esiste
* @throws NullPointerException
* se il nodo passato è nullo
*/
public abstract Set<GraphNode<L>> getPredecessorNodesOf(GraphNode<L> node);
/**
* Restituisce l'insieme di tutti i nodi collegati tramite un arco entrante
* nel nodo con etichetta data in un grafo orientato.
*
* @param label
* l'etichetta del nodo di cui cercare i nodi predecessori
* @return l'insieme di tutti i nodi collegati tramite un arco entrante al
* nodo con etichetta data
* @throws UnsupportedOperationException
* se il grafo su cui il metodo è
* chiamato non è orientato
* @throws IllegalArgumentException
* se l'etichetta passata non
* esiste in nessun nodo del grafo
* @throws NullPointerException
* se l'etichetta passata è nulla
*/
public abstract Set<GraphNode<L>> getPredecessorNodesOf(L label);
/**
* Restituisce l'insieme di tutti i nodi collegati tramite un arco entrante
* nel nodo con indice dato in un grafo orientato.
*
* Questa operazione è opzionale.
*
* @param i
* l'indice del nodo di cui cercare i nodi predecessori
* @return l'insieme di tutti i nodi collegati tramite un arco entrante al
* nodo con indice dato
* @throws UnsupportedOperationException
* se il grafo su cui il metodo è
* chiamato non è orientato
* @throws IndexOutOfBoundsException
* se l'indice passato non
* corrisponde a nessun nodo o è
* fuori dai limiti
* dell'intervallo
* <code>[0, this.nodeCount() - 1]</code>
* @throws UnsupportedOperationException
* se questa operazione non è
* supportata dall'implementazione
* di questo grafo
*/
public abstract Set<GraphNode<L>> getPredecessorNodesOf(int i);
/**
* Restituisce l'insieme di tutti gli archi connessi a un certo nodo in
* questo grafo. Nel caso di grafo orientato vengono restituiti solo gli
* archi uscenti.
*
* @param node
* il nodo di cui sono richiesti gli archi connessi
*
* @return un insieme contenente tutti gli archi connessi al nodo dato in
* questo grafo (solo gli archi uscenti in caso di grafo orientato)
*
* @throws IllegalArgumentException
* se il nodo passato non esiste
* @throws NullPointerException
* se il nodo passato è nullo
*/
public abstract Set<GraphEdge<L>> getEdgesOf(GraphNode<L> node);
/**
* Restituisce l'insieme di tutti gli archi connessi al nodo con etichetta
* data in questo grafo. Nel caso di grafo orientato vengono restituiti solo
* gli archi uscenti.
*
* @param label
* l'etichetta del nodo di cui sono richiesti gli archi
* connessi
*
* @return un insieme contenente tutti gli archi connessi al nodo con
* etichetta data in questo grafo (solo gli archi uscenti in caso di
* grafo orientato)
*
* @throws IllegalArgumentException
* se il nodo con l'etichetta passata
* non esiste
* @throws NullPointerException
* se l'etichetta passata è nulla
*/
public abstract Set<GraphEdge<L>> getEdgesOf(L label);
/**
* Restituisce l'insieme di tutti gli archi connessi al nodo con indice dato
* in questo grafo. Nel caso di grafo orientato vengono restituiti solo gli
* archi uscenti.
*
* Questa operazione è opzionale.
*
* @param i
* l'indice del nodo di cui sono richiesti gli archi
* connessi
*
* @return un insieme contenente tutti gli archi connessi al nodo con indice
* dato in questo grafo (solo gli archi uscenti in caso di grafo
* orientato)
*
* @throws IndexOutOfBoundsException
* se l'indice passato non
* corrisponde a nessun nodo o è
* fuori dai limiti
* dell'intervallo
* <code>[0, this.nodeCount() - 1]</code>
* @throws UnsupportedOperationException
* se questa operazione non è
* supportata dall'implementazione
* di questo grafo
*/
public abstract Set<GraphEdge<L>> getEdgesOf(int i);
/**
* Restituisce l'insieme di tutti gli archi entranti in un certo nodo in un
* grafo orientato.
*
* @param node
* il nodo di cui determinare tutti gli archi entranti
*
* @return un insieme contenente tutti gli archi entranti nel nodo passato
* in questo grafo orientato.
*
* @throws UnsupportedOperationException
* se il grafo su cui il metodo è
* chiamato non è orientato
* @throws IllegalArgumentException
* se il nodo passato non esiste
* @throws NullPointerException
* se il nodo passato è nullo
*/
public abstract Set<GraphEdge<L>> getIngoingEdgesOf(GraphNode<L> node);
/**
* Restituisce l'insieme di tutti gli archi entranti nel nodo con etichetta
* data in un grafo orientato.
*
* Questa operazione è opzionale.
*
* @param label
* l'etichetta del nodo di cui determinare tutti gli archi
* entranti
*
* @return un insieme contenente tutti gli archi entranti nel nodo con
* etichetta data in questo grafo orientato.
*
* @throws UnsupportedOperationException
* se il grafo su cui il metodo è
* chiamato non è orientato
* @throws IllegalArgumentException
* se l'etichetta passata non
* esiste in nessun nodo del grafo
* @throws NullPointerException
* se l'etichetta passata è nulla
*/
public abstract Set<GraphEdge<L>> getIngoingEdgesOf(L label);
/**
* Restituisce l'insieme di tutti gli archi entranti nel nodo con indice
* dato in un grafo orientato.
*
* Questa operazione è opzionale.
*
* @param i
* l'indice del nodo di cui determinare tutti gli archi
* entranti
*
* @return un insieme contenente tutti gli archi entranti nel nodo con
* indice dato in questo grafo orientato.
*
* @throws UnsupportedOperationException
* se il grafo su cui il metodo è
* chiamato non è orientato
* @throws IndexOutOfBoundsException
* se l'indice passato non
* corrisponde a nessun nodo o è
* fuori dai limiti
* dell'intervallo
* <code>[0, this.nodeCount() - 1]</code>
* @throws UnsupportedOperationException
* se questa operazione non è
* supportata dall'implementazione
* di questo grafo
*/
public abstract Set<GraphEdge<L>> getIngoingEdgesOf(int i);
/**
* Restituisce l'insieme di tutti gli archi in questo grafo.
*
* @return un insieme, possibilmente vuoto, contenente tutti gli archi di
* questo grafo
*/
public abstract Set<GraphEdge<L>> getEdges();
/**
* Restituisce il grado di un nodo, cioè il numero di archi connessi al
* nodo. Nel caso di grafo orientato è la somma del numero di archi in
* entrata e del numero di archi in uscita.
*
* @param node
* il nodo di cui calcolare il grado
* @return il grado del nodo passato
*
* @throws IllegalArgumentException
* se il nodo passato non esiste
* @throws NullPointerException
* se il nodo passato è nullo
*/
public int getDegreeOf(GraphNode<L> node) {
if (node == null)
throw new NullPointerException(
"Tentativo di calcolare il grado di un nodo null");
if (this.getNode(node) == null)
throw new IllegalArgumentException(
"Tentativo di calcolare il grado di un nodo non "
+ "presente in questo grafo");
if (!this.isDirected())
return this.getEdgesOf(node).size();
else
return this.getEdgesOf(node).size()
+ this.getIngoingEdgesOf(node).size();
}
/**
* Restituisce il grado del nodo con l'etichetta data, cioè il numero di
* archi connessi al nodo. Nel caso di grafo orientato è la somma del numero
* di archi in entrata e del numero di archi in uscita.
*
* @param label
* l'etichetta del nodo di cui calcolare il grado
* @return il grado del nodo con l'etichetta data
*
* @throws IllegalArgumentException
* se l'etichetta passata non esiste in
* nessun nodo del grafo
* @throws NullPointerException
* se l'etichetta passata è nulla
*/
public int getDegreeOf(L label) {
if (label == null)
throw new NullPointerException(
"Tentativo di calcolare il grado di un nodo null");
if (this.getNode(label) == null)
throw new IllegalArgumentException(