23. Graphen in Java

Werbung
2
Prof. U. Aßmann, Softwaretechnologie, TU Dresden
2) Das Graph Framework JGraphT
Institut für Software- und
Multimediatechnik
Version 13-1.1, 13.05.13
Softwaretechnologie, © Prof. Uwe Aßmann
Technische Universität Dresden, Fakultät Informatik
Technische Universität Dresden
Fakultät für Informatik
Lehrstuhl Softwaretechnologie
1) Entwurfsmuster Fabrikmethode
Prof. Dr. rer. nat. Uwe Aßmann
►
–
–
Javadoc http://www.jgrapht.org/javadoc
http://sourceforge.net/apps/mediawiki/jgrapht/index.php?title=jgrapht:Docs
JDK Tutorial für J2SE oder J2EE, www.java.sun.com
Dokumentation der Jgrapht library http://www.jgrapht.org/
Obligatorische Literatur
►
1
23. Graphen in Java
3
4
Prof. U. Aßmann, Softwaretechnologie, TU Dresden
Prof. U. Aßmann, Softwaretechnologie, TU Dresden
–
–
Facetten von Graphen und wie man sie systematisch, noch besser in einem
Framework anordnet
Siehe Vorlesung “Design Patterns and Frameworks”
[HB01] Roberto E. Lopez-Herrejon and Don S. Batory. A standard problem
for evaluating product-line methodologies. In Jan Bosch, editor, GCSE,
volume 2186 of Lecture Notes in Computer Science, pages 10-23. Springer,
2001.
►
►
►
►
–
–
Generatoren
Graphanalysen
Eine Graphen-Bibliothek kennenlernen
Graphen als spezielle Kollaborationen kennenlernen
Fabriken, Iteratoren und Streams in der Anwendung bei Graphen
Generische Graphalgorithmen kennenlernen
Ziele
►
Nicht-obligatorische Literatur
6
Prof. U. Aßmann, Softwaretechnologie, TU Dresden
►
►
ConcreteProduct2
ConcreteProduct1
Product
Wie variiert man die Erzeugung für eine polymorphe Hierarchie von
Produkten?
Problem: Konstruktoren sind nicht polymorph!
Problem der Fabrikmethode
Softwaretechnologie, © Prof. Uwe Aßmann
Technische Universität Dresden, Fakultät Informatik
und zum Verbergen von Produkt-Arten
zur polymorphen Variation von Komponenten (Produkten)
...
Product = new ConcreteProduct1()
...
5
23.1 Implementierungsmuster
Fabrikmethode (FactoryMethod)
7
8
Prof. U. Aßmann, Softwaretechnologie, TU Dresden
ConcreteProduct
Product
FactoryMethod()
ConcreteCreator
FactoryMethod()
anOperation()
Creator
return new ConcreteProduct
...
Product = FactoryMethod()
...
FactoryMethod ist eine Variante von TemplateMethod, zur
Produkterzeugung [Gamma95]
►

// Concrete creator class
// Concrete creator class
public class ConcreteCreator
public class ConcreteCreator
extends Creator {
extends Creator {
public Set createSet(int n) {
public Set createSet(int n) {
return new ListBasedSet(n);
return new ListBasedSet(n);
}
}
...
...
}
}
Konkrete Unterklassen spezialisieren den Allokator
// Abstract creator class
// Abstract creator class
public abstract class Creator {
public abstract class Creator {
// factory method
// factory method
public abstract Set createSet(int n);
public abstract Set createSet(int n);
}
}
Allokatoren in einer abstrakten Oberklasse nennt man Fabrikmethoden (polymorphe
Konstruktoren)
Fabrikmethode (Factory Method)
►
Struktur Fabrikmethode
public class Client {
public class Client {
... Creator cr = [.. subclass ]..
... Creator cr = [.. subclass ]..
public void collect() {
public void collect() {
Set mySet = cr.createSet(10);
Set mySet = cr.createSet(10);
....
....
}
}
}
}
Prof. U. Aßmann, Softwaretechnologie, TU Dresden
9
10
Prof. U. Aßmann, Softwaretechnologie, TU Dresden
Prof. U. Aßmann, Softwaretechnologie, TU Dresden
Klasse Formular hat eine
Schablonenmethode zur Planung
der Struktur von Formularen
Abstrakte Methoden: createPart,
createFormField, createTable,
createRow
Benutzer können Art des Formulars
verfeinern
Wie kann das Rahmenwerk neue
Arten von Formularen behandeln?


