Print nodes having maximum and minimum degrees
Given an undirected graph having N nodes, the task is to print the nodes having minimum and maximum degree.
Examples:
Input:
1-----2
| |
3-----4
Output:
Nodes with maximum degree : 1 2 3 4
Nodes with minimum degree : 1 2 3 4
Every node has a degree of 2.
Input:
1
/ \
2 3
/
4
Output:
Nodes with maximum degree : 1 2
Nodes with minimum degree : 3 4
Approach: For an undirected graph, the degree of a node is the number of edges incident to it, so the degree of each node can be calculated by counting its frequency in the list of edges. Hence the approach is to use a map to calculate the frequency of every vertex from the edge list and use the map to find the nodes having maximum and minimum degrees.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to print the nodes having // maximum and minimum degree void minMax(int edges[][2], int len, int n) { // Map to store the degrees of every node map<int, int> m; for (int i = 0; i < len; i++) { // Storing the degree for each node m[edges[i][0]]++; m[edges[i][1]]++; } // maxi and mini variables to store // the maximum and minimum degree int maxi = 0; int mini = n; for (int i = 1; i <= n; i++) { maxi = max(maxi, m[i]); mini = min(mini, m[i]); } // Printing all the nodes with maximum degree cout << "Nodes with maximum degree : "; for (int i = 1; i <= n; i++) { if (m[i] == maxi) cout << i << " "; } cout << endl; // Printing all the nodes with minimum degree cout << "Nodes with minimum degree : "; for (int i = 1; i <= n; i++) { if (m[i] == mini) cout << i << " "; } } // Driver code int main() { // Count of nodes and edges int n = 4, m = 6; // The edge list int edges[][2] = { { 1, 2 }, { 1, 3 }, { 1, 4 }, { 2, 3 }, { 2, 4 }, { 3, 4 } }; minMax(edges, m, 4); return 0; } |
Python3
# Python3 implementation of the approach # Function to print the nodes having # maximum and minimum degree def minMax(edges, leng, n) : # Map to store the degrees of every node m = {}; for i in range(leng) : m[edges[i][0]] = 0; m[edges[i][1]] = 0; for i in range(leng) : # Storing the degree for each node m[edges[i][0]] += 1; m[edges[i][1]] += 1; # maxi and mini variables to store # the maximum and minimum degree maxi = 0; mini = n; for i in range(1, n + 1) : maxi = max(maxi, m[i]); mini = min(mini, m[i]); # Printing all the nodes # with maximum degree print("Nodes with maximum degree : ", end = "") for i in range(1, n + 1) : if (m[i] == maxi) : print(i, end = " "); print() # Printing all the nodes # with minimum degree print("Nodes with minimum degree : ", end = "") for i in range(1, n + 1) : if (m[i] == mini) : print(i, end = " "); # Driver code if __name__ == "__main__" : # Count of nodes and edges n = 4; m = 6; # The edge list edges = [[ 1, 2 ], [ 1, 3 ], [ 1, 4 ], [ 2, 3 ], [ 2, 4 ], [ 3, 4 ]]; minMax(edges, m, 4); # This code is contributed by AnkitRai01 |
Nodes with maximum degree : 1 2 3 4 Nodes with minimum degree : 1 2 3 4
Recommended Posts:
- Sum of degrees of all nodes of a undirected graph
- Minimum and maximum node that lies in the path connecting two nodes in a Binary Tree
- Detect cycle in the graph using degrees of nodes of graph
- Minimum adjacent swaps to move maximum and minimum to corners
- Print all neighbour nodes within distance K
- Print the path between any two nodes of a tree | DFS
- Print Leaf Nodes at a given Level
- Print all odd nodes of Binary Search Tree
- Print all internal nodes of a Binary tree
- Print all nodes between two given levels in Binary Tree
- Print all leaf nodes of a binary tree from right to left
- Print leaf nodes in binary tree from left to right using one stack
- Sum of nodes at maximum depth of a Binary Tree | Set 2
- Find maximum among all right nodes in Binary Tree
- Level with maximum number of nodes using DFS in a N-ary tree
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : AnkitRai01



