network x graph and floyd warshall












0















I am a newbie in Python. I have a map like this map, and I want to create the shortest paths from each node to every other nodes using network x. I've tried to write a simple code like this:



shp = nx.read_shp("../Shapefiles/Shapefiles/Station_in_Corridors/Group_1.shp")

G = nx.DiGraph()
for data in shp.edges(data = True):
G.add_edge(data[0],data[1],weight = data[2]["Length_Km"])

nx.floyd_warshall(G)
pos = nx.spring_layout(G)
nx.draw_networkx_nodes(G, pos = pos, node_size=100)
nx.draw_networkx_edges(G, pos = pos)
plt.show()


Prior to calling the result of floyd warshall, I'd like to see the graph first. Turns out the graph return like this:result. I don't think that graph is similar to the input (or is it?).



Anyhow, I've also tried to manually append the points with this code:



cor1 = driver.Open(cor1Data)
cor2 = driver.Open(cor2Data)

ly1 = cor1.GetLayer()
ly2 = cor2.GetLayer()

allpoints = {}
kreuz =
arcs = {}
for i in range(ly1.GetFeatureCount()):
for j in range(ly2.GetFeatureCount()): #Create road
feat1 = ly1.GetFeature(i)
geom1 = feat1.GetGeometryRef()
points1 = geom1.GetPoints()
feat2 = ly2.GetFeature(j)
geom2 = feat2.GetGeometryRef()
points2 = geom2.GetPoints()
arcs[i] = [(points1[0],points1[1],geom1.Length()),feat1]
arcs[len(ly1)+j] = [(points2[0],points2[1],geom2.Length()),feat2]
#Create OD trips
if not points1[0] in allpoints.values():
allpoints[i] = [points1[0],geom1.Length(),feat1]
else:
allpoints[i] = [points1[1],geom1.Length(),feat1]
if not points2[0] in allpoints.values():
allpoints[len(ly1)+j] = [points2[0],geom1.Length(),feat1]
else:
allpoints[len(ly1)+j] = [points2[1],geom1.Length(),feat1]
#append kreuz
if points1[0] == points2[0] or points1[0] == points2[1]:
kreuz.append(points1[0])
elif points1[1] == points2[0] or points1[1] == points2[1]:
kreuz.append(points1[1])

G = nx.DiGraph() #Set a directed graph
for k,v in arcs.items():
G.add_edge(v[0][0],v[0][1], weight = v[0][2])

G.add_nodes_from(allpoints.values())

nx.floyd_warshall(G)
pos = nx.spring_layout(G)
nx.draw_networkx_nodes(G, pos = pos, node_size=100)
nx.draw_networkx_edges(G, pos = pos)
plt.show()


and the result:
Result of second code



Is it a normal graph? And can anybody give some insights on how to calculate the shortest path right?










share|improve this question























  • You are not assigning the output of nx.floyd_warshall(G) to anything. What do you expect that line to do at the moment? Amend G in-place?

    – Paul Brodersen
    Nov 23 '18 at 14:33













  • Yes, I just want to see the G first before doing further with the floyd warshall algorithm. Does it have any effect to the G or should I assign it to the draw function?

    – botibo
    Nov 23 '18 at 23:03
















0















I am a newbie in Python. I have a map like this map, and I want to create the shortest paths from each node to every other nodes using network x. I've tried to write a simple code like this:



shp = nx.read_shp("../Shapefiles/Shapefiles/Station_in_Corridors/Group_1.shp")

G = nx.DiGraph()
for data in shp.edges(data = True):
G.add_edge(data[0],data[1],weight = data[2]["Length_Km"])

nx.floyd_warshall(G)
pos = nx.spring_layout(G)
nx.draw_networkx_nodes(G, pos = pos, node_size=100)
nx.draw_networkx_edges(G, pos = pos)
plt.show()


Prior to calling the result of floyd warshall, I'd like to see the graph first. Turns out the graph return like this:result. I don't think that graph is similar to the input (or is it?).



Anyhow, I've also tried to manually append the points with this code:



cor1 = driver.Open(cor1Data)
cor2 = driver.Open(cor2Data)

ly1 = cor1.GetLayer()
ly2 = cor2.GetLayer()

