Advertisement
Guest User

Untitled

a guest
Feb 13th, 2021
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Julia 1.99 KB | None | 0 0
  1. function ShortestPath(DAG, K2Score)
  2.  
  3.     for i = 1:length(K2Score)
  4.         ind = findall(K2Score .== 0.0)
  5.         #K2Score[1,ind] ~= root,
  6.         #K2Score[1,ind] = 0.01 # ORIGINAL
  7.         K2Score[ind] .= 0.01 # CHANGED BY HB
  8.     end
  9.  
  10.     #  Finding the root.
  11.     G = DAG
  12.     M,N = size(DAG)
  13.     k = 1
  14.     for i in 1:M
  15.         for j in 1:N
  16.             #if G[i,j] ~= 0 # ORIGINAL
  17.             if isapprox(G[i,j], 0; atol = 0.01) # CHANGED BY HB, ~= does not exit
  18.                 # Also, check how isapprox works, G[i,j] ~= 0 does not make sense
  19.                 # without an absolute tolerance.
  20.                 a[1,k] = i
  21.                 a[2,k] = j
  22.                 k = k +1
  23.             end
  24.         end
  25.     end
  26.  
  27.     ##
  28.     n = k-1
  29.     k = 1;
  30.     for j in 1:N
  31.         flag = 0
  32.         for i in 1:M
  33.             # if G[i,j] ~= 0 # ORIGINAL
  34.             if isapprox(G[i,j], 0; atol = 0.01) # CHANGED BY HB
  35.                 flag = 1
  36.             end
  37.         end
  38.         if flag .== 0
  39.             root[1,k] = j; # Must store all the values
  40.             k = k+1
  41.         end
  42.     end
  43.  
  44.     ## Applying bellman algorithm
  45.     for ii = 1:length(root)
  46.         k =1
  47.         for i = 1:n
  48.             if a[1,i] .== root[1,ii]
  49.                 b[1,k] = a[1,i]
  50.                 b[2,k] = a[2,i]
  51.                 k = k+1
  52.             end
  53.         end
  54.  
  55.         for i = 1:n
  56.             # if a[1,i] ~= root[1,ii] # ORIGINAL
  57.             if isapprox(a[1,i], root[1, ii]; atol = 0.01) # CHANGED BY HB
  58.                 b[1,k] = a[1,i]
  59.                 b[2,k] = a[2,i]
  60.                 k = k+1
  61.             end
  62.         end
  63.  
  64.         for i = 1:M
  65.             for ii = 1:length(root)
  66.                 dist[ii,i] = 20000; # infinity Value
  67.             end
  68.         end
  69.  
  70.         dist[ii,root[1,ii]] = 0
  71.         for i = 1: M
  72.             for j = 1:n
  73.                 k = dist[ii,b[1,j]] + 1
  74.                 if k .< dist[ii,b[2,j]]
  75.                     dist[ii,b[2,j]] = k
  76.                 end
  77.             end
  78.         end
  79.         ## Finding the minimum of distance
  80.         final_dist = minimum(dist, dims = 1)
  81.     end
  82.  
  83.     return final_dist
  84.  
  85. end
  86.  
  87. #using CSV, DataFrames
  88. #dag = CSV.read("UCIV_WOMEN_DAG.csv", DataFrame)
  89. #k2s = CSV.read("UCIV_WOMEN_K2Score.csv", DataFrame)
  90. #ShortestPath(dag, k2s)
  91.  
  92. import DelimitedFiles
  93. dag, header = DelimitedFiles.readdlm(
  94.     "./UCIV_WOMEN_DAG.csv", ',', Float64, '\n'; header = true
  95. )
  96. k2s, header = DelimitedFiles.readdlm(
  97.     "./UCIV_WOMEN_K2Score.csv", ',', Float64, '\n'; header = true
  98. )
  99. ShortestPath(dag, k2s)
  100.  
  101.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement