Assume I have a graph.You can run following code to get it.
nearGraph =Import["http://halirutan.github.io/Mathematica-SE-Tools/decode.m"][
"https://i.sstatic.net/TWP6J.png"]
It is a disconnected graph.But I can connect it every component in this method.
SeedRandom[331]
pathGraph =
PathGraph[
v = point[[Last@
FindShortestTour[
point = RandomChoice /@ ConnectedComponents[nearGraph]]]],
VertexCoordinates -> Most@v]
Union this graph with original graph.
resultGraph = GraphUnion[nearGraph, pathGraph];
HighlightGraph[
Graph[resultGraph,
VertexCoordinates -> VertexList[resultGraph]], pathGraph]
{KEdgeConnectedGraphQ[resultGraph, 1],KEdgeConnectedGraphQ[resultGraph, 2]}
{True, False}
Yep.It's a connected now.But I don't really content with it.In my expectation.I wanna get a graph (maybe) like:
It just cost a shortest edge can do this.I know the position or distance in Graph
will not impact the result of calculation.In some case I have a demand like this maybe.I think we can see the distance as a edge weight to solve it.But I don't know how to implement it.
Update
As the @Rahul comments.I reemphasize the target that is the title of this topic :). We want to get a 1-edge-connected(but not 2-edge-connected).It can be the picture of the following