allpoints = {}
kreuz =
arcs = {}
for i in range(ly1.GetFeatureCount()):
for j in range(ly2.GetFeatureCount()): #Create road
feat1 = ly1.GetFeature(i)
geom1 = feat1.GetGeometryRef()
points1 = geom1.GetPoints()
feat2 = ly2.GetFeature(j)
geom2 = feat2.GetGeometryRef()
points2 = geom2.GetPoints()
arcs[i] = [(points1[0],points1[1],geom1.Length()),feat1]
arcs[len(ly1)+j] = [(points2[0],points2[1],geom2.Length()),feat2]
#Create OD trips
if not points1[0] in allpoints.values():
allpoints[i] = [points1[0],geom1.Length(),feat1]
else:
allpoints[i] = [points1[1],geom1.Length(),feat1]
if not points2[0] in allpoints.values():
allpoints[len(ly1)+j] = [points2[0],geom1.Length(),feat1]
else:
allpoints[len(ly1)+j] = [points2[1],geom1.Length(),feat1]
#append kreuz
if points1[0] == points2[0] or points1[0] == points2[1]:
kreuz.append(points1[0])
elif points1[1] == points2[0] or points1[1] == points2[1]:
kreuz.append(points1[1])

G = nx.DiGraph() #Set a directed graph
for k,v in arcs.items():
G.add_edge(v[0][0],v[0][1], weight = v[0][2])

G.add_nodes_from(allpoints.values())

nx.floyd_warshall(G)
pos = nx.spring_layout(G)
nx.draw_networkx_nodes(G, pos = pos, node_size=100)
nx.draw_networkx_edges(G, pos = pos)
plt.show()


and the result:
Result of second code



Is it a normal graph? And can anybody give some insights on how to calculate the shortest path right?










share|improve this question























  • You are not assigning the output of nx.floyd_warshall(G) to anything. What do you expect that line to do at the moment? Amend G in-place?

    – Paul Brodersen
    Nov 23 '18 at 14:33













  • Yes, I just want to see the G first before doing further with the floyd warshall algorithm. Does it have any effect to the G or should I assign it to the draw function?

    – botibo
    Nov 23 '18 at 23:03














0












0








0








I am a newbie in Python. I have a map like this map, and I want to create the shortest paths from each node to every other nodes using network x. I've tried to write a simple code like this:



shp = nx.read_shp("../Shapefiles/Shapefiles/Station_in_Corridors/Group_1.shp")

G = nx.DiGraph()
for data in shp.edges(data = True):
G.add_edge(data[0],data[1],weight = data[2]["Length_Km"])

nx.floyd_warshall(G)
pos = nx.spring_layout(G)
nx.draw_networkx_nodes(G, pos = pos, node_size=100)
nx.draw_networkx_edges(G, pos = pos)
plt.show()


Prior to calling the result of floyd warshall, I'd like to see the graph first. Turns out the graph return like this:result. I don't think that graph is similar to the input (or is it?).



Anyhow, I've also tried to manually append the points with this code:



cor1 = driver.Open(cor1Data)
cor2 = driver.Open(cor2Data)

ly1 = cor1.GetLayer()
ly2 = cor2.GetLayer()

allpoints = {}
kreuz =
arcs = {}
for i in range(ly1.GetFeatureCount()):
for j in range(ly2.GetFeatureCount()): #Create road
feat1 = ly1.GetFeature(i)
geom1 = feat1.GetGeometryRef()
points1 = geom1.GetPoints()
feat2 = ly2.GetFeature(j)
geom2 = feat2.GetGeometryRef()
points2 = geom2.GetPoints()
arcs[i] = [(points1[0],points1[1],geom1.Length()),feat1]
arcs[len(ly1)+j] = [(points2[0],points2[1],geom2.Length()),feat2]
#Create OD trips
if not points1[0] in allpoints.values():
allpoints[i] = [points1[0],geom1.Length(),feat1]
else:
allpoints[i] = [points1[1],geom1.Length(),feat1]
if not points2[0] in allpoints.values():
allpoints[len(ly1)+j] = [points2[0],geom1.Length(),feat1]
else:
allpoints[len(ly1)+j] = [points2[1],geom1.Length(),feat1]
#append kreuz
if points1[0] == points2[0] or points1[0] == points2[1]:
kreuz.append(points1[0])
elif points1[1] == points2[0] or points1[1] == points2[1]:
kreuz.append(points1[1])

G = nx.DiGraph() #Set a directed graph
for k,v in arcs.items():
G.add_edge(v[0][0],v[0][1], weight = v[0][2])

G.add_nodes_from(allpoints.values())

nx.floyd_warshall(G)
pos = nx.spring_layout(G)
nx.draw_networkx_nodes(G, pos = pos, node_size=100)
nx.draw_networkx_edges(G, pos = pos)
plt.show()


and the result:
Result of second code



Is it a normal graph? And can anybody give some insights on how to calculate the shortest path right?










