CS209 lab3


http://www.maths.nuigalway.ie/~gettrick/teach/cs209/l3.html




http://www.maths.nuigalway.ie/~gettrick/teach/cs209/progs/
Consider the Floyd "all pairs shortest path" algorithm described in lectures. This finds the shortest route on a labelled graph (network) between any two vertices (points).

  1. Write the Floyd algorithm in PYTHON, using lists (of lists...) to store the necessary matrices.
  2. Run your program on the data given at http://www.maths.nuigalway.ie/~gettrick/teach/cs209/nums.txt. Hence, state the shortest distance between nodes
    • 3 and 15
    • 4 and 6
    • 17 and 19
    (For this part - we are not worried about how to read in the data - you can read it in from a file, from the user, or just "hard wire" it in to the program.) Note: Instead of the "infinity" talked about in lectures, or the "zero" in the actual data, corresponding to points that are not connected, do the following. Assume the data is, as given, with 0 in positions where i and j are not directly connected. To "mimic" infinity, your program should put in a large positive number in this position. More precisely: that large positive number can be just (sum of all the non-zero entries of the matrix)+1, to guarantee it will be bigger than the other entries. Do this calculation and replacement in PYTHON. (Example - were the matrix [[0,0,2],[0,0,5],[2,5,0]] we would replace the zeros by 15 = 2+5+2+5+1).
  3. Adapt your program as follows:
    1. For each matrix - do not store n x n numbers: Since the matrix is always symmetric, store just the diagonal and upper triangular part. [Hint - if the data type is a list (L) of lists, then element j of L can be just a list of (n-j) numbers].
    2. For the recursion Dijk = min( Dijk-1, Dikk-1 + Dkjk-1), make sure that in the cases where k=i or k=j, you do not calculate min( Dijk-1, Dikk-1 + Dkjk-1). In these cases, just assign Dijk = Dijk-1.
    Submit your new program.

© NUI, Galway