-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.java
More file actions
110 lines (80 loc) · 2.81 KB
/
Graph.java
File metadata and controls
110 lines (80 loc) · 2.81 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
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class Graph {
private ArrayList<Integer>[] adjacent;
private int couleurs[];
public Graph(String path) throws IOException {
InputStream stream = new FileInputStream(path);
Scanner scan = new Scanner(stream);
int points;
int p1, p2;
points = scan.nextInt();
adjacent = new ArrayList[points];
couleurs = new int[points];
for (int i=0; i<points; i++){
adjacent[i] = new ArrayList<>();
couleurs[i] = -1;
}
while (scan.hasNextLine()){
p1 = scan.nextInt();
p2 = scan.nextInt();
adjacent[p1-1].add(p2-1);
adjacent[p2-1].add(p1-1);
}
scan.close();
}
public Graph(ArrayList<Integer>[] ad){
adjacent = ad;
couleurs = new int[adjacent.length];
Arrays.fill(couleurs, -1);
}
public boolean color(int k, int sommet){
Integer[] avail_col = available_colors(k, sommet); //O(avail_color)
if (avail_col.length == 0) return false; //pas de couleur possible
ArrayList<Integer> liste_adj = adjacent[sommet]; //cst
boolean res = true; //cst
for (Integer color : avail_col) { //O(k-1)
couleurs[sommet] = color; //cst
for (int s_adj : liste_adj) { //O(E) max le nombre de vecteur
if (couleurs[s_adj] == -1) res = color(k, s_adj); //O(color)
if (!res) break; //cst
}
if (res) return true; //cst
}
return false; //cst
}
public boolean color(int k){
return color(k, 0);
}
private Integer[] available_colors(int k, int sommet){
ArrayList<Integer> avail = new ArrayList<>();
ArrayList<Integer> taken = new ArrayList<>();
for (int i : adjacent[sommet]){ //O(V)
if (couleurs[i] != -1 && !taken.contains(couleurs[i])) taken.add(couleurs[i]);
}
for (int i = 0; i < k; i++){ //O(k)
if (!taken.contains(i)) avail.add(i);
}
return avail.toArray(new Integer[0]); //O(?) cst?
}
public void Print_color(){
System.out.println("coloration : ");
for (int i = 0; i < couleurs.length; i++){
System.out.println((i+1) + " " + couleurs[i]);
}
}
public boolean solved(){
for (int couleur : couleurs){
if (couleur == -1) return false;
}
return true;
}
public void setAdjacent( ArrayList<Integer>[] adj){
adjacent = adj;
}
public int[] getColors(){
return couleurs;
}
}