How to find neighbors within a distance for an unconnected node within a networkx python graph - networkx

I would like to simulate a wireless network with time-varying and mobile behaviour of the nodes. Thus, I need every time that the node wakes up or moves to search for its neighbours within a distance. How can I find the nearby nodes? There exist any functions? Thank you

It's a single function: ego_graph. It lets you specify a distance parameter, called the radius.
import networkx as nx
# Sample data
G = nx.florentine_families_graph()
nx.draw_networkx(G, with_labels=True)
# Desired graph
H = nx.ego_graph(G, node=4, radius=2)
nx.draw_networkx(H, with_labels=True)
The entire Florentine families graph:
And just those within distance 2 of the node 'Acciauoli':
If you're using a distance measure other than simple topological distance (i.e. counting edges), you can provide the distance parameter to the ego_graph function to specify an edge attribute to use for distance.

Related

How to generate a random network graph based on degrees AND network density in NetworkX

There are a number of functions in NetworkX which allow for different types of random graphs to be generated.
Are there any which allow for the specified degree of the nodes as well as the overall network density (or similar metric) to be considered?
There may be other metrics that are possible to specify in the creation of a graph, but for your examples of degree and density, there exists only one combination of node and edge numbers that can meet specified degree an density criteria.
For an undirected graph, the density is calculated as 2*m/(n*(n-1)) where m is the number of edges and n is the number of nodes. The average degree is calculated as 2*m/n.
Using a bit of substitution, we can then say that n = (degree/density) + 1 and m = (n*degree)/2.
With NetworkX, you can use nx.gnm_random_graph() to specify the number of nodes and edges to match those calculated above.
If you use nx.gnp_random_graph(), note that the p parameter is equal to the density of the graph. Density is defined as the number of edges divided by the maximum number of possible edges, so including a probability that a node will attach to any of the other nodes (p) in generating the random graph effectively does the same thing. The resulting number of expected edges and average degree can then be easily calculated using that value and the number of nodes.

What is the alternate to npts function of basemap in cartopy?

I want to emulate the function of ntps which is in basemap.Geod class.
Its function is described as follows:
Given a single initial point and terminus point (specified by
python floats lon1,lat1 and lon2,lat2), returns a list of
longitude/latitude pairs describing npts equally spaced
intermediate points along the geodesic between the initial and
terminus points.
I want to do the same thing in cartopy, aparantly one way is to calculate the distance needed between each pair of successive coordinate and use that distance to compute coordinates from start to end point. Is there any other way to do so?
Based on cartopy's source, https://github.com/SciTools/cartopy/tree/master/lib/cartopy/geodesic
, it is clearly that no equivalent of ntps() function is available from cartopy.
However, it is not difficult to compute locations of points along a geodesic if you know the bearing from one of the end point. Here are steps that you can follow:
Use Geodesic.inverse() with input of (long, lat) of point1 and2, and get (forward_bearing, backward_bearing, geodesic_dist) as the result.
Suppose you want to get (long, lat) of a point (say 1/4 of the whole distance) along that geodesic (in 1), use Geodesic.direct() with long1, lat1, the obtained forward_bearing and geodesic_dist/4.
A better alternative is to use pyproj and forget all the above.

How a clustering algorithm in R can end up with negative silhouette values? AB

We know that clustering methods in R assign observations to the closest medoids. Hence, it is supposed to be the closest cluster each observation can have. So, I wonder how it is possible to have negative values of silhouette , while we are supposedly assign each observation to the closest cluster and the formula in silhouette method cannot get negative?
Behnam.
Two errors:
most clustering algorithms do not use the medoid, only PAM does.
the silhouette does not use the distance to the medoid, but the average distance to all cluster members. If the closest cluster is very wide, the average distance can be larger than the distance to the medoid. Consider a cluster with one point in the center, and all others on a sphere around it.

Query regarding k-means clustering in MATLAB

I have a very large amount of data in the form of matrix.I have already clustered it using k-means clustering in MATLAB R2013a. I want the exact coordinates of the centroid of each cluster formed.. Is it possible using any formula or anything else?
I want to find out the centroid of each cluster so that whenever some new data arrives in matrix, i can compute its distance from each centroid so as to find out the cluster to which new data will belong
My data is heterogeneous in nature.So,its difficult to find out average of data of each cluster.So, i am trying to write some code for printing the centroid location automatically.
In MATLAB, use
[idx,C] = kmeans(..)
instead of
idx = kmeans(..)
As per the documentation:
[idx,C] = kmeans(..) returns the k cluster centroid locations in the k-by-p matrix C.
The centroid is simply evaluated as the average value of all the points' coordinates that are assigned to that cluster.
If you have the assignments {point;cluster} you can easily evaluate the centroid: let's say you have a given cluster with n points assigned to it and these points are a1,a2,...,an. You can evaluate the centroid for such cluster by using:
centroid=(a1+a2+...+an)/n
Obviously you can run this process in a loop, depending on how your data structure (i.e. the assignment point/centroid) is organized.

K means clustring find k farthest points in java

I'm trying to implement k means clustering.
I've a set of points with coordinates (x,y) and i am using Euclidean distance for finding distance. I've computed distance between all points in a matrix
dist[i][j] - distance between points i and j
when i choose a[1][3] farthest from pt 1 as 3.
then when i search farthest from 3 i may get a[3][j] but a[1][j] may be minimum.
[pt j is far from pt3 but near to 1]
so how to choose k farthest points using the distance matrix.
Note that the k-farthest points do not necessarily yield the best result: they clearly aren't the best cluster center estimates.
Plus, since k-means heuristics may get stuck in a local minimum, you will want a randomized algorithm that allows you to restart the process multiple times and get potentiall different results.
You may want to look at k-means++ which is a known good heuristic for k-means initialization.