在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

开源免费跨平台基于JVM的复杂网络分析软件Gephi简介

2017-2-9 13:05| 发布者: zhangjf| 查看: 716| 评论: 0

摘要: Gephi是一款开源免费跨平台基于JVM的复杂网络分析软件, 其主要用于各种网络和复杂系统, 特别是在处理网络关系数据这方面很有优势,下面是两个不错的例子 编程语言关系图 微博传播分析 那么,我们拿到图数据 ...

Gephi是一款开源免费跨平台基于JVM的复杂网络分析软件, 其主要用于各种网络和复杂系统, 特别是在处理网络关系数据这方面很有优势,下面是两个不错的例子

编程语言关系图

开源免费跨平台基于JVM的复杂网络分析软件Gephi简介

微博传播分析

开源免费跨平台基于JVM的复杂网络分析软件Gephi简介

那么,我们拿到图数据后, 应该如何画出这个图表呢?

图表绘制

通过分析上面两个例子的代码可知,前端插件用的都是 sigma.js ,但是在展示网络数据的时候插件并不会自动的对节点和边进行布局,需要通过后台传给前台数据文件,常用的有两种格式 jsongexf

虽然gephi是一个gui程序,但是它提供了一套叫 gephi-toolkit 的东西,可以方便的编程化方式处理数据, 首先加入gephi仓库并引入依赖

  1. <repositories>
  2. <repository>
  3. <id>gephi-snapshots</id>
  4. <name>Gephi Snapshots</name>
  5. <url>http://nexus.gephi.org/nexus/content/repositories/snapshots/</url>
  6. </repository>
  7. <repository>
  8. <id>gephi-releases</id>
  9. <name>Gephi Releases</name>
  10. <url>http://nexus.gephi.org/nexus/content/repositories/releases/</url>
  11. </repository>
  12. </repositories>
  13. <dependencies>
  14. <dependency>
  15. <groupId>org.gephi</groupId>
  16. <artifactId>gephi-toolkit</artifactId>
  17. <version>0.8.2</version>
  18. </dependency>
  19. </dependencies>
复制代码

添加依赖完成之后,可以参考这个 slide 熟悉一下API

布局算法

布局算法是图算法的一大部分, 常用的有 Force-directed_graph_drawing 力导向算法, 其基本思想是:

对网络状态进行初始化 计算每次迭代局部区域内,两两节点间的斥力所产生的单位位移(一般为正值) 计算每次迭代每条边的引力对两端节点所产生的单位位移(一般为负值) 上面两步中的斥力和引力系数直接影响到最终态的理想效果,它与节点间的距离、节点在系统所在区域的平均单位区域均有关,需要开发人员在实践中不断调整 累加所有节点的单位位移,结合单次最大移动距离确定每个节点新的位置 迭代n次,达到最优效果

gephi-tookit中也自带了几种布局算法, 而且易于使用, 如果你的数据源是数据库, 可以参考 这个例子 ,当然也可以纯手工构建一个图, 示例代码如下所示:

  1. ProjectController pc = Lookup.getDefault().lookup(ProjectController.class);
  2. pc.newProject();
  3. Workspace workspace = pc.getCurrentWorkspace();
  4. //Generate a new random graph into a container
  5. Container container = Lookup.getDefault().lookup(ContainerFactory.class).newContainer();
  6. GraphModel graphModel = Lookup.getDefault().lookup(GraphController.class).getModel();
  7. DirectedGraph graph = graphModel.getDirectedGraph();
  8. Node n0 = graphModel.factory().newNode("n0");
  9. n0.getNodeData().setLabel("n0");
  10. Node n1 = graphModel.factory().newNode("n1");
  11. n1.getNodeData().setLabel("n1");
  12. Edge edge = graphModel.factory().newEdge(n0, n1, 1f, true);
  13. graph.addNode(n0);
  14. graph.addNode(n1);
  15. graph.addEdge(edge);
  16. for(int i = 0 ; i < 100; i++) {
  17. Node ntmp = graphModel.factory().newNode("tmp" + i);
  18. Edge edgetmp = graphModel.factory().newEdge(n0, ntmp, 1f, true);
  19. graph.addNode(ntmp);
  20. graph.addEdge(edgetmp);
  21. }
  22. System.out.println("Nodes: " + graph.getNodeCount());
  23. System.out.println("Edges: " + graph.getEdgeCount());
  24. //Layout for 15 seconds
  25. AutoLayout autoLayout = new AutoLayout(20, TimeUnit.SECONDS);
  26. autoLayout.setGraphModel(graphModel);
  27. YifanHuLayout firstLayout = new YifanHuLayout(null, new StepDisplacement(1f));
  28. ForceAtlasLayout secondLayout = new ForceAtlasLayout(null);
  29. AutoLayout.DynamicProperty adjustBySizeProperty = AutoLayout.createDynamicProperty("forceAtlas.adjustSizes.name", Boolean.TRUE, 0.1f);//True after 10% of layout time
  30. AutoLayout.DynamicProperty repulsionProperty = AutoLayout.createDynamicProperty("forceAtlas.repulsionStrength.name", new Double(500.), 0f);//500 for the complete period
  31. autoLayout.addLayout(firstLayout, 0.9f);
  32. autoLayout.addLayout(secondLayout, 0.1f, new AutoLayout.DynamicProperty[]{adjustBySizeProperty, repulsionProperty});
  33. autoLayout.execute();
  34. //Export pdf & gexf
  35. ExportController ec = Lookup.getDefault().lookup(ExportController.class);
  36. try {
  37. File pdfFile = new File("/tmp/data.pdf");
  38. File gexfFile = new File("/tmp/data.gexf");
  39. pdfFile.getParentFile().mkdirs();
  40. gexfFile.getParentFile().mkdirs();
  41. ec.exportFile(pdfFile);
  42. ec.exportFile(gexfFile);
  43. } catch (IOException ex) {
  44. ex.printStackTrace();
  45. }
复制代码

其中导出的 gexf 数据文件可以用于 sigmajs的展示

参考资料 http://gephi.github.io/ http://www.slideshare.net/gephi/gephi-toolkit-tutorialtoolkit https://github.com/gephi/gephi/wiki/How-to-code-with-the-Toolkit

THE END

声明: 本文 “Gephi简介” 采用 CC BY-NC-SA 4.0 协议进行授权.

转载请注明原文地址: http://stackbox.org/2015-08-about-gephi/index.html

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-7-9 20:31

Copyright 2015-2025 djqfx

返回顶部