Skip to content
Snippets Groups Projects
Commit c96750e8 authored by AREZKI Celia's avatar AREZKI Celia
Browse files

Graph : completing the class methods

parent dbd09d2e
No related branches found
No related tags found
No related merge requests found
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_13" default="false" project-jdk-name="13" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_13" project-jdk-name="jbr-17" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
......@@ -27,42 +27,97 @@ public class Graph {
// par suppression de sommet, ou l'opération de contraction d’arête.
// Autrement, on pourra asssumer que upperBound==order.
// à compléter
this.upperBound = upperBound;
this.order = 0;
this.edgeCardinality = 0;
this.incidency = new ArrayList<>(upperBound);
this.inIncidency = new ArrayList<>(upperBound);
this.outIncidency = new ArrayList<>(upperBound);
for (int i = 0; i < upperBound; i++) {
incidency.add(new LinkedList<>());
inIncidency.add(new LinkedList<>());
outIncidency.add(new LinkedList<>());
}
}
public boolean isVertex(int vertex) {
// Après avori supprimé certains sommets
// pas tous le sommets numerotés 0,...,n-1 sont 'vivant'.
// à compléter
return true;
return vertex >= 0 && vertex < order && incidency.get(vertex) != null;
}
public void addVertex(int vertex) {
// à compléter
if (vertex >= upperBound) {
throw new IllegalArgumentException("Sommet hors limites.");
}
while (incidency.size() <= vertex) {
incidency.add(new LinkedList<>());
inIncidency.add(new LinkedList<>());
outIncidency.add(new LinkedList<>());
}
order = Math.max(order, vertex + 1);
}
public void deleteVertex(int vertex) {
// à compléter
if (vertex >= incidency.size() || incidency.get(vertex) == null) {
throw new IllegalArgumentException("Sommet inexistant.");
}
for (Edge edge : incidency.get(vertex)) {
int other = edge.oppositeExtremity(vertex);
incidency.get(other).remove(edge);
}
incidency.set(vertex, null);
inIncidency.set(vertex, null);
outIncidency.set(vertex, null);
}
public void ensureVertex(int vertex) {
// Synonime de addVertex ?
// à compléter
public void ensureVertex(int vertex) {
if (vertex >= upperBound) {
throw new IllegalArgumentException("Sommet hors limites.");
}
if(incidency.size() <= vertex){
addVertex(vertex);
}
}
public void addArc(Arc arc) {
// à compléter
int source = arc.getSource();
int dest = arc.getDest();
ensureVertex(source);
ensureVertex(dest);
outIncidency.get(source).add(arc);
inIncidency.get(dest).add(arc);
}
public void addEdge(Edge edge) {
// à compléter
int source = edge.getSource();
int dest = edge.getDest();
addVertex(source);
addVertex(dest);
incidency.get(source).add(edge);
incidency.get(dest).add(edge);
addArc(new Arc(edge, false));
addArc(new Arc(edge, true));
edgeCardinality++;
}
public Arc[] outEdges(int vertex) {
// à modifier, si nécessaire
public Arc[] outEdges(int vertex) {
if (!isVertex(vertex)) {
throw new IllegalArgumentException("Sommet inexistant.");
}
// Pour la prochaine ligne voir
// https://www.baeldung.com/java-collection-toarray-methods
return outIncidency.get(vertex).toArray(new Arc[0]);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment