📜  无向图中具有素数的节点

📅  最后修改于: 2021-10-25 04:47:06             🧑  作者: Mango

给定一个有N个顶点和M 个边的无向图,任务是打印给定图中度为素数的所有节点。
例子:

方法:

  1. 使用埃拉托色尼筛法计算最大为 10 5的素数。
  2. 对于每个顶点,度数可以通过给定图在相应顶点的邻接表的长度来计算。
  3. 打印给定图中度数为素数的顶点。

下面是上述方法的实现:

C++
// C++ implementation of the approach
 
#include 
using namespace std;
 
int n = 10005;
 
// To store Prime Numbers
vector Prime(n + 1, true);
 
// Function to find the prime numbers
// till 10^5
void SieveOfEratosthenes()
{
 
    int i, j;
    Prime[0] = Prime[1] = false;
    for (i = 2; i * i <= 10005; i++) {
 
        // Traverse all multiple of i
        // and make it false
        if (Prime[i]) {
 
            for (j = 2 * i; j < 10005; j += i) {
                Prime[j] = false;
            }
        }
    }
}
 
// Function to print the nodes having
// prime degree
void primeDegreeNodes(int N, int M,
                      int edges[][2])
{
    // To store Adjacency List of
    // a Graph
    vector Adj[N + 1];
 
    // Make Adjacency List
    for (int i = 0; i < M; i++) {
        int x = edges[i][0];
        int y = edges[i][1];
 
        Adj[x].push_back(y);
        Adj[y].push_back(x);
    }
 
    // To precompute prime numbers
    // till 10^5
    SieveOfEratosthenes();
 
    // Traverse each vertex
    for (int i = 1; i <= N; i++) {
 
        // Find size of Adjacency List
        int x = Adj[i].size();
 
        // If length of Adj[i] is Prime
        // then print it
        if (Prime[x])
            cout << i << ' ';
    }
}
 
