📌  相关文章
📜  从图中找到一组最多N / 2个节点,以便所有其余节点都直接连接到所选节点中的一个

📅  最后修改于: 2021-04-17 17:57:28             🧑  作者: Mango

给定一个整数N ,该整数表示无向图中存在的节点数,每个节点的值从1N,并且具有2D数组Edges [] [] ,表示通过边连接的一对顶点,任务是查找一组最多N / 2个节点,以使该组中不存在的节点与该组中存在的任何一个节点相邻连接。

例子 :

方法:可以根据以下观察结果解决给定问题:

  • 假设一个节点是节点,那么每个顶点到节点的距离将是奇数或偶数。
  • 根据奇偶校验将节点分为两个不同的集合,其中至少一个集合的大小不会超过N / 2 。由于某个奇偶校验的每个节点都连接到至少一个相对奇偶校验的节点,因此满足选择最多N / 2个节点的标准。

请按照以下步骤解决问题:

  • 假定任何顶点为节点。
  • 初始化两个集,例如evenParityoddParity,以分别存储与源节点具有偶数奇数距离的节点。
  • 在给定的图形上执行BFS遍历,并根据顶点到的距离的奇偶性将顶点分为两个不同的集合:
    • 如果每个连接的节点到节点的距离为奇数,则将当前节点插入设置的奇数奇偶性中
    • 如果每个连接节点到节点的距离是偶数,则将当前节点插入到set evenParity中
  • 完成上述步骤后,以最小尺寸打印集合中的元素。
C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to add an edge
// to the adjacency list
void addEdge(vector >& adj,
             int u, int v)
{
    adj[u].push_back(v);
    adj[v].push_back(u);
}
 
// Function to perform BFS
// traversal on a given graph
vector > BFS(
    int N, int source,
    vector > adjlist)
{
    // Stores the distance of each
    // node from the source node
    int dist[N + 1];
 
    vector > vertex_set;
 
    // Update the distance of all
    // vertices from source as -1
    memset(dist, -1, sizeof dist);
 
    // Assign two seperate vectors
    // for parity odd and even parities
    vertex_set.assign(2, vector(0));
 
    // Perform BFS Traversal
    queue Q;
 
    // Push the source node
    Q.push(source);
    dist = 0;
 
    // Iterate until queue becomes empty
    while (!Q.empty()) {
 
        // Get the front node
        // present in the queue
        int u = Q.front();
        Q.pop();
 
        // Push the node into vertex_set
        vertex_set[dist[u] % 2].push_back(u);
 
        // Check if the adjacent
        // vertices are visited
        for (int i = 0;
             i < (int)adjlist[u].size(); i++) {
 
            // Adjacent node
            int v = adjlist[u][i];
 
            // If the node v is unvisited
            if (dist[v] == -1) {
 
                // Update the distance
                dist[v] = dist[u] + 1;
 
                // Enqueue the node v
                Q.push(v);
            }
        }
    }
 
    // Return the possible set of nodes
    return vertex_set;
}
 
// Function to find a set of vertices
// of at most N/2 nodes such that each
// unchosen node is connected adjacently
// to one of the nodes in the set
void findSet(int N,
             vector > adjlist)
{
    // Source vertex
    int source = 1;
 
    // Store the vertex set
    vector > vertex_set
        = BFS(N, source, adjlist);
 
    // Stores the index
    // with minimum size
    int in = 0;
 
    if (vertex_set[1].size()
        < vertex_set[0].size())
        in = 1;
 
    // Print the nodes present in the set
    for (int node : vertex_set[in]) {
        cout << node << " ";
    }
}
 
// Driver Code
int main()
{
    int N = 5;
    int M = 8;
    vector > adjlist;
    adjlist.assign(N + 1, vector(0));
 
    // Graph Formation
    addEdge(adjlist, 2, 5);
    addEdge(adjlist, 2, 1);
    addEdge(adjlist, 5, 1);
    addEdge(adjlist, 4, 5);
    addEdge(adjlist, 1, 4);
    addEdge(adjlist, 2, 4);
    addEdge(adjlist, 3, 4);
    addEdge(adjlist, 3, 5);
 
    // Function Call to print the
    // set of at most N / 2 nodes
    findSet(N, adjlist);
 
    return 0;
}


Python3
# Python3 program for the above approach
from collections import deque
 
# Function to add an edge
# to the adjacency list
def addEdge(adj, u, v):
    adj[u].append(v)
    adj[v].append(u)
    return adj
 
# Function to perform BFS
# traversal on a given graph
def BFS(N, source, adjlist):
   
    # Stores the distance of each
    # node from the source node
    dist = [-1]*(N + 1)
    vertex_set = [[] for i in range(2)]
 
    # Perform BFS Traversal
    Q = deque()
 
    # Push the source node
    Q.append(source)
    dist = 0
 
    # Iterate until queue becomes empty
    while len(Q) > 0:
 
        # Get the front node
        # present in the queue
        u = Q.popleft()
 
        # Push the node into vertex_set
        vertex_set[dist[u] % 2].append(u)
 
        # Check if the adjacent
        # vertices are visited
        for i in range(len(adjlist[u])):
           
            # Adjacent node
            v = adjlist[u][i]
 
            # If the node v is unvisited
            if (dist[v] == -1):
 
                # Update the distance
                dist[v] = dist[u] + 1
 
                # Enqueue the node v
                Q.append(v)
 
    # Return the possible set of nodes
    return vertex_set
 
# Function to find a set of vertices
# of at most N/2 nodes such that each
# unchosen node is connected adjacently
# to one of the nodes in the set
def findSet(N, adjlist):
   
    # Source vertex
    source = 1
 
    # Store the vertex set
    vertex_set = BFS(N, source, adjlist)
 
    # Stores the index
    # with minimum size
    inn = 0
 
    if (len(vertex_set[1]) < len(vertex_set[0])):
        inn = 1
 
    # Prthe nodes present in the set
    for node in vertex_set[inn]:
        print(node, end=" ")
 
# Driver Code
if __name__ == '__main__':
    N = 5
    M = 8
    adjlist = [[] for i in range(N+1)]
 
    # Graph Formation
    adjlist = addEdge(adjlist, 2, 5)
    adjlist = addEdge(adjlist, 2, 1)
    adjlist = addEdge(adjlist, 5, 1)
    adjlist = addEdge(adjlist, 4, 5)
    adjlist = addEdge(adjlist, 1, 4)
    adjlist = addEdge(adjlist, 2, 4)
    adjlist = addEdge(adjlist, 3, 4)
    adjlist = addEdge(adjlist, 3, 5)
 
    # Function Call to prthe
    # set of at most N / 2 nodes
    findSet(N, adjlist)
 
# This code is contributed by mohit kumar 29.


输出:
1 3

时间复杂度: O(N + M)
辅助空间: O(N + M)