I created this code while solving the Euler Project problem 83.
In the 5 by 5 matrix below, the minimal path sum from the top left to the bottom right, by moving left, right, up, and down, is indicated in bold red and is equal to 2297.
Find the minimal path sum, in matrix.txt (right click and "Save Link/Target As..."), a 31K text file containing a 80 by 80 matrix, from the top left to the bottom right by moving left, right, up, and down.
I have a matrix with weights stored as a list-of-lists. The task is to traverse the matrix from the top-left element to the bottom right element. At every point, I can go up, down, left or right. The problem is to find the path with the minimal summed weights.
To answer this, I converted the matrix to a graph (Make_Graph
), represented as a dictionary in Python. The key in the dictionary is the string row_index, column_index
(Name_Name
). My next step in solving the Euler problem (not shown here) is to use Dijkstra's algorithm to find the shortest path.
I'm an amateur Python enthusiast, not a professional programmer. I have the feeling that Make_Graph
can be made more Pythonic, but I don't know how. It looks a bit messy to me.
def node_name(row_index, col_index):
return str(row_index)+","+str(col_index)
def make_graph(matrix):
graph={}
for rowindex,row in enumerate(matrix):
for colindex,value in enumerate(row):
graph[node_name(rowindex,colindex)]={}
# Up
if rowindex>0: graph[node_name(rowindex,colindex)][node_name(rowindex-1,colindex)]=matrix[rowindex-1][colindex]
# Down
if rowindex<len(matrix)-1: graph[node_name(rowindex,colindex)][node_name(rowindex+1,colindex)]=matrix[rowindex+1][colindex]
# Left
if colindex>0: graph[node_name(rowindex,colindex)][node_name(rowindex,colindex-1)]=matrix[rowindex][colindex-1]
# Right
if colindex<len(row)-1: graph[node_name(rowindex,colindex)][node_name(rowindex,colindex+1)]=matrix[rowindex][colindex+1]
return graph
print make_graph([[1,2],[3,4],[5,6]])