shortest

Networkx - How to get shortest path length between nodes showing node id instead of label

时光毁灭记忆、已成空白 提交于 2020-01-15 04:11:28
问题 I'm new to using NetworkX library with Python. Let's say that I import a Pajek-formatted file: import networkx as nx G=nx.read_pajek("pajek_network_file.net") G=nx.Graph(G) The contents of my file are (In Pajek, nodes are called "Vertices"): *Network *Vertices 6 123 Author1 456 Author2 789 Author3 111 Author4 222 Author5 333 Author6 *Edges 123 333 333 789 789 222 222 111 111 456 Now, I want to calculate all the shortest path lengths between the nodes in my network, and I'm using this function

Networkx - How to get shortest path length between nodes showing node id instead of label

与世无争的帅哥 提交于 2020-01-15 04:11:26
问题 I'm new to using NetworkX library with Python. Let's say that I import a Pajek-formatted file: import networkx as nx G=nx.read_pajek("pajek_network_file.net") G=nx.Graph(G) The contents of my file are (In Pajek, nodes are called "Vertices"): *Network *Vertices 6 123 Author1 456 Author2 789 Author3 111 Author4 222 Author5 333 Author6 *Edges 123 333 333 789 789 222 222 111 111 456 Now, I want to calculate all the shortest path lengths between the nodes in my network, and I'm using this function

Finding the shortest string in a string of words in python

你。 提交于 2019-12-31 05:36:10
问题 I want to write a function that would return the length of the shortest string in a string of words. Sample: "I eat apples" would return 1 since "I" is the shorted string. This is what I did but mincount does neither increment nor set up to 0 at the end of each string. What is wrong with it? def find_short(s): min=99 for i in s: mincount = 0 for j in i: print(j) mincount += 1 print(mincount) if (mincount < min): min = mincount return min 回答1: line = "I eat apples" mins = min(len(word) for

Find the Shortest Cycle in Graph

断了今生、忘了曾经 提交于 2019-12-25 19:30:29
问题 I have a problem with finding cycles in graph. In the condition we have to find the shortest cycle in directed graph. My graph is (A,B,C,D) and the connections (arcs) between the elements are: (A->B), (A->A), (B->C), (B->A), (C->D), (C->A), (D->A) and so cycles are the following: А->B->C->D->A; A->B->C->A; A->B->A; A->A. Program should print the shortest cycle, ie A->A. To solve it i need first to find all cycles, then put them each in a separate list and finally bring the smallest list,

Find the minimal common path from any nodes to one node

只愿长相守 提交于 2019-12-24 04:15:06
问题 My problem is the following. I have a " backup " node and others nodes. From theses nodes, I need to generate a common path to the backup node which is minimal (unweighted and undirected graph) I don't need a solution everytime. Just how I can know if I can generate this path or not. I was thinking about splitting the graph into some sub-graphs and searching for minimal " subpath ". But I'm not so good in graph theory. I use Python and C++. Thanks you from advance. (Sorry If there is already

Shortest path in a map

不打扰是莪最后的温柔 提交于 2019-12-21 19:50:43
问题 I have designed a weighted graph using a normalized adjacency list in mysql. Now I need to find the shortest path between two given nodes. I have tried to use Dijkstra in php but I couldnt manage to implement it (too difficult for me). Another problem I felt was that if I use Dijkstra I would need to consider all the nodes, that may be perhaps very inefficient in a large graph. So does anybody has a code relating to the above problem? It would be great if somebody atleast shows me a way of

Finding all paths/walks of given length in a networkx graph

这一生的挚爱 提交于 2019-12-21 17:38:35
问题 I'm using networkx and trying to find all the walks with length 3 in the graph, specifically the paths with three edges. I tried to find some information about the algorithms in the networkx documentation but I could only find the algorithms for the shortest path in the graph. Can I find a length of a path trough specific nodes, for example a path trough nodes 14 -> 11 -> 12 -> 16 if the shortest path is 14 -> 15 -> 16? Here's an image of a graph for an example: 回答1: Simplest version (another

Shortest path on a grid with obstacles [closed]

核能气质少年 提交于 2019-12-13 23:12:56
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 5 years ago . Please help me finding the shortest path between two points on a 2d grid with obstacles. you will be provide starting coordinates and end cordinates. Also you know where the obstacles are. 回答1: I suggest A* Pathfinding. It's a really neat algorithm that should do what you want. 来源

Apply Dijkstra's algorithm in a undirected graph with negative weights

冷暖自知 提交于 2019-12-13 09:15:36
问题 Can anyone apply Dijkstra's algorithm in the undirected graph with negative weights above? Even if the algorithm fails. Adjancency's list: A -> (B, 3), (C, 2), (D, 4) B -> (A, 3), (C, -2), (F, 6) C -> (A, 2), (B, -2), (E, 5) D -> (A, 4), (E, 3), (F, 2) E -> (C, 5), (D, 3), (F, -2) F -> (B, 6), (D, 2), (E, -2) 回答1: Seed the traversal list with source node A, and it's cost with 0. Add an infinite cost for every other node: {}, [A=0, B=inf, C=inf, D=inf, E=inf, F=inf] Then take the lowest

Finding all paths/walks of given length in a networkx graph

泄露秘密 提交于 2019-12-04 09:53:25
I'm using networkx and trying to find all the walks with length 3 in the graph, specifically the paths with three edges. I tried to find some information about the algorithms in the networkx documentation but I could only find the algorithms for the shortest path in the graph. Can I find a length of a path trough specific nodes, for example a path trough nodes 14 -> 11 -> 12 -> 16 if the shortest path is 14 -> 15 -> 16? Here's an image of a graph for an example: Simplest version (another version is below which I think is faster): def findPaths(G,u,n): if n==0: return [[u]] paths = [[u]+path