英文:
Adding minimum nodes to a disconnected graph in order to be able to reach every node from other, with connectivity constraints
问题
以下是您要翻译的内容:
这里有一个图,其中每个节点都有一个空间位置(x,y),只有当节点位于同一垂直或水平线上时,节点之间的边才会相连。换句话说,只有当(xA == xB)OR(yA == yB)时,节点A(具有空间位置(xA,yA))和B(具有空间位置(xB,yB))才会连接。
图中还有一些其他未连接的节点,因为没有与其具有相同x或y的节点。
示例:
import networkx as nx
G=nx.Graph()
G.add_node(1,pos=(1,1))
G.add_node(2,pos=(1,2))
G.add_node(3,pos=(1,3))
G.add_node(4,pos=(1,4))
G.add_node(5,pos=(1,5))
G.add_node(6,pos=(2,1))
G.add_node(7,pos=(3,1))
G.add_node(8,pos=(4,1))
G.add_node(9,pos=(5,3))
G.add_node(10,pos=(6,7))
G.add_node(11,pos=(10,6))
G.add_edge(1,2)
G.add_edge(2,3)
G.add_edge(3,4)
G.add_edge(4,5)
G.add_edge(1,6)
G.add_edge(6,7)
G.add_edge(7,8)
G.add_edge(3,9)
pos=nx.get_node_attributes(G,'pos')
nx.draw(G,pos)
有人能帮助我找到一种算法,以便我添加尽可能少的节点,使图连接(每个节点都可以从任何其他节点到达)吗?
我找到了这个与我的问题类似的问题,但我无法获得算法。
英文:
There is this graph where each node has a spatial position (x,y), and the edges between the nodes are only connected if the nodes are on the same vertical or horizontal line. By other words, nodes A (with spatial position(xA, yA)) and B (with spatial position(xB, yB)) are connected if and only if (xA == xB) OR (yA == yB).
There are also some other nodes in the graph which are not connected, because there is no node with the same x or y.
Example:
import networkx as nx
G=nx.Graph()
G.add_node(1,pos=(1,1))
G.add_node(2,pos=(1,2))
G.add_node(3,pos=(1,3))
G.add_node(4,pos=(1,4))
G.add_node(5,pos=(1,5))
G.add_node(6,pos=(2,1))
G.add_node(7,pos=(3,1))
G.add_node(8,pos=(4,1))
G.add_node(9,pos=(5,3))
G.add_node(10,pos=(6,7))
G.add_node(11,pos=(10,6))
G.add_edge(1,2)
G.add_edge(2,3)
G.add_edge(3,4)
G.add_edge(4,5)
G.add_edge(1,6)
G.add_edge(6,7)
G.add_edge(7,8)
G.add_edge(3,9)
pos=nx.get_node_attributes(G,'pos')
nx.draw(G,pos)
Can anyone help me find the algorithm which gives me least number of nodes that should be added so that the graph will be connected (every node can be reached from any other)?
I found this question similar to mine but I couldn't get the algorithm.
答案1
得分: 0
Notice that if two unconnected node lie on the same row or column, then they can be connected to the other connected nodes by just one new node.
So:
- Construct a 3D vector [行/列][行或列索引][节点索引]
- 循环遍历未连接的节点
- 将节点索引添加到3D向量中
示例:在第3行第2列的未连接节点,您应将其索引添加到向量[行][3]和向量[列][2]中。
- 查找向量[行][行范围]和向量[列][列范围]的最大计数
- 添加连接找到的最大节点的节点
- 从向量中删除新连接的节点
- 重复,直到所有节点都连接
如果需要实现此功能的C++代码,请告诉我。
英文:
Notice that if two unconnected node lie on the same row or column, then they can be connected to the other connected nodes by just one new node.
So:
- Construct a 3D vector [row/column][row or column index][node index]
- LOOP over unconnected nodes
-Add node index to 3d vector
example: unconnected node at row 3 column 2 you would add its index to vector[row][3] and vector[col][2]
- FIND maximum count of vector[row][row range] AND vector[col][col range]
- Add node that connects the nodes in the found maximum
- Remove newly connected nodes from vector
- Repeat until all nodes connected
Let me know if some C++ code that implements this would be useful.
答案2
得分: 0
如果我们在图上运行深度优先搜索(DFS),并每次计算DFS运行的次数,结果将显示图中连接组件的数量。我们需要为每个新组件添加一个节点(将其连接到其余部分)。因此,结果将是计算得到的组件数量减去1!
英文:
If we run DFS over the graph and count each time the DFS ran, the result shows us the number of connected components in the graph. We need one node for each new component (to connect it to the rest). So the result would be the counted number of components - 1!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论