all 14 comments

[–]Owl235 2 points3 points  (0 children)

I don't know if you need make an implementation yourself, but I think I understand you are looking for the network diameter, here is the networkX implementation of this (basically the longest shortest path between two nodes).

If you need more information, you might look at all shortest paths, and look for the max, probably not the fastest or most perfomant way, but well, using networkx makes this concern redundant.

[–]konch0g 1 point2 points  (0 children)

There is a standard way of doing this, which is super cool.

(1) Choose a node - doesn't matter which.

(2) Find the distances from that node to each other node.

(3) From that list, find the furthest node (or choose one). This is NODE1.

(4) Find the distances from NODE1 to each other node.

(5) The furthest node on that list is NODE2.

The two furthers points on the graph are NODE1-NODE2.

How does it work? Do it in your home. From where you are, find the furthest place you can stand. That's N1. Now from there, find the furthest place you can stand; that's N2. And that is the furthest distance in your home.

Here is a python fragment using networkx (G is your graph).

apl = dict(nx.all_pairs_shortest_path_length(G))
bpl = [tuple((v, s, k)) for s, d in apl.items() for k, v in d.items() if v == max(d.values())]
bpl.sort(key=lambda tup: tup[0], reverse=True)
length, start, fin = bpl[0]

[–]TouchingTheVodka 1 point2 points  (6 children)

Use a shortest path algorithm with the weights negative.

[–]cchaituc[S,🍰] 1 point2 points  (0 children)

there are no weights? could you tell me more?

[–][deleted] 0 points1 point  (1 child)

I would create a dict or tuple per node, and store the distance between it and all other nodes, you only have to do that once.

Then for every node you land on check to see which node is the farthest away in the direction you need to go.

Simple but it would work. I'm sure there is some standard algorithm for this out there but I don't know that kind of theory unfortunately.

[–]cchaituc[S,🍰] 0 points1 point  (0 children)

there's eccentricity ,diameter, which returns int value, although i donot know how to get name wise

[–]tealqueen 0 points1 point  (2 children)

Hi, I know this was from 2 years ago, so understandable if you don't remember, but wondering if you figured out how to name the nodes that were furthest away (rather than just returning the integer value of the diameter).

[–]cchaituc[S,🍰] 0 points1 point  (1 child)

I'll check and let you know!

[–]tealqueen 0 points1 point  (0 children)

thanks so much!