GRAPHDIAMETER Diameter of a graph. D = graphDiameter(V, E) Computes the diameter of the graph given by V and E. The diameter of the graph is the greatest eccentricity over all vertices in the graph. D = graphDiameter(V, E, L) Specifies the weight of each edge for computing the distances. Default is to consider a weight of 1 for each edge. Example nodes = [20 20;20 50;20 80;50 50;80 20;80 50;80 80]; edges = [1 2;2 3;2 4;4 6;5 6;6 7]; figure; drawGraph(nodes, edges); axis([0 100 0 100]); axis equal; hold on D = graphDiameter(nodes, edges) D = 4 See also grPropagateDistance, grVertexEccentricity graphCenter, graphRadius, graphPeripheralVertices
0001 function diam = graphDiameter(v, e, l) 0002 %GRAPHDIAMETER Diameter of a graph. 0003 % 0004 % D = graphDiameter(V, E) 0005 % Computes the diameter of the graph given by V and E. The diameter of 0006 % the graph is the greatest eccentricity over all vertices in the graph. 0007 % 0008 % D = graphDiameter(V, E, L) 0009 % Specifies the weight of each edge for computing the distances. Default 0010 % is to consider a weight of 1 for each edge. 0011 % 0012 % Example 0013 % nodes = [20 20;20 50;20 80;50 50;80 20;80 50;80 80]; 0014 % edges = [1 2;2 3;2 4;4 6;5 6;6 7]; 0015 % figure; drawGraph(nodes, edges); 0016 % axis([0 100 0 100]); axis equal; hold on 0017 % D = graphDiameter(nodes, edges) 0018 % D = 0019 % 4 0020 % 0021 % See also 0022 % grPropagateDistance, grVertexEccentricity 0023 % graphCenter, graphRadius, graphPeripheralVertices 0024 % 0025 0026 % ------ 0027 % Author: David Legland 0028 % E-mail: david.legland@inrae.fr 0029 % Created: 2010-09-07, using Matlab 7.9.0.529 (R2009b) 0030 % Copyright 2010-2024 INRA - Cepia Software Platform 0031 0032 % ensure there is a valid length array 0033 if nargin<3 0034 l = ones(size(e,1), 1); 0035 end 0036 0037 g = grVertexEccentricity(v, e, l); 0038 0039 diam = max(g);