# Weighted graph using SparseCSC julia

**URL:** <https://discourse.julialang.org/t/weighted-graph-using-sparsecsc-julia/84071>\
**Category:** Graphs\
**Tags:** sparse, ijulia, graphs\
**Created:** [July 11, 2022, 8:16pm UTC](https://discourse.julialang.org/t/weighted-graph-using-sparsecsc-julia/84071 "2022-07-11T20:16:05Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ayodyas](https://avatars.discourse-cdn.com/v4/letter/a/90db22/32.png) [@ayodyas](https://discourse.julialang.org/u/ayodyas)\
**Post date:** [July 11, 2022, 8:16pm UTC](https://discourse.julialang.org/t/weighted-graph-using-sparsecsc-julia/84071/1 "2022-07-11T20:16:05Z")

</div>

I am new to julia and was trying to do weighted graph partition using metis in Julia. I coudnt undertstand how the SparseCSc for graph creation with edge and vertex weight as required for format of the metis (graph partition) is created in Julia.

 ![image](https://global.discourse-cdn.com/julialang/original/3X/5/1/51314882b02ad5a5942d9d3ccd00ddb09d98dd71.png)

---

<div class="post-metadata">

**Author:** ![acxz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/acxz/32/16759_2.png) [@acxz](https://discourse.julialang.org/u/acxz)\
**Post date:** [July 11, 2022, 10:11pm UTC](https://discourse.julialang.org/t/weighted-graph-using-sparsecsc-julia/84071/2 "2022-07-11T22:11:37Z")

</div>

Ayy, I actually just went through the metis documentation on this a couple of weeks ago.

One thing to note is that Metis uses CSR and Julia uses CSC. See [Sparse Arrays · The Julia Language](https://docs.julialang.org/en/v1/stdlib/SparseArrays/) on how to create a CSC sparse matrix.

The best way to understand the interplay between Julia and Metis would be to look at Metis.jl: [Metis.jl/Metis.jl at master · JuliaSparse/Metis.jl · GitHub](https://github.com/JuliaSparse/Metis.jl/blob/master/src/Metis.jl)

In fact, there is a function that converts a Julia CSC into Metis CSR: [Metis.jl/Metis.jl at b6723294307cdba28ccf3f11c4990ce1ea797758 · JuliaSparse/Metis.jl · GitHub](https://github.com/JuliaSparse/Metis.jl/blob/b6723294307cdba28ccf3f11c4990ce1ea797758/src/Metis.jl#L36-L59)

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [July 11, 2022, 11:49pm UTC](https://discourse.julialang.org/t/weighted-graph-using-sparsecsc-julia/84071/3 "2022-07-11T23:49:47Z")

</div>

There is a stale/forgotten PR here: [JuliaSparse/Metis.jl#37](https://github.com/JuliaSparse/Metis.jl/pull/37) which adds weight support for [JuliaGraphs/SimpleWeightedGraphs.jl](https://github.com/JuliaGraphs/SimpleWeightedGraphs.jl) graphs. For sparse matrix input I also just now opened [JuliaSparse/Metis.jl#45](https://github.com/JuliaSparse/Metis.jl/pull/45).

---

<div class="post-metadata">

**Author:** ![ayodyas](https://avatars.discourse-cdn.com/v4/letter/a/90db22/32.png) [@ayodyas](https://discourse.julialang.org/u/ayodyas)\
**Post date:** [July 12, 2022, 8:27am UTC](https://discourse.julialang.org/t/weighted-graph-using-sparsecsc-julia/84071/4 "2022-07-12T08:27:14Z")

</div>

Thanks all, it could be really helpful if someone can post simple example in julia for creating a weighted graph using sparseCSC for using it in metis partition with consideration of vertex weights

thanks in advance

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [July 12, 2022, 3:05pm UTC](https://discourse.julialang.org/t/weighted-graph-using-sparsecsc-julia/84071/5 "2022-07-12T15:05:22Z")

</div>

[JuliaSparse/Metis.jl#45](https://github.com/JuliaSparse/Metis.jl/pull/45) is merged and included in version 1.2.0 of Metis.jl. You can now use the entries of a sparse matrix as edge weights. For example, this graph (`#N` are the vertices and `N` the weights):

```julia
#1 --- 5 --- #2
| |
6 7
| |
#3 --- 8 --- #4

```

Can be constructed and partitioned as follows:

```julia
# Construct the sparse matrix (note that we add both 1 -> 2, and 2 -> 1, etc)
I = [1, 2, 1, 3, 3, 4, 2, 4]
J = [2, 1, 3, 1, 4, 3, 4, 2]
V = [5, 5, 6, 6, 8, 8, 7, 7]

A = sparse(I, J, V)

# Construct the graph with weights
G = Metis.graph(A; weights=true)

# Do partition
Metis.partition(G, 2)

```

---

<div class="post-metadata">

**Author:** ![ayodyas](https://avatars.discourse-cdn.com/v4/letter/a/90db22/32.png) [@ayodyas](https://discourse.julialang.org/u/ayodyas)\
**Post date:** [July 12, 2022, 8:26pm UTC](https://discourse.julialang.org/t/weighted-graph-using-sparsecsc-julia/84071/6 "2022-07-12T20:26:10Z")

</div>

Thank you for the explanation. It could be much useful if the Vertex weights could be passed as an input argument

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [July 13, 2022, 8:47am UTC](https://discourse.julialang.org/t/weighted-graph-using-sparsecsc-julia/84071/7 "2022-07-13T08:47:18Z")

</div>

I left that as a TODO in the code: [Metis.jl/Metis.jl at 0d1b486c5b56ce38a109248a3310d402bb2f3da4 · JuliaSparse/Metis.jl · GitHub](https://github.com/JuliaSparse/Metis.jl/blob/0d1b486c5b56ce38a109248a3310d402bb2f3da4/src/Metis.jl#L47) should be trivial to contribute that functionality if you want to.

---

<div class="post-metadata">

**Author:** ![ayodyas](https://avatars.discourse-cdn.com/v4/letter/a/90db22/32.png) [@ayodyas](https://discourse.julialang.org/u/ayodyas)\
**Post date:** [July 13, 2022, 8:48pm UTC](https://discourse.julialang.org/t/weighted-graph-using-sparsecsc-julia/84071/8 "2022-07-13T20:48:21Z")

</div>

will defintely try that. Which plotting module can be used to plot the graph with weights for visualising metis. As the gplot is not working with this format

---

<div class="post-metadata">

**Author:** ![ayodyas](https://avatars.discourse-cdn.com/v4/letter/a/90db22/32.png) [@ayodyas](https://discourse.julialang.org/u/ayodyas)\
**Post date:** [July 19, 2022, 7:31am UTC](https://discourse.julialang.org/t/weighted-graph-using-sparsecsc-julia/84071/9 "2022-07-19T07:31:43Z")

</div>

Is there any optional input to prevent isolated nodes after METIS partitions (assume that no isolated nodes in inputs) ? Means that each partioned zones are always together and is connected
