#include <bits/stdc++.h> using namespace std; void extractIntegerfromstr(string str, int &i, int &j) { stringstream s; string temp; int num; s << str; num = 0; while (!s.eof()) { s >> temp; if (num == 0 && stringstream(temp) >> i) { num = 1; } else if (num == 1 && stringstream(temp) >> j) { num++; } temp = ""; } } class Graph { int V; vector<vector<int>> adj; public: Graph(int V); void addEdge(int v, int w); bool isReachable(int start, int end); void graphprint(); int degreecentrality1(int &max); int degreecentrality2(int &max); int Closeness_centrality_1(int &max); void DFS(int current, int &cnt, vector<int> &visited, vector<int> &duringdfs); int Closeness_centrality_2(int &max); }; Graph::Graph(int V) { this->V = V; adj.resize(V + 1); } void Graph::addEdge(int i, int j) { if (find(adj[i].begin(), adj[i].end(), j) == adj[i].end() && i != j) adj[i].push_back(j); } void Graph::graphprint() { for (int i = 0; i < adj.size(); i++) { cout << i << " "; for (auto j = 0; j < adj[i].size(); j++) { cout << " --> " << adj[i].at(j); } cout << endl; } } int Graph ::degreecentrality1(int &max) { vector<int> size; for (auto i = 1; i < adj.size(); i++) { size.push_back(adj[i].size()); } max = *max_element(size.begin(), size.end()); int node = distance(size.begin(), max_element(size.begin(), size.end())) + 1; return node; } int Graph ::degreecentrality2(int &max) { vector<int> degree; degree.resize(V + 1); for (auto i = 1; i <= V; i++) { for (int j = 0; j < adj[i].size(); j++) { degree[i]++; degree[adj[i].at(j)]++; } } max = *max_element(degree.begin(), degree.end()); for (int i = 0; i < V + 1; i++) { if (degree[i] == max) return i; } return -1; } bool Graph::isReachable(int s, int d) { if (s == d) return true; bool *visited = new bool[V]; for (int i = 0; i < V; i++) visited[i] = false; list<int> queue; visited[s] = true; queue.push_back(s); vector<int>::iterator i; while (!queue.empty()) { s = queue.front(); queue.pop_front(); for (i = adj[s].begin(); i != adj[s].end(); ++i) { if (*i == d) return true; if (!visited[*i]) { visited[*i] = true; queue.push_back(*i); } } } return false; } int Graph ::Closeness_centrality_1(int &max) { int nodesconectivity[V + 1] = {0}; for (int i = 1; i <= V; i++) { for (int j = 1; j <= V; j++) { if (isReachable(i, j)) { nodesconectivity[i]++; } } } max = *max_element(nodesconectivity, nodesconectivity + V + 1); for (int i = 1; i < V + 1; i++) { if (nodesconectivity[i] == max) return i; } return -1; } void Graph ::DFS(int current, int &count, vector<int> &visited, vector<int> &duringdfs) { visited[current] = 1; ++count; duringdfs.push_back(current); for (auto &child : adj[current]) { if (visited[child] == 0) { DFS(child, count, visited, duringdfs); } } } int Graph ::Closeness_centrality_2(int &max) { vector<int> visited(V + 1, 0); vector<int> ans(V + 1); vector<int> duringdfs; for (int i = 1; i <= V; ++i) { duringdfs.clear(); int count = 0; if (visited[i] == 0) { count = 0; DFS(i, count, visited, duringdfs); } for (auto &x : duringdfs) { ans[x] = count; } } int max2 = *max_element(ans.begin(), ans.begin() + V + 1); max = max2; int index = distance(ans.begin(), find(ans.begin(), ans.end(), max2)); return index; } int main() { string str; int i, j, c; int maxnode1, maxnode2, max; int n; cout << "Enter total number of nodes\n"; cin >> n; Graph g(n); int number; cout << "Enter total number of connection\n"; cin >> number; fflush(stdin); for (int k = 0; k < number; k++) { getline(cin, str); extractIntegerfromstr(str, i, j); g.addEdge(i, j); } g.graphprint(); cout << "Most influencler node according to Degree Centrality Method\n"; maxnode1 = g.degreecentrality1(max); cout << "If we considered Only one side of influence then the node which has most influence power is " << maxnode1 << " which is connected to " << max << " nodes" << endl; maxnode2 = g.degreecentrality2(max); cout << "If we considered both side of influence then the node which has most influence power is " << maxnode2 << " which is connected to " << max << " nodes" << endl; cout << endl; cout << "Most influencler node according to Closeness Centrality Method\n"; int met2 = g.Closeness_centrality_1(max); cout << "If we considered Only one side of influence then the node which has most influence power is " << met2 << " which is connected to " << max << " nodes" << endl; int ans2 = g.Closeness_centrality_2(max); cout << "If we considered both side of influence then the node which has most influence power is " << ans2 << " which is connected to " << max << " nodes" << endl; return 0; }
Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++
and start coding!
OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Enter name:";
getline (cin, name);
cout << "Hello " << name;
return 0;
}
C++ is a widely used middle-level programming language.
When ever you want to perform a set of operations based on a condition If-Else is used.
if(conditional-expression) {
//code
}
else {
//code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression){
case value1:
// code
break; // optional
case value2:
// code
break; // optional
......
default:
code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
// code
}