Count the number of nodes in a Graph whose sum of neighbours is at most K


Introduction

Undirected graphs are an essential component of computer science and graph theory, representing a set of vertices connected by edges without any directionality. One common problem associated with undirected graphs is the counting the number of nodes in a graph whose sum of neighbours is at most K. In computer science, the graph theory field will deal with the connection between the elements in the given matrix. Usually the graphs consists of the elements namely the edges and the nodes.

Count the number of nodes in a Graph

The Graph that is exclusively used is the undirected graph in this case. The undirected graphs consists of 'N' nodes, 'M' edges and the conditions need to be satisfied to get the count of the node which is at most K. One common example of the undirected graph with four nodes is given below,

Example

The adjacent matrix is taken as the input with four vertices holding the values.

1 2
2 3
3 4
4 5

The neighbors are the one which can be from the left or right of the nodes. In the above case, node 1 returns the sum of neighbor as 1, node 2 returns the sum of neighbor as 2, node 3 returns the sum of neighbor as 3, node 4 returns the sum of neighbor as 2 and finally node 5 returns the sum of neighbor as 1. We need to check the nodes sum, when the sum of neighbors less than or equal to K. So the node 1 is the only one that satisfies the condition and it returns value as 1.

Approach 1: C code to Count the number of nodes in a Graph whose sum of neighbours is at most K using the recursive function

The adjacent matrix is initialized as the input and to traverse through the list we can use either Depth first search (DFS) or Breadth First Search (BFS) algorithms.

Algorithm

  • Step 1 − The required header files are stdio and the stdlib, included in the given program.

  • Step 2 − The function is defined with three arguments as the graph, N and K of int data type.

  • Step 3 − The count variable is initialized as 0 and the for loop is used to iterate through the adjacent list of the graph from 1 to N.

  • Step 4 − The edges connect the two points A and B, it is the adjacent node in the given adjacency list.

  • Step 5 − The sum value is stored in the variable named "sum" which sums up the value of the weight or the attributes in the given vertices.

  • Step 6 − The condition is checked whether the sum value is less than or equal to K, then it is increments the count by one.

  • Step 7 − The final output holding the count of the number of nodes in a Graph when the sum of neighbours is at most K.

Example

//The header files are included
#include <stdio.h>
#include <stdlib.h>

// Function defined with three arguments and declared as output
int output(int** graph, int node, int edges) {
   //Initializing the variable as 0
   int val = 0; 
   //for loop will iterate through the values
   for (int m = 1; m <= node; ++m) {
      int sum = 0;
      for (int neighbor = 1; neighbor <= node; ++neighbor) {
         sum += graph[m][neighbor];
      }
      //To check for the condition whether the sum value is less than or equal to edges
      if (sum <= edges) {
         //When equal it is incremented by one
         val++;
      }
   }

   return val;
}

// Main function to test the code
int main() {
   //Initializing the variables with values
   int node = 5, M = 4, edges = 2;
    
   int** graph = (int**)malloc((node+1)*sizeof(int*)); // Indexed from 1 to N 

   for(int i=0; i<=node; ++i){
      graph[i] = (int*)malloc((node+1)*sizeof(int));
      for(int j=0; j<=node; ++j){
         graph[i][j] = 0;
      }
   }

   // The edges values are declared in the graph
   graph[1][2] = 1;
   graph[2][1] = 1;
   graph[2][3] = 1;
   graph[3][2] = 1;
   graph[3][4] = 1;
   graph[4][3] = 1;
   graph[4][5] = 1;
   graph[5][4] = 1;
   //The function is called recursively with the parameters
   int nodeCount = output(graph, node, edges);

   printf("The total number of nodes with a sum of neighbors less than or equal to %d:\n%d\n", edges, nodeCount);

   return 0;
}

Output

The total number of nodes with a sum of neighbors less than or equal to 2:
5

Conclusion

Using the C program, the code is constructed – we can implement the count of the given undirected graph can be done using the recursive function. The function is called recursively along with the three parameters. The conditions are got with the help of pushback function and by adding the neighbor elements of the values given by the user.

Updated on: 25-Aug-2023

110 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements