Skip to content
Snippets Groups Projects
Commit c9783796 authored by Sarah CHERCHEM's avatar Sarah CHERCHEM
Browse files

finalisation du tp

parent c96750e8
No related branches found
No related tags found
No related merge requests found
Showing
with 115 additions and 17 deletions
File added
File added
File added
File added
File added
File added
File added
File added
resources/AldousBroder.png

271 KiB

resources/Contraction.png

268 KiB

resources/Insertion.png

267 KiB

resources/Prim.png

215 KiB

resources/RandomSearch.png

269 KiB

resources/Wilson.png

271 KiB

resources/random.png

272 KiB

resources/search.png

253 KiB

...@@ -4,7 +4,7 @@ public class Edge implements Comparable<Edge> { ...@@ -4,7 +4,7 @@ public class Edge implements Comparable<Edge> {
public int source; public int source;
public int dest; public int dest;
double weight; public double weight;
public Edge(int source, int dest, double weight) { public Edge(int source, int dest, double weight) {
this.source = source; this.source = source;
......
...@@ -2,6 +2,7 @@ package Graph; ...@@ -2,6 +2,7 @@ package Graph;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List;
public class Graph { public class Graph {
...@@ -50,7 +51,10 @@ public class Graph { ...@@ -50,7 +51,10 @@ public class Graph {
} }
public void addVertex(int vertex) { public void addVertex(int vertex) {
if (vertex >= upperBound) { if (vertex >= upperBound) {
throw new IllegalArgumentException("Sommet hors limites."); throw new IllegalArgumentException("Sommet hors limites.");
} }
while (incidency.size() <= vertex) { while (incidency.size() <= vertex) {
...@@ -59,6 +63,7 @@ public class Graph { ...@@ -59,6 +63,7 @@ public class Graph {
outIncidency.add(new LinkedList<>()); outIncidency.add(new LinkedList<>());
} }
order = Math.max(order, vertex + 1); order = Math.max(order, vertex + 1);
} }
...@@ -112,7 +117,10 @@ public class Graph { ...@@ -112,7 +117,10 @@ public class Graph {
edgeCardinality++; edgeCardinality++;
} }
public int degreSommet(int vertex) {
// Le degré d'un sommet est simplement le nombre d'arêtes qui lui sont incidentes
return incidency.get(vertex).size();
}
public Arc[] outEdges(int vertex) { public Arc[] outEdges(int vertex) {
if (!isVertex(vertex)) { if (!isVertex(vertex)) {
...@@ -123,4 +131,90 @@ public class Graph { ...@@ -123,4 +131,90 @@ public class Graph {
return outIncidency.get(vertex).toArray(new Arc[0]); return outIncidency.get(vertex).toArray(new Arc[0]);
} }
public int getEdgeCardinality() {
return edgeCardinality;
}
public ArrayList<LinkedList<Edge>> getIncidency() {
return incidency;
}
public Edge getEdge(int u, int v) {
if (!isVertex(u) || !isVertex(v)) {
throw new IllegalArgumentException("Un des sommets n'existe pas.");
}
// Chercher l'arête entre u et v dans la liste des incidences du sommet u
for (Edge edge : incidency.get(u)) {
if (edge.oppositeExtremity(u) == v) {
return edge;
}
}
// Si l'arête n'existe pas, retourner null
return null;
}
public List<Integer> getVertices() {
List<Integer> vertices = new ArrayList<>();
for (int i = 0; i < order; i++) {
if (incidency.get(i) != null) { // vérifier si le sommet existe
vertices.add(i);
}
}
return vertices;
}
public Graph copy() {
Graph newGraph = new Graph(this.upperBound); // Crée un nouveau graphe avec le même upperBound
newGraph.order = this.order; // Copie l'ordre du graphe
newGraph.edgeCardinality = this.edgeCardinality; // Copie le nombre d'arêtes
// Copier les arêtes (incidency)
for (int i = 0; i < this.incidency.size(); i++) {
if (this.incidency.get(i) != null) {
LinkedList<Edge> newEdges = new LinkedList<>();
for (Edge edge : this.incidency.get(i)) {
// Crée un nouvel Edge en copiant la source, destination et poids
newEdges.add(new Edge(edge.getSource(), edge.getDest(), edge.weight));
}
newGraph.incidency.set(i, newEdges);
} else {
newGraph.incidency.set(i, null);
}
}
// Copier les arcs entrants (inIncidency)
for (int i = 0; i < this.inIncidency.size(); i++) {
if (this.inIncidency.get(i) != null) {
LinkedList<Arc> newInArcs = new LinkedList<>();
for (Arc arc : this.inIncidency.get(i)) {
// Crée un nouvel Arc en copiant le support (Edge) et la direction (reversed)
Edge edgeCopy = new Edge(arc.support.getSource(), arc.support.getDest(), arc.support.weight);
newInArcs.add(new Arc(edgeCopy, arc.reversed));
}
newGraph.inIncidency.set(i, newInArcs);
} else {
newGraph.inIncidency.set(i, null);
}
}
// Copier les arcs sortants (outIncidency)
for (int i = 0; i < this.outIncidency.size(); i++) {
if (this.outIncidency.get(i) != null) {
LinkedList<Arc> newOutArcs = new LinkedList<>();
for (Arc arc : this.outIncidency.get(i)) {
// Crée un nouvel Arc en copiant le support (Edge) et la direction (reversed)
Edge edgeCopy = new Edge(arc.support.getSource(), arc.support.getDest(), arc.support.weight);
newOutArcs.add(new Arc(edgeCopy, arc.reversed));
}
newGraph.outIncidency.set(i, newOutArcs);
} else {
newGraph.outIncidency.set(i, null);
}
}
return newGraph;
}
} }
...@@ -189,6 +189,7 @@ public class RootedTree { ...@@ -189,6 +189,7 @@ public class RootedTree {
public int getDepth(int vertex) { public int getDepth(int vertex) {
return nodes[vertex].depth; return nodes[vertex].depth;
} }
public int getSubtreeSize(int vertex) { public int getSubtreeSize(int vertex) {
...@@ -290,6 +291,7 @@ public class RootedTree { ...@@ -290,6 +291,7 @@ public class RootedTree {
nodes = new Node[order]; nodes = new Node[order];
nodes[root] = new Node(root); nodes[root] = new Node(root);
this.bfsOrder.add(nodes[root]); this.bfsOrder.add(nodes[root]);
for (Arc arc : sortedArcs) { for (Arc arc : sortedArcs) {
createNode(nodes,arc); createNode(nodes,arc);
...@@ -315,7 +317,7 @@ public class RootedTree { ...@@ -315,7 +317,7 @@ public class RootedTree {
computeAllDepths(); computeAllDepths();
} }
public int getOrder() {
return order;
}
} }
...@@ -192,7 +192,9 @@ public class Labyrinth extends JPanel { ...@@ -192,7 +192,9 @@ public class Labyrinth extends JPanel {
if (grid.isHorizontal(e)) drawHorizontalEdge(g,e); if (grid.isHorizontal(e)) drawHorizontalEdge(g,e);
else drawVerticalEdge(g,e); else drawVerticalEdge(g,e);
} }
for (int i = 0; i < grid.graph.order; i++) { for (int i = 0; i < tree.getOrder(); i++) {
drawVertex(g,i); drawVertex(g,i);
} }
if (tree != null) drawRoot(g,tree.getRoot()); if (tree != null) drawRoot(g,tree.getRoot());
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment