2

To make a Distance Matrix for the Floyd Warshall algorithm "Shortest path" (https://www.cs.usfca.edu/~galles/visualization/Floyd.html?hc_location=ufi), you need some roads as vertices and distances between these roads as edges. For example (departure, destination, distance): roads = [["Philadelphia", "New York City", 120 ], ["New York City", "Philadelphia", 97 ],[ "Millburn, "New York City", 25 ],["Morristown", "Harrisburg", 150] How can I make this matrix in python?

This is the structure:

network[0] = #list destinations
for i in range (len(roads)):
      network [i][0] = #list departures

I don't know how to fill the distances in the correct position, because network[roads[i][0],[roads[i][1]] isn't the right solution when a destination or departure is used more times than one.

Thanks a lot!

Materials
  • 149
  • 1
  • 3
  • 12

2 Answers2

1

If you have N cities you will need matrix of dimensions N x N.

First you have to map cities to numbers.

'Millburn' : 0
'Morristown': 1
...

Count number of cities - N and make empty matrix of dimensions N x N. Now set each entry of matrix (i, j) to distance between cities i and j. If there exists no direct connection set value to infinity.

Once you have that matrix just run the FLoyd Warshall algorithm on it.

Neithrik
  • 2,002
  • 2
  • 20
  • 33
0

If you're looking for fast lookups, you could consider dict instead of list of lists (with a tradeoff of extra space)

distance = {'Millburn': {'New York': 25}),
 'Morristown':  {'Harrisburg': 150}),
 'New York City': {'Philadelphia': 97}),
 'Philadelphia':  {'New York City': 120})}

(take care of inserting the reverse edges and the values as well)

you can use defaultdict to create a dictionary like the one above from the array you've given.

from collections import defaultdict
roads = [["Philadelphia", "New York City", 120 ], ["New York City", "Philadelphia", 97 ],[ "Millburn", "New York", 25 ],["Morristown", "Harrisburg", 150]]
d = defaultdict(lambda : defaultdict(lambda:0))
for source,dest,distance in roads:
    d[source][dest] = distance

if you are using pandas, you can convert the above dictionary to a pandas dataframe.

import pandas
dist = pandas.DataFrame.from_dict(distance)
print dist
#               Millburn  Morristown  New York City  Philadelphia
#Harrisburg          NaN         150            NaN           NaN
#New York             25         NaN            NaN           NaN
#New York City       NaN         NaN            NaN           120
#Philadelphia        NaN         NaN             97           NaN
dist['Philadelphia']['New York City']
# 120.0
srj
  • 9,591
  • 2
  • 23
  • 27
  • I'm afraid I can't use pandas... That's why I thought working with dictionaries was more complicated, because these are unordered and you can't select elements by index. And this is important, because I can't use for example "distance[Millburn]", I have to construct a general matrix, which works with every given tuple of roads. – Materials Apr 17 '15 at 17:04
  • @Materials, I've edited the answer to show how i created the dictionary from the array that you have provided. so constructing a general purpose matrix is not an issue. do elaborate if you need to use an integer index instead of string index; as that would be trivial to make. – srj Apr 19 '15 at 11:19
  • @Materials, if you could edit the question, and also provide information on how you'd want to use the datastructure, then someone can help you with the creation of that datastructure – srj Apr 19 '15 at 13:13