MATLAB: How to graph an adjacency list

algorithmdataMATLABstructures

Hello everyone.
I have a graph being respresented as an Adjacency list:
AdjTable{1} = [2];
AdjTable{2} = [1, 3, 4];
AdjTable{3} = [2, 5];
AdjTable{4} = [2];
AdjTable{5} = [3];
G = AdjTable;
I was wondering how I can visually show this on a plot. I understand you're able to do this with Adjacency matrixers but can I stick to only using the lists?

Best Answer

Easiest way is to convert the adjacency list into an adjacency matrix. For each entry, set the matrix true at the row number corresponding to the cell index, and the column numbers given inside the entries. Then you can use graph() or digraph() and plot() the graph or digraph object.
(It can all be done in one line, but that is advanced MATLAB. Using sparse arrays helps.)
Related Question