-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
1079 lines (807 loc) · 35.6 KB
/
Copy pathMain.java
File metadata and controls
1079 lines (807 loc) · 35.6 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
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
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
/** The main part of our program, where all the magic happens, multiple methods, all explained.
*
*/
public class Main
{
/** vertices represent our graph, it is an arraylist of Node objects representing each vertex */
public static ArrayList<Node> vertices = new ArrayList<>();
/** verticesFull is used in our BK algorithm, representing all the possible maximal cliques in a graph */
public static ArrayList<ArrayList<Node>> verticesFull = new ArrayList<>();
public static void main(String[] args)
{
/* we first specify the input file */
String inputFile = args[0];
System.out.println(inputFile);
int[][] eArr = ReadGraph.readFile(inputFile);
int numberOfVertices = ReadGraph.getNumberOfVertices();
int numberOfEdges = ReadGraph.getNumberOfEdges();
/* convert it to our finalArray from first phase, more explanation can be found in ArrayMethods and SolveGraph from first phase */
ArrayList<ArrayList<ArrayList<Integer>>> finArr = ArrayMethods.inputArrayFinal(numberOfVertices, numberOfEdges, eArr);
/* here we create our objects that represent the said graph, we decided to use objects for this phase as they are much easier and cleaner to work with */
createNodesOOP(numberOfVertices, finArr);
/* initializing variables */
int lowerBound = 0;
int upperBound = 0;
int chromaticNumber = 0;
/* here we see how big the graph is, if it is deemed too big, we use our random walks pruning method, as it is a lot faster, and without it, we woudln't be able to
* compute the bounds in time */
if (eArr.length > 100000)
{
ArrayList<ArrayList<ArrayList<Integer>>> finArr2 = randomWalksAlgorithmForPruning(numberOfVertices, finArr);
vertices.clear();
/* recreate the objects without some, that were deemed unnecesarry */
createNodesOOP(finArr2.size(), finArr2);
lowerBound = BKalgorithm();
upperBound = DSatur(vertices);
vertices.clear();
/* here we recreate the original graph, so that we can use our DFS to find bridges and then find connected components */
createNodesOOP(numberOfVertices, finArr);
}
else
{
upperBound = DSatur(vertices);
lowerBound = BKalgorithm();
}
/* if our bounds match, we don't even need to run other parts of our code, as we've definitely found the exact chromatic number */
if (lowerBound == upperBound)
{
chromaticNumber = lowerBound;
}
else
{
/* call the method to delete bridges and then to find connected components in the graph, so that they can be identified and their chromatic numbers
* can be written inside the solutions arraylist
*/
deleteBridges();
ArrayList<ArrayList<Node>> allComponents = findConnectedComponents();
ArrayList<Integer> solutions = new ArrayList<>();
int numberOfComponents = 0;
for (ArrayList<Node> component : allComponents)
{
int chromaticNumberOfComponent = 0;
/* if we can't determine the whether the component is a specialType we run our greedy brute force algorithm */
boolean isSpecialType = false;
if (isComplete(component))
{
isSpecialType = true;
//System.out.println("COMPLETE");
if ((component.size() % 2) == 0)
{
chromaticNumberOfComponent = component.size() - 1;
solutions.add(chromaticNumberOfComponent);
}
else
{
chromaticNumberOfComponent = component.size();
solutions.add(chromaticNumberOfComponent);
}
}
if (isWheelGraph(component))
{
isSpecialType = true;
//System.out.println("WHEEL");
if ((component.size() % 2) == 0)
{
chromaticNumberOfComponent = 4;
solutions.add(chromaticNumberOfComponent);
}
else
{
chromaticNumberOfComponent = 3;
solutions.add(chromaticNumberOfComponent);
}
}
if (isCycleGraph(component))
{
isSpecialType = true;
//System.out.println("CYCLE");
if ((component.size() % 2) == 0)
{
chromaticNumberOfComponent = 2;
solutions.add(chromaticNumberOfComponent);
}
else
{
chromaticNumberOfComponent = 3;
solutions.add(chromaticNumberOfComponent);
}
}
if (isBipartite(component))
{
isSpecialType = true;
//System.out.println("BIPARTITE");
chromaticNumberOfComponent = 2;
solutions.add(chromaticNumberOfComponent);
}
if (isTree(component))
{
isSpecialType = true;
//System.out.println("TREE");
chromaticNumberOfComponent = 2;
solutions.add(chromaticNumberOfComponent);
}
/* if the component is deemed small enough we use our brute force algorithm */
if (((isSpecialType == false) || (component.size() < 50)) && (numberOfComponents < 5))
{
ArrayList<ArrayList<ArrayList<Integer>>> finArr3 = OOPtofinArr(component);
chromaticNumberOfComponent = SolveGraph.SolveGraphBruteForce(finArr3, finArr3.size());
solutions.add(chromaticNumberOfComponent);
numberOfComponents++;
}
}
/* if we weren't able to find the exact chromatic number we say so here by outputing that it is -1*/
if (solutions.size() == 0)
{
chromaticNumber = -1;
}
else
{
chromaticNumber = Collections.max(solutions);
}
}
System.out.println("NEW BEST UPPER BOUND=" + upperBound);
System.out.println("NEW BEST LOWER BOUND=" + lowerBound);
/* same thing, if our chromatic number is somehow (which shouldn't happen) not in between our bounds, we output it is -1 as we weren't able to get it */
if ((chromaticNumber < lowerBound) || (chromaticNumber > upperBound))
{
chromaticNumber = -1;
}
System.out.println("CHROMATIC NUMBER=" + chromaticNumber);
}
/**
* The randomWalksPath method is used to generate a random path of nodes in a graph starting from a given vertex and continuing for a given number of walks.
*
* @param vertexStart Node object representing the starting vertex for the random walk
* @param numberOfWalks int representing the number of times the walk should continue
* @param pathTraveled ArrayList of Node objects representing the path traveled so far
* @param WALKS int representing the number of walks to be taken
* @return ArrayList of Node objects representing the path traveled
*/
public static ArrayList<Node> randomWalksPath(Node vertexStart, int numberOfWalks, ArrayList<Node> pathTraveled, int WALKS)
{
if (numberOfWalks == WALKS)
{
return pathTraveled;
}
else
{
pathTraveled.add(vertexStart);
numberOfWalks++;
int randomContinuation = (int)((Math.random()*vertexStart.getNodesVertexIsConnectedTo().size()));
ArrayList<Node> possibleMoves = vertexStart.getNodesVertexIsConnectedTo();
if (possibleMoves.size() == 0)
{
return pathTraveled;
}
Node nextVertex = possibleMoves.get(randomContinuation);
return randomWalksPath(nextVertex, numberOfWalks, pathTraveled, WALKS);
}
}
/**
* The bronKerboschPivoting method is used to find all the cliques of a graph using the Bron-Kerbosch algorithm with pivot.
* It uses recursion to find all the cliques and stores them in a ArrayList of ArrayList of Nodes.
*
* @param currentClique ArrayList of Node objects representing the current clique being built
* @param candidateSetOfVertices ArrayList of Node objects representing the set of candidate vertices for the current clique
* @param excludingSetOfVertices ArrayList of Node objects representing the set of vertices that are excluded from the current clique
*/
public static void bronKerboschPivoting(ArrayList<Node> currentClique, ArrayList<Node> candidateSetOfVertices, ArrayList<Node> excludingSetOfVertices)
{
if ((candidateSetOfVertices.size() + excludingSetOfVertices.size()) == 0)
{
verticesFull.add(currentClique);
}
else
{
// Getting the union of candidate and excluding vertices to choose a pivot vertex
ArrayList<Node> unionOfCandidateAndExludedVertices = new ArrayList<Node>(candidateSetOfVertices);
unionOfCandidateAndExludedVertices.addAll(excludingSetOfVertices);
// Choosing a pivot vertex and getting it's neighbours
int randomPivotVertexNumber = (int)(Math.random()*(unionOfCandidateAndExludedVertices.size()));
Node pivotVertex = unionOfCandidateAndExludedVertices.get(randomPivotVertexNumber);
ArrayList<Node> pivotVertexNeighbours = pivotVertex.getNodesVertexIsConnectedTo();
// Getting the candidate vertices minus the neighbours of our pivot vertex
ArrayList<Node> candidatesMinusPivotNeighbours = new ArrayList<Node>(candidateSetOfVertices);
candidatesMinusPivotNeighbours.removeAll(pivotVertexNeighbours);
for (Node node : (candidatesMinusPivotNeighbours))
{
ArrayList<Node> currentCliqueNew = new ArrayList<>(currentClique);
currentCliqueNew.add(node);
ArrayList<Node> candidateSetOfVerticesNew = new ArrayList<>(candidateSetOfVertices);
ArrayList<Node> excludingSetOfVerticesNew = new ArrayList<>(excludingSetOfVertices);
candidateSetOfVerticesNew.retainAll(node.getNodesVertexIsConnectedTo());
excludingSetOfVerticesNew.retainAll(node.getNodesVertexIsConnectedTo());
bronKerboschPivoting(currentCliqueNew, candidateSetOfVerticesNew, excludingSetOfVerticesNew);
candidateSetOfVertices.remove(node);
excludingSetOfVertices.add(node);
}
}
}
/**
* The BKalgorithm method is used to find the maximum clique of a graph using the Bron-Kerbosch algorithm with pivot.
* It uses the bronKerboschPivoting method to find all the cliques and finds the maximum clique by returning the size of the clique with the most number of vertices.
*
* @return an Integer representing the size of the maximum clique in the graph
*/
public static int BKalgorithm()
{
ArrayList<Node> currentClique = new ArrayList<Node>();
ArrayList<Node> candidateSetOfVertices = new ArrayList<>(vertices);
ArrayList<Node> excludingSetOfVertices = new ArrayList<Node>();
bronKerboschPivoting(currentClique, candidateSetOfVertices, excludingSetOfVertices);
ArrayList<Integer> verticesAllCliques = new ArrayList<>();
for (int i = 0; i < verticesFull.size(); i++)
{
ArrayList<Integer> verticesAndTheirNumbers = new ArrayList<>();
for (int x = 0; x < verticesFull.get(i).size(); x++)
{
verticesAndTheirNumbers.add(verticesFull.get(i).get(x).vertexNumber);
}
verticesAllCliques.add(verticesAndTheirNumbers.size());
}
int result = Collections.max(verticesAllCliques);
return result;
}
/**
* The randomWalksAlgorithmForPruning method is used to prune a graph using random walks algorithm.
* It takes in a number of vertices and an ArrayList of ArrayList of Integer objects representing the edges in the graph as input,
* and performs random walks on the graph to find the most important vertices and edges.
*
* @param numberOfVertices an Integer represents the number of vertices in the graph
* @param finArr an ArrayList of ArrayList of ArrayList of Integer objects representing the edges in the graph
* @return ArrayList of ArrayList of ArrayList of Integer objects representing the edges in the pruned graph
*/
public static ArrayList<ArrayList<ArrayList<Integer>>> randomWalksAlgorithmForPruning(int numberOfVertices, ArrayList<ArrayList<ArrayList<Integer>>> finArr)
{
LinkedHashMap<Integer, Integer> vertexAndTheNumberOfTimesItWasWalkedOn = new LinkedHashMap<>();
for (int i = 0; i < numberOfVertices; i++)
{
vertexAndTheNumberOfTimesItWasWalkedOn.put(i+1, 0);
}
int numberOfWalks = vertices.size();
for (int i = 0; i < numberOfWalks; i++)
{
ArrayList<Integer> verticesThatWereWalkedOn = new ArrayList<>();
int randomNumber = (int)((Math.random()*vertices.size()));
ArrayList<Node> pathTraveled = new ArrayList<>();
pathTraveled = randomWalksPath(vertices.get(randomNumber), 0, pathTraveled, 500);
for (int x = 0; x < pathTraveled.size(); x++)
{
verticesThatWereWalkedOn.add(pathTraveled.get(x).getVertexNumber());
}
for (int y = 0; y < verticesThatWereWalkedOn.size(); y++)
{
vertexAndTheNumberOfTimesItWasWalkedOn.compute(verticesThatWereWalkedOn.get(y), (key, val) -> val + 1);
}
}
float sum = 0.0f;
for (float f : vertexAndTheNumberOfTimesItWasWalkedOn.values())
{
sum += f;
}
ArrayList<Integer> verticesWalkedOnTheMost = new ArrayList<>();
for (int i = 1; i < vertexAndTheNumberOfTimesItWasWalkedOn.size()+1; i++)
{
if (vertexAndTheNumberOfTimesItWasWalkedOn.get(i) > (sum/vertexAndTheNumberOfTimesItWasWalkedOn.size()))
{
verticesWalkedOnTheMost.add(i);
}
}
ArrayList<ArrayList<ArrayList<Integer>>> finArr2 = new ArrayList<ArrayList<ArrayList<Integer>>>();
for (int i = 0; i < finArr.size(); i++)
{
if (verticesWalkedOnTheMost.contains(finArr.get(i).get(0).get(0)))
{
finArr2.add(finArr.get(i));
}
}
for (int i = 0; i < finArr2.size(); i++)
{
for (int x = 0; x < finArr2.get(i).get(1).size(); x++)
{
if (!(verticesWalkedOnTheMost.contains(finArr2.get(i).get(1).get(x))))
{
finArr2.get(i).get(1).remove(x);
}
}
}
return finArr2;
}
/**
* The createNodesOOP method is used to create a graph using Object Oriented Programming.
* It takes in a number of vertices and an ArrayList of ArrayList of Integer objects representing the edges in the graph as input,
* and creates Node objects for each vertex, adds them to the 'vertices' ArrayList and creates edges between them.
*
* @param numberOfVertices an Integer represents the number of vertices in the graph
* @param finArr an ArrayList of ArrayList of ArrayList of Integer objects representing the edges in the graph
*/
public static void createNodesOOP(int numberOfVertices, ArrayList<ArrayList<ArrayList<Integer>>> finArr)
{
HashMap<Integer, Node> verticesAndTheirNumbers = new HashMap<>();
for (int i = 0; i < numberOfVertices; i++)
{
int vertexNumber = finArr.get(i).get(0).get(0);
Node vertex = new Node(vertexNumber);
vertices.add(vertex);
verticesAndTheirNumbers.put(vertexNumber, vertex);
}
for (int i = 0; i < finArr.size(); i++)
{
ArrayList<ArrayList<Integer>> listOfCurrentConnections = finArr.get(i);
int nodeToAddConnectionsTo = listOfCurrentConnections.get(0).get(0);
ArrayList<Integer> listOfConnectedNodes = listOfCurrentConnections.get(1);
for (int j = 0; j < listOfConnectedNodes.size(); j++)
{
int nodeToConnect = listOfCurrentConnections.get(1).get(j);
verticesAndTheirNumbers.get(nodeToAddConnectionsTo).addAdjacentVertices(verticesAndTheirNumbers.get(nodeToConnect));
}
}
}
/**
* The DFS method is a Depth First Search algorithm used to traverse a graph.
* It takes a starting vertex and an ArrayList of Node objects representing a connected component as input,
* and finds all vertices connected to the starting vertex by adding them to the connected component.
*
* @param vertex the starting vertex from which the graph will be traversed
* @param connectedComponent an ArrayList of Node objects representing a connected component in the graph
*/
public static void DFS(Node vertex, ArrayList<Node> connectedComponent)
{
vertex.setVertexVisitedStatusToTrue();
connectedComponent.add(vertex);
for (Node node : vertex.getNodesVertexIsConnectedTo())
{
if (node.getVertexVisitedStatus() == false)
{
DFS(node, connectedComponent);
}
}
}
/**
* The findConnectedComponents method is used to find all connected components in a graph.
* It uses a Depth First Search algorithm to traverse the graph and find all connected components.
*
* @return ArrayList of ArrayList of Node objects representing the connected components in the graph
*/
public static ArrayList<ArrayList<Node>> findConnectedComponents()
{
changeAllNodesToUnvisited();
ArrayList<ArrayList<Node>> allComponents = new ArrayList<>();
for (Node node : vertices)
{
ArrayList<Node> connectedComponent = new ArrayList<>();
if (node.getVertexVisitedStatus() == false)
{
DFS(node, connectedComponent);
if (connectedComponent.size() > 1)
{
allComponents.add(connectedComponent);
}
connectedComponent = new ArrayList<>();
}
}
return allComponents;
}
/**
* The changeAllNodesToUnvisited method is used to change the visited status in all the nodes of the graph to false (make them unvisited)
*
*/
public static void changeAllNodesToUnvisited()
{
for (int i = 0; i < vertices.size(); i++)
{
vertices.get(i).setVertexVisitedStatusToFalse();
}
}
/**
* The deleteBridges method is used to delete bridges in the graph, calls the depthFirstSearch method to do the job
*
* initializes all the important variables and objects that will be used by the depthFirstSearch method
* calls depthFirstSearch on all unvisited nodes in the graph
*/
public static void deleteBridges()
{
changeAllNodesToUnvisited();
int[] d = new int[vertices.size()];
int[] low = new int[vertices.size()];
int[] parent = new int[vertices.size()];
for (Node node : vertices)
{
if (node.getVertexVisitedStatus() == false)
{
Integer time = 0;
depthFirstSearch(node, d, low, parent, time);
}
}
}
/**
* The depthFirstSearch method is used to find bridges in a given graph.
*
* @param vertex a Node object representing the current vertex we're looking at
* @param d an int[] which we use to keep track of discovery times for the vertices
* @param low an int[] that has the lowest time between two vertices
* @param parent an int[] representing the parent and the child node
* @param time an Integer representing the time discovery time of the vertex
* deletes connection between two vertices if it deemed important to do so
*/
public static void depthFirstSearch(Node vertex, int[] d, int[] low, int[] parent, Integer time)
{
vertex.setVertexVisitedStatusToTrue();
d[vertex.getVertexNumber()-1] = low[vertex.getVertexNumber() - 1] = ++time;
for (Node neighbourNode : vertex.staticNodesVertexIsConnectedTo)
{
if (neighbourNode.getVertexVisitedStatus() == false)
{
parent[neighbourNode.getVertexNumber() - 1] = vertex.getVertexNumber() - 1;
time++;
depthFirstSearch(neighbourNode, d, low, parent, time);
low[vertex.getVertexNumber() - 1] = Math.min(low[vertex.getVertexNumber() - 1], low[neighbourNode.getVertexNumber() - 1]);
if (low[neighbourNode.getVertexNumber() - 1] > d[vertex.getVertexNumber() - 1])
{
deleteConnection(vertex, neighbourNode);
}
}
else if (parent[vertex.getVertexNumber() - 1] != (neighbourNode.getVertexNumber() - 1))
{
low[vertex.getVertexNumber() - 1] = Math.min(low[vertex.getVertexNumber() - 1], low[neighbourNode.getVertexNumber() - 1]);
}
}
}
/**
* The deleteConnection method is used to delete a connection between two vertex objects.
*
* @param vertexA a Node object representing the vertex a connection will be removed from
* @param vertexB a Node object representing the vertex a connection will be removed from
*/
public static void deleteConnection(Node vertexA, Node vertexB)
{
vertexA.getNodesVertexIsConnectedTo().remove(vertexB);
vertexB.getNodesVertexIsConnectedTo().remove(vertexA);
}
public static ArrayList<ArrayList<ArrayList<Integer>>> OOPtofinArr(ArrayList<Node> verticesInput)
{
ArrayList<ArrayList<ArrayList<Integer>>> finArr = new ArrayList<>();
for (int i = 0; i < verticesInput.size(); i++)
{
ArrayList<ArrayList<Integer>> tempArr = new ArrayList<>();
ArrayList<Integer> connectionsToTheVertex = new ArrayList<>();
ArrayList<Integer> vertexNumber = new ArrayList<>();
vertexNumber.add(verticesInput.get(i).getVertexNumber());
for (int x = 0; x < verticesInput.get(i).getNodesVertexIsConnectedTo().size(); x++)
{
connectionsToTheVertex.add(verticesInput.get(i).getNodesVertexIsConnectedTo().get(x).getVertexNumber());
}
tempArr.add(vertexNumber);
tempArr.add(connectionsToTheVertex);
finArr.add(tempArr);
}
return finArr;
}
/**
* The isComplete method is used to check if a given graph is complete or not.
* A complete graph is a graph where each vertex is connected to all other vertices.
*
* @param verticesInput an ArrayList of Node objects representing the vertices in the graph
* @return true if the graph is complete, false otherwise
*/
public static boolean isComplete(ArrayList<Node> verticesInput)
{
changeAllNodesToUnvisited();
int counter = 0;
for (Node node : verticesInput)
{
ArrayList<Node> connections = node.getNodesVertexIsConnectedTo();
ArrayList<Node> verticesInputMinusNode = new ArrayList<>(verticesInput);
verticesInputMinusNode.remove(node);
if (connections.equals(verticesInputMinusNode))
{
counter+=1;
}
if (counter == verticesInput.size())
{
return true;
}
}
return false;
}
/**
* The isWheelGraph method takes an ArrayList<Node> object as input (representing the component) and returns true/false if the component is a wheel or not.
*
* @param verticesInput the ArrayList<Node> input
* @return true or false
*/
public static boolean isWheelGraph(ArrayList<Node> verticesInput)
{
changeAllNodesToUnvisited();
int centralVertexCount = 0;
Node centralVertex = null;
for (Node vertex : verticesInput)
{
if (vertex.getNodesVertexIsConnectedTo().size() == verticesInput.size() - 1)
{
centralVertexCount++;
centralVertex = vertex;
}
}
if (centralVertexCount != 1)
{
return false;
}
for (Node vertex : verticesInput)
{
if (vertex != centralVertex)
{
if (!vertex.getNodesVertexIsConnectedTo().contains(centralVertex))
{
return false;
}
for (Node connectedVertex : vertex.getNodesVertexIsConnectedTo())
{
if (connectedVertex != centralVertex && !vertex.getNodesVertexIsConnectedTo().contains(connectedVertex))
{
return false;
}
}
}
}
return true;
}
/**
* The isCycleGraph method takes an ArrayList<Node> object as input (representing the component) and returns true/false if the component is a cycle or not.
*
* @param verticesInput the ArrayList<Node> input
* @return true or false
*/
public static boolean isCycleGraph(ArrayList<Node> verticesInput)
{
changeAllNodesToUnvisited();
for (Node vertex : verticesInput)
{
if (!vertex.getVertexVisitedStatus())
{
if (DFSforCycleGraph(vertex))
{
return true;
}
}
}
return false;
}
/**
* The DFSforCycleGraph method is used to check if a given graph contains a cycle.
* It uses a Depth First Search algorithm to traverse the graph starting from a given vertex.
*
* @param vertex the starting vertex from which the graph will be traversed
* @return true if the graph contains a cycle, false otherwise
*/
public static boolean DFSforCycleGraph(Node vertex)
{
vertex.setVertexVisitedStatusToTrue();
for (Node neighbor : vertex.getNodesVertexIsConnectedTo())
{
if (!neighbor.getVertexVisitedStatus())
{
if (DFSforCycleGraph(neighbor))
{
return true;
}
} else if (neighbor != vertex)
{
return true;
}
}
vertex.setVertexVisitedStatusToFalse();
return false;
}
/**
* The isBipartite method takes an ArrayList<Node> object as input (representing the component) and returns true/false if the component is a bipartite or not.
*
* @param verticesInput the ArrayList<Node> input
* @return true or false
*/
public static boolean isBipartite(ArrayList<Node> verticesInput)
{
changeAllNodesToUnvisited();
for (Node vertex : verticesInput)
{
if (!vertex.getVertexVisitedStatus())
{
if (!checkBipartite(vertex, true))
{
return false;
}
}
}
return true;
}
private static boolean checkBipartite(Node vertex, boolean color)
{
vertex.setVertexVisitedStatusToTrue();
for (Node connectedVertex : vertex.getNodesVertexIsConnectedTo())
{
if (!connectedVertex.getVertexVisitedStatus())
{
if (!checkBipartite(connectedVertex, !color))
{
return false;
}
} else if (connectedVertex.getVertexVisitedStatus() == color)
{
return false;
}
}
return true;
}
/**
* The checkTree method takes an ArrayList<Node> object as input (representing the component) and returns true/false if the component is a tree or not.
*
* @param verticesInput the ArrayList<Node> input
* @return true or false
*/
public static boolean isTree(ArrayList<Node> verticesInput)
{
changeAllNodesToUnvisited();
int visitedCount = 0;
for (Node vertex : verticesInput)
{
if (!vertex.getVertexVisitedStatus())
{
if (!checkTree(vertex))
{
return false;
}
visitedCount++;
}
}
return visitedCount == 1;
}
/**
* The checkTree method takes an Node object as input and returns true if all neighbours of the vertex node are unvisited.
*
* @param vertex the Node input
* @return true or false
*/
private static boolean checkTree(Node vertex)
{
vertex.setVertexVisitedStatusToTrue();
for (Node connectedVertex : vertex.getNodesVertexIsConnectedTo())
{
if (connectedVertex.getVertexVisitedStatus())
{
return false;
}
checkTree(connectedVertex);
}
return true;
}
/**
* The DSatur method takes an ArrayList<Node> object as input and returns an integer denoting the highest colour used (the upper bound).
*
* @param verticesInput the ArrayList<Node> object
* @return the chromaticNumber (upper bound)
*/
public static Node findNextVertex(ArrayList<Node> verticesInput)
{
Node nextVertex = verticesInput.get(0);
for (Node node : verticesInput)
{
if (getSaturationDegree(node) > getSaturationDegree(nextVertex))
{
nextVertex = node;
}
else if (getSaturationDegree(node) == getSaturationDegree(nextVertex))
{
if (getUncolouredConnections(node) > getUncolouredConnections(nextVertex))
{
nextVertex = node;
}
}
}
return nextVertex;
}
/**
* The DSatur method takes an ArrayList<Node> object as input and returns an integer denoting the highest colour used (the upper bound).
*
* @param verticesInput the ArrayList<Node> object
* @return the chromaticNumber (upper bound)
*/
public static int DSatur(ArrayList<Node> verticesInput)
{
ArrayList<Node> verticesDynamic = new ArrayList<>(verticesInput);
while (verticesDynamic.size() > 0)
{
Node node = findNextVertex(verticesDynamic);
int newColour = findLowestMissingNumber(coloursOfAdjacentVertices(node));
node.changeColour(newColour);
verticesDynamic.remove(node);
}
ArrayList<Integer> coloursUsed = new ArrayList<>();
for (Node node : verticesInput)
{
if (!coloursUsed.contains(node.getColour()))
{
coloursUsed.add(node.getColour());
}
}
int chromaticNumber = coloursUsed.size();
return chromaticNumber;
}
/**
* The coloursOfAdjacentVertices method takes an Node object as input and returns an arraylist of all colours of adjacent vertices.
*
* @param node the Node object
* @return the arraylist of all colours of adjacent vertices
*/
public static ArrayList<Integer> coloursOfAdjacentVertices(Node vertex)
{
ArrayList<Integer> coloursOfAdjacentVertices = new ArrayList<>();
for (Node neighbourNode : vertex.getNodesVertexIsConnectedTo())
{
if (neighbourNode != null)
{
if (!coloursOfAdjacentVertices.contains(neighbourNode.getColour()))
{
coloursOfAdjacentVertices.add(neighbourNode.getColour());