例1: 添加点、边

import java.net.*;

import org.jgrapht.*;
import org.jgrapht.graph.*; /**
* A simple introduction to using JGraphT.
*
* @author Barak Naveh
* @since Jul 27, 2003
*/
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.toString()); // create a graph based on URL objects
DirectedGraph<URL, DefaultEdge> hrefGraph = createHrefGraph(); // note directed edges are printed as: (<v1>,<v2>)
System.out.println(hrefGraph.toString());
} /**
* 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<URL, DefaultEdge>(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<String, DefaultEdge>(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;
}
}

结果

([v1, v2, v3, v4], [{v1,v2}, {v2,v3}, {v3,v4}, {v4,v1}])
([http://www.amazon.com, http://www.yahoo.com, http://www.ebay.com], [(http://www.yahoo.com,http://www.amazon.com), (http://www.yahoo.com,http://www.ebay.com)])

例2:强、弱连通

import java.util.List;
import java.util.Set; import org.jgrapht.DirectedGraph;
import org.jgrapht.alg.KosarajuStrongConnectivityInspector;
import org.jgrapht.alg.interfaces.StrongConnectivityAlgorithm;
import org.jgrapht.alg.ConnectivityInspector;
import org.jgrapht.graph.DefaultDirectedGraph;
import org.jgrapht.graph.DefaultEdge; public class GraphConnDemo
{
private DirectedGraph<String, DefaultEdge> directedGraph; public GraphConnDemo(){
directedGraph = new DefaultDirectedGraph<String, DefaultEdge>(DefaultEdge.class);
directedGraph.addVertex("a");
directedGraph.addVertex("b");
directedGraph.addVertex("c");
directedGraph.addVertex("d");
directedGraph.addVertex("e");
directedGraph.addVertex("f");
directedGraph.addVertex("h");
directedGraph.addVertex("i");
directedGraph.addEdge("a", "b");
directedGraph.addEdge("b", "c");
directedGraph.addEdge("c", "d");
directedGraph.addEdge("d", "a");
directedGraph.addEdge("c", "e");
directedGraph.addEdge("f", "h");
directedGraph.addEdge("f", "i");
} public void testStrongConn(){
StrongConnectivityAlgorithm<String, DefaultEdge> scAlg =
new KosarajuStrongConnectivityInspector<String, DefaultEdge>(directedGraph);
List<Set<String>> stronglyConnetedSet =
scAlg.stronglyConnectedSets(); System.out.println("Strongly connected components:");
for (int i = 0; i < stronglyConnetedSet.size(); i++) {
System.out.println(stronglyConnetedSet.get(i));
}
System.out.println(); }
public void testWeakConn() {
ConnectivityInspector<String, DefaultEdge> connectivityInspector = new ConnectivityInspector<String, DefaultEdge>(directedGraph);
List<Set<String>> weaklyConnectedSet = connectivityInspector.connectedSets();
System.out.println("Weakly connected components:");
for (int i = 0; i < weaklyConnectedSet.size(); i++) {
System.out.println(weaklyConnectedSet.get(i));
}
System.out.println(); } public static void main(String args[])
{
GraphConnDemo test = new GraphConnDemo();
test.testStrongConn();
test.testWeakConn();
}
}

图示

结果

Strongly connected components:
[f]
[h]
[i]
[a, b, c, d]
[e] Weakly connected components:
[a, b, c, d, e]
[f, h, i]

例3:子图

import java.util.HashSet;
import java.util.List;
import java.util.Set; import org.jgrapht.DirectedGraph;
import org.jgrapht.alg.KosarajuStrongConnectivityInspector;
import org.jgrapht.alg.interfaces.StrongConnectivityAlgorithm;
import org.jgrapht.alg.ConnectivityInspector;
import org.jgrapht.graph.DefaultDirectedGraph;
import org.jgrapht.graph.DefaultEdge;
import org.jgrapht.graph.DirectedSubgraph; public class GraphConnDemo
{
private DirectedGraph<String, DefaultEdge> directedGraph;
private DirectedGraph<String, DefaultEdge> directedSubGraph; public GraphConnDemo(){
directedGraph = new DefaultDirectedGraph<String, DefaultEdge>(DefaultEdge.class);
directedGraph.addVertex("a");
directedGraph.addVertex("b");
directedGraph.addVertex("c");
directedGraph.addVertex("d");
directedGraph.addVertex("e");
directedGraph.addVertex("f");
directedGraph.addVertex("h");
directedGraph.addVertex("i");
directedGraph.addEdge("a", "b");
directedGraph.addEdge("b", "c");
directedGraph.addEdge("c", "d");
directedGraph.addEdge("d", "a");
directedGraph.addEdge("c", "e");
directedGraph.addEdge("f", "h");
directedGraph.addEdge("f", "i");
} public void subGraph() {
Set<String> subNode = new HashSet<String>();
subNode.add("a");
subNode.add("d");
subNode.add("c");
subNode.add("f");
directedSubGraph = new DirectedSubgraph(directedGraph, subNode);
System.out.println(directedSubGraph.vertexSet());
System.out.println(directedSubGraph.edgeSet()); } public static void main(String args[])
{
GraphConnDemo test = new GraphConnDemo();
test.subGraph();
}
}

结果

[a, c, d, f]
[(c : d), (d : a)]

压测: 和networkX对比

对比数据:点:593514    边: 2373298

NetworkX(str)

JGraphT(str)

JGraphT(int)

建路网

955s

240s

224s

强连通算法

255s

76s

71s

内存峰值

71.82G

78.87G

62.9G

CPU峰值

3.2%

27%

28.5%

释放内存

33min

2min

2min

例4: 得到最短路径

    void testDijkstraShortestPath() {
DirectedGraph<String, DefaultEdge> g2 = new DefaultDirectedGraph<String, DefaultEdge>(DefaultEdge.class); String v1 = "v1";
String v2 = "v2";
String v3 = "v3"; // add the vertices
g2.addVertex(v1);
g2.addVertex(v2);
g2.addVertex(v3); // add edges to create a circuit
g2.addEdge(v1, v2);
g2.addEdge(v2, v3); DijkstraShortestPath dijk = new DijkstraShortestPath(g2);
GraphPath<Integer, DefaultWeightedEdge> shortestPath1 = dijk.getPath(v1, v3);
GraphPath<Integer, DefaultWeightedEdge> shortestPath2 = dijk.getPath(v1, v2);
GraphPath<Integer, DefaultWeightedEdge> shortestPath3 = dijk.getPath(v1, v1);
GraphPath<Integer, DefaultWeightedEdge> shortestPath4 = dijk.getPath(v2, v1);
GraphPath<Integer, DefaultWeightedEdge> shortestPath5 = dijk.getPath(v2, v3);
GraphPath<Integer, DefaultWeightedEdge> shortestPath6 = dijk.getPath(v2, v2);
GraphPath<Integer, DefaultWeightedEdge> shortestPath7 = dijk.getPath(v3, v1);
GraphPath<Integer, DefaultWeightedEdge> shortestPath8 = dijk.getPath(v3, v2);
GraphPath<Integer, DefaultWeightedEdge> shortestPath9 = dijk.getPath(v3, v3); }

同时也可以判断两点之间是否可连通

JGraphT的更多相关文章

  1. 基于JGraphT实现的路径探寻

    基于JGraphT实现的路径探寻 业务中提出基于内存,探寻的两点间的有向以及无向路径,多点间的最小子图等需求,以下记录使用JGraphT的实现过程. GraphT是免费的Java类库,提供数学图论对象 ...

  2. 利用JGrapht对有向无环图进行广度优先遍历

    环境需求:JDK:1.8 jar:jgrapht-core-1.01.jar package edu; import org.jgrapht.experimental.dag.DirectedAcyc ...

  3. Java资源大全中文版(Awesome最新版)

    Awesome系列的Java资源整理.awesome-java 就是akullpp发起维护的Java资源列表,内容包括:构建工具.数据库.框架.模板.安全.代码分析.日志.第三方库.书籍.Java 站 ...

  4. Java资源大全中文版(Awesome最新版)(转载)

    原文地址:http://www.cnblogs.com/best/p/5876559.html 目录 业务流程管理套件 字节码操作 集群管理 代码分析 编译器生成工具 构建工具 外部配置工具 约束满足 ...

  5. Java资源大全

    古董级工具 这些工具伴随着Java一起出现,在各自辉煌之后还在一直使用. Apache Ant:基于XML的构建管理工具. cglib:字节码生成库. GlassFish:应用服务器,由Oracle赞 ...

  6. 最受欢迎的Java第三方库

    前言 翻译自programcreek: 典型的Java项目通常会依赖一些第三方库,本文总结了一些最受欢迎的Java库,这些类库在各种应用程序中被广泛使用: 当然,Java SDK是最广泛使用的Java ...

  7. Github优秀java项目集合(中文版) - 涉及java所有的知识体系

    Java资源大全中文版 我想很多程序员应该记得 GitHub 上有一个 Awesome - XXX 系列的资源整理.awesome-java 就是 akullpp 发起维护的 Java 资源列表,内容 ...

  8. twitter storm源码走读之8 -- TridentTopology创建过程详解

    欢迎转载,转载请注明出处,徽沪一郎. 从用户层面来看TridentTopology,有两个重要的概念一是Stream,另一个是作用于Stream上的各种Operation.在实现层面来看,无论是str ...

  9. [Java,JavaEE] 最常用的Java库一览

    引用自:http://www.importnew.com/7530.html 本文由 ImportNew - 邢 敏 翻译自 programcreek.欢迎加入Java小组.转载请参见文章末尾的要求. ...

随机推荐

  1. MFC坐标系

    MFC坐标系分为设备坐标系和逻辑坐标系两种.在设备坐标系中,一个像素表示一个单位长度,设备的原点(0,0)始终在显示平面的左上角位置,x轴正向向右,y轴正向向下. 逻辑坐标系中,原点可以放在任何一个位 ...

  2. Windows核心编程:第5章 作业

    Github https://github.com/gongluck/Windows-Core-Program.git //第5章 作业.cpp: 定义应用程序的入口点. // #include &q ...

  3. 在Winform中菜单动态添加“最近使用文件”

    最近在做文件处理系统中,要把最近打开文件显示出来,方便用户使用.网上资料有说,去遍历“C:\Documents and Settings\Administrator\Recent”下的最近文档本.文主 ...

  4. wpf 水波进度条 用户控件

    之前看公司web前端做了个 圆形的水波纹 进度条,就想用wpf 做一个,奈何自己太菜 一直做不出来,在看过 “普通的地球人” 的 “ WPF实现三星手机充电界面 博客之后 我也来照葫芦画个瓢. 废话不 ...

  5. 重写TreeView,自定义图标,生成通行的下划线,取消默认获得焦点失去焦点的效果,并支持拖拽节点到外界

    1.运行效果: 2.前端代码 <UserControl x:Class="iPIS.UI.Base.Tree.VideoTreeControl" xmlns="ht ...

  6. 在ASP.NET MVC应用中开发插件框架(中英对照)

    [原文] Developing a plugin framework in ASP.NET MVC with medium trust [译文] 在ASP.NET MVC应用中开发一个插件框架 I’v ...

  7. DataFrame 操作

    # 删除td数据框中的 指定列 td.drop(columns=['ship-city'],axis=1,inplace=True) #删除指定列 并且修改掉原始数据 # 删除td数据框中的 最后一列 ...

  8. nginx在Linux下的安装

    安装之前的环境装备: 1.ngiinx 是C 语言开发的,我们上传的文件还是源码,需要gcc环境编译源码 : yum install gcc-c++ 2.nginx的http模块使用pcre来解析正则 ...

  9. sublime text syntaxdef

    http://sublimetext.info/docs/en/extensibility/syntaxdefs.html

  10. 跟着刚哥学习Spring框架--Spring容器(二)

    Spring容器 启动Spring容器(实例化容器) -- IOC容器读取Bean配置创建Bean实例之前,必须对它进行实例化(加载启动),这样才可以从容器中获取Bean的实例并使用.  Bean是S ...