diff --git a/examples/AdvancedPrim.jl b/examples/AdvancedPrim.jl new file mode 100644 index 00000000..83eea02d --- /dev/null +++ b/examples/AdvancedPrim.jl @@ -0,0 +1,49 @@ +#= +AdvancedPrim: +- Julia version: +- Author: samuel +- Date: 2021-09-10 +=# + +using SuiteSparseGraphBLAS +using SparseArrays +using LinearAlgebra + +function stoppingCondition(m, n) + +infinito = true + +for i = 1:n + if(m[i] == 0.0) + return false + end +end + return infinito +end + +#A is the input matrix, n is the number of nodes, m is the source matrix. Output: minimum spanning tree and minimum spanning tree cost +function a_prim(A, n, m) + + d = GBVector{Float64}(n) + + weight = 0.0 + + d = A[1,:] + mst = GBVector{Float64}(n) #minimum spanning tree + + while(stoppingCondition(m, n) == false) + + u = argmin(m'+d) + m[u[2]] = Inf + push!(mst, d[u[2]]) + weight = weight + d[u[2]] + print("WEIGHT: ") + print(weight) + d = emul(d, A[u[1],:], BinaryOps.MIN) + print(" ITERATION FINISHED") + print("\n\n\n") + end + + return weight, mst + +end \ No newline at end of file diff --git a/examples/BFSLevelsMultiSource.jl b/examples/BFSLevelsMultiSource.jl new file mode 100644 index 00000000..78aeaab9 --- /dev/null +++ b/examples/BFSLevelsMultiSource.jl @@ -0,0 +1,47 @@ +#= +BFSLevelsMultiSource: +- Julia version: 1.6.2 +- Author: samuel +- Date: 2021-09-10 +=# + +using SuiteSparseGraphBLAS +using SparseArrays +using LinearAlgebra + +#A is the input matrix, S is the source matrix, and n is the number of nodes, s is the number of sources in the matrix S, Output: Levels matrix +function ms_bfsl(A, S, n, s) + + #this vector allows to check if a value is already present inside the distance matrix, + #if it's present we don't want it to be changed + levels = GBVector{Int64}(n) + for i = 1:n + levels[i] = i + end + + #distance matrix + distance = GBMatrix{Int64}(s, n) + for i = 1:s + for j = 1:n + distance[i, j] = 0 + end + end + + for level = 1:n-1 + + for i = 1:s + for j = 1:n + if(S[i, j]!=0 && distance[i, j] ∉ levels) #* + distance[i, j] = level #* + end + end + end + + + S = mul(S, A, Semirings.LOR_LAND, mask=distance, desc=Descriptors.RC) + + end + + return distance + +end \ No newline at end of file diff --git a/examples/BFSParents.jl b/examples/BFSParents.jl new file mode 100644 index 00000000..c41dbb5c --- /dev/null +++ b/examples/BFSParents.jl @@ -0,0 +1,74 @@ +#= +BFSParents: +- Julia version: 1.6.2 +- Author: samuel +- Date: 2021-09-10 +=# + +using SuiteSparseGraphBLAS +using SparseArrays +using LinearAlgebra + +#function to insert inside the result array +function insert(p, result) + for i=1:length(p) + if(p[i]!=nothing && result[i]==nothing) + result[i] = p[i] + end + end + + return result + +end + +#function to update visited nodes +function updateVisited(f, visited) + for i=1:length(f) + if(f[i]!=nothing) + visited[i] = true + end + end + + return visited + +end + +#A is the input matrix, s is the source node, and n is the number of nodes, Output: parent matrix +function bfs_parents(A, s, n) + + index = GBVector{Int64}(n) + for i = 1:n + index[i] = i + end + + #wavefront vector -> w[s]=source node insrted in wavefront + f = GBVector{Int64}(n) + f[s] = 1 + + #parent vector -> p[s]=1 because the source is already visited + p = GBVector{Int64}(n) + + #result vector -> result[s]=0 because the parent of the source is considered node 0 + result = GBVector{Int64}(n) + result[s] = 0 + + #visited vector -> v[s]=1 because the source is already visited + visited = GBVector{Bool}(n) + visited[s] = true + + for i = 1:n-1 + f = mul(f, A, Semirings.MIN_FIRST, mask=p, desc=Descriptors.SC) + p = f[:, mask=f, desc=Descriptors.S] + f = index[:, mask=f, desc=Descriptors.S] + result = insert(p, result) + visited = updateVisited(f, visited) + unvisited = filter(x -> x!=true, visited) + if(isempty(unvisited)) + break + end + + end + + return result + +end \ No newline at end of file diff --git a/examples/BFSParentsMultiSource.jl b/examples/BFSParentsMultiSource.jl new file mode 100644 index 00000000..f5bbdc18 --- /dev/null +++ b/examples/BFSParentsMultiSource.jl @@ -0,0 +1,69 @@ +#= +BFSParentsMultiSource: +- Julia version: 1.6.2 +- Author: samuel +- Date: 2021-09-10 +=# + +using SuiteSparseGraphBLAS +using SparseArrays +using LinearAlgebra + +function insert(P, R, s, n) + for i = 1:s + for j = 1:n + if(P[i, j] !== nothing && R[i, j] === nothing) #* + R[i, j] = P[i, j] + end + end + end + + return R + +end + +#A is the input matrix, S are the sources, n is the number of nodes, s is the number of sources, Output: Parents matrix +function ms_bfsp(A, S, n, s) + + index = GBMatrix{Int64}(s, n) + for i = 1:n + for j = 1:s + index[j, i] = i + end + end + + #frontier matrix -> F=S source nodes insrted in frontier + F = GBMatrix{Int64}(n, n) + F = S + + #parent matrix -> P[S]=S because the source is already visited + P = GBMatrix{Int64}(n, n) + P = S + + #result matrix -> R[S]=0 because the parents of the source are considered node 0 + R = GBMatrix{Int64}(s, n) + LR = GBMatrix{Int64}(s, n) + R = copy(S) + for i = 1:n + for j = 1:s + if(R[j, i]!=nothing) + R[j, i] = 0 + end + end + end + for _ ∈ 1:n-1 + F = mul(F, A, (min, first), mask=P, desc=Descriptors.RC) + P = F[:, :, mask=F, desc=Descriptors.S] + F = index[:, :, mask=F, desc=Descriptors.S] + R = insert(P, R, s, n) + if(R == LR) + break + end + LR = copy(R) + + end + + return R + +end + diff --git a/examples/BasicPrim.jl b/examples/BasicPrim.jl new file mode 100644 index 00000000..4214f5ea --- /dev/null +++ b/examples/BasicPrim.jl @@ -0,0 +1,44 @@ +#= +BasicPrim: +- Julia version: 1.6.2 +- Author: samuel +- Date: 2021-09-10 +=# + +using SuiteSparseGraphBLAS +using SparseArrays + +function stoppingCondition(m, n) + +infinito = true + +for i = 1:n + if(m[i] == 0.0) + return false + end +end + return infinito +end + +#A is the input matrix, n is the number of nodes, m is the source matrix Output: minimum spanning tree cost +function b_prim(A, n, m) + + d = GBVector{Float64}(n) + + weight = 0.0 + + d = A[1,:] + print("INITIAL WEIGHT: ") + print(weight) + print("\n\n") + while(stoppingCondition(m, n) == false) + + u = argmin(m'+d) + m[u[2]] = Inf + weight = weight + d[u[2]] + d = min.(d, A[u[1],:]) + end + + return weight + +end \ No newline at end of file diff --git a/examples/BellmanFord.jl b/examples/BellmanFord.jl new file mode 100644 index 00000000..460995e6 --- /dev/null +++ b/examples/BellmanFord.jl @@ -0,0 +1,27 @@ +#= +BellmanFord: +- Julia version: 1.6.2 +- Author: samuel +- Date: 2021-09-10 +=# + +using SuiteSparseGraphBLAS +using SparseArrays + +#A is the input matrix, s is the source node, and n is the number of nodes, Output: Shortest path from source node +function bellmanford(A, s, n) + + #vector init +inf + d = GBVector{Float64}(n) + for i = 1:n + d[i] = Inf + end + + d[s]=0.0 + for _ ∈ 1:n + d = mul(d, A, (min, +), mask=d, desc=Descriptors.S) + end + + return d + +end \ No newline at end of file diff --git a/examples/CDLP.jl b/examples/CDLP.jl new file mode 100644 index 00000000..e5ad3921 --- /dev/null +++ b/examples/CDLP.jl @@ -0,0 +1,80 @@ +#= +CDLP: +- Julia version: 1.6.2 +- Author: samuel +- Date: 2021-09-10 +=# + +using SuiteSparseGraphBLAS +using SparseArrays +using LinearAlgebra +using DataStructures + +#minMode function: given a collection [1, 5, 5, 3, 3, 7] will return 3 because the highest repetition is 2 +#and the lowest most repeated number is 3 +function minMode(c) + + minVal = -1 + i = 0 + + for row in eachrow(c) + i = i+1 + row = collect(row) + temp = filter(x -> x!=0, row) + count = counter(temp) + sortedCollection = sort(collect(count), by=x->x[2], rev=true) + minVal = sortedCollection[1][1] + repetitions = sortedCollection[1][2] + for (key, value) in sortedCollection + if(value == repetitions) + if(key < minVal) + minVal = key + end + end + end + + end + + return minVal +end + +#can be useful to tweak the algorithm +function diag_conversion(lab, n) +for i = 1:n + for j = 1:n + if(i==j) + lab[i,j]=i + else + lab[i,j]=0 + end + end + end + return lab +end + +#A is the input matrix, n is the number of nodes, t the number of iterations Output: label propagation matrix +function cdlp(A, n, t) + + lab = GBMatrix{Int64}(n,n) + + for i = 1:n + for j = 1:n + if(i==j) + lab[i,j]=i + else + lab[i,j]=0 + end + end + end + + for k = 1:t + F = A * lab + for i = 1:n + r = F[i,:] + r = sort(r, dims=2) + lab[i, i] = minMode(r) + end + end + return lab + +end \ No newline at end of file diff --git a/examples/CMU.jl b/examples/CMU.jl new file mode 100644 index 00000000..1d6da176 --- /dev/null +++ b/examples/CMU.jl @@ -0,0 +1,26 @@ +#= +CMU: +- Julia version: 1.6.2 +- Author: samuel +- Date: 2021-09-10 +=# + +using SuiteSparseGraphBLAS +using SparseArrays +using LinearAlgebra + +#A is the input matrix, n is the number of nodes in the original matrix, Output: Triangle Count +function cmu(A, n) + t = 0 + for i ∈ 2:n-1 + #rpva + A20 = A[i+1:n,begin:i] + a10 = A[begin:i, i] + a12 = A[i, i+1:n] + + partial1 = a10' * A20' + partial2 = partial1 * a12' + t += partial2[1] + end + return t +end \ No newline at end of file diff --git a/examples/Cohen.jl b/examples/Cohen.jl new file mode 100644 index 00000000..b8f112f2 --- /dev/null +++ b/examples/Cohen.jl @@ -0,0 +1,17 @@ +#= +Cohen: +- Julia version: 1.6.2 +- Author: samuel +- Date: 2021-09-10 +=# + +using SuiteSparseGraphBLAS +using SparseArrays +using LinearAlgebra + +##A is the input matrix, Output: Triangle Count +function cohen(A) + U = select(TRIU, A) + L = select(TRIL, A) + return reduce(+, mul(L, U, (+, pair); mask=A)) ÷ 2 +end \ No newline at end of file diff --git a/examples/FloydWarshall.jl b/examples/FloydWarshall.jl new file mode 100644 index 00000000..fde73a78 --- /dev/null +++ b/examples/FloydWarshall.jl @@ -0,0 +1,25 @@ +#= +FloydWarshall: +- Julia version: 1.6.2 +- Author: samuel +- Date: 2021-09-10 +=# + +using SuiteSparseGraphBLAS +using SparseArrays + +#A is the input matrix for Floyd-Warshall multiple sources multiple destinations, Output: shortest paths +function floydwarshall(A, n) + + #distance matrix = input matrix + D = A + result = GBMatrix{Int64}(n, n) + + for i = 1:n + partial = mul(D[:,i], D[i,:], (min, +)) + D = emul(D, partial, min) + result = partial + end + return result + +end \ No newline at end of file diff --git a/examples/KTruss.jl b/examples/KTruss.jl new file mode 100644 index 00000000..39bf5c27 --- /dev/null +++ b/examples/KTruss.jl @@ -0,0 +1,26 @@ +#= +KTruss: +- Julia version: 1.6.2 +- Author: samuel +- Date: 2021-09-10 +=# + +using SuiteSparseGraphBLAS +using SparseArrays +using LinearAlgebra + +#A is the original matrix, k is the requested truss, n the nodes in the original matrix, Output: K-Truss graph +function ktruss(A, k, n) + + C = A + for _ ∈ 1:n-1 + C = mul(C, C, Semirings.PLUS_LAND, mask=C) + C = select(>=, C, k-2) + print(C) + end + return C + +end + +#*** +#optional: we could also tell the algorithm to stop if the non zero values didn't change from the previous iteration \ No newline at end of file diff --git a/examples/NodeWiseTriangleCount.jl b/examples/NodeWiseTriangleCount.jl new file mode 100644 index 00000000..2aba3b8f --- /dev/null +++ b/examples/NodeWiseTriangleCount.jl @@ -0,0 +1,19 @@ +#= +NodeWiseTriangleCount: +- Julia version: 1.6.2 +- Author: samuel +- Date: 2021-09-10 +=# + +using SuiteSparseGraphBLAS +using SparseArrays +using LinearAlgebra + +#A is the input matrix. Output: triangles originated by each node +function nodewise_tc(A) + + partial = mul(A, A, (+, *), mask=A) + t = reduce(+, partial, dims=2) + return t/2 + +end \ No newline at end of file diff --git a/examples/Sandia.jl b/examples/Sandia.jl new file mode 100644 index 00000000..46d90b98 --- /dev/null +++ b/examples/Sandia.jl @@ -0,0 +1,19 @@ +#= +Sandia: +- Julia version: 1.6.2 +- Author: samuel +- Date: 2021-09-10 +=# + +using SuiteSparseGraphBLAS +using SparseArrays +using LinearAlgebra + +#A is the input matrix, Output: Triangle Count +function sandia(A) + + L = tril(A) + C = *(+, *; mask=L)(L, L) + return reduce(+, C, dims=:) + +end \ No newline at end of file