赞
踩
使用IntelliJ IDEA开发
这些说明假定您正在启动一个新的Java项目,或者您已有一个Java项目。注意,JGraphT需要Java 8。
通过Maven导入JGraphT
1.在IntelliJ IDEA中,新建一个project
2.在“项目结构”对话框中定义库。转到File→ Project Structure,或按Ctrl + Shift + Alt + S
3.选择 Libraries 单击+号,然后选择 From Maven。
3.复制后面这段加粗字母org.jgrapht:jgrapht-core:1.0.1 粘贴到图片位置,点OK
大致变成下图即可:
新建HelloJGraphT.Java文件 运行官方测试代码
package org.jgrapht.demo; import java.net.*; import org.jgrapht.*; import org.jgrapht.graph.*; public final class HelloJGraphT { private HelloJGraphT() { } // ensure non-instantiability. /** * The starting point for the demo. * * @param args ignored. */ public static void main(String [] args) { UndirectedGraph<String, DefaultEdge> stringGraph = createStringGraph(); // note undirected edges are printed as: {<v1>,<v2>} System.out.println(stringGraph); // create a graph based on URL objects DirectedGraph<URL, DefaultEdge> hrefGraph = createHrefGraph(); // note directed edges are printed as: (<v1>,<v2>) System.out.println(hrefGraph); } /** * Creates a toy directed graph based on URL objects that represents link * structure. * * @return a graph based on URL objects. */ private static DirectedGraph<URL, DefaultEdge> createHrefGraph() { DirectedGraph<URL, DefaultEdge> g = new DefaultDirectedGraph<>(DefaultEdge.class); try { URL amazon = new URL("http://www.amazon.com"); URL yahoo = new URL("http://www.yahoo.com"); URL ebay = new URL("http://www.ebay.com"); // add the vertices g.addVertex(amazon); g.addVertex(yahoo); g.addVertex(ebay); // add edges to create linking structure g.addEdge(yahoo, amazon); g.addEdge(yahoo, ebay); } catch (MalformedURLException e) { e.printStackTrace(); } return g; } /** * Create a toy graph based on String objects. * * @return a graph based on String objects. */ private static UndirectedGraph<String, DefaultEdge> createStringGraph() { UndirectedGraph<String, DefaultEdge> g = new SimpleGraph<>(DefaultEdge.class); String v1 = "v1"; String v2 = "v2"; String v3 = "v3"; String v4 = "v4"; // add the vertices g.addVertex(v1); g.addVertex(v2); g.addVertex(v3); g.addVertex(v4); // add edges to create a circuit g.addEdge(v1, v2); g.addEdge(v2, v3); g.addEdge(v3, v4); g.addEdge(v4, v1); return g; } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。