博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
scala-graph by example (0) visualization
阅读量:6464 次
发布时间:2019-06-23

本文共 1526 字,大约阅读时间需要 5 分钟。

Graph Theory should be illustrated.

I'm starting to learn scala-graph by example. In each post, I will post the sample code and the generated pictures.

The following code shows how to creat a graph, calculate the shortest path of two nodes and export the particular graph with a highlighted path to dot format.

import scalax.collection.Graphimport scalax.collection.GraphEdge.DiEdgeimport scalax.collection.GraphPredef._import scalax.collection.io.dot._import implicits._import java.io.PrintWriterimport sys.process._object Main extends App {  val dg = Graph(0~>1, 2~>0, 2~>3, 3~>2, 3~>5, 4~>2,    4~>3, 5~>4, 6~>0, 6~>4, 6~>9, 7~>6, 7~>8, 8~>7,    8~>9, 9~>10, 9~>11, 10~>12, 11~>12, 12~>9)  def n(outer: Int): dg.NodeT = dg get outer  val path = (n(7) shortestPathTo n(0)).get  val root = new DotRootGraph(true, id = Some(Id("Dot")))  def edgeTransformer(graph: Graph[Int, DiEdge], path: Graph[Int, DiEdge]#Path,                      innerEdge: Graph[Int,DiEdge]#EdgeT): Option[(DotGraph,DotEdgeStmt)]    = innerEdge match {      case graph.EdgeT(source, target) =>        if (path.edges.exists(e => e.equals(innerEdge)))          Some((root, DotEdgeStmt(source.toString, target.toString,                  List(DotAttr("color", "#ff0000")))))        else          Some((root, DotEdgeStmt(source.toString, target.toString)))    }  val dot = dg.toDot(root, edgeTransformer(dg, path, _))  val dotFile = new PrintWriter("graph.dot")  dotFile.println(dot.toString)  dotFile.close  "dot -Tpng graph.dot -o graph.png" !}

References

转载地址:http://rcezo.baihongyu.com/

你可能感兴趣的文章
echarts
查看>>
javascript 总结(String篇)
查看>>
cenots6.8搭建FTP服务器
查看>>
简单说 JavaScript实现雪花飘落效果
查看>>
开源库推荐文章
查看>>
CSS盒子模型以及外边框合并的问题
查看>>
基于Vue + Node.js + MongoDB的图片上传组件,实现图片的预览和删除
查看>>
Netty4.x 源码实战系列(二):服务端bind流程详解
查看>>
springmvc+mybatis 构建cms内容发布系统
查看>>
Koa v2.x 中文文档 从 Koa v1.x 迁移到 v2.x
查看>>
Spring Cloud中声明式服务调用Feign
查看>>
Longest Valid Parentheses
查看>>
想知道es6的导入导出吗?来,我来告诉你~
查看>>
PHP面试之一:PHP基础知识点
查看>>
Higher-order Components 高阶组件
查看>>
Jupyter 魔术命令(magic commands)
查看>>
FastReport .Net:通过FTP发送PDF报表
查看>>
Condition 实现消费者生产者
查看>>
react-native 布局篇之position
查看>>
实用:快速定位前端界面交互JS代码
查看>>