-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompute_functions.cpp
More file actions
195 lines (189 loc) · 6.35 KB
/
Copy pathcompute_functions.cpp
File metadata and controls
195 lines (189 loc) · 6.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include <iostream>
#include <map>
#include <deque>
#include <queue>
#include <set>
#include <algorithm>
#include <string>
#include <sstream>
#include <random>
#include <time.h>
#include <iterator>
using namespace std;
bool is_integer(string str) //Test if a given string is an integer
{
if (str.empty()) return false;
while (str.front() == ' ') {
str = str.substr(1);
}
size_t start = 0;
if (str[0] == '-' || str[0] == '+') {
if (str.size() == 1) return false;
start = 1;
}
for (size_t i = start; i < str.size(); ++i) {
if (!std::isdigit(str[i])) {
return false;
}
}
return true;
}
int e = 0;
bool weighted = false, max_mode = false;
map <string, set <string>> mst_graph, all_graph;
map <pair <string, string>, int> all_weights;
deque <pair <string, string>> all_edges, mst_edges;
set <string> all_vertices, mst_vertices;
int path_ans = 0;
deque <deque <string>> paths_count;
bool comp(pair <string, string> a, pair <string, string> b) {
return (all_weights[make_pair(a.first, a.second)] < all_weights[make_pair(b.first, b.second)]);
}
bool comp_max(pair <string, string> a, pair <string, string> b) {
return (all_weights[make_pair(a.first, a.second)] > all_weights[make_pair(b.first, b.second)]);
}
void clear_all() {
e = 0, path_ans = 0;
mst_graph.clear();
all_edges.clear();
mst_edges.clear();
all_vertices.clear();
mst_vertices.clear();
paths_count.clear();
}
//Compute and Counting Menu
void dfs_count_paths(string node, string dest, map <string, bool> visited, deque <string> current_path) {
if (node == dest) {
path_ans++;
paths_count.push_back(current_path);
return;
}
visited[node] = true;
current_path.push_back(node);
for (string neighbor : all_graph[node]) {
if (!visited[neighbor]) {
dfs_count_paths(neighbor, dest, visited, current_path);
}
}
visited[node] = false;
}
void main_count_paths() {
string src, dest;
while (true) {
cout << "Enter the initial source vertex:\n";
getline(cin, src);
if (all_graph.find(src) != all_graph.end()) {
break;
}
else { cout << "Invalid source! Attempt aborted.\n\n"; }
}
while (true) {
cout << "Enter the final destination vertex:\n";
getline(cin, dest);
if (all_graph.find(dest) != all_graph.end()) {
break;
}
else { cout << "Invalid destination! Attempt aborted.\n\n"; }
}
cout << "\n\nCounting paths between initial source node " << src << " and final destination node " << dest << ":\n\n";
map <string, bool> visited;
path_ans = 0;
dfs_count_paths(src, dest, visited, {});
for (deque <string> path : paths_count) {
for (string node : path) {
cout << node << " -> ";
}
cout << dest << '\n';
}
cout << "\nTotal count: " << path_ans << " paths\n";
}
void main_count_vertices() {
cout << "\n\nList of All Vertices:\n";
for (string a : all_vertices) {
cout << a << ' ';
}
cout << "\nCurrent total vertex count (order): " << all_vertices.size() << '\n';
}
void main_count_edges() {
cout << "\n\nList of All Edges:\n";
for (pair <string, string> x : all_edges) {
cout << " Nodes: { " << x.first << ' ' << x.second << " }";
if (weighted) {
cout << ", Weight: " << all_weights[make_pair(x.first, x.second)] << '\n';
}
}
cout << "\nCurrent total edge count (size): " << all_edges.size() << '\n';
}
void main_count_neighbors() {
string temp;
while (true) {
cout << "\nEnter a target node to find its neighbors:\n";
getline(cin, temp);
if (all_vertices.find(temp) != all_vertices.end()) {
cout << "List of All Neighbors of Node " << temp << ":\n";
for (string x : all_graph[temp]) {
cout << x << ' ';
}
cout << "\nCurrent total neighbor count (degree): " << all_graph[temp].size() << '\n';
break;
}
else { cout << "\n\"" << temp << "\" is an invalid node! Attempt aborted.\n"; }
}
}
void main_count_degree() {
string temp;
while (true) {
cout << "\nEnter a target degree to find nodes with that given neighbor count:\n";
getline(cin, temp);
if (is_integer(temp) && (stoi(temp) >= 0)) { break; }
else { cout << "\n\"" << temp << "\" is an invalid depth! Attempt aborted.\n"; }
}
int count_degree = 0;
cout << "\nList of All Vertices of Degree " << temp << ":\n";
for (string x : all_vertices) {
if (all_graph[x].size() == stoi(temp)) {
cout << x << ' ';
count_degree++;
}
}
cout << "\nCount of all vertices of degree " << temp << ": " << count_degree << '\n';
}
void main_count_root_depth() {
string root, target;
while (true) {
cout << "\nEnter a node to serve as the root of the current graph:\n";
getline(cin, root);
if (all_vertices.find(root) != all_vertices.end()) { break; }
else { cout << "\n\"" << root << "\" is an invalid node! Attempt aborted.\n"; }
}
while (true) {
cout << "\nEnter a target depth to find nodes from the root:\n";
getline(cin, target);
if (is_integer(target) && (stoi(target) >= 0)) { break; }
else { cout << "\n\"" << target << "\" is an invalid depth! Attempt aborted.\n"; }
}
int count_depth = 0;
map <string, int> depth;
for (string x : all_vertices) { depth[x] = -1; }
queue <string> q;
depth[root] = 0;
q.push(root);
while (!q.empty()) {
string u = q.front();
q.pop();
for (string v : all_graph[u]) {
if (depth[v] == -1) {
depth[v] = depth[u] + 1;
q.push(v);
}
}
}
cout << "\nList of All Vertices of Depth " << target << " from Root " << root << ":\n";
for (string x : all_vertices) {
if (depth[x] == stoi(target)) {
count_depth++;
cout << x << ' ';
}
}
cout << "\nCount of all vertices of depth " << target << "from root " << root << ": " << count_depth << '\n';
}