Graphen
parent
048d1621a8
commit
f7afae8a41
Binary file not shown.
After Width: | Height: | Size: 25 KiB |
|
@ -0,0 +1,32 @@
|
|||
package Graphen;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Graph {
|
||||
ArrayList<Knote> Knoten = new ArrayList<>();
|
||||
|
||||
public void addknoten(Knote kn) {
|
||||
Knoten.add(kn);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Graph graph = new Graph();
|
||||
|
||||
Knote kn1 = new Knote(1);
|
||||
Knote kn2 = new Knote(2);
|
||||
Knote kn3 = new Knote(3);
|
||||
|
||||
graph.addknoten(kn1);
|
||||
graph.addknoten(kn2);
|
||||
graph.addknoten(kn3);
|
||||
|
||||
// Verbindet kn1 mit kn2 und kn1 mit kn3
|
||||
kn1.addKante(kn2, 5); // Strecke 5 Meter
|
||||
kn1.addKante(kn3, 10); // Strecke 10 Meter
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
package Graphen;
|
||||
|
||||
public class Kanten {
|
||||
//jede Kante verbindet zwei Knoten
|
||||
Knote kn1;
|
||||
Knote kn2;
|
||||
// eine Kante hat einen meter von einem Knote zu einer anderen Knote
|
||||
int strecke;
|
||||
|
||||
public Kanten(Knote kn1, Knote kn2, int strecke) {
|
||||
this.kn1 = kn1;
|
||||
this.kn2 = kn2;
|
||||
this.strecke = strecke;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
package Graphen;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Knote {
|
||||
int value;
|
||||
//eine Knote kann beliebig viele Kanten haben
|
||||
ArrayList<Kanten> KantenList = new ArrayList<>();
|
||||
|
||||
public Knote(int value) {
|
||||
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
// Methode, um eine Kante zwischen diesem Knoten und einem anderen Knoten
|
||||
// hinzuzufügen
|
||||
public void addKante(Knote zielKnote, int strecke) {
|
||||
Kanten kante = new Kanten(this, zielKnote, strecke);
|
||||
KantenList.add(kante);
|
||||
zielKnote.KantenList.add(kante);
|
||||
}
|
||||
|
||||
}
|
Binary file not shown.
After Width: | Height: | Size: 40 KiB |
Loading…
Reference in New Issue