Uebungsaufgaben/GraphenTeorie/Graph.java

191 lines
4.2 KiB
Java
Raw Normal View History

2024-05-18 20:35:28 +00:00
package GraphenTeorie;
import java.util.ArrayList;
public class Graph {
private ArrayList<Knoten> knotenArrayList;
private ArrayList<Kante> kantenArrayList;
2024-05-22 21:00:22 +00:00
ArrayList<ArrayList<Object>> adjazenzmatrix;
ArrayList<ArrayList<Object>> adjazenzliste;
2024-05-18 20:35:28 +00:00
public Graph () {
this.knotenArrayList = new ArrayList<>();
this.kantenArrayList = new ArrayList<>();
2024-05-22 21:00:22 +00:00
this.adjazenzmatrix = createAdjazenzmatrix();
this.adjazenzliste = createAdjazenzliste();
}
public static void main(String[] args) {
Graph meinGraph = new Graph();
meinGraph.addKnoten();
meinGraph.addKnoten();
meinGraph.addKnoten();
// meinGraph.addKnoten();
// meinGraph.addKnoten();
// meinGraph.addKnoten();
System.out.println(meinGraph.createAdjazenzmatrix());
2024-05-18 20:35:28 +00:00
}
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());
}
2024-05-22 21:00:22 +00:00
public ArrayList<ArrayList<Object>> createAdjazenzmatrix() {
ArrayList<ArrayList<Object>> matrix = new ArrayList<>();
matrix.add(new ArrayList<Object>());
for (int i = 0; i < this.knotenArrayList.size(); i++) {
matrix.get(0).add(knotenArrayList.get(i));
matrix.add(new ArrayList<Object>());
matrix.get(i+1).add(knotenArrayList.get(i));
}
for (ArrayList<Object> i: matrix) {
for (int j = 0; j < matrix.getFirst().size(); j++) {
i.add(0);
}
}
for (Kante i: kantenArrayList) {
int von = matrix.getFirst().indexOf(i.getStartKnoten());
int nach = matrix.getFirst().indexOf(i.getEndKnoten());
ArrayList<Object> neu = matrix.get(von+1);
neu.set(nach, i.getGewichtung());
matrix.set(von+1, neu);
}
return matrix;
}
public ArrayList<ArrayList<Object>> createAdjazenzliste() {
return null;
}
public boolean isUntergraphOf(Graph g1) {
return true;
}
public boolean isTeilgraphOf(Graph g1) {
return true;
}
public boolean isZusammenhaengend() {
return true;
}
public boolean isVollstaendig() {
return true;
}
// Graph is 2 colorble or no odd length cycles
public boolean isBipartit() {
return true;
}
public boolean isVollstaendigBipartit() {
return true;
}
public boolean isZyklus() {
return true;
}
public boolean isRegulaer() {
return true;
}
public boolean isPlanar() {
return true;
}
public int durchmesser() {
return 1;
}
public int radius() {
return 1;
}
public ArrayList<Knoten> rand() {
return null;
}
public ArrayList<Knoten> zentrum() {
return null;
}
public boolean isIsomorphTo(Graph g1) {
return true;
}
2024-05-18 20:35:28 +00:00
}