How can i create weighted undirected graph from randomly generated data points in 2d ?

13 Ansichten (letzte 30 Tage)
I want to create undirected weighted graph in the form of adjacency matrix by randomly creating 100 data points in 2d plane having their X and y coordinates.

Antworten (1)

Steven Lord
Steven Lord am 21 Jun. 2018
For a given collection of points, there are often many different graphs you can create. For 100 points, that would be a lot of different potential graphs. Let's examine a simpler case with just 4 points.
>> x = [0 1 1 0];
>> y = [0 0 1 1];
>> plot(x, y, 'o');
>> axis([-1 2 -1 2])
Is this your graph?
>> g = graph([0 1 0 1; 1 0 1 0; 0 1 0 1; 1 0 1 0]);
>> figure
>> plot(g, 'XData', x, 'YData', y)
>> axis([-1 2 -1 2])
Or this?
>> g = graph([0 1 0 1; 1 0 1 0; 0 1 0 0; 1 0 0 0]);
>> figure
>> plot(g, 'XData', x, 'YData', y)
>> axis([-1 2 -1 2])
Or this?
>> g = graph(ones(4)-eye(4));
>> figure
>> plot(g, 'XData', x, 'YData', y)
>> axis([-1 2 -1 2])
Or, adding in self loops:
>> g = graph(ones(4));
>> figure
>> plot(g, 'XData', x, 'YData', y)
>> axis([-1 2 -1 2])
A graph is a set of vertices with a set of edges. Your random points are the set of vertices, but without also specifying the set of edges we don't know how they're connected.
If you just want a graph (or digraph) with random connections, use the functions that generate random numbers (like rand, randn, randi, sprand, or sprandsym) to create a matrix and call the graph or digraph function with that random matrix.
Note that while I had the coordinates in separate arrays in the previous examples, if you want them to be more closely associated or tied to your graph you can store them in the table of nodes of the graph object itself.
>> r = rand(4);
>> r(r < 0.5) = 0;
>> r = (r + r.')/2;
>> g = graph(r);
>> g.Nodes.XData = x.';
>> g.Nodes.YData = y.';
>> plot(g, 'XData', g.Nodes.XData, 'YData', g.Nodes.YData, 'EdgeLabel', g.Edges.Weight);
>> axis([-1 2 -1 2])

Kategorien

Mehr zu Graph and Network Algorithms finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by