68 lines
1.7 KiB
Java
68 lines
1.7 KiB
Java
package GraphenTeorie;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
public class Graph {
|
|
|
|
private ArrayList<Knoten> knotenArrayList;
|
|
private ArrayList<Kante> kantenArrayList;
|
|
|
|
|
|
public Graph () {
|
|
this.knotenArrayList = new ArrayList<>();
|
|
this.kantenArrayList = new ArrayList<>();
|
|
}
|
|
|
|
|
|
public Knoten addKnoten() {
|
|
Knoten neu = new Knoten();
|
|
this.knotenArrayList.add(neu);
|
|
return neu;
|
|
}
|
|
|
|
|
|
public Kante addKante(Knoten von, Knoten nach) {
|
|
Kante neu = new Kante(von, nach);
|
|
this.kantenArrayList.add(neu);
|
|
return neu;
|
|
}
|
|
|
|
|
|
public void removeKnoten(Knoten loeschObjekt) {
|
|
for (Kante i: loeschObjekt.getAnliegendeKanten()) {
|
|
this.removeKante(i);
|
|
}
|
|
this.knotenArrayList.remove(loeschObjekt);
|
|
}
|
|
|
|
|
|
public void removeKante(Kante loeschObjekt) {
|
|
this.kantenArrayList.remove(loeschObjekt);
|
|
}
|
|
|
|
|
|
public void fusioniereKnoten(Knoten k1, Knoten k2) {
|
|
Knoten fusion = this.addKnoten();
|
|
for (Kante i: k1.getEingehendeKanten()) {
|
|
fusion.addEingehendeKante(this.addKante(i.getOtherSide(k1), fusion));
|
|
}
|
|
for (Kante i: k2.getEingehendeKanten()) {
|
|
fusion.addEingehendeKante(this.addKante(i.getOtherSide(k2), fusion));
|
|
}
|
|
for (Kante i: k1.getAusgehendeKanten()) {
|
|
fusion.addEingehendeKante(this.addKante(fusion, i.getOtherSide(k1)));
|
|
}
|
|
for (Kante i: k2.getAusgehendeKanten()) {
|
|
fusion.addEingehendeKante(this.addKante(fusion, i.getOtherSide(k2)));
|
|
}
|
|
|
|
this.removeKnoten(k1);
|
|
this.removeKnoten(k2);
|
|
}
|
|
|
|
|
|
public void kontrahiereKante(Kante k1) {
|
|
this.fusioniereKnoten(k1.getStartKnoten(), k1.getEndKnoten());
|
|
}
|
|
}
|