Home > matGeom > graphs > adjacencyListToEdges.m

adjacencyListToEdges

PURPOSE ^

ADJACENCYLISTTOEDGES Convert an adjacency list to an edge array.

SYNOPSIS ^

function edges = adjacencyListToEdges(adjList)

DESCRIPTION ^

ADJACENCYLISTTOEDGES Convert an adjacency list to an edge array.

   EDGES = adjacencyListToEdges(ADJ)
   Converts the adjacency list ADJ, given as a cell array of adjacent
   indices, to an edge array. 

   Example
     % create adjacency list for a simple graph
     adj = {[2 3], [1 4 5], [1 4], [2 3 5], [2 4]};
     edges = adjacencyListToEdges(adj)
     edges =
          1     2
          1     3
          2     4
          2     5
          3     4
          4     5

   See also
     graphs, polygonSkeletonGraph

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function edges = adjacencyListToEdges(adjList)
0002 %ADJACENCYLISTTOEDGES Convert an adjacency list to an edge array.
0003 %
0004 %   EDGES = adjacencyListToEdges(ADJ)
0005 %   Converts the adjacency list ADJ, given as a cell array of adjacent
0006 %   indices, to an edge array.
0007 %
0008 %   Example
0009 %     % create adjacency list for a simple graph
0010 %     adj = {[2 3], [1 4 5], [1 4], [2 3 5], [2 4]};
0011 %     edges = adjacencyListToEdges(adj)
0012 %     edges =
0013 %          1     2
0014 %          1     3
0015 %          2     4
0016 %          2     5
0017 %          3     4
0018 %          4     5
0019 %
0020 %   See also
0021 %     graphs, polygonSkeletonGraph
0022 
0023 % ------
0024 % Author: David Legland
0025 % E-mail: david.legland@inrae.fr
0026 % Created: 2020-06-02, using Matlab 9.8.0.1323502 (R2020a)
0027 % Copyright 2020-2024 INRAE - BIA Research Unit - BIBS Platform (Nantes)
0028 
0029 % count total number of edges
0030 ne = 0;
0031 for iVertex = 1:length(adjList)
0032     ne = ne + length(adjList{iVertex});
0033 end
0034 
0035 % allocate memory
0036 edges = zeros([ne 2]);
0037 
0038 % create edges by iterating on vertices
0039 ie = 0;
0040 for iVertex = 1:length(adjList)
0041     neighs = adjList{iVertex};
0042     for iNeigh = 1:length(neighs)
0043         edge = sort([iVertex neighs(iNeigh)]);
0044         ie = ie + 1;
0045         edges(ie,:) = edge;
0046     end
0047 end
0048 
0049 % sort edges
0050 edges = unique(edges, 'rows');

Generated on Thu 21-Nov-2024 11:30:22 by m2html © 2003-2022