Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <string>
- #include <string_view>
- #include <sstream>
- #include <map>
- #include <set>
- using namespace std;
- bool is_number(const std::string &s) {
- std::string::const_iterator it = s.begin();
- while (it != s.end() && std::isdigit(*it)) ++it;
- return !s.empty() && it == s.end();
- }
- void get_number_convert(int &first, int &second) {
- string input;
- while (true) {
- getline(cin, input);
- string word;
- stringstream iss(input);
- bool isMinus = 0;
- bool zero = 0;
- while (iss >> word) {
- if (word[0] == '-' && word[1] == '1') {
- isMinus = 1;
- word[0] = '0';
- }
- if (is_number(word)) {
- if (zero == 0) {
- zero = 1;
- if (isMinus) {
- first = -1 * stoi(word);
- } else {
- first = stoi(word);
- }
- if (first == -1)
- return;
- } else {
- second = stoi(word);
- return;
- }
- }
- }
- cout << "please, do a correct int input" << '\n';
- }
- }
- void get_correct_direction(bool &get_correct_direction) {
- string input;
- while (true) {
- getline(cin, input);
- if (is_number(input)) {
- int n = stoi(input);
- if (n == 0 || n == 1) {
- get_correct_direction = (n % 2 == 0);
- return;
- } else
- cout << "please, do a correct boolean input" << '\n';
- } else {
- cout << "please, do a correct boolean input" << '\n';
- }
- }
- }
- bool isContainValue(map<int, vector<int>> graph, const int &finding_key) {
- bool is_Contain = 0;
- for (const auto &[key, value]: graph) {
- if (finding_key == key) {
- is_Contain = true;
- break;
- }
- }
- if (is_Contain)
- return true;
- return false;
- }
- void DFS_strong_connected(const int v, map<int, vector<int>> &graph, map<int, bool> &mark) {
- mark[v] = 1;
- for (const auto &[key, value]: graph) // Foreach edge
- {
- for (auto i: value) {
- if (mark[i] != 1) {
- DFS_strong_connected(i, graph, mark); // Load from the another node
- }
- }
- }
- }
- void DFS_for_one_way(bool& is_reach_pin, const int &pin_vertex, const int v, map<int, vector<int>> &graph, map<int, bool> &mark, int &color,
- map<int, int> &colors) {
- mark[v] = 1;
- colors[v] = color;
- if (v == pin_vertex){
- is_reach_pin = true;
- return;
- }
- for (const auto &[key, value]: graph) // Foreach edge
- {
- for (auto i: value) {
- if (mark[i] != 1 && isContainValue(graph, key)) {
- DFS_for_one_way(is_reach_pin, pin_vertex, i, graph, mark, color, colors); // Load from the another node
- }
- }
- }
- }
- void DFS(const int v, map<int, vector<int>> &graph, map<int, bool> &mark, int &color, map<int, int> &colors) {
- mark[v] = 1;
- colors[v] = color;
- for (const auto &[key, value]: graph) // Foreach edge
- {
- for (auto i: value) {
- if (mark[i] != 1) {
- DFS(i, graph, mark, color, colors); // Load from the another node
- }
- }
- }
- }
- //only for the directed graphs
- int amountOfConnectivity(map<int, vector<int>> &graph) {
- int color = 1;
- int n = graph.size();
- map<int, int> colors;
- for (const auto &[key, value]: graph) {
- colors.insert(make_pair(key, 0));
- }
- map<int, bool> mark;
- for (const auto &[key, value]: graph) {
- mark.insert(make_pair(key, 0));
- }
- for (const auto &[key, value]: graph) {
- if (mark[key] != 1) {
- DFS(key, graph, mark, color, colors);
- color++;
- }
- }
- set<int> count;
- for (const auto &[key, value]: colors) {
- count.insert(value);
- }
- return count.size();
- }
- bool isStrongConnection(map<int, vector<int>> &graph) {
- for (const auto &[key_external, value_external]: graph) {
- map<int, bool> mark;
- for (const auto &[key, value]: graph) {
- mark.insert(make_pair(key, 0));
- }
- DFS_strong_connected(key_external, graph, mark);
- for (const auto &[key, value]: mark) {
- if (mark[key] == false)
- return false;
- }
- }
- return true;
- }
- bool isOneWayConnection(map<int, vector<int>> &graph) {
- map<int, bool> marks_for_each;
- for (const auto &[key, value]: graph) {
- for (auto i: value) {
- if (!marks_for_each.contains(i)) {
- marks_for_each.insert(make_pair(i, false));
- }
- }
- }
- for (const auto &[key_external, value_external]: graph) {
- const int pin_vertex = key_external;
- bool is_reach_vertex = false;
- for(const auto &[key_in, value_in]: graph){
- int color = 1;
- map<int, int> colors;
- for (const auto &[key, value]: graph) {
- colors.insert(make_pair(key, 0));
- }
- map<int, bool> mark;
- for (const auto &[key, value]: graph) {
- mark.insert(make_pair(key, 0));
- }
- DFS_for_one_way(is_reach_vertex, pin_vertex, key_in, graph, mark, color, colors);
- }
- if (is_reach_vertex)
- marks_for_each[key_external] = true;
- }
- for (const auto &[key, value]: marks_for_each) {
- if (!marks_for_each[key])
- return false;
- }
- return true;
- }
- bool isWeak(map<int, vector<int>> &graph) {
- map<int, vector<int>> temp_graph;
- for(const auto& [key, values] : graph){
- for(auto value : values){
- if(temp_graph.contains(key)){
- temp_graph[key].push_back(value);
- } else{
- temp_graph.insert(make_pair(key, value));
- }
- if (key != value){
- temp_graph[value].push_back(key);
- }
- }
- }
- if (amountOfConnectivity(temp_graph) == 1)
- return true;
- return false;
- };
- void print_map(std::string_view comment, const map<int, vector<int>> &m) {
- std::cout << comment << '\n';
- for (const auto &[key, value]: m) {
- std::cout << '[' << key << "] = ";
- for (auto i: value)
- cout << i << "; ";
- cout << '\n';
- }
- std::cout << '\n';
- }
- int calculateCountOfVertices(map<int, vector<int>> &graph) {
- set<int> vertecies;
- for (const auto &[key, value]: graph) {
- vertecies.insert(key);
- for (auto i: value)
- vertecies.insert(i);
- }
- return vertecies.size();
- }
- int calculateCountOfLoops(map<int, vector<int>> &graph) {
- int count = 0;
- for (const auto &[key, value]: graph) {
- for (auto i: value) {
- if (key == i) {
- count++;
- }
- }
- }
- return count;
- }
- int calculateCountOfEdges(map<int, vector<int>> &graph) {
- int count = 0;
- for (const auto &[key, value]: graph) {
- for (auto i: value) {
- count++;
- }
- }
- return count;
- }
- int calculateMaxDegreeOut(map<int, vector<int>> &graph) {
- int maxx = 0;
- for (const auto &[key, value]: graph) {
- int temp = 0;
- for (auto i: value) {
- temp++;
- }
- if (temp > maxx)
- maxx = temp;
- }
- return maxx;
- }
- int calculateMaxDegreeIn(map<int, vector<int>> &graph) {
- map<int, int> maxx_vals;
- for (const auto &[key, value]: graph) {
- maxx_vals.insert(make_pair(key, 0));
- }
- for (const auto &[key, value]: graph) {
- for (auto i: value) {
- maxx_vals[i]++;
- }
- }
- int maxx = 0;
- for (const auto &[key, value]: maxx_vals) {
- if (value > maxx)
- maxx = value;
- }
- return maxx;
- }
- int calculateMaxDegreeInAndOut(map<int, vector<int>> &graph) {
- int maxx = 0;
- for (const auto &[key, value]: graph) {
- int temp = 0;
- for (auto i: value) {
- temp++;
- if (i == key)
- temp++;
- }
- if (temp > maxx) {
- maxx = temp;
- }
- }
- return maxx;
- }
- int main() {
- map<int, vector<int>> graph;
- cout << "Choose a type of graph(0 - Direct, 1 - Undirect):\n";
- bool isDirectedGraph = 0;
- get_correct_direction(isDirectedGraph);
- cout
- << "Enter edges with 2 positive integer nodes separated by a space, on one line you can put the only one edge. To finish an input put -1 in a first node.\n";
- int first, second;
- while (true) {
- get_number_convert(first, second);
- if (first == -1) {
- break;
- }
- if (graph.contains(first)) {
- graph[first].push_back(second);
- } else {
- graph.insert(make_pair(first, vector<int>() = {second}));
- }
- if (!isDirectedGraph && second != first) {
- graph[second].push_back(first);
- }
- }
- print_map("Your graph by a list of vertices:", graph);
- int countOfVertices = calculateCountOfVertices(graph);
- int countOfLoops = calculateCountOfLoops(graph);
- int countOfEdges = calculateCountOfEdges(graph);
- countOfEdges -= countOfLoops;
- if (!isDirectedGraph) {
- countOfEdges /= 2;
- }
- cout << "Count Of Vertices:" << countOfVertices << '\n';
- cout << "Count Of Loops:" << countOfLoops << '\n';
- cout << "Count Of Edges:" << countOfEdges << '\n';
- if (isDirectedGraph) {
- cout << "Max degree of your directed graph is:" << '\n';
- cout << "In vertex:" << calculateMaxDegreeIn(graph) << '\n';
- cout << "Out of vertex:" << calculateMaxDegreeOut(graph) << '\n';
- } else {
- cout << "Max degree of your Undirected graph is:" << calculateMaxDegreeInAndOut(graph) << '\n';
- }
- if (!isDirectedGraph) {
- int countConnections = amountOfConnectivity(graph);
- if (countConnections == 1) {
- cout << "Connected graph";
- } else {
- cout << "Unconnected graph";
- }
- cout << '\n';
- cout << "Connections count:" << countConnections << '\n';
- } else {
- if (isStrongConnection(graph)) {
- cout << "Strong connected graph\n";
- }
- else if (isOneWayConnection(graph)) {
- cout << "One-way connected graph\n";
- } else if (isWeak(graph)) {
- cout << "Weak connected graph";
- } else {
- cout << "Unconnected graph\n";
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement