Julia - Plotting minimum spanning tree with GraphPlots

Viewed 211

I'm using the package LightGraphs.jl to generate a minimum spanning tree:

LightGraphs.kruskal_mst(g)

The output is an array of edges:

4-element Array{SimpleWeightedEdge{Int64,Float64},1}:
Edge 6 => 9 with weight 1.0
Edge 8 => 9 with weight 1.
Edge 9 => 10 with weight 1.0
Edge 6 => 11 with weight 1.0

My question is then how to plot this spanning tree?

1 Answers

I was able to figure it out. The best way to do this is to create a new graph, and then use the output from kruskal_mst() to populate the new graph. Here is an example:

g_mst = SimpleWeightedGraph(size(g)[1]) #Create a new graph
for ew in kruskal_mst(g)
   add_edge!(g_mst,ew.src,ew.dst,ew.weight)
end
gplot(g_mst)
Related