If i have 10 random (x,y) coordinates in form of Z given below gives the location of nodes.How to give number labelling to these coordinates?And how to construct a (node and link i.e G(v,e) )graph with weights equal to the distance between the nodes?

2 次查看(过去 30 天)
z =
9 0
5 1
10 4
1 3
0 0
7 1
4 4
8 4
0 5
4 1
here X1=(9,0),X2=(5,1)..X10=(4,1) How to label the coordinates 1,2 3....10 And how to construct graph ie G= graph(s,t,weights) where weights is the distance between the nodes/coordinates

采纳的回答

Rollin Baker
Rollin Baker 2017-4-12
Hi Sneha,
You can create your graph using the "digraph" function, and determine the weights using the "pdist" function. This can achieved with the following code:
% Define the coordinate points of the nodes
z = [ 9, 0; 5, 1; 10, 4; 1, 3; 0, 0; 7, 1; 4, 4; 8, 4; 0, 5; 4, 1];
% Get the x and y coordinates of the nodes
xPoints = z(:, 1);
yPoints = z(:, 2);
% Define the source nodes of the edges
s = [1 2 3 4 5 6 7 8 9];
% Define the destination nodes of the edges
t = [2 3 4 5 6 7 8 9 10];
% Initialize the weights array
weights = zeros(1, numel(s));
% Define the weights
for idx = 1:numel(weights)
weights(idx) = pdist([z(idx, :); z(idx + 1, :)]);
end
% Create graph
G = digraph(s,t, weights);
% Plot the graph with edge weights
p = plot(G, 'EdgeLabel', G.Edges.Weight);
% Set the x-coordinates of the nodes
p.XData = xPoints;
% Set the y-coordinates of the nodes
p.YData = yPoints;
Since you didn't specify the connectivity of the graph, I assumed X1 was connected to X2, X2 was connected to X3, and so on. This can be changed by altering the definitions of s and t.
The default labeling of the nodes will be the number assigned to them in the definitions for s and t, so as long as you define the edges correctly, you won't have to worry about any explicit labeling.
For more detailed documentation on using the digraph function, you can check out the link below:
https://www.mathworks.com/help/matlab/ref/graph.html
Good luck with your graph project!
-Rollin

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Graph and Network Algorithms 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by