weighted-graph

A BFS Algorithm for Weighted Graphs - To Find Shortest Distance

社会主义新天地 提交于 2021-02-08 04:56:07
问题 I've seen quite a few posts (viz. post1, post2, post3) on this topic but none of the posts provides an algorithm to back up respective queries. Consequently I'm not sure to accept the answers to those posts. Here I present a BFS based shortest-path (single source) algorithm that works for non-negative weighted graph. Can anyone help me understand why BFS (in light of below BFS based algorithm) are not used for such problems (involving weighted graph)! The Algorithm: SingleSourceShortestPath

A BFS Algorithm for Weighted Graphs - To Find Shortest Distance

别说谁变了你拦得住时间么 提交于 2021-02-08 04:55:28
问题 I've seen quite a few posts (viz. post1, post2, post3) on this topic but none of the posts provides an algorithm to back up respective queries. Consequently I'm not sure to accept the answers to those posts. Here I present a BFS based shortest-path (single source) algorithm that works for non-negative weighted graph. Can anyone help me understand why BFS (in light of below BFS based algorithm) are not used for such problems (involving weighted graph)! The Algorithm: SingleSourceShortestPath

How to create random graph where each node has at least 1 edge using Networkx

好久不见. 提交于 2020-06-28 03:58:11
问题 I've managed to create a random undirected weighted graph for testing with Dijkstra's algorithm, but how can I make it so each node has at least one edge that connects them to the graph? I'm using Networkx and my graph generator is as follows: import networkx as nx import random random.seed() nodes = random.randint(5,10) seed = random.randint(1,10) probability = random.random() G = nx.gnp_random_graph(nodes,probability,seed, False) for (u, v) in G.edges(): G.edges[u,v]['weight'] = random

R igraph convert parallel edges to weight attribute

天大地大妈咪最大 提交于 2019-12-18 19:08:44
问题 I'm working with igraph for R. My graph is based on an edgelist which includes parallel edges (more than one edge with the same source and target). I would like to convert these parallel edges to an edge attribute weight. Is there an eay way to do this? If there is no easy way. how can I identify these parallel edges? duplicated(E(net)) does not return a single duplicate. I suppose its looking for duplicated edge ids. 回答1: You can also use E(graph)$weight <- 1 followed by simplify(graph, edge

Plot hist2d with weights

梦想与她 提交于 2019-12-11 04:22:58
问题 I need to plot a hist2d with contour curves and colorbar from a pandas dataframe. The dataframe has three cols: x_col, y_col, z_col I want to plot something like this where z_col are the weights of the hist2d : But I don't know how to transform the z_col into a weight 1D array from the hist2d function. fdf = df.groupby([valueX, valueY], as_index=False).mean().sort([valueX, valueY]) x = fdf[valueX] y = fdf[valueY] z = fdf[valueZ] (... axes instantiation) bins = 100 counts, xbins, ybins, image

R iGraph: How to get weighted adjacency matrix from a graph?

我们两清 提交于 2019-12-04 05:35:51
问题 While there are some questions dealing with creating a graph from an adjacency matrix, I haven't found much about extracting the weighted adjacency matrix from a weighted graph. Say I have the following graph: library(igraph) nodes <- data.frame(name=c("a","b", "c", "d", "f", "g")) col1 <- c("a", "g", "f","f", "d","c") col2 <- c("b", "f","c","d","a","a") weight <- c(1,4,2,6,2,3) edges <- cbind.data.frame(col1,col2,weight) g <- graph.data.frame(edges, directed=F, vertices=nodes) E(g)$weight <-

Assign edge weights to a networkx graph using pandas dataframe

纵饮孤独 提交于 2019-12-02 00:31:02
问题 I am contructing a networkx graph in python 3. I am using a pandas dataframe to supply the edges and nodes to the graph. Here is what I have done : test = pd.read_csv("/home/Desktop/test_call1", delimiter = ';') g_test = nx.from_pandas_edgelist(test, 'number', 'contactNumber', edge_attr='callDuration') What I want is that the "callDuration" column of the pandas dataframe act as the weight of the edges for the networkx graph and the thickness of the edges also change accordingly. I also want

R igraph convert parallel edges to weight attribute

≯℡__Kan透↙ 提交于 2019-11-30 17:57:38
I'm working with igraph for R. My graph is based on an edgelist which includes parallel edges (more than one edge with the same source and target). I would like to convert these parallel edges to an edge attribute weight. Is there an eay way to do this? If there is no easy way. how can I identify these parallel edges? duplicated(E(net)) does not return a single duplicate. I suppose its looking for duplicated edge ids. You can also use E(graph)$weight <- 1 followed by simplify(graph, edge.attr.comb=list(weight="sum")) to assign a weight of 1 to each edge and then collapsing multiple edges into

Create a histogram for weighted values

安稳与你 提交于 2019-11-28 11:58:18
If I have a vector (e.g., v<-runif(1000) ), I can plot its histogram (which will look, more or less, as a horizontal line because v is a sample from the uniform distribution). However, suppose I have a vector and its associated weights (e.g., w<-seq(1,1000) in addition to v<-sort(runif(1000)) ). E.g., this is the result of table() on a much larger data set. How do I plot the new histogram? (it should look more of less like the y=x line in this example). I guess I could reverse the effects of table by using rep ( hist(rep(v,w)) ) but this "solution" seems ugly and resource-heavy (creates an

Create a histogram for weighted values

梦想的初衷 提交于 2019-11-27 06:38:39
问题 If I have a vector (e.g., v<-runif(1000) ), I can plot its histogram (which will look, more or less, as a horizontal line because v is a sample from the uniform distribution). However, suppose I have a vector and its associated weights (e.g., w<-seq(1,1000) in addition to v<-sort(runif(1000)) ). E.g., this is the result of table() on a much larger data set. How do I plot the new histogram? (it should look more of less like the y=x line in this example). I guess I could reverse the effects of