JUNG 计算图属性,中心度,偏心率,直径,半径

本文介绍利用Java的第三方API JUNG 计算图中:

closeness centrality;// 图中某节点的 接近中心性/亲密中心性

betweenness centrality;// 图中某节点的 中介中心性/介数中心性

distance;  // 图中两节点的最短距离

eccentricity;  // 图中某节点的 偏心率/离心率

radius;   //  半径

diameter.  // 直径

 


 

JUNG 下载地址

 

https://sourceforge.net/projects/jung/files/

 

JUNG api参考文档:

http://jung.sourceforge.net/doc/api/overview-summary.html


 

预处理

JUNG 中的计算方法基于JUNG内置图类,本文基于自定义图计算图属性,故需要先将自定义图转存为JUNG图对象。

JUNG 提供 泛型接口,进行转化或创建时利用自定义边类型与节点类型即可。

以以下代码为例:

/*** 将graph.Graph 转为 JUNG.graph.Graph 过滤掉超边.* * @param g - 基于 graph.Graph* @return edu.uci.ics.jung.graph.Graph*/public static edu.uci.ics.jung.graph.Graph<Vertex, Edge> graphTransform(Graph<Vertex, Edge> g) {edu.uci.ics.jung.graph.Graph<Vertex, Edge> graph = new SparseGraph<>(); // 稀疏图for (Vertex vertex : g.vertices()) {graph.addVertex(vertex);}for (Edge edge : g.edges()) {if (edge.sourceVertices().size() == 0) {// 超边continue;}if (edge.sourceVertices().size() == 1) {// 有向边
                graph.addEdge(edge, getVertex(edge.sourceVertices()),getVertex(edge.targetVertices()), EdgeType.DIRECTED);} else {// 无向边Vertex existV = getVertex(edge.vertices());graph.addEdge(edge, existV, getVertexExcept(edge.vertices(), existV),EdgeType.UNDIRECTED);}}return graph;}

如此,获得一个jung 图对象。

 

 


 

Closeness Centrality

 

/*** 计算图g中节点v的 closeness centrality.* * @param g - to be calculate.* @param v - central vertex.* @return closeness centrality.*/public static double closenessCentrality(Graph<Vertex, Edge> g, Vertex v) {edu.uci.ics.jung.graph.Graph<Vertex, Edge> graph = helper.Transformer.graphTransform(g);// 新建计算对象,传入图与待计算节点。ClosenessCentrality<Vertex, Edge> closenessCentrality = new ClosenessCentrality<>(graph, t);// 获得 closeness centrality.double degree =  closenessCentrality.getVertexScore(v);return degree;}

 


 

 

Betweenness Centrality

 

/*** 计算图g中节点v的 betweenness centrality.* * @param g - to be calculate.* @param v - central vertex. ** @return betweeness centrality.*/public static double betweennessCentrality(Graph<Vertex, Edge> g, Vertex v) {edu.uci.ics.jung.graph.Graph<Vertex, Edge> graph = helper.Transformer.graphTransform(g);// 新建计算对象,传入图与待计算节点。BetweennessCentrality<Vertex, Edge> betweennessCentrality =new BetweennessCentrality<>(graph, t);// 获得 betweenness centrality.double degree = betweennessCentrality.getVertexScore(v);return degree;}

 


 

 

Distance (Dijkstra算法)

 

 

/*** 节点start和end之间的最短距离(需要区分有向图和无向图).* * @param g - to be calculated in.* @param start - from.* @param end - to.* @return distance.*/public static double distance(Graph<Vertex, Edge> g, Vertex start, Vertex end) {edu.uci.ics.jung.graph.Graph<Vertex, Edge> graph = helper.Transformer.graphTransform(g);DijkstraShortestPath<Vertex, Edge> dijkstraShortestPath =new DijkstraShortestPath<>(graph, t);// 计算由start到end的最短路径,返回值为路径上的边组List<Edge> path = dijkstraShortestPath.getPath(start, end);// 统计总权值double distance = 0;for (Edge edge : path) {distance += edge.getWeight();}return distance;}

 

 

 

 


 

 

Eccentricity

在图论中,顶点v偏心率(eccentricity),用来表示连接图G中的顶点v到图G中其它顶点之间的最大距离。

 

 /*** 偏心率.* * @param g - to be calculated.* @param v - central vertex.* @return eccentricity of the specific vertex.*/public static double eccentricity(Graph<Vertex, Edge> g, Vertex v) {double eccentricity = 0;for (Vertex end : g.vertices()) {// 跳过 v 自身if (v.equals(end)) {continue;}// 计算 distance 并记录最远距离double distance = distance(g, v, end);if (distance > eccentricity) {eccentricity = distance;}}return eccentricity;}

 

 

 

 


 

Radius

在图论中,半径(radius)表示图的所有点的偏心率的最小值。

 

/*** 半径,即偏心率的最小值.* * @param g - to be calculated.* @return*/public static double radius(Graph<Vertex, Edge> g) {double radius = 2 ^ 10; // 初始极大值// 遍历节点计算偏心率,记录偏心率最小值for (Vertex vertex : g.vertices()) {double distance = eccentricity(g, vertex);if (radius > distance && distance > 0) {radius = distance;}}return radius;}

 

 

 

 


 

Diameter

在图论中,图的直径(diameter),表示取遍图的所有顶点,得到的偏心率的最大值。

 

 

/*** 直径,即偏心率的最大值.* * @param g - to be calculated.* @return*/public static double diameter(Graph<Vertex, Edge> g) {double diameter = 0;// 遍历节点计算偏心率,记录偏心率最大值for (Vertex vertex : g.vertices()) {double distance = eccentricity(g, vertex);if (diameter < distance) {diameter = distance;}}return diameter;}

 

 

 

 

转载于:https://www.cnblogs.com/standingby/p/9148165.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/366856.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Java VM –提防YoungGen空间

您可能从我们以前的面向性能的文章中看到&#xff0c;健康的JVM是实现最佳应用程序性能和稳定性的最重要目标之一。 这样的健康评估通常仅关注主要收集的频率&#xff08;避免&#xff09;或检测内存泄漏的存在。 年轻一代空间或短寿命物体的大小和足迹如何&#xff1f; 本文…

小程序绘图工具painter-json文件绘制保存分享图-可点击任意元素触发函数

Painter是由酷家乐移动前端团队打造的一款小程序绘图组件。 原项目地址&#xff1a;https://github.com/Kujiale-Mobile/Painter 新版地址&#xff1a;https://github.com/shesw/Painter 这款交互版原来是为了针对业务中的新需求而由我自己开发的&#xff0c;后来需求改动&a…

sweetalert php,SweetAlert插件

用户#姓名操作{% for user in all_user %}{{ forloop.counter }}{{ user.username }}编辑删除{% endfor %}var _thisthis;swal({title:"你确定要删除吗&#xff1f;",text:"删除可就找不回来了哦&#xff01;",type:"warning",showCancelButton:…

python集合以及编码初识

一.集合 set 集合是无序的,天然能去重,是可变的.例:s {1,2,3,4,5}   1 s {} 2 s1 {1} 3 print(type(s)) # 空{}就是字典 4 print(type(s1)) #集合 集合的基本操作: 1.增 s {1,2,3,22,,ss,(3,4)} s.add(元素) s.update(alex) #迭代添加 alex会被分割添加 2.删 s…

JS 字符串操作总结

【MDN】https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String 【参考】https://www.cnblogs.com/guoyeqiang/p/8178336.html 字符串转换 1、toString() var age 11; var ageAsString age.toString(); //字符串“11” var found true;…

4 张动图解释为什么(什么时候)使用 Redux

dev-reading/fe 是一个阅读、导读、速读的 repo&#xff0c;不要依赖于 dev-reading/fe 学习知识。本 repo 只是一个快速了解文章内容的工具&#xff0c;并不提供全文解读和翻译。你可以通过本平台快速了解文章里面的内容&#xff0c;找到感兴趣的文章&#xff0c;然后去阅读全…

您正在使用什么垃圾收集器?

我们的研究实验室正全速前进。 随着最近的资金注入 &#xff0c;我们只能保证我们不断创新的步伐只会加快。 我们进行的部分研究与GC优化有关。 在处理这个有趣领域中的问题时&#xff0c;我们认为可以分享一些有关GC算法使用的见解。 为此&#xff0c;我们对使用特定GC算法的…

基于混沌的图像置乱加密算法及matlab的实现,基于混沌的图像置乱加密算法及MATLAB的实现...

基于混沌的图像置乱加密算法及MATLAB的实现提出了一种基于混沌映射的图像置乱加密算法。借助MATLAB6.5软(本文共3页)阅读全文>>数字水印(Digital Watermark)技术属于国际上新兴的研究领域,其主要目的是为了实现数字作品的版权保护,将与作品内容相关或不相关的一些标示信息…

poj1857 To Europe! To Europe!

思路&#xff1a; 一维dp。 实现&#xff1a; 1 #include <cstdio>2 #include <iostream>3 using namespace std;4 const int INF 0x3f3f3f3f;5 int w[1005], v[1005], sum[1005];6 double dp[1005];7 int main()8 {9 int b, l, n; 10 while (cin >>…

前端布局推进剂 - 间距规范化

我是一个爱折腾设计的前端&#xff0c;一直都在标榜自己的页面还原是多么的牛 X 。怎么做到页面还原&#xff1f;我有一个最笨但是有效的方法&#xff0c;就是把设计稿直接存成图片&#xff0c;作为背景图然后临摹着设计稿进行开发。我觉得自己太有才了。像素级还原有没有&…

战略模式并不意味着春天!

是的&#xff0c;所以可以说您正在编写一个Spring MVC应用程序&#xff0c;然后您决定&#xff1a;“我想做一些单独的封装算法&#xff0c;这些算法可以互换来执行特定的行为”。 对此的经典回应是“您需要一个战略模式男孩&#xff01;”。 所以&#xff0c;这就是我所做的&…

echarts折线图相关

optionJKDLine {  title: {text: 告警数量趋势图,textStyle:{  //标题样式fontStyle:normal,fontFamily:sans-serif,fontSize:12    }},tooltip: {trigger: axis},legend: {  //图例,默认显示},grid: {  //图表距离left: -3%,right: 5%,bottom: 3%,top:20%,contai…

南邮java实验报告,南邮微机原理实验报告精选.doc

南邮微机原理实验报告精选《微型计算机原理与接口技术》上机实验学 院&#xff1a; 电子科学与工程专 业&#xff1a; 电磁场与无线技术姓 名&#xff1a; 陈秀慧课 程 号&#xff1a; B0300062S学 号&#xff1a;任课老师&#xff1a; 欧晓鸥2016年 3 月 21日实验目的熟悉第四…

Mybatis动态SQL语句使用

在实际开发中&#xff0c;有时候查询条件可能是不确定的&#xff0c;查询条件可能有多条也可能没有&#xff0c;这时候就需要用到动态的sql语句拼接功能。 一、if、where、sql标签的使用 需求&#xff1a;在一些高级查询中&#xff0c;查询条件存在的个数不确定。如&#xff0c…

为什么Vue不能观察到数组length的变化?

官网解释如下 由于 JavaScript 的限制&#xff0c;Vue 不能检测以下变动的数组&#xff1a; 当你利用索引直接设置一个项时&#xff0c;例如&#xff1a;vm.items[indexOfItem] newValue 当你修改数组的长度时&#xff0c;例如&#xff1a;vm.items.length newLength 因为vue…

规则引擎drools的简单使用

规则引擎适用于有复杂多变的规则&#xff0c;如商品满减、积分赠送、考勤规则等 一、引入maven依赖 <dependency><groupId>org.drools</groupId><artifactId>drools-core</artifactId><version>7.13.0.Final</version> </depende…

使用MongoDB进行乐观锁定重试

在我以前的文章中&#xff0c;我谈到了对MongoDB批处理程序采用乐观锁定的好处。 如我之前所写&#xff0c;乐观锁异常是可恢复的异常&#xff0c;只要我们获取最新的Entity&#xff0c;我们就会对其进行更新并保存。 因为我们使用的是MongoDB&#xff0c;所以我们不必担心本地…

cx oracle 配置,cx_Oracle的配置啊。。终于搞出来了

参考。。http://www.blogjava.net/jelver/articles/294583.htmlhttp://shanchao7932297.blog.163.com/blog/static/1363624200710911543428/http://aofengblog.blog.163.com/blog/static/6317021201157111336764/http://www.cnblogs.com/ysisl/archive/2010/12/20/1911870.html…

JavaScript中发布/订阅模式的理解

订阅发布模式的介绍 发布订阅模式&#xff0c;它定义了一种一对多的关系&#xff0c;可以使多个观察者对象对一个主题对象进行监听&#xff0c;当这个主题对象发生改变时&#xff0c;依赖的所有对象都会被通知到。 在生活中我们常常遇到这样一种情况&#xff0c;我们在使用新…

java的list遍历

for(String str : list) {//增强for循环&#xff0c;其内部实质上还是调用了迭代器遍历方式&#xff0c;这种循环方式还有其他限制&#xff0c;不建议使用。System.out.println(str); } for( int i 0 ; i < list.size() ; i) {//普通for循环&#xff0c;内部不锁定&#xf…