# Plotting points using adjacency matrix

Given an adjacency matrix on n vertices and a polygon on n vertices, how to add edges from adjacency matrix on the Polygon?

eg: if polygon is {(0,0),(0,1),(1,0),(1,1)} and the adjacency matrix has 1 in the position corresponding to vertices 1 and 4 then how to draw that line on the polygon.

It would be great if there is a function to execute this.

SeedRandom[5]
pts = RandomReal[1, {6, 2}];
pts = pts[[FindShortestTour[pts][[2]]]];
am = RandomChoice[{.7, .3} -> {0, 1}, {6, 6}];


AdjacencyGraph using the polygon vertices as vertex coordinates:

Labeled[AdjacencyGraph[am, VertexCoordinates -> pts, DirectedEdges -> False,
Vertexlabels->"Name", Prolog -> {Yellow, Polygon[pts]}],
MatrixForm[am], Left]


OP's example:

pts = {{0, 0}, {0, 1}, {1, 0}, {1, 1}};
am = {{0, 0, 0, 1}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}};
Labeled[AdjacencyGraph[am, VertexCoordinates -> pts, DirectedEdges -> False,
VertexLabels->"Name", Prolog -> {Opacity[.5, Yellow], Polygon[pts]}],
MatrixForm[am], Left]


• Elegant... but it doesn't show the polygon. Commented May 26, 2016 at 23:05

Here's an example where the adjacency matrix shows a link from pt[[1]] to pt[[4]] and from pt[[2]] to pt[[3]]:

pts = {{0, 0}, {0, 1}, {1, 1}, {1, 0}};
adMat = {{0, 0, 0, 1},
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 1, 0, 0}};
myfig = Graphics[

{{Opacity[0.2], Yellow, Polygon[pts]},
{Red, PointSize[0.02], Point[pts]},
Line@({pts[[#1]], pts[[#2]]} & @@@ Position[adMat, 1])
}]


This assumes one needs only one entry to link two points (e.g., just 1 -> 2 without 2 -> 1).