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