Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new examples #57

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions examples/AdvancedPrim.jl
Original file line number Diff line number Diff line change
@@ -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
47 changes: 47 additions & 0 deletions examples/BFSLevelsMultiSource.jl
Original file line number Diff line number Diff line change
@@ -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
74 changes: 74 additions & 0 deletions examples/BFSParents.jl
Original file line number Diff line number Diff line change
@@ -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
69 changes: 69 additions & 0 deletions examples/BFSParentsMultiSource.jl
Original file line number Diff line number Diff line change
@@ -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

44 changes: 44 additions & 0 deletions examples/BasicPrim.jl
Original file line number Diff line number Diff line change
@@ -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
27 changes: 27 additions & 0 deletions examples/BellmanFord.jl
Original file line number Diff line number Diff line change
@@ -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
Loading