Created
October 23, 2014 16:12
-
-
Save alieseparker/45838d838f3593aea3f5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Warshall | |
def initialize(adjacencyMatrix) | |
@adjacencyMatrix=adjacencyMatrix | |
end | |
def getPathMatrix | |
numNodes=@adjacencyMatrix[0].length | |
pathMatrix=Array.new(@adjacencyMatrix) | |
for k in 0...numNodes | |
for i in 0...numNodes | |
#Optimization: if no path from i to k, no need to test k to j | |
if pathMatrix[i][k]==1 then | |
for j in 0...numNodes | |
if pathMatrix[k][j]==1 then | |
pathMatrix[i][j]=1 | |
end | |
end | |
end | |
end | |
end | |
return pathMatrix | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Your optimization is not working. All jumps across 2 nodes (searching for path from a -> d in graph a -> b, b -> c, c ->d) will fail.