share|improve this question














I am a newbie in Python. I have a map like this map, and I want to create the shortest paths from each node to every other nodes using network x. I've tried to write a simple code like this:



shp = nx.read_shp("../Shapefiles/Shapefiles/Station_in_Corridors/Group_1.shp")

G = nx.DiGraph()
for data in shp.edges(data = True):
G.add_edge(data[0],data[1],weight = data[2]["Length_Km"])

nx.floyd_warshall(G)
pos = nx.spring_layout(G)
nx.draw_networkx_nodes(G, pos = pos, node_size=100)
nx.draw_networkx_edges(G, pos = pos)
plt.show()


Prior to calling the result of floyd warshall, I'd like to see the graph first. Turns out the graph return like this:result. I don't think that graph is similar to the input (or is it?).



Anyhow, I've also tried to manually append the points with this code:



cor1 = driver.Open(cor1Data)
cor2 = driver.Open(cor2Data)

ly1 = cor1.GetLayer()
ly2 = cor2.GetLayer()

allpoints = {}
kreuz =
arcs = {}
for i in range(ly1.GetFeatureCount()):
for j in range(ly2.GetFeatureCount()): #Create road
feat1 = ly1.GetFeature(i)
geom1 = feat1.GetGeometryRef()
points1 = geom1.GetPoints()
feat2 = ly2.GetFeature(j)
geom2 = feat2.GetGeometryRef()
points2 = geom2.GetPoints()
arcs[i] = [(points1[0],points1[1],geom1.Length()),feat1]
arcs[len(ly1)+j] = [(points2[0],points2[1],geom2.Length()),feat2]
#Create OD trips
if not points1[0] in allpoints.values():
allpoints[i] = [points1[0],geom1.Length(),feat1]
else:
allpoints[i] = [points1[1],geom1.Length(),feat1]
if not points2[0] in allpoints.values():
allpoints[len(ly1)+j] = [points2[0],geom1.Length(),feat1]
else:
allpoints[len(ly1)+j] = [points2[1],geom1.Length(),feat1]
#append kreuz
if points1[0] == points2[0] or points1[0] == points2[1]:
kreuz.append(points1[0])
elif points1[1] == points2[0] or points1[1] == points2[1]:
kreuz.append(points1[1])

G = nx.DiGraph() #Set a directed graph
for k,v in arcs.items():
G.add_edge(v[0][0],v[0][1], weight = v[0][2])

G.add_nodes_from(allpoints.values())

nx.floyd_warshall(G)
pos = nx.spring_layout(G)
nx.draw_networkx_nodes(G, pos = pos, node_size=100)
nx.draw_networkx_edges(G, pos = pos)
plt.show()


and the result:
Result of second code



Is it a normal graph? And can anybody give some insights on how to calculate the shortest path right?







python graph networkx shortest-path floyd-warshall






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 14:26









botibobotibo

32




32













  • You are not assigning the output of nx.floyd_warshall(G) to anything. What do you expect that line to do at the moment? Amend G in-place?

    – Paul Brodersen
    Nov 23 '18 at 14:33













  • Yes, I just want to see the G first before doing further with the floyd warshall algorithm. Does it have any effect to the G or should I assign it to the draw function?

    – botibo
    Nov 23 '18 at 23:03



















  • You are not assigning the output of nx.floyd_warshall(G) to anything. What do you expect that line to do at the moment? Amend G in-place?

    – Paul Brodersen
    Nov 23 '18 at 14:33













  • Yes, I just want to see the G first before doing further with the floyd warshall algorithm. Does it have any effect to the G or should I assign it to the draw function?

    – botibo
    Nov 23 '18 at 23:03

















You are not assigning the output of nx.floyd_warshall(G) to anything. What do you expect that line to do at the moment? Amend G in-place?

– Paul Brodersen
Nov 23 '18 at 14:33







You are not assigning the output of nx.floyd_warshall(G) to anything. What do you expect that line to do at the moment? Amend G in-place?

– Paul Brodersen
Nov 23 '18 at 14:33















Yes, I just want to see the G first before doing further with the floyd warshall algorithm. Does it have any effect to the G or should I assign it to the draw function?

– botibo
Nov 23 '18 at 23:03





Yes, I just want to see the G first before doing further with the floyd warshall algorithm. Does it have any effect to the G or should I assign it to the draw function?

– botibo
Nov 23 '18 at 23:03












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53448478%2fnetwork-x-graph-and-floyd-warshall%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53448478%2fnetwork-x-graph-and-floyd-warshall%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Ottavio Pratesi

Tricia Helfer

15 giugno