I was trying to find the distances between vertices in graphs. But as the number of vertices are increasing up to 25 vertices or more, its becoming a tedious job for me to calculate $distance$ and $eccentricity$.
Can I draw graphs in MATLAB and then calculate the $dist$ and $ecc$?
For ex. What is the code if I want to draw $P_n$ and then the distances between vertices.
I am very new to MATLAB. Started few days back only. Can anyone help me here.
Edit : I want the code to draw the graph which are Undirected and non-weighted graphs. And also distance between every two vertices. Can I make the graph if I am taking adjacency matrix. Is it possible to make the graph when entries are like ordered pairs of adjacent vertices .
I would be very thankful. thanks
Coding for any small graph like $P_3$ or $K_4$ will be helpful for me.
Best Answer
To work out graph distance use Dijkstra's algorithm which is available for MATLAB here
You could download and install MATLABS graph theory toolbox (can't give a link on SE), which has the functions
grEccentricity
andgrShortPath
but it requires the optimization toolbox, which is propriety. So assuming you don't have the money to pay for a toolbox, here is an eccentricity function. There are surely faster algorithms than this one ($O(E^2)\times O(\text{dijkstra})$), but it is really simple - check every pair (assumes undirected edges).should work, but I don't own MATLAB to test it, you will need to put in a file called "eccent.m" in your working directory, because, well, it's MATLAB and that's how it works.
A function for $K_n$ is:
and for $P_n$ is
and the cyclic graphs just add a one to the antidiagonal corners
These would need to be in files "Kn.m", "Pn.m" and "Cn.m" respectively.
To make a directed graph for a list of pairs of the form:
then
and for an undirected graph