Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function ShortestPath(DAG, K2Score)
- for i = 1:length(K2Score)
- ind = findall(K2Score .== 0.0)
- #K2Score[1,ind] ~= root,
- #K2Score[1,ind] = 0.01 # ORIGINAL
- K2Score[ind] .= 0.01 # CHANGED BY HB
- end
- # Finding the root.
- G = DAG
- M,N = size(DAG)
- k = 1
- for i in 1:M
- for j in 1:N
- #if G[i,j] ~= 0 # ORIGINAL
- if isapprox(G[i,j], 0; atol = 0.01) # CHANGED BY HB, ~= does not exit
- # Also, check how isapprox works, G[i,j] ~= 0 does not make sense
- # without an absolute tolerance.
- a[1,k] = i
- a[2,k] = j
- k = k +1
- end
- end
- end
- ##
- n = k-1
- k = 1;
- for j in 1:N
- flag = 0
- for i in 1:M
- # if G[i,j] ~= 0 # ORIGINAL
- if isapprox(G[i,j], 0; atol = 0.01) # CHANGED BY HB
- flag = 1
- end
- end
- if flag .== 0
- root[1,k] = j; # Must store all the values
- k = k+1
- end
- end
- ## Applying bellman algorithm
- for ii = 1:length(root)
- k =1
- for i = 1:n
- if a[1,i] .== root[1,ii]
- b[1,k] = a[1,i]
- b[2,k] = a[2,i]
- k = k+1
- end
- end
- for i = 1:n
- # if a[1,i] ~= root[1,ii] # ORIGINAL
- if isapprox(a[1,i], root[1, ii]; atol = 0.01) # CHANGED BY HB
- b[1,k] = a[1,i]
- b[2,k] = a[2,i]
- k = k+1
- end
- end
- for i = 1:M
- for ii = 1:length(root)
- dist[ii,i] = 20000; # infinity Value
- end
- end
- dist[ii,root[1,ii]] = 0
- for i = 1: M
- for j = 1:n
- k = dist[ii,b[1,j]] + 1
- if k .< dist[ii,b[2,j]]
- dist[ii,b[2,j]] = k
- end
- end
- end
- ## Finding the minimum of distance
- final_dist = minimum(dist, dims = 1)
- end
- return final_dist
- end
- #using CSV, DataFrames
- #dag = CSV.read("UCIV_WOMEN_DAG.csv", DataFrame)
- #k2s = CSV.read("UCIV_WOMEN_K2Score.csv", DataFrame)
- #ShortestPath(dag, k2s)
- import DelimitedFiles
- dag, header = DelimitedFiles.readdlm(
- "./UCIV_WOMEN_DAG.csv", ',', Float64, '\n'; header = true
- )
- k2s, header = DelimitedFiles.readdlm(
- "./UCIV_WOMEN_K2Score.csv", ',', Float64, '\n'; header = true
- )
- ShortestPath(dag, k2s)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement