Java uses JGraphT to create directed, undirected, and weight graphs
The steps to create directed, undirected, and weight graphs using JGraphT in Java are as follows: 1. Add Maven coordinates for JGraphT dependent class libraries. Add the following dependencies to the pom.xml file: ```xml <dependency> <groupId>org.jgrapht</groupId> <artifactId>jgrapht-core</artifactId> <version>1.6.0</version> </dependency> ``` 2. Introduction to JGraphT: JGraphT is a Java graph class library that provides rich implementations of graphs and graph algorithms. It supports the creation of directed graphs, undirected graphs, weighted graphs, etc., and provides common graph algorithms, such as shortest path, Minimum spanning tree, maximum flow, etc. 3. Example of creating a directed graph: ```java import org.jgrapht.Graph; import org.jgrapht.Graphs; import org.jgrapht.graph.DefaultDirectedGraph; import org.jgrapht.graph.DefaultEdge; public class DirectedGraphExample { public static void main(String[] args) { //Create a directed graph object Graph<String, DefaultEdge> directedGraph = new DefaultDirectedGraph<>(DefaultEdge.class); //Add Vertex directedGraph.addVertex("A"); directedGraph.addVertex("B"); directedGraph.addVertex("C"); //Add Edge directedGraph.addEdge("A", "B"); directedGraph.addEdge("B", "C"); //Output vertices and edges of the graph System. out. println ("Vertex set:"+directedGraph. vertexSet()); System. out. println ("edge set:"+directedGraph. edgeSet()); } } ``` 4. Example of creating an undirected graph: ```java import org.jgrapht.Graph; import org.jgrapht.graph.DefaultEdge; import org.jgrapht.graph.SimpleGraph; public class UndirectedGraphExample { public static void main(String[] args) { //Create an undirected graph object Graph<String, DefaultEdge> undirectedGraph = new SimpleGraph<>(DefaultEdge.class); //Add Vertex undirectedGraph.addVertex("A"); undirectedGraph.addVertex("B"); undirectedGraph.addVertex("C"); //Add Edge undirectedGraph.addEdge("A", "B"); undirectedGraph.addEdge("B", "C"); //Output vertices and edges of the graph System. out. println ("Vertex set:"+undirectedGraph. vertexSet()); System. out. println ("Edge Set:"+undirectedGraph. edgeSet()); } } ``` 5. Example of creating a weight chart: ```java import org.jgrapht.Graph; import org.jgrapht.graph.DefaultWeightedEdge; import org.jgrapht.graph.SimpleWeightedGraph; public class WeightedGraphExample { public static void main(String[] args) { //Creating Weight Graph Objects Graph<String, DefaultWeightedEdge> weightedGraph = new SimpleWeightedGraph<>(DefaultWeightedEdge.class); //Add Vertex weightedGraph.addVertex("A"); weightedGraph.addVertex("B"); weightedGraph.addVertex("C"); //Adding Weighted Edges DefaultWeightedEdge edge1 = weightedGraph.addEdge("A", "B"); weightedGraph.setEdgeWeight(edge1, 1.5); DefaultWeightedEdge edge2 = weightedGraph.addEdge("B", "C"); weightedGraph.setEdgeWeight(edge2, 2.0); //Output vertices and edges of the graph System. out. println ("Vertex set:"+weightedGraph. vertexSet()); System. out. println ("Edge Set:"+weightedGraph. edgeSet()); System. out. println ("Edge Weight:"+weightedGraph. getEdgeWeight (edge1)+","+weightedGraph. getEdgeWeight (edge2)); } } ``` Summary: JGraphT is a Java graph class library that can be used to create directed, undirected, and weight graphs, and provides implementations of common graph algorithms. By adding corresponding dependency class libraries, JGraphT can be easily used in Java. The above example provides complete Java code for creating directed graphs, undirected graphs, and weight graphs.