graph-theory

Combine (join) networkx Graphs

主宰稳场 提交于 2020-06-09 08:01:45
问题 Say I have two networkx graphs, G and H : G=nx.Graph() fromnodes=[0,1,1,1,1,1,2] tonodes=[1,2,3,4,5,6,7] for x,y in zip(fromnodes,tonodes): G.add_edge(x,y) H=nx.Graph() fromnodes=range(2,8) tonodes=range(8,14) for x,y in zip(fromnodes,tonodes): H.add_edge(x,y) What is the best way to join the two networkx graphs? I'd like to preserve the node names (note the common nodes, 2 to 7). When I used nx.disjoint_union(G,H) , this did not happen: >>> G.nodes() [0, 1, 2, 3, 4, 5, 6, 7] >>> H.nodes() [2

Cut in a Weighted Undirected Connected Graph

随声附和 提交于 2020-06-01 07:07:28
问题 Background: I am a newbie to graph theory, specially in "graph cut". Please don't be too technical and fast. Thank you. Suppose I have a weighted undirected connected graph G=(V,E). I have a variable A that holds an integer value and I want to remove/cut all edges from the graph G whose weight are below the value of A. Question 1: If this already exist in graph theory (I saw max-cut, min-cut, s-t cut, etc) how is it called? Question 2: How can I formally express/define this approach using

Cut in a Weighted Undirected Connected Graph

筅森魡賤 提交于 2020-06-01 07:05:44
问题 Background: I am a newbie to graph theory, specially in "graph cut". Please don't be too technical and fast. Thank you. Suppose I have a weighted undirected connected graph G=(V,E). I have a variable A that holds an integer value and I want to remove/cut all edges from the graph G whose weight are below the value of A. Question 1: If this already exist in graph theory (I saw max-cut, min-cut, s-t cut, etc) how is it called? Question 2: How can I formally express/define this approach using

How to find the shortest path between two coordinates in a 2-dimensional array?

安稳与你 提交于 2020-05-30 04:18:09
问题 I am trying to find the shortest way to get from one point in a 2D array (one coordinate with x and y values representing its position in the array) to another. I would like to output an array of coordinates which must be travelled through to get from the initial to the final coordinates. One example of an array like this could be arr = [ [15, 7, 3], [1, 2, 6], [7, 4, 67] ] In this case, we can say that we will begin at arr[0][0] and end at arr[2][2] . Thus, the coordinates would be (0, 0)

How to convert an undirected graph to a DAG?

久未见 提交于 2020-05-25 06:39:11
问题 The Wiki page says Any undirected graph may be made into a DAG by choosing a total order for its vertices and orienting every edge from the earlier endpoint in the order to the later endpoint. But I don't know how to get the total order of an undirected graph. Should I use DFS? If so, how would I proceed? More info : I'm working on an un-directed graph which has one source and one sink. I'm trying to direct these edges so that by following the edge direction I can get from the source to the

Boost Graph Library, Erdos Renyi Generator. Graphs always have same number of edges

岁酱吖の 提交于 2020-05-16 02:42:07
问题 I'm trying to generate Erdos-Renyi graphs using boost graph library. In the code below, which is taken from The Boost 1.72 documentation the networks always have the same number of edges (they should not, for particular p values). I have tried using different random seeds to no avail. Thanks for any help. #include <boost/graph/adjacency_list.hpp> #include <boost/graph/erdos_renyi_generator.hpp> #include <boost/random/linear_congruential.hpp> #include <iostream> using namespace std; typedef

Boost Graph Library, Erdos Renyi Generator. Graphs always have same number of edges

≡放荡痞女 提交于 2020-05-16 02:42:07
问题 I'm trying to generate Erdos-Renyi graphs using boost graph library. In the code below, which is taken from The Boost 1.72 documentation the networks always have the same number of edges (they should not, for particular p values). I have tried using different random seeds to no avail. Thanks for any help. #include <boost/graph/adjacency_list.hpp> #include <boost/graph/erdos_renyi_generator.hpp> #include <boost/random/linear_congruential.hpp> #include <iostream> using namespace std; typedef

Get all edges linked to a given node in a networkx graph

Deadly 提交于 2020-05-11 03:48:26
问题 Just wondering if there is convenient networkx function that returns a list of edges connected to a given node (or nodes) (e.g. my_node_name ) in a graph (e.g. G ). I can do it this way: edlist=[] for ed in G.edges(): if 'my_node_name' in ed: edlist.append(ed) but expect there might be a better way? 回答1: If the graph is undirected, you can use G.edges(node) In networkx 2.x this is an EdgeDataView object. In networkx 1.x this is a list - if you want a generator in 1.x rather than getting the

Get all edges linked to a given node in a networkx graph

陌路散爱 提交于 2020-05-11 03:46:07
问题 Just wondering if there is convenient networkx function that returns a list of edges connected to a given node (or nodes) (e.g. my_node_name ) in a graph (e.g. G ). I can do it this way: edlist=[] for ed in G.edges(): if 'my_node_name' in ed: edlist.append(ed) but expect there might be a better way? 回答1: If the graph is undirected, you can use G.edges(node) In networkx 2.x this is an EdgeDataView object. In networkx 1.x this is a list - if you want a generator in 1.x rather than getting the

Finding cycles: DFS versus union-find?

南楼画角 提交于 2020-04-13 06:07:40
问题 DFS with coloring would take O(V+E) vs union find would take O(ElogV) reference: http://www.geeksforgeeks.org/detect-cycle-undirected-graph/ So union find approach is slower. If V = 100, E = 100, DFS = 200, Union find is 1,000. Is there a reason to use Union find? I personally like it because it produces a clean code. Or anything I missed that union find is better in real practice? 回答1: I suspect that you may be misinterpreting how big-O notation works. The notation O(V + E) doesn't mean "the