- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Print Nodes have Maximum and Minimum Degrees
The diploma of a node withinside the idea of graphs is the full variety of edges that join it. Finding the nodes in a graph with the very best and lowest diploma may monitor critical information about the hyperlinks and shape of the network.We will examine three approaches to resolving this issue utilising CPP algorithms in this article. We will go over the algorithm for each method, offer related code execution, and show off each approach's unique results.
Methods Used
Brute Force Approach
Priority Queue
Adjacency List
Brute Force
The brute force method entails counting the degrees of each node in the network as you iterate over all of the nodes. As we come across nodes with greater or lower degrees, we modify the node(s) with the highest and lowest degrees.
Algorithm
Initialise parameters with the highest and lowest degrees and associated nodes in the algorithm.
Go through each node in the graph iteratively.
Determine the degree for every node and contrast it with the existing highest and lowest degrees.
Example
#include <iostream> #include <vector> using namespace std; void printNodesWithMaxMinDegrees(const vector<vector<int>>& graph) { int numVertices = graph.size(); int maxDegree = 0; int minDegree = numVertices + 1; vector<int> nodesWithMaxDegree; vector<int> nodesWithMinDegree; for (int i = 0; i < numVertices; i++) { int degree = 0; for (int j = 0; j < numVertices; j++) { if (graph[i][j] == 1) { degree++; } } if (degree > maxDegree) { maxDegree = degree; nodesWithMaxDegree.clear(); nodesWithMaxDegree.push_back(i); } else if (degree == maxDegree) { nodesWithMaxDegree.push_back(i); } if (degree < minDegree) { minDegree = degree; nodesWithMinDegree.clear(); nodesWithMinDegree.push_back(i); } else if (degree == minDegree) { nodesWithMinDegree.push_back(i); } } // Print nodes with maximum degree cout << "Nodes with maximum degree (" << maxDegree << "): "; for (int node : nodesWithMaxDegree) { cout << node << " "; } cout << endl; // Print nodes with minimum degree cout << "Nodes with minimum degree (" << minDegree << "): "; for (int node : nodesWithMinDegree) { cout << node << " "; } cout << endl; } int main() { // Create a graph represented by an adjacency matrix vector<vector<int>> graph = { {0, 1, 1, 1, 0}, {1, 0, 0, 1, 0}, {1, 0, 0, 0, 1}, {1, 1, 0, 0, 0}, {0, 0, 1, 0, 0} }; // Call the printNodesWithMaxMinDegrees function printNodesWithMaxMinDegrees(graph); return 0; }
Output
Nodes with maximum degree (3): 0 Nodes with minimum degree (1): 4
Priority Queue
The third approach includes easily locating the nodes with the highest and lowest degrees by employing a priority queue. After adding the degrees of each of the nodes, the priority queue by default keeps the order of degrees in not increasing form.
Algorithm
Make a priority queue and put all the degrees of the nodes in it.
Add all nodes' degrees to the highest−priority queue.
To get the node with the lowest degree, remove the highest component.
To get to the node with the highest degree, remove the bottom portion.
Example
#include <iostream> #include <vector> #include <queue> using namespace std; class Graph { private: vector<vector<int>> adjacencyMatrix; public: Graph(const vector<vector<int>>& adjMatrix) : adjacencyMatrix(adjMatrix) {} int getNumVertices() const { return adjacencyMatrix.size(); } int getDegree(int vertex) const { int degree = 0; for (int i = 0; i < adjacencyMatrix.size(); i++) { if (adjacencyMatrix[vertex][i] == 1) { degree++; } } return degree; } }; void printNodesWithMaxMinDegrees(Graph graph) { int numVertices = graph.getNumVertices(); priority_queue<int, vector<int>, greater<int>> pq; // Insert degrees of nodes into priority queue for (int i = 0; i < numVertices; i++) { pq.push(graph.getDegree(i)); } // Node with minimum degree int minDegreeNode = pq.top(); pq.pop(); // Node with maximum degree int maxDegreeNode; while (!pq.empty()) { maxDegreeNode = pq.top(); pq.pop(); } // Print nodes with minimum and maximum degrees cout << "Node with minimum degree: " << minDegreeNode << endl; cout << "Node with maximum degree: " << maxDegreeNode << endl; } int main() { // Create an example adjacency matrix vector<vector<int>> adjacencyMatrix = { {0, 1, 1, 0}, {1, 0, 1, 1}, {1, 1, 0, 1}, {0, 1, 1, 0} }; // Create a Graph object and call the printNodesWithMaxMinDegrees function Graph graph(adjacencyMatrix); printNodesWithMaxMinDegrees(graph); return 0; }
Output
Node with minimum degree: 2 Node with maximum degree: 3
Adjacency List
The fourth technique entails building a list of dependencies to symbolise the graph. The nodes with the lowest and highest degrees are noted when we loop via the adjacency list.
Algorithm
Create the graph's adjacency list form using the following algorithm.
Set up parameters to hold the highest and lowest degrees as well as the associated nodes.
Determine each node's degree by iteratively going via the adjacency list.
Modify the nodes, the upper and lower degrees as necessary.
Example
#include <iostream> #include <vector> #include <algorithm> using namespace std; class Graph { private: vector<vector<int>> adjacencyList; public: Graph(const vector<vector<int>>& adjList) : adjacencyList(adjList) {} int getNumVertices() const { return adjacencyList.size(); } const vector<vector<int>>& getAdjacencyList() const { return adjacencyList; } }; void printNodesWithMaxMinDegrees(Graph graph) { int numVertices = graph.getNumVertices(); vector<int> degrees(numVertices); const vector<vector<int>>& adjacencyList = graph.getAdjacencyList(); // Calculate degrees of nodes for (int i = 0; i < numVertices; i++) { degrees[i] = adjacencyList[i].size(); } // Find node with minimum degree int minDegreeNode = min_element(degrees.begin(), degrees.end()) - degrees.begin(); // Find node with maximum degree int maxDegreeNode = max_element(degrees.begin(), degrees.end()) - degrees.begin(); // Print nodes with minimum and maximum degrees cout << "Node with minimum degree: " << minDegreeNode << endl; cout << "Node with maximum degree: " << maxDegreeNode << endl; } int main() { // Create an example adjacency list vector<vector<int>> adjacencyList = { {1, 2}, {0, 2, 3}, {0, 1, 3}, {1, 2} }; // Create a Graph object and call the printNodesWithMaxMinDegrees function Graph graph(adjacencyList); printNodesWithMaxMinDegrees(graph); return 0; }
Output
Node with minimum degree: 0 Node with maximum degree: 1
Conclusion
In this blog post, we looked at three distinct approaches for leveraging CPP algorithms to generate nodes with the highest and least degrees. From brute force iterations to more effective techniques like employing priority queues, and creating adjacency lists, each method presented a distinctive approach. These techniques make it simple to pinpoint the nodes that are essential to graph connection. In terms of efficiency and intricacy, a particular approach might be better suited than others based on the dimensions and layout of the graph. The particular needs of the current challenge eventually determine the strategy to be used.
To Continue Learning Please Login
Login with Google