Write JSON File

#!/usr/bin/python3
# ====================================================================
# write minimum path matrix to a JSON file
# ====================================================================

import shortest_path as sp

## ---- outbound connections -------------------------------------
## ---- source nodes    [0]    [1]    [2]   (columns)

compact_costs = [ [0, [(1,4), (5,8)       ], "home"],
                  [1, [(0,4), (4,6), (2,5)], None],
                  [2, [(3,3), (1,10)      ], None],
                  [3, [(2,3), (4,4), (5,5)], "work"],
                  [4, [(3,4), (1,6), (0,2)], None],
                  [5, [(3,5), (0,8)       ], "shopping"] ]

jfile = "shortest_path.json"

# --------------------------------------------------------------------
# ---- main
# --------------------------------------------------------------------

# ---- create a graph

g = sp.Graph(compact_costs)

# ---- calculate minimum paths and accumulated costs to
# ---- all vertices (destination nodes)
  
minimum_paths     = []              # paths matrix 
accumulated_costs = []              # acc costs matrix
    
for row in range(len(compact_costs)):
    min_path,acc_cost = g.shortestPath(row)
    minimum_paths.append(min_path)
    accumulated_costs.append(acc_cost)

sp.output_json_file(minimum_paths,jfile)

print()
print(f'file {jfile} created')
print()