Skip to content

Commit 6ddb94e

Browse files
Merge pull request #8 from JuliaDiffEq/pkj-m-patch-3
Created matrix to graph conversion function
2 parents 68cda19 + f92965f commit 6ddb94e

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

src/matrix2graph.jl

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using SparseArrays
2+
include("custom_graph.jl")
3+
4+
"""
5+
matrix2graph(SparseMatrix)
6+
7+
A utility function to generate a graph from input
8+
sparse matrix, columns are represented with vertices
9+
and 2 vertices are connected with an edge only if
10+
the two columns are mutually orthogonal.
11+
"""
12+
function matrix2graph(SparseMatrix::SparseMatrixCSC{Int64,Int64})
13+
dropzeros(SparseMatrix)
14+
(rows_index, cols_index, val) = findnz(SparseMatrix)
15+
16+
V = col = size(SparseMatrix, 2)
17+
row = size(SparseMatrix, 1)
18+
19+
vertices = zeros(Int64, 0)
20+
edges = [Vector{Int64}() for _ in 1:V]
21+
22+
graph = CGraph(vertices, edges)
23+
for i = 1:V
24+
push!(vertices, i)
25+
end
26+
27+
for i = 1:length(cols_index)
28+
cur_col = cols_index[i]
29+
for j = 1:(i-1)
30+
next_col = cols_index[j]
31+
if cur_col != next_col
32+
if row_index[i] == row_index[j]
33+
#add edge
34+
add_edge!(graph, i, j)
35+
end
36+
end
37+
end
38+
end
39+
return graph
40+
end

0 commit comments

Comments
 (0)