the jgrapht library - polito.it...the jgrapht library tecniche di programmazione –a.a. 2017/2018...
TRANSCRIPT
![Page 1: The jGraphT library - polito.it...The jGraphT library Tecniche di Programmazione –A.A. 2017/2018 Summary 2 Tecniche di programmazione A.A. 2017/2018 The JGraphT library Creating](https://reader034.vdocuments.us/reader034/viewer/2022042322/5f0cc3e37e708231d4370533/html5/thumbnails/1.jpg)
The jGraphT library
Tecniche di Programmazione – A.A. 2017/2018
![Page 2: The jGraphT library - polito.it...The jGraphT library Tecniche di Programmazione –A.A. 2017/2018 Summary 2 Tecniche di programmazione A.A. 2017/2018 The JGraphT library Creating](https://reader034.vdocuments.us/reader034/viewer/2022042322/5f0cc3e37e708231d4370533/html5/thumbnails/2.jpg)
Summary
A.A. 2017/2018Tecniche di programmazione2
The JGraphT library
Creating graphs
![Page 3: The jGraphT library - polito.it...The jGraphT library Tecniche di Programmazione –A.A. 2017/2018 Summary 2 Tecniche di programmazione A.A. 2017/2018 The JGraphT library Creating](https://reader034.vdocuments.us/reader034/viewer/2022042322/5f0cc3e37e708231d4370533/html5/thumbnails/3.jpg)
Introduction to jGraphT
The jGraphT library
![Page 4: The jGraphT library - polito.it...The jGraphT library Tecniche di Programmazione –A.A. 2017/2018 Summary 2 Tecniche di programmazione A.A. 2017/2018 The JGraphT library Creating](https://reader034.vdocuments.us/reader034/viewer/2022042322/5f0cc3e37e708231d4370533/html5/thumbnails/4.jpg)
JGraphT
A.A. 2017/2018Tecniche di programmazione4
http://jgrapht.org
(do not confuse with jgraph.com)
Free Java graph library that provides graph objects and algorithms
Easy, type-safe and extensible thanks to <generics>
Just add jgrapht-core-1.1.0.jar to your project
![Page 5: The jGraphT library - polito.it...The jGraphT library Tecniche di Programmazione –A.A. 2017/2018 Summary 2 Tecniche di programmazione A.A. 2017/2018 The JGraphT library Creating](https://reader034.vdocuments.us/reader034/viewer/2022042322/5f0cc3e37e708231d4370533/html5/thumbnails/5.jpg)
JGraphT structure
A.A. 2017/2018Tecniche di programmazione5
Packages
org.jgraphtThe front-end API's interfaces and classes,
including Graph, DirectedGraph and UndirectedGraph.
org.jgrapht.alg.* Algorithms provided with JGraphT.
org.jgrapht.eventEvent classes and listener interfaces, used to provide a change
notification mechanism on graph modification events.
org.jgrapht.generate Generators for graphs of various topologies.
org.jgrapht.graph Implementations of various graphs.
org.jgrapht.traverse Graph traversal means.
http://jgrapht.org/javadoc/
![Page 6: The jGraphT library - polito.it...The jGraphT library Tecniche di Programmazione –A.A. 2017/2018 Summary 2 Tecniche di programmazione A.A. 2017/2018 The JGraphT library Creating](https://reader034.vdocuments.us/reader034/viewer/2022042322/5f0cc3e37e708231d4370533/html5/thumbnails/6.jpg)
Graph objects
A.A. 2017/2018Tecniche di programmazione6
All graphs derive from:
Interface org.jgrapht.Graph<V,E>
V = type of vertices
Any class
E = type of edges
org.jgrapht.graph.DefaultEdge
org.jgrapht.graph.DefaultWeightedEdge
Your own custom subclass
![Page 7: The jGraphT library - polito.it...The jGraphT library Tecniche di Programmazione –A.A. 2017/2018 Summary 2 Tecniche di programmazione A.A. 2017/2018 The JGraphT library Creating](https://reader034.vdocuments.us/reader034/viewer/2022042322/5f0cc3e37e708231d4370533/html5/thumbnails/7.jpg)
<V, E>
A.A. 2017/2018Tecniche di programmazione7
User-defined objects, depending on the problem
Must properly define hashCode and equals
The Graph implementation and many graph algorithms use
HashSet and HashMap internally!
Vertex typeV
Your own object
Define hashCode and equals
Edge type E
Subclass of DefaultEdge or DefaultWeightedEdge
Do not redefine (override) the provided hashCode and equals
![Page 8: The jGraphT library - polito.it...The jGraphT library Tecniche di Programmazione –A.A. 2017/2018 Summary 2 Tecniche di programmazione A.A. 2017/2018 The JGraphT library Creating](https://reader034.vdocuments.us/reader034/viewer/2022042322/5f0cc3e37e708231d4370533/html5/thumbnails/8.jpg)
What is a Graph?
A.A. 2017/2018Tecniche di programmazione8
![Page 9: The jGraphT library - polito.it...The jGraphT library Tecniche di Programmazione –A.A. 2017/2018 Summary 2 Tecniche di programmazione A.A. 2017/2018 The JGraphT library Creating](https://reader034.vdocuments.us/reader034/viewer/2022042322/5f0cc3e37e708231d4370533/html5/thumbnails/9.jpg)
Graph classes
A.A. 2017/2018Tecniche di programmazione9
Graph
DefaultDirectedGraph
org.jgrapht
org.jgrapht.graph
DefaultDirectedWeightedGraph
DirectedWeightedMultigraph
DirectedWeightedPseudograph
DirectedMultigraph DirectedPseudograph
MultigraphPseudograph
SimpleDirectedGraph
SimpleGraph
SimpleWeightedGraph
SimpleDirectedWeightedGraph
WeightedMultigraph WeightedPseudograph
I
![Page 10: The jGraphT library - polito.it...The jGraphT library Tecniche di Programmazione –A.A. 2017/2018 Summary 2 Tecniche di programmazione A.A. 2017/2018 The JGraphT library Creating](https://reader034.vdocuments.us/reader034/viewer/2022042322/5f0cc3e37e708231d4370533/html5/thumbnails/10.jpg)
Graph classes (in org.jgrapht.graph)
A.A. 2017/2018Tecniche di programmazione10
![Page 11: The jGraphT library - polito.it...The jGraphT library Tecniche di Programmazione –A.A. 2017/2018 Summary 2 Tecniche di programmazione A.A. 2017/2018 The JGraphT library Creating](https://reader034.vdocuments.us/reader034/viewer/2022042322/5f0cc3e37e708231d4370533/html5/thumbnails/11.jpg)
Graph classes
A.A. 2017/2018Tecniche di programmazione11
Undirected Directed
![Page 12: The jGraphT library - polito.it...The jGraphT library Tecniche di Programmazione –A.A. 2017/2018 Summary 2 Tecniche di programmazione A.A. 2017/2018 The JGraphT library Creating](https://reader034.vdocuments.us/reader034/viewer/2022042322/5f0cc3e37e708231d4370533/html5/thumbnails/12.jpg)
Graph classes
A.A. 2017/2018Tecniche di programmazione12
WeightedNot
weighted
Not
weighted
![Page 13: The jGraphT library - polito.it...The jGraphT library Tecniche di Programmazione –A.A. 2017/2018 Summary 2 Tecniche di programmazione A.A. 2017/2018 The JGraphT library Creating](https://reader034.vdocuments.us/reader034/viewer/2022042322/5f0cc3e37e708231d4370533/html5/thumbnails/13.jpg)
Graph classes
A.A. 2017/2018Tecniche di programmazione13
Simple
Multi
Pseudo
Non simple (loops allowed, no multi edges)
![Page 14: The jGraphT library - polito.it...The jGraphT library Tecniche di Programmazione –A.A. 2017/2018 Summary 2 Tecniche di programmazione A.A. 2017/2018 The JGraphT library Creating](https://reader034.vdocuments.us/reader034/viewer/2022042322/5f0cc3e37e708231d4370533/html5/thumbnails/14.jpg)
Creating graphs
The jGraphT library
![Page 15: The jGraphT library - polito.it...The jGraphT library Tecniche di Programmazione –A.A. 2017/2018 Summary 2 Tecniche di programmazione A.A. 2017/2018 The JGraphT library Creating](https://reader034.vdocuments.us/reader034/viewer/2022042322/5f0cc3e37e708231d4370533/html5/thumbnails/15.jpg)
Creating graphs (1/2)
A.A. 2017/2018Tecniche di programmazione15
Decide what is the vertex class V
Decide which graph class suits your needs
For unweighted graphs, use DefaultEdge as E
For weighted graphs, use DefaultWeightedEdge as E
Create the graph object
Graph<V,E> graph = newSimpleGraph<V,E>(E.class) ;
![Page 16: The jGraphT library - polito.it...The jGraphT library Tecniche di Programmazione –A.A. 2017/2018 Summary 2 Tecniche di programmazione A.A. 2017/2018 The JGraphT library Creating](https://reader034.vdocuments.us/reader034/viewer/2022042322/5f0cc3e37e708231d4370533/html5/thumbnails/16.jpg)
Creating graphs (2/2)
A.A. 2017/2018Tecniche di programmazione16
Add vertices
boolean addVertex(V v)
Add edges
E addEdge(V sourceVertex, V targetVertex)
boolean addEdge(V sourceVertex, V targetVertex, E e)
void setEdgeWeight(E e, double weight)
Print graph (for debugging)
toString()
Remember: E and V should correctly implement .equals()
and .hashCode()
![Page 17: The jGraphT library - polito.it...The jGraphT library Tecniche di Programmazione –A.A. 2017/2018 Summary 2 Tecniche di programmazione A.A. 2017/2018 The JGraphT library Creating](https://reader034.vdocuments.us/reader034/viewer/2022042322/5f0cc3e37e708231d4370533/html5/thumbnails/17.jpg)
Example
A.A. 2017/2018Tecniche di programmazione17
UndirectedGraph<String, DefaultEdge> graph = new SimpleGraph<>(DefaultEdge.class) ;
graph.addVertex("A") ;
graph.addVertex("B") ;
graph.addVertex("C") ;
graph.addEdge("A", "B") ;
graph.addEdge("A", "C") ;
A
B C
![Page 18: The jGraphT library - polito.it...The jGraphT library Tecniche di Programmazione –A.A. 2017/2018 Summary 2 Tecniche di programmazione A.A. 2017/2018 The JGraphT library Creating](https://reader034.vdocuments.us/reader034/viewer/2022042322/5f0cc3e37e708231d4370533/html5/thumbnails/18.jpg)
Querying graph structure
A.A. 2017/2018Tecniche di programmazione18
Navigate structure
java.util.Set<V> vertexSet()
boolean containsVertex(V v)
boolean containsEdge(V sourceVertex, V targetVertex)
java.util.Set<E> edgesOf(V vertex)
java.util.Set<E> getAllEdges(V sourceVertex, V targetVertex)
Query Edges
V getEdgeSource(E e)
V getEdgeTarget(E e)
double getEdgeWeight(E e)
![Page 19: The jGraphT library - polito.it...The jGraphT library Tecniche di Programmazione –A.A. 2017/2018 Summary 2 Tecniche di programmazione A.A. 2017/2018 The JGraphT library Creating](https://reader034.vdocuments.us/reader034/viewer/2022042322/5f0cc3e37e708231d4370533/html5/thumbnails/19.jpg)
Graph manipulation functions
A.A. 2017/2018Tecniche di programmazione19
![Page 20: The jGraphT library - polito.it...The jGraphT library Tecniche di Programmazione –A.A. 2017/2018 Summary 2 Tecniche di programmazione A.A. 2017/2018 The JGraphT library Creating](https://reader034.vdocuments.us/reader034/viewer/2022042322/5f0cc3e37e708231d4370533/html5/thumbnails/20.jpg)
The Graphs utility class
A.A. 2017/2018Tecniche di programmazione20
![Page 21: The jGraphT library - polito.it...The jGraphT library Tecniche di Programmazione –A.A. 2017/2018 Summary 2 Tecniche di programmazione A.A. 2017/2018 The JGraphT library Creating](https://reader034.vdocuments.us/reader034/viewer/2022042322/5f0cc3e37e708231d4370533/html5/thumbnails/21.jpg)
Utility functions
A.A. 2017/2018Tecniche di programmazione21
Static class org.jgrapht.Graphs
Easier creation public static <V,E> E addEdge(Graph<V,E> g, V sourceVertex,
V targetVertex, double weight)
public static <V,E> E addEdgeWithVertices(Graph<V,E> g, V sourceVertex, V targetVertex)
Easier navigation public static <V,E> java.util.List<V> neighborListOf(Graph<V,E> g,
V vertex)
public static String getOppositeVertex(Graph<String, DefaultEdge> g, DefaultEdge e, String v)
public static <V,E> java.util.List<V> predecessorListOf(DirectedGraph<V,E> g, V vertex)
public static <V,E> java.util.List<V> successorListOf(DirectedGraph<V,E> g, V vertex)
![Page 22: The jGraphT library - polito.it...The jGraphT library Tecniche di Programmazione –A.A. 2017/2018 Summary 2 Tecniche di programmazione A.A. 2017/2018 The JGraphT library Creating](https://reader034.vdocuments.us/reader034/viewer/2022042322/5f0cc3e37e708231d4370533/html5/thumbnails/22.jpg)
Example
A.A. 2017/2018Tecniche di programmazione22
for( String s: graph.vertexSet() ) {
System.out.println("Vertex "+s) ;
for( DefaultEdge e: graph.edgesOf(s) ) {
System.out.println("Degree: “
+graph.degreeOf(s)) ;
System.out.println(
Graphs.getOppositeVertex(
graph, e, s)) ;
}
}
A
B C
![Page 23: The jGraphT library - polito.it...The jGraphT library Tecniche di Programmazione –A.A. 2017/2018 Summary 2 Tecniche di programmazione A.A. 2017/2018 The JGraphT library Creating](https://reader034.vdocuments.us/reader034/viewer/2022042322/5f0cc3e37e708231d4370533/html5/thumbnails/23.jpg)
Example
A.A. 2017/2018Tecniche di programmazione23
r s t u
v w x y
z
![Page 24: The jGraphT library - polito.it...The jGraphT library Tecniche di Programmazione –A.A. 2017/2018 Summary 2 Tecniche di programmazione A.A. 2017/2018 The JGraphT library Creating](https://reader034.vdocuments.us/reader034/viewer/2022042322/5f0cc3e37e708231d4370533/html5/thumbnails/24.jpg)
Licenza d’uso
A.A. 2017/2018Tecniche di programmazione24
Queste diapositive sono distribuite con licenza Creative Commons “Attribuzione - Non commerciale - Condividi allo stesso modo (CC BY-NC-SA)”
Sei libero: di riprodurre, distribuire, comunicare al pubblico, esporre in pubblico,
rappresentare, eseguire e recitare quest'opera
di modificare quest'opera
Alle seguenti condizioni: Attribuzione — Devi attribuire la paternità dell'opera agli autori originali
e in modo tale da non suggerire che essi avallino te o il modo in cui tu usi l'opera.
Non commerciale — Non puoi usare quest'opera per fini commerciali.
Condividi allo stesso modo — Se alteri o trasformi quest'opera, o se la usi per crearne un'altra, puoi distribuire l'opera risultante solo con una licenza identica o equivalente a questa.
http://creativecommons.org/licenses/by-nc-sa/3.0/