例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. pickle 继承

    1.什么是方法,什么是函数 class Foo: def chi(self): print("我是吃") @staticmethod def static_method(): pa ...

  2. ubuntu-12.04.4-alternate-i386安装

    Ubuntu-12.04.4安装 0.网络配置环境: 1. 选择中文简体. 2.选择第一个,安装ubuntu . 3.询问是否安装所选择的语言版本,选择yes. 4.选择“否”默认键盘布局,选择 no ...

  3. hdu 5047 大数找规律

    http://acm.hdu.edu.cn/showproblem.php?pid=5047 找规律 信kuangbin,能AC #include <stdio.h> #include & ...

  4. PHP搜索 搜索 搜索

    //搜索界面 public function search(){ $param=input('param.'); $where=[]; //搜索框 if(!empty($param['content' ...

  5. MODBUS串行通信协议详细说明

    第一章  简介 本文详细地描述了装置在MODBUS 通讯模式下的输入和输出命令.信息和数据,以便第三方使用和开发. 1.1 串行通讯协议的目的 通信协议的作用是使信息和数据在上位机主站和装置之间有效地 ...

  6. vnc搭建

    PS:偶以前基本不用Linux的远程图形桌面,前几天有开发的同事配置CentOS的vnc有问题,找我解决,就顺便记录总结一下,这个总结是比较完整的.下面的配置在CentOS5.x和6.x.SUSE企业 ...

  7. C# 实现简单仿QQ登陆注册功能

    闲来没事,想做一个仿QQ登陆注册的winform,于是利用工作之余,根据自己的掌握和查阅的资料,历时4天修改完成,新手水平,希望和大家共同学习进步,有不同见解希望提出! 废话不多说,进入正题: 先来看 ...

  8. ASP.NET MVC NPOI导入Excel DataTable批量导入到数据库

    使用NPOI导入Excel 首先在MVC项目中导入NPOI 查询NPOI安装,排序依据,选择:最高下载量,选择第一个. 在控制器中创建ExcelController 在Index视图中写入代码: @u ...

  9. 【BZOJ3555】 [Ctsc2014]企鹅QQ

    BZOJ3555 [Ctsc2014]企鹅QQ Solution 只需要前缀Hash,然后考虑每一段的贡献就好了!!! 代码实现 #include<stdio.h> #include< ...

  10. 深入字节码理解invokeSuper无限循环的原因

    来一段简单的cglib代码 public class SampleClass { public void test(){ System.out.println("hello world&qu ...