DirectedGraph typeclass and witnesses for the Jung package

Directed Graph

Imports and implicits

import cats.implicits._
import cats.Show
import axle._

import edu.uci.ics.jung.graph.DirectedSparseGraph
import axle.jung._

import axle.algebra._
import axle.syntax.directedgraph.directedGraphOps
import axle.syntax.undirectedgraph.undirectedGraphOps
import axle.syntax.finite.finiteOps

Example with String is the vertex value

val (a, b, c, d) = ("a", "b", "c", "d")
// a: String = a
// b: String = b
// c: String = c
// d: String = d

And an Edge type with two values (a String and an Int) to represent the edges

class Edge(val s: String, val i: Int)
// defined class Edge

Invoke the DirectedGraph typeclass with type parameters that denote that we will use Jung’s DirectedSparseGraph as the graph type, with String and Edge as vertex and edge values, respectively.

val jdg = DirectedGraph.k2[DirectedSparseGraph, String, Edge]
// jdg: axle.algebra.DirectedGraph[edu.uci.ics.jung.graph.DirectedSparseGraph[String,Edge],String,Edge] = axle.jung.package$$anon$4@4a9b50e3

Use the jdg witness’s make method to create the directed graph

val dg = jdg.make(List(a, b, c, d),
  List(
    (a, b, new Edge("hello", 1)),
    (b, c, new Edge("world", 4)),
    (c, d, new Edge("hi", 3)),
    (d, a, new Edge("earth", 1)),
    (a, c, new Edge("!", 7)),
    (b, d, new Edge("hey", 2))))
// dg: edu.uci.ics.jung.graph.DirectedSparseGraph[String,Edge] =
// Vertices:a,b,c,d
// Edges:Edge@3f8c82fe[a,b] Edge@71281e5a[d,a] Edge@30c28588[c,d] Edge@2ae8e9f3[a,c] Edge@c615cfd[b,d] Edge@2d516fdc[b,c]
dg.size
// res2: Int = 4

dg.findVertex(_ === "a").map(v => dg.successors(v))
// res3: Option[Set[String]] = Some(Set(b, c))

dg.findVertex(_ === "c").map(v => dg.successors(v))
// res4: Option[Set[String]] = Some(Set(d))

dg.findVertex(_ === "c").map(v => dg.predecessors(v))
// res5: Option[Set[String]] = Some(Set(a, b))

dg.findVertex(_ === "c").map(v => dg.neighbors(v))
// res6: Option[Set[String]] = Some(Set(a, b, d))

Visualize the graph

import axle.visualize._
// import axle.visualize._

import axle.web._
// import axle.web._

implicit val showEdge: Show[Edge] = new Show[Edge] {
  def show(e: Edge): String = e.s + " " + e.i
}
// showEdge: cats.Show[Edge] = $anon$1@42413b44

val vis = DirectedGraphVisualization(
  dg,
  width = 300,
  height = 300,
  border = 10,
  radius = 10,
  arrowLength = 10,
  color = Color.green,
  borderColor = Color.black,
  fontSize = 12
)
// vis: axle.visualize.DirectedGraphVisualization[edu.uci.ics.jung.graph.DirectedSparseGraph[String,Edge]] =
// DirectedGraphVisualization(Vertices:a,b,c,d
// Edges:Edge@3f8c82fe[a,b] Edge@71281e5a[d,a] Edge@30c28588[c,d] Edge@2ae8e9f3[a,c] Edge@c615cfd[b,d] Edge@2d516fdc[b,c] ,300,300,10,10,10,Color(0,255,0),Color(0,0,0),12)

svg(vis, "SimpleDirectedGraph.svg")

directed graph

Undirected Graph

Imports

import edu.uci.ics.jung.graph.UndirectedSparseGraph
// import edu.uci.ics.jung.graph.UndirectedSparseGraph

Example using the Edge edge value defined above and the same vertex values defined above.

Invoke the UndirectedGraph typeclass with type parameters that denote that we will use Jung’s UndirectedSparseGraph as the graph type, with String and Edge as vertex and edge values, respectively.

val jug = UndirectedGraph.k2[UndirectedSparseGraph, String, Edge]
// jug: axle.algebra.UndirectedGraph[edu.uci.ics.jung.graph.UndirectedSparseGraph[String,Edge],String,Edge] = axle.jung.package$$anon$8@592675ba

Use the jug witness’s make method to create the undirected graph

val ug = jug.make(List(a, b, c, d),
  List(
    (a, b, new Edge("hello", 10)),
    (b, c, new Edge("world", 1)),
    (c, d, new Edge("hi", 3)),
    (d, a, new Edge("earth", 7)),
    (a, c, new Edge("!", 1)),
    (b, d, new Edge("hey", 2))))
// ug: edu.uci.ics.jung.graph.UndirectedSparseGraph[String,Edge] =
// Vertices:a,b,c,d
// Edges:Edge@79120c63[a,b] Edge@393b71f8[b,c] Edge@16e2ddfa[c,d] Edge@3e0153c[d,a] Edge@6a787037[a,c] Edge@8ae6831[b,d]
ug.size
// res8: Int = 4

ug.findVertex(_ == "c").map(v => ug.neighbors(v))
// res9: Option[Iterable[String]] = Some(Wrappers(a, b, d))

ug.findVertex(_ == "a").map(v => ug.neighbors(v))
// res10: Option[Iterable[String]] = Some(Wrappers(b, c, d))

Visualize the graph

import axle.visualize._
// import axle.visualize._

import axle.web._
// import axle.web._

val vis = UndirectedGraphVisualization(ug, width=300, height=300, border=10, color=Color.yellow)
// vis: axle.visualize.UndirectedGraphVisualization[edu.uci.ics.jung.graph.UndirectedSparseGraph[String,Edge]] =
// UndirectedGraphVisualization(Vertices:a,b,c,d
// Edges:Edge@79120c63[a,b] Edge@393b71f8[b,c] Edge@16e2ddfa[c,d] Edge@3e0153c[d,a] Edge@6a787037[a,c] Edge@8ae6831[b,d] ,300,300,10,10,Color(255,255,0),Color(0,0,0),12)

svg(vis, "SimpleUndirectedGraph.svg")

undirected graph