Graphen
parent
7fe3e8f765
commit
7f3f5d6ded
|
@ -12,6 +12,8 @@ public class Graph {
|
||||||
KnotenGraph.add(k);
|
KnotenGraph.add(k);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public Knote getKnote(String name) {
|
public Knote getKnote(String name) {
|
||||||
for (Knote kAusKnotenGraph: KnotenGraph) {
|
for (Knote kAusKnotenGraph: KnotenGraph) {
|
||||||
if (kAusKnotenGraph.getName().equalsIgnoreCase(name))
|
if (kAusKnotenGraph.getName().equalsIgnoreCase(name))
|
||||||
|
|
|
@ -1,22 +1,61 @@
|
||||||
package Graphen;
|
package Graphen;
|
||||||
|
|
||||||
public class Kanten {
|
public class Kanten {
|
||||||
//jede Kante verbindet zwei Knoten
|
// jede Kante verbindet zwei Knoten
|
||||||
Knote anfangsKnote; // mannheim
|
private Knote anfangsKnote;
|
||||||
Knote endeKnote; // heidelberg
|
private Knote endeKnote;
|
||||||
|
private boolean UndirectedGraph;
|
||||||
|
private int gewicht;
|
||||||
|
|
||||||
|
// Kante verbindet zwei Knoten (Undirected Graph)
|
||||||
|
public Kanten(Knote anfangsKnote, Knote endeKnote, boolean UndirectedGraph) {
|
||||||
|
this.anfangsKnote = anfangsKnote;
|
||||||
|
this.endeKnote = endeKnote;
|
||||||
|
this.UndirectedGraph = UndirectedGraph;
|
||||||
|
anfangsKnote.addKante(this); //speichert dann zu ihrem ArrayList diese Kante
|
||||||
|
endeKnote.addKante(this);//speichert dann auch zu ihrem ArrayList diese gleiche Kante
|
||||||
|
}
|
||||||
|
|
||||||
|
// Kante verbindet eine Knote (Directed Graph)
|
||||||
public Kanten(Knote anfangsKnote, Knote endeKnote) {
|
public Kanten(Knote anfangsKnote, Knote endeKnote) {
|
||||||
this.anfangsKnote = anfangsKnote;
|
this.anfangsKnote = anfangsKnote;
|
||||||
this.endeKnote = endeKnote;
|
this.endeKnote = endeKnote;
|
||||||
anfangsKnote.addKante(this);
|
anfangsKnote.addKante(this);
|
||||||
endeKnote.addKante(this);
|
}
|
||||||
|
|
||||||
|
// Kante verbindet eine Knoten und mit Gewicht (Weighted Directed Graph)
|
||||||
|
public Kanten(Knote anfangsKnote, Knote endeKnote, int gewicht) {
|
||||||
|
this.anfangsKnote = anfangsKnote;
|
||||||
|
this.endeKnote = endeKnote;
|
||||||
|
//anfangsKnote.addkanteMitGewicht(this,gewicht);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Kante verbindet zwei Knoten und mit Gewicht (Weighted UnDirected Graph)
|
||||||
|
public Kanten(Knote anfangsKnote, Knote endeKnote, int gewicht, boolean UndirectedGraph) {
|
||||||
|
this.anfangsKnote = anfangsKnote;
|
||||||
|
this.endeKnote = endeKnote;
|
||||||
|
// anfangsKnote.addkanteMitGewicht(this, gewicht);
|
||||||
|
// endeKnote.addkanteMitGewicht(this, gewicht);
|
||||||
|
this.UndirectedGraph = UndirectedGraph;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Knote getPartner(Knote k) {
|
public Knote getPartner(Knote k) {
|
||||||
Knote partner = (k == anfangsKnote) ? endeKnote : anfangsKnote;
|
return (k == anfangsKnote) ? endeKnote : anfangsKnote;
|
||||||
|
|
||||||
return partner;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getGewicht() {
|
||||||
|
return gewicht;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Knote getAnfangsKnote() {
|
||||||
|
return anfangsKnote;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Knote getEndeKnote() {
|
||||||
|
return endeKnote;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,20 +1,24 @@
|
||||||
package Graphen;
|
package Graphen;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
public class Knote {
|
public class Knote {
|
||||||
String name;
|
private String name;
|
||||||
ArrayList<Kanten> kantenList = new ArrayList<>();
|
private ArrayList<Kanten> kantenList = new ArrayList<>();
|
||||||
|
|
||||||
|
|
||||||
Knote(String name){
|
Knote(String name){
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//add kante zu this (knote)
|
||||||
public void addKante(Kanten k) {
|
public void addKante(Kanten k) {
|
||||||
kantenList.add(k);
|
kantenList.add(k);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
@ -24,13 +28,12 @@ public class Knote {
|
||||||
return kantenList;
|
return kantenList;
|
||||||
}
|
}
|
||||||
|
|
||||||
ArrayList<Knote> verbindeteKnoten;
|
|
||||||
public void getPartner() {
|
public void getPartner() {
|
||||||
ArrayList<Kanten> merker = getKantenList();
|
ArrayList<Kanten> merker = getKantenList();
|
||||||
if (merker == null)
|
if (merker == null)
|
||||||
return ;
|
return ;
|
||||||
|
|
||||||
verbindeteKnoten = new ArrayList<>();
|
ArrayList<Knote> verbindeteKnoten = new ArrayList<>();
|
||||||
for (Kanten m : merker)
|
for (Kanten m : merker)
|
||||||
verbindeteKnoten.add(m.getPartner(this));
|
verbindeteKnoten.add(m.getPartner(this));
|
||||||
|
|
||||||
|
|
|
@ -4,15 +4,17 @@ public class Test {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
Graph graph = new Graph();
|
Graph graph = new Graph();
|
||||||
|
|
||||||
Knote mannheim = new Knote("Mannheim");
|
Knote mannheim = new Knote("Mannheim");
|
||||||
Knote heidelberg = new Knote("Heidelberg");
|
Knote heidelberg = new Knote("Heidelberg");
|
||||||
|
Knote Maiz = new Knote("Maiz");
|
||||||
graph.addknoten(mannheim);
|
graph.addknoten(mannheim);
|
||||||
graph.addknoten(heidelberg);
|
graph.addknoten(heidelberg);
|
||||||
|
graph.addknoten(Maiz);
|
||||||
|
|
||||||
new Kanten(graph.getKnote("Mannheim"),graph.getKnote("Heidelberg"));
|
|
||||||
|
|
||||||
mannheim.getPartner();
|
Kanten k3 = new Kanten(graph.getKnote("Heidelberg"),graph.getKnote("Maiz"),25,true);
|
||||||
heidelberg.getPartner();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue