1

I would like to know how to implement floyd on an undirected graph. With this implementation,

for k in graph:
    for i in graph:
        for j in graph:
            if dist[i][k] + dist[k][j] < dist[i][j]:
                dist[i][j] = dist[i][k] + dist[k][j]
                pred[i][j] = pred[k][j]

I am able to get the adjacency matrix of:

   __0 __1 __2 __3 __4 

0|  0  28  38  33  63 

1|  inf  0  10  60  44 

2|  inf  inf  0  50  80 

3|  inf  inf  inf  0  30 

4|  inf  inf  inf  inf  0 

Which is a directed graph. That's fine, but I would like the adjacency matrix to display undirected graphs. From what I understand, for an undirected graph, these paths should be mirrored along the 0 diagonal, but I am not sure how to do this.

I tried:

for k in graph:
    for i in graph:
        for j in graph:
            if dist[i][k] + dist[k][j] < dist[i][j]:
                dist[i][j] = dist[i][k] + dist[k][j]
                dist[j][i] = dist[i][k] + dist[k][j]
                pred[i][j] = pred[k][j]

But my graph turns out wrong and it looks like the algorithm is going through unneeded edges to get to the desired vertex.

   __0 __1 __2 __3 __4 

0|  0  48  38  93  63 

1|  48  0  110  60  44 

2|  38  110  0  110  80 

3|  93  60  110  0  30 

4|  63  inf  80  inf  0 

Here is the graph that I am using:

{0 : {1:28, 3:33},
1 : {2:10, 4:44},
2 : {3:50},
3 : {4:30},
4 : {}}

EDIT: Here is the full code

import ast

def floyd(graph):
    dist = {}
    pred = {} 

    for u in graph:
        dist[u] = {}
        pred[u] = {}

        for v in graph:
            dist[u][v] = float('INF')
            pred[u][v] = -1
            dist[u][u] = 0
        for z in graph[u]:
            dist[u][z] = graph[u][z]
            pred[u][z] = u

    for k in graph:
        for i in graph:
            for j in graph:
                if dist[i][k] + dist[k][j] < dist[i][j]:
                    dist[i][j] = dist[i][k] + dist[k][j]

    return dist, pred

graph = {}
graph = ast.literal_eval(open("file2.txt").read())
print graph

dist, pred = floyd(graph)

print "  ",
for j in dist: print "__" + str(j),
print "\n"
for i in dist:
        print str(i) + "|",
        for v in dist: print " %s" % (dist[i][v]),
        print "\n"
moomin
  • 49
  • 6
  • The undirected graph version is **identical** to the directed graph version. If your original, unmodified algorithm doesn't work on directed graphs, it's probably because you set the adjacency matrix up wrong. – user2357112 Mar 02 '16 at 00:05
  • Can You show us Your full code and example input on which it is not working? – Tony Babarino Mar 02 '16 at 00:15
  • @user2357112 It does work on undirected graphs, and what I am essentially doing is printing out a python dictionary, just with formatting. Maybe that's it, but I still cannot see what is going on. I'll edit the question. – moomin Mar 02 '16 at 00:17
  • For your undirected graph, do you add all the edges to your dictionary going the other way, like adding `0: 33` to node 3's edges? – Marius Mar 02 '16 at 00:22
  • @Tony Babarino I've edited the question and included the code. – moomin Mar 02 '16 at 00:24
  • @Marius No, just one way, but I would like floyd to interpret it as an undirected graph. – moomin Mar 02 '16 at 00:27
  • 1
    @moomin thats cool but we still can't run it because we don't know what is in `file2.txt`. Please read http://stackoverflow.com/help/mcve. Also, I'm still not sure what is the main problem - what You wan't to achieve and where Your code is failing. The question is too chaotic. – Tony Babarino Mar 02 '16 at 00:30
  • @TonyBabarino I already included the file contents in the original question. It is the dictionary input above. I'm not sure where the code is failing, I had originally thought that floyd's needed to be modified to work on undirected graphs, but apparently something is wrong with how I set up my adjacency matrix? The printing statement should be fine, I am just printing a formatted version of the dictionary's contents, so I can only pinpoint things down to the initialisation of the dictionaries within floyd(), not the actual floyd algorithm itself. – moomin Mar 02 '16 at 00:35

1 Answers1

1

I think your issue can be fixed by just mirroring all the edges in your graph dictionary, like:

graph = {0 : {1:28, 3:33},
1 : {2:10, 4:44},
2 : {3:50},
3 : {4:30},
4 : {}}
# Mirror all edges
for u in graph:
    for v in graph[u]:
        if not u in graph[v]:
            graph[v][u] = graph[u][v]

That's the easiest way of setting up your input as an undirected graph (although obviously now you need to be more careful if you then edit/delete edges). When I insert this into your code, I get:

 __0__1__2__3__4

0| 0 28 38 33 63

1| 28 0 10 60 44

2| 38 10 0 50 54

3| 33 60 50 0 30

4| 63 44 54 30 0
Marius
  • 58,213
  • 16
  • 107
  • 105
  • This is a good quickfix for the current scope. I thought the problem was with the floyd's implementation, but apparently it's alright. Anyway, thank you. – moomin Mar 02 '16 at 00:48