// Driver code
int main()
{
    // Vertices and Edges
    int N = 4, M = 6;
 
    // Edges
    int edges[M][2] = { { 1, 2 }, { 1, 3 },
                        { 1, 4 }, { 2, 3 },
                        { 2, 4 }, { 3, 4 } };
 
    // Function Call
    primeDegreeNodes(N, M, edges);
 
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
class GFG{
 
static int n = 10005;
 
// To store Prime Numbers
static boolean []Prime = new boolean[n + 1];
 
// Function to find the prime numbers
// till 10^5
static void SieveOfEratosthenes()
{
    int i, j;
    Prime[0] = Prime[1] = false;
    for (i = 2; i * i <= 10005; i++)
    {
 
        // Traverse all multiple of i
        // and make it false
        if (Prime[i])
        {
            for (j = 2 * i; j < 10005; j += i)
            {
                Prime[j] = false;
            }
        }
    }
}
 
// Function to print the nodes having
// prime degree
static void primeDegreeNodes(int N, int M,
                              int edges[][])
{
    // To store Adjacency List of
    // a Graph
    Vector []Adj = new Vector[N + 1];
    for(int i = 0; i < Adj.length; i++)
        Adj[i] = new Vector();
 
    // Make Adjacency List
    for (int i = 0; i < M; i++)
    {
        int x = edges[i][0];
        int y = edges[i][1];
 
        Adj[x].add(y);
        Adj[y].add(x);
    }
 
    // To precompute prime numbers
    // till 10^5
    SieveOfEratosthenes();
 
    // Traverse each vertex
    for (int i = 1; i <= N; i++)
    {
 
        // Find size of Adjacency List
        int x = Adj[i].size();
 
        // If length of Adj[i] is Prime
        // then print it
        if (Prime[x])
            System.out.print(i + " ");
    }
}
 
// Driver code
public static void main(String[] args)
{
    // Vertices and Edges
    int N = 4, M = 6;
 
    // Edges
    int edges[][] = { { 1, 2 }, { 1, 3 },
                      { 1, 4 }, { 2, 3 },
                      { 2, 4 }, { 3, 4 } };
    Arrays.fill(Prime, true);
     
    // Function Call
    primeDegreeNodes(N, M, edges);
}
}
 
// This code is contributed by sapnasingh4991


Python3
# Python3 implementation of
# the above approach
n = 10005;
  
# To store Prime Numbers
Prime = [True for i in range(n + 1)]
  
# Function to find
# the prime numbers
# till 10^5
def SieveOfEratosthenes():
  
    i = 2   
    Prime[0] = Prime[1] = False;
     
    while i * i <= 10005:
  
        # Traverse all multiple
        # of i and make it false
        if (Prime[i]):           
            for j in range(2 * i, 10005):
                Prime[j] = False       
        i += 1  
     
# Function to print the
# nodes having prime degree
def primeDegreeNodes(N, M, edges):
 
    # To store Adjacency
    # List of a Graph
    Adj = [[] for i in range(N + 1)];
  
    # Make Adjacency List
    for i in range(M):
        x = edges[i][0];
        y = edges[i][1];
  
        Adj[x].append(y);
        Adj[y].append(x);   
  
    # To precompute prime
    # numbers till 10^5
    SieveOfEratosthenes();
  
    # Traverse each vertex
    for i in range(N + 1):
  
        # Find size of Adjacency List
        x = len(Adj[i]);
  
        # If length of Adj[i] is Prime
        # then print it
        if (Prime[x]):
            print(i, end = ' ')          
 
# Driver code
if __name__ == "__main__":
     
    # Vertices and Edges
    N = 4
    M = 6
  
    # Edges
    edges = [[1, 2], [1, 3],
             [1, 4], [2, 3],
             [2, 4], [3, 4]];
  
    # Function Call
    primeDegreeNodes(N, M, edges);
 
# This code is contributed by rutvik_56


C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
 
class GFG{
 
static int n = 10005;
 
// To store Prime Numbers
static bool []Prime = new bool[n + 1];
 
// Function to find the prime numbers
// till 10^5
static void SieveOfEratosthenes()
{
    int i, j;
    Prime[0] = Prime[1] = false;
    for(i = 2; i * i <= 10005; i++)
    {
        
       // Traverse all multiple of i
       // and make it false
       if (Prime[i])
       {
           for(j = 2 * i; j < 10005; j += i)
           {
              Prime[j] = false;
           }
       }
    }
}
 
// Function to print the nodes having
// prime degree
static void primeDegreeNodes(int N, int M,
                             int [,]edges)
{
     
    // To store Adjacency List of
    // a Graph
    List []Adj = new List[N + 1];
    for(int i = 0; i < Adj.Length; i++)
       Adj[i] = new List();
 
    // Make Adjacency List
    for(int i = 0; i < M; i++)
    {
       int x = edges[i, 0];
       int y = edges[i, 1];
        
       Adj[x].Add(y);
       Adj[y].Add(x);
    }
     
    // To precompute prime numbers
    // till 10^5
    SieveOfEratosthenes();
 
    // Traverse each vertex
    for(int i = 1; i <= N; i++)
    {
         
       // Find size of Adjacency List
       int x = Adj[i].Count;
        
       // If length of Adj[i] is Prime
       // then print it
       if (Prime[x])
           Console.Write(i + " ");
    }
}
 
// Driver code
public static void Main(String[] args)
{
     
    // Vertices and Edges
    int N = 4, M = 6;
 
    // Edges
    int [,]edges = { { 1, 2 }, { 1, 3 },
                     { 1, 4 }, { 2, 3 },
                     { 2, 4 }, { 3, 4 } };
                      
    for(int i = 0; i < Prime.Length; i++)
       Prime[i] = true;
     
    // Function Call
    primeDegreeNodes(N, M, edges);
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
1 2 3 4

时间复杂度: O(N + M) ,其中 N 是顶点数,M 是边数。