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
/
GrapheHHAdj.java
90 lines (73 loc) · 2.56 KB
/
GrapheHHAdj.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
package graphe.implems;
import java.util.*;
import graphe.core.*;
public class GrapheHHAdj implements IGraphe {
private Map<String, Map<String , Integer>> hhadj;
public GrapheHHAdj(String graph){
this.hhadj = new HashMap<>();
this.peupler(graph);
}
public GrapheHHAdj(){
this.hhadj = new HashMap<>();
}
@Override
public void ajouterSommet(String noeud) {
this.hhadj.putIfAbsent(noeud,new HashMap<>());
}
@Override
public void ajouterArc(String source, String destination, Integer valeur) throws IllegalArgumentException {
if (!this.hhadj.containsKey(source)){
ajouterSommet(source);
}
if (!this.hhadj.containsKey(destination)){
ajouterSommet(destination);
}
if (!this.hhadj.get(source).containsKey(destination)){
if (valeur < 0){
throw new IllegalArgumentException("Valeur négative.");
}
this.hhadj.get(source).put(destination,valeur);
}
else throw new IllegalArgumentException("Arc existe");
}
@Override
public void oterSommet(String noeud) {
if (this.hhadj.containsKey(noeud)) {
this.hhadj.remove(noeud);
}
}
@Override
public void oterArc(String source, String destination)throws IllegalArgumentException {
Map<String , Integer> interieur= this.hhadj.get(source);
if (interieur == null || !interieur.containsKey(destination))
throw new IllegalArgumentException("Arc existe pas");
this.hhadj.get(source).remove(destination);
}
@Override
public List<String> getSommets() {
List<String> l_sommet = new ArrayList<>();
this.hhadj.forEach((key, value) -> l_sommet.add((String) key));
return l_sommet;
}
@Override
public List<String> getSucc(String sommet) {
Map<String , Integer> interieur = this.hhadj.get(sommet);
List<String> l_succeseur = new ArrayList<>(interieur.keySet());
return l_succeseur;
}
@Override
public int getValuation(String src, String dest) {
return this.hhadj.get(src).getOrDefault(dest, -1);
}
@Override
public boolean contientSommet(String sommet) {
return this.hhadj.containsKey(sommet);
}
@Override
public boolean contientArc(String src, String dest) {
return this.hhadj.containsKey(src)&& this.hhadj.get(src).containsKey(dest);
}
public String toString(){
return toAString();
}
}