Framework (Rahmenwerk) für
Formulare
createPart()
createFormField()
createTable()
createRow()
Bestellung
construct()
createPartl()
createFormField()
createTable()
createRow()
Formular
►
Extensions
Framework
...
form = new Formular();
form.constructPart();
...
form.createTable();
...
form.createRow();
...
form.createFormField();
...
// concrete creator class
// concrete creator class
public class Bestellung extends Formular {
public class Bestellung extends Formular {
Bestellung() {
Bestellung() {
...
...
}
}
public Formular createFormular() {
public Formular createFormular() {
... fill in more info ...
... fill in more info ...
return new Bestellung();
return new Bestellung();
}
}
...
...
}
}
// abstract creator class
// abstract creator class
public abstract class Formular {
public abstract class Formular {
public abstract
public abstract
Formular createFormular();
Formular createFormular();
...
...
}
}
Bilde createFormular() als Fabrikmethode aus
Lösung mit FactoryMethod
►
►
►
Beispiel FactoryMethod für Formulare
11
12
Prof. U. Aßmann, Softwaretechnologie, TU Dresden
Prof. U. Aßmann, Softwaretechnologie, TU Dresden

#createPeer(): StockImpl
CountingStockImpl
+clone() : Object
#createPeer(): StockImpl
StockImpl
z.B. mit einem CountingStockImpl, der weiß, wieviele Produkte noch da sind
Anwender von SalesPoint verfeinern die StockImpl-Klasse, die ein Produkt
des Warenhauses im Lager repräsentiert
Bestellung
Anwendung
Lieferschein
Rechnung
Formular
Klassischer
Stil
Formular
Stil
Moderner
Stil
In Rahmenwerk-Architekturen wird die Fabrikmethode eingesetzt, um von
oberen Schichten (Anwendungsschichten) aus die Rahmenwerkschicht zu
konfigurieren:
Rahmenwerk
►
Einsatz in Komponentenarchitekturen
►
Factory Method im SalesPoint-Rahmenwerk
14
Prof. U. Aßmann, Softwaretechnologie, TU Dresden
13
►
►
Person
Person
Person
Person
Vorgesetzter
*
Mitarbeiter
Vorgesetzter
Mitarbeiter
<<Collaboration>>
Veranstaltungsplan
Treffen
Treffen
Ein Graph verknüpft zwei Mengen von Objekten (Knotenmengen) mit einer
Assoziation und bietet Navigationsverhalten an
Ein Hypergraph verknüpft mehrere Knotenmengen mit einer n-stelligen
Relation
*
*
Termin
Termin
Graphen bilden spezielle binäre Kollaborationen; Hypergraphen spezielle nstellige Kollaborationen,
die Navigationen als Verhalten anbieten
*
–
–
UML bietet das Sprachkonstrukt “Assoziation”. Eine nicht-fixe Assoziation
oder Relation besteht aus einer dynamisch wachsenden Tabelle mit einer
Menge von Tupeln
Graphen als spezielle Kollaborationen
Softwaretechnologie, © Prof. Uwe Aßmann
Technische Universität Dresden, Fakultät Informatik
Fabriken überall
23.2 Das JGraphT Framework
15
16
Prof. U. Aßmann, Softwaretechnologie, TU Dresden
Prof. U. Aßmann, Softwaretechnologie, TU Dresden
Fabrikmethoden, Generics und Iteratoren werden genutz
–
Multi-Graphen (mit mehreren gleichen Kanten zwischen 2 Knoten)
Typisierte Graphen (mit Typen und Attributen)
Konstante Graphen, nicht-modifizierbar
Kantenobjekte mit Attributen, z.B. gewichtete Graphen
Beobachtbare Graphen (mit Observer-Entwurfsmuster)
–
–
–
–
–
Andere: Netzwerkflüsse...
–
Multiple Edges
Unique Edges
Multiple Edges
Directed
Bidirectional
Direction
Cyclic
Cycle graph
(hamiltonian)
Acyclic
Cyclicity
Weight
Klassifikationsfacetten von Graphen
Navigation: Pfadsuche, Iteration, Navigation, Abstände
–
Generische Algorithmen auf Graphen
Teilgraphen
–
►
Untergraphen
–
Für Graphen auf Objekten, XML Objekten, URLs, Strings, Graphen …
Inverse Graphen
–
Sichten auf Graphen
Gerichtete azyklische Graphen (directed acyclic graphs, DAG)
–
Unterscheidung von speziellen Formen von Graphen
[JGraphT] stellt eine Bibliothek mit einer einfachen Abstraktion von Graphen dar
–
Es gibt sehr viele Varianten von Graphen; ähnlich zu Collections haben sie viele Facetten
Java bietet keine Sprachkonstrukte für Assoziationen und Graphen; Graphen müssen durch
ein Framework dargestellt werden
►
►
►
►
►
Ziele einer Graph-Bibliothek
17
18
Prof. U. Aßmann, Softwaretechnologie, TU Dresden
Prof. U. Aßmann, Softwaretechnologie, TU Dresden
Sichten
Mutatoren
EdgeReversedGraph
AsUndirectedGraph
Utilities
mit Beispielen
DefaultDirectedGraph
WeightedMultigraph
Kern-Datenstrukturen
gerichtet, ungerichtet
DepthFirstIterator
Iteratoren
(Streams)
DijkstraShortestPath
Graphanalyse
RingGraphGenerator
Generatoren
Graphanalyse
Sichten einziehen
Kanten addieren
Knoten addieren
Allokation eines Graphen spezifischer
Kategorie
Graphen dienen dem Aufbau von
Objektnetzen
►
NeighborIndex
Indizierer
MatrixExporter
Extender
Kategorien von Graphalgorithmen in
JGraphT
19
20
Prof. U. Aßmann, Softwaretechnologie, TU Dresden
Prof. U. Aßmann, Softwaretechnologie, TU Dresden
// Constructors (doesnt use a factory)
DefaultDirectedGraph(java.lang.Class<? extends E> edgeClass)
// Creates a new directed graph.
DefaultDirectedGraph(EdgeFactory<V,E> ef)
// Creates a new directed graph with the specified edge factory.
// Query methods
java.util.Set<E> incomingEdgesOf(V vertex)
// Returns a set of all edges incoming into the specified vertex.
int
inDegreeOf(V vertex)
// Returns the "in degree" of the specified vertex.
int
outDegreeOf(V vertex)
// Returns the "out degree" of the specified vertex.
java.util.Set<E> outgoingEdgesOf(V vertex)
// Returns a set of all edges outgoing from the specified vertex.
DirectedGraph<V,E>
DirectedGraph.java in JGraphT
// Query-Methoden
// Query-Methoden edgeSet()
java.util.Set<E>
java.util.Set<E> vertexSet()
edgeSet()
java.util.Set<V>
java.util.Set<V>
vertexSet()
java.util.Set<E>
edgesOf(V vertex)
java.util.Set<E>
vertex)
// Returns aedgesOf(V
set of all
edges touching the specified vertex.
// Returns a set of all edges touching the specified vertex.
java.util.Set<E>
getAllEdges(V sourceVertex, V targetVertex)
java.util.Set<E>
getAllEdges(V sourceVertex, V targetVertex)
E
getEdge(V sourceVertex, V targetVertex)
E
getEdge(V sourceVertex, V targetVertex)
// Returns an edge connecting source vertex to target vertex if such vertices
Returns
an edge
connecting
source vertex to target vertex if such vertices
////and
such edge
exist
in this graph.
//
and
such
edge
exist
in
this
EdgeFactory<V,E>
getEdgeFactory()graph.
EdgeFactory<V,E>
getEdgeFactory()
V
getEdgeSource(E
e)
V
getEdgeSource(E
V
getEdgeTarget(E e)e)
V
getEdgeTarget(Ee)e)
double
getEdgeWeight(E
double
getEdgeWeight(E e)
// Check-Methoden
//
Check-Methoden
boolean containsEdge(E e)
boolean containsEdge(V
containsEdge(EsourceVertex,
e)
boolean
V targetVertex)
boolean containsEdge(V sourceVertex, V targetVertex)
boolean containsVertex(V v)
containsVertex(V v)
// boolean
Modifikatoren
//
Modifikatoren
E
addEdge(V sourceVertex, V targetVertex)
E
addEdge(V sourceVertex,
V targetVertex)
boolean
addVertex(V
v)
boolean
addVertex(V
v)
boolean removeAllEdges(java.util.Collection<? extends E> edges)
boolean //removeAllEdges(java.util.Collection<?
extends
edges)
Removes all the edges in this graph that
are E>
also
contained in the
//
Removes
all
the
edges
in
this
graph
that
are
also
contained in the
// specified edge collection.
//
specified
edge
collection.
java.util.Set<E>
removeAllEdges(V sourceVertex, V targetVertex)
java.util.Set<E>
removeAllEdges(V sourceVertex,extends
V targetVertex)
boolean
removeAllVertices(java.util.Collection<?
V> vertices)
boolean //removeAllVertices(java.util.Collection<?
extends
vertices)
Removes all the vertices in this graph that are V>
also
contained in the
//
Removes
all
the
vertices
in
this
graph
that
are
also
contained in the
// specified vertex collection.
//
specified
vertex
collection.
boolean removeEdge(E e)
removeEdge(EsourceVertex,
e)
E boolean removeEdge(V
V targetVertex)
E
removeEdge(V
sourceVertex,
targetVertex)
// Removes an edge
going from Vsource
vertex to target vertex, if such vertices
//
Removes
an
edge
going
from
source vertex to target vertex, if such vertices
// and such edge exist in this graph.
// and such edge
boolean removeVertex(V
v) exist in this graph.
boolean removeVertex(V v)
<<interface>> DirectedGraph<V,E>
21
22
Prof. U. Aßmann, Softwaretechnologie, TU Dresden
Prof. U. Aßmann, Softwaretechnologie, TU Dresden
V,E
V,E
Undirected
Graph
V,E
<<abstract>>
AbstractGraph
Weighted
Graph
V,E
Listenable
Graph
gerichtet
Default
DirectedGraph
V,E
Abstract
BaseGraph
V,E
mehrkantig
mehrkantig
Multigraph
V,E
V,E
Directed
MultiGraph
GraphUnion
V,E
V,E
Masked
SubGraph
azyklisch
Simple
DirectedGraph
V,E
Graph
Delegator
V,E
AbstractGraph
V,E
V,E
V,E
azyklisch
SimpleGraph
V,E
SubGraph
Die Implementierungshierarchie Graph
Directed
Graph
Abstract
BaseGraph
<<interface>>
Directed
Graph
<<interface>>
Graph
Die Klassenhierarchie Graph
23
Adam
Alice
Sommerlath
Walter
Sommerlath
Sibylla
GustavAdolf
Ralf
Silvia
Carl Gustav
Desiree
Familienbeziehungen sind immer azyklisch
Die schwedische Königsfamilie:
Madeleine
Carl Philipp
Victoria
// add edges
// add edges
parentOf.addEdge("Adam",
"Gustav Adolf");
parentOf.addEdge("Adam","Sibylla");
"Gustav Adolf");
parentOf.addEdge("Adam",
parentOf.addEdge("Adam",
"Sibylla");
parentOf.addEdge("Adam", "Walter
Sommerlath");
parentOf.addEdge("Adam","Alice
"Walter
Sommerlath");
parentOf.addEdge("Adam",
Sommerlath");
parentOf.addEdge("Adam",
"Alice
Sommerlath");
parentOf.addEdge("Walter Sommerlath","Sylvia");
parentOf.addEdge("Walter
Sommerlath","Sylvia");
parentOf.addEdge("Alice
Sommerlath","Sylvia");
parentOf.addEdge("Alice Sommerlath","Ralf");
Sommerlath","Sylvia");
parentOf.addEdge("Walter
parentOf.addEdge("Walter
Sommerlath","Ralf");
parentOf.addEdge("Alice
Sommerlath","Ralf");
parentOf.addEdge("Alice Adolf",
Sommerlath","Ralf");
parentOf.addEdge("Gustav
"Carl Gustav");
parentOf.addEdge("Gustav Adolf",
"Carl Gustav");
parentOf.addEdge("Sibylla",
"Carl Gustav");
parentOf.addEdge("Sibylla",
"Carl
Gustav");
parentOf.addEdge("Gustav Adolf", "Desiree");
parentOf.addEdge("Gustav Adolf",
"Desiree");
parentOf.addEdge("Sibylla",
"Desiree");
parentOf.addEdge("Sibylla",
"Desiree");
parentOf.addEdge("Carl
Gustav",
"Victoria");
parentOf.addEdge("CarlGustav",
Gustav","Carl
"Victoria");
parentOf.addEdge("Carl
Philipp");
parentOf.addEdge("Carl
Gustav",
"Carl Philipp");
parentOf.addEdge("Carl Gustav", "Madeleine");
parentOf.addEdge("Carl Gustav",
"Madeleine");
parentOf.addEdge("Sylvia",
"Victoria");
parentOf.addEdge("Sylvia","Carl
"Victoria");
parentOf.addEdge("Sylvia",
Philipp");
parentOf.addEdge("Sylvia","Madeleine");
"Carl Philipp");
parentOf.addEdge("Sylvia",
"Madeleine");
/*parentOf.addEdge("Sylvia",
1 */ // parentOf.addEdge(victoria,adam);
/* 1 */ // parentOf.addEdge(victoria,adam);
Aufbau gerichteter Graphen
►
►
Beispiel: Verwandtschaftsbeziehungen
// SwedishKingFamilyDemo.java
//// SwedishKingFamilyDemo.java
////constructs a directed graph with
constructs
directed and
graph
with
////the
specifieda vertices
edges
//
the
specified
vertices
and
edges
DirectedGraph<String, DefaultEdge> parentOf =
DirectedGraph<String,
DefaultEdge>DefaultEdge>
parentOf =
new DefaultDirectedGraph<String,
new
DefaultDirectedGraph<String, DefaultEdge>
(DefaultEdge.class);
String (DefaultEdge.class);
adam = "Adam";
Stringvictoria
adam = "Adam";
String
= "Victoria";
String
victoria == "Madeleine";
"Victoria";
String madeleine
String
madeleine
=
"Madeleine";
parentOf.addVertex(adam);
parentOf.addVertex(adam);
parentOf.addVertex("Eve");
parentOf.addVertex("Eve");
parentOf.addVertex("Sibylla");
parentOf.addVertex("Sibylla");
parentOf.addVertex("Gustav
Adolf");
parentOf.addVertex("Gustav
Adolf");
parentOf.addVertex("Alice
Sommerlath");
parentOf.addVertex("Alice
Sommerlath");
parentOf.addVertex("Walter Sommerlath");
parentOf.addVertex("Walter Sommerlath");
parentOf.addVertex("Sylvia");
parentOf.addVertex("Sylvia");
parentOf.addVertex("Ralf");
parentOf.addVertex("Ralf");
parentOf.addVertex("Carl
Gustav");
parentOf.addVertex("Carl Gustav");
parentOf.addVertex("Desiree");
parentOf.addVertex("Desiree");
parentOf.addVertex(victoria);
parentOf.addVertex(victoria);
parentOf.addVertex("Carl
Philipp");
parentOf.addVertex("Carl Philipp");
parentOf.addVertex(madeleine);
parentOf.addVertex(madeleine);
24
Prof. U. Aßmann, Softwaretechnologie, TU Dresden
Prof. U. Aßmann, Softwaretechnologie, TU Dresden
►
►
CycleDetector findet Zyklen im Graphen, jenseits von Selbstkanten
Die meisten generischen Algorithmen von jgrapht sind Funktionalobjekte
(Entwurfsmuster Command)
Konsistenzprüfung und Navigation
// (a) cycle detection in graph parentOf
// (a) cycle detection in graph parentOf
CycleDetector<String, DefaultEdge> cycleDetector =
CycleDetector<String, DefaultEdge> cycleDetector =
new CycleDetector<String, DefaultEdge>(parentOf);
new CycleDetector<String, DefaultEdge>(parentOf);
Set<String> cycleVertices = cycleDetector.findCycles();
Set<String> cycleVertices = cycleDetector.findCycles();
System.out.println("Cycle: "+cycleVertices.toString());
System.out.println("Cycle: "+cycleVertices.toString());
25
26
V,E,I
GraphIterator
CrossComponent
Iterator
generateGraph(Graph<V,E> target,
VertexFactory<V> vertexFactory,
java.util.Map<java.lang.String,T>
resultMap)
GraphGenerator
Abstract
GraphIterator
V,E
V,E
V,E
V,E
In Reihe
Schicht für
Schicht
Pfad für Pfad
BreadthFirst
Iterator
DepthFirst
Iterator
Topological
SortIterator
V,E
Man kann den Graphen ablaufen, ohne seine Struktur zu kennen
V,E
Iterator
dummy Oberklasse
►
Iteratoren laufen Graphen ab
System.out.println("breadth first enumeration: ");
System.out.println("breadth first enumeration: ");
BreadthFirstIterator<String,DefaultEdge> bfi = new BreadthFirstIterator(parentOf);
BreadthFirstIterator<String,DefaultEdge> bfi = new BreadthFirstIterator(parentOf);
for (String node = bfi.next(); bfi.hasNext(); node = bfi.next()) {
for (String node = bfi.next(); bfi.hasNext(); node = bfi.next()) {
System.out.println("node: "+node);
System.out.println("node: "+node);
}
}
// (b) breadth-first iteration in graph parentOf
// (b) breadth-first iteration in graph parentOf
Prof. U. Aßmann, Softwaretechnologie, TU Dresden
Prof. U. Aßmann, Softwaretechnologie, TU Dresden
27
28
Prof. U. Aßmann, Softwaretechnologie, TU Dresden
Prof. U. Aßmann, Softwaretechnologie, TU Dresden
Dijkstra's algorithmus findet zwischen 2 Knoten den kürzesten Pfad
// (c) Shortest path with Dijkstra's method
// (c) Shortest path with Dijkstra's method
DijkstraShortestPath<String,DefaultEdge>
descendantPath
DijkstraShortestPath<String,DefaultEdge>
descendantPath
= new DijkstraShortestPath(parentOf,adam,victoria);
=
new
DijkstraShortestPath(parentOf,adam,victoria);
System.out.println("shortest path between Adam and Victoria ("
System.out.println("shortest
path between Adam and Victoria ("
+descendantPath.getPathLength()+"):");
+descendantPath.getPathLength()+"):");
GraphPath<String,DefaultEdge> path = descendantPath.getPath();
descendantPath.getPath();
//GraphPath<String,DefaultEdge>
Hint: Graphs is an algorithm path
class= (helper
class)
//
Hint:
Graphs
is
an
algorithm
class
(helper class)
List<String> nodeList = Graphs.getPathVertexList(path);
List<String>
nodeList
= Graphs.getPathVertexList(path);
for
(String node
: nodeList)
{
for (String node : nodeList) {
System.out.println("node: "+node);
System.out.println("node: "+node);
}
}
►
Finden kürzester Pfade
Left-to-right
DepthFirst
DepthFirstIterator läuft über den Graphen in Tiefensuche,
sozusagen “Pfad für Pfad”
BreadthFirstIterator läuft über den Graphen in Breitensuche,
sozusagen “Schicht für Schicht”
Left-to-right
BreadthFirst
►
►
Arten von Durchläufen mit Iteratoren
29
30
Prof. U. Aßmann, Softwaretechnologie, TU Dresden
Prof. U. Aßmann, Softwaretechnologie, TU Dresden
V,E
g:DefaultDirectedGraph
f:AsUndirectedGraph
weggeblendet
Masked
SubGraph
►
f:AsUndirectedGraph
g:DefaultDirectedGraph
k:EdgeReversedGraph
m:MaskedGraph
h:AsUndirectedGraph
l:UnmodifiableGraph
Was sieht ein Aufrufer (client) eines spezifischen Graphs?
konstant
Unmodifiable
Graph
V,E
h:AsUndirectedGraph
V,E
Schichtung von Graphen (Layering of
Graphs)
// Constructor for GraphDelegator:
// shadowing the Graph g (layering)
GraphDelegator(Graph<V,E> g)
V,E
AsWeighted
Graph
inverser
Graph
V,E
EdgeReversed
Graph
GraphDelegator
AsUndirected
Graph
bidirectional
GraphDelegator
V,E
Delegatoren erzeugen Sichten
31
32
Prof. U. Aßmann, Softwaretechnologie, TU Dresden
Prof. U. Aßmann, Softwaretechnologie, TU Dresden
ClassBased
VertexFactory
createVertex(V)
VertexFactory
V
V
ClassBased
EdgeFactory
V,E
createEdge(V,V)
EdgeFactory
V,E
#DefaultEdge()
DefaultEdge
Fabriken für Knoten und Kanten
// (d) Shortest path with Dijkstra's method in the undirected graph
// (d) Shortest path with Dijkstra's method
in the undirected graph
DijkstraShortestPath<String,DefaultEdge>
ancestorPath
DijkstraShortestPath<String,DefaultEdge>
ancestorPath
= new DijkstraShortestPath(descendantOrAscendant,madeleine,adam);
= new DijkstraShortestPath(descendantOrAscendant,madeleine,adam);
System.out.println("shortest
path between Madeleine and Adam ("+ancestorPath.getPathLength()+"):");
System.out.println("shortest
path
= ancestorPath.getPath(); path between Madeleine and Adam ("+ancestorPath.getPathLength()+"):");
path = ancestorPath.getPath();
nodeList
= Graphs.getPathVertexList(path);
nodeList
Graphs.getPathVertexList(path);
for
(String= node
: nodeList) {
forSystem.out.println("node:
(String node : nodeList) "+node);
{
System.out.println("node:
"+node);
}
}
// Now interpret the directed graph as undirected
// Now interpret the directed graph as
undirected
AsUndirectedGraph<String,DefaultEdge>
descendantOrAscendant
= new AsUndirectedGraph(parentOf);
AsUndirectedGraph<String,DefaultEdge>
descendantOrAscendant
= new AsUndirectedGraph(parentOf);
System.out.println("related graph: "+descendantOrAscendant.toString());
System.out.println("related graph: "+descendantOrAscendant.toString());
Finden kürzester Pfade im ungerichteten
Graphen (Sicht)
33
V,E
V,E
●
►
vollbesetzter
Graph
StartGraph
Generator
V,E
sternförmig
GridGraph
Generator
V,E
logarithmisch
verknüpft
Berühmter Algorithmus zum Berechnen von Wegen in Netzen
www.bahn.de
Logistik, Handlungsreisende, etc.
Optimierung von Problemen mit Gewichten
In einem Zusammenhangsbereich sind alle Knoten gegenseitig erreichbar
u.v.m.
–
StrongConnectivityInspector liefert “Zusammenhangsbereiche”,
starke Zusammenhangskomponenten, des Graphen
–
–
–
–
V,E
HypercubeGraph
Generator
BellmanFordShortestPath findet kürzeste Wege in gewichteten
Graphen
Analyseklassen
►
V,E,F
GraphGenerator
Complete
GraphGenerator
GraphGenerator
EmptyGraph
Generator
leer
F: VertexFactory
generateGraph(Graph<V,E> target,
VertexFactory<V> vertexFactory,
java.util.Map<java.lang.String,T>
resultMap)
►
Generatoren erzeugen verschiedene Arten
von Graphen
34
Prof. U. Aßmann, Softwaretechnologie, TU Dresden
Prof. U. Aßmann, Softwaretechnologie, TU Dresden
35
Prof. U. Aßmann, Softwaretechnologie, TU Dresden
►
►
►
►
Es gibt viele Varianten von Graphen
Fabrikmethoden für verschiedene Implementierungen von Knoten, Kanten,
Graphen
Sichten auf Graphen möglich
Analyen durch Funktionalobjekte
Analysen sind weitreichend nutzbar (s. Vorlesung Softwaretechnologie-II)
–
–
UML Assoziationen können mit JGraphT direkt realisiert werden
Was haben wir gelernt?
Herunterladen