Before answering the question about GraphPeriphery
I would make one suggestion for speeding up your code. The SemanticImport
call that you make returns a Dataset
object, which is nice for visualizing the data but very slow here. Using Normal
will convert it into a usable form. So instead of doing
LaunchKernels[];
edges = ParallelTable[
data[[i, 1]] \[UndirectedEdge] data[[i, 2]], {i, 1,
Length[data]}];
CloseKernels[];
just do
edges = UndirectedEdge @@@ Normal[data]
it is orders of magnitude faster. Secondly, don't create a graph with one edge and then add the remaining edges like this
g = Graph[edges[[1]]];
f = EdgeAdd[g, Take[edges, -(Length[edges] - 1)]];
instead just use
f = Graph[edges]
Now to the reason you see a difference between GraphPeriphery
and Position[ecc, Max[ecc]]
is because the position of the vertex in the list is not the same as the vertex. In general you might expect that the VertexList
will be just {1, 2, 3, 4, ...}
but really the vertices can be in any order. When you create a graph from a list of edges, you don't control the order of the vertices like you do when creating a graph via Graph[verts, edges]
.
So for your graph you can see
In[30]:= VertexList[f] // Shallow
Out[30]//Shallow= {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ...}
which is why periphery found by Position
is wrong - an off-by-one error in this case. If you always want to work with a graph whose vertices are {1, 2, 3, ...}
then use IndexGraph
like
f = IndexGraph[UndirectedEdge @@@ Normal[data]]
You can check the results visually with
GraphPlot@HighlightGraph[f, GraphPeriphery[f]]
compare this to the periphery you were computing before when you had the off-by-one error: