This repository has been archived by the owner on Oct 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GrapheMAdj.java
148 lines (122 loc) · 4.42 KB
/
GrapheMAdj.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
package graphe.implems;
import java.util.*;
import static java.lang.Math.ceil;
import graphe.core.*;
public class GrapheMAdj implements IGraphe {
private int step = 1;
private int realIndiceCount = 0;
private int[][] matrice;
private final Map<String, Integer> indices;
private final int NO_VALUATION = -1;
public GrapheMAdj() {
matrice = new int[0][0];
this.indices = new HashMap<>();
}
public GrapheMAdj(String graph) {
this();
this.peupler(graph);
}
@Override
public void ajouterSommet(String noeud) {
if (!indices.containsKey(noeud)) {
indices.put(noeud, null);
}
}
private int getMaxValue() {
int max = -1;
for (String val : indices.keySet()) {
if (indices.get(val) != null && indices.get(val) > max) {
max = indices.get(val);
}
}
return max;
}
private void addToGraph(String node) {
indices.replace(node, null, getMaxValue()+1);
realIndiceCount++;
if (matrice.length <= realIndiceCount) {
int[][] newMatrice = new int[realIndiceCount + step][realIndiceCount + step];
for (int i = 0; i < newMatrice.length; i++) {
Arrays.fill(newMatrice[i], 0, newMatrice.length, NO_VALUATION);
if (i < matrice.length)
System.arraycopy(matrice[i], 0, newMatrice[i], 0, matrice[i].length);
}
matrice = newMatrice;
System.gc();
step = (int) ceil(matrice.length / 10.0);
}
}
@Override
public void ajouterArc(String source, String destination, Integer valeur)
throws IllegalArgumentException {
if (valeur < 0) throw new IllegalArgumentException("Valeur négative.");
ajouterSommet(source);
ajouterSommet(destination);
if (indices.get(source) != null && indices.get(destination) != null) {
if (matrice[indices.get(source)][indices.get(destination)] != NO_VALUATION)
throw new IllegalArgumentException("Arc déjà présent");
} else {
if (indices.get(source) == null)
addToGraph(source);
if (indices.get(destination) == null)
addToGraph(destination);
}
matrice[indices.get(source)][indices.get(destination)] = valeur;
}
@Override
public void oterSommet(String noeud) {
if (indices.containsKey(noeud)) {
int removeAfter = indices.get(noeud);
indices.remove(noeud);
indices.forEach((sommet, valeur) -> {
if (valeur != null && valeur > removeAfter) valeur--;
});
for (int[] line : matrice) {
System.arraycopy(line, removeAfter, line, removeAfter, line.length - removeAfter);
}
}
}
@Override
public void oterArc(String source, String destination)
throws IllegalArgumentException {
try {
int src = indices.get(source), dest = indices.get(destination);
matrice[src][dest] = NO_VALUATION;
} catch (NullPointerException NPE) {
throw new IllegalArgumentException("Sommets inexistants");
}
}
@Override
public List<String> getSommets() {
return new ArrayList<>(indices.keySet());
}
@Override
public List<String> getSucc(String sommet) {
List<String> listeDesSucc = new ArrayList<>();
if (indices.get(sommet) != null) {
final int srcId = indices.get(sommet);
for (Map.Entry<String, Integer> dest : indices.entrySet()) {
if (dest.getValue() != null && matrice[srcId][dest.getValue()] != NO_VALUATION)
listeDesSucc.add(dest.getKey());
}
}
return listeDesSucc;
}
@Override
public int getValuation(String src, String dest) {
return matrice[indices.get(src)][indices.get(dest)];
}
@Override
public boolean contientSommet(String sommet) {
return indices.containsKey(sommet);
}
@Override
public boolean contientArc(String src, String dest) {
if (indices.getOrDefault(src, null) == null || indices.getOrDefault(dest, null) == null) return false;
return (matrice[indices.get(src)][indices.get(dest)] != NO_VALUATION);
}
@Override
public String toString() {
return this.toAString();
}
}