From c96750e8e1bd7d3c25f902bd7ddc7aafe684dab0 Mon Sep 17 00:00:00 2001 From: celia <celia.arezki.1@etu.univ-amu.fr> Date: Thu, 12 Dec 2024 16:22:51 +0100 Subject: [PATCH] Graph : completing the class methods --- .idea/misc.xml | 2 +- src/Graph/Arc.java | 8 ++--- src/Graph/Graph.java | 81 +++++++++++++++++++++++++++++++++++++------- 3 files changed, 73 insertions(+), 18 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index 8f5f4fa..8a372ca 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,6 @@ <?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 diff --git a/src/Graph/Arc.java b/src/Graph/Arc.java index 7de5608..1b79160 100644 --- a/src/Graph/Arc.java +++ b/src/Graph/Arc.java @@ -3,18 +3,18 @@ package Graph; public class Arc { public Edge support; boolean reversed; - + public Arc(Edge e, boolean reversed) { this.support = e; this.reversed = reversed; } - + public int getSource() { return (reversed ? support.getDest() : support.getSource()); } - + public int getDest() { return (reversed ? support.getSource() : support.getDest()); } - + } diff --git a/src/Graph/Graph.java b/src/Graph/Graph.java index bffae38..383b0b9 100644 --- a/src/Graph/Graph.java +++ b/src/Graph/Graph.java @@ -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 + + public void deleteVertex(int vertex) { + 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]); -- GitLab