|
@@ -74,29 +74,14 @@ void Graph::remove_edge(Node* begin, Node* end)
|
|
|
}
|
|
|
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-int get_number_from_string(int start, string word) {
|
|
|
- int dec = 1, res = 0;
|
|
|
- while (start>=0 && word[start] >= 48 && word[start] <= 57 ) {
|
|
|
- res += ((word[start] - 48) * dec);
|
|
|
- dec *= 10;
|
|
|
- start--;
|
|
|
- }
|
|
|
- return res;
|
|
|
-
|
|
|
-
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
void Graph::read_file(string file_name)
|
|
|
{
|
|
|
|
|
|
ifstream input;
|
|
|
input.open(file_name);
|
|
|
- map <string, Node> saved;
|
|
|
if (input.is_open()) {
|
|
|
string line;
|
|
|
+
|
|
|
getline(input, line);
|
|
|
while (getline(input, line)) {
|
|
|
string fir = "", sec = "";
|
|
@@ -112,14 +97,22 @@ void Graph::read_file(string file_name)
|
|
|
sec += line[splitter];
|
|
|
splitter++;
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
|
|
|
|
|
|
saved[fir] = taken_names[fir] ? saved[fir] : Node(fir);
|
|
|
saved[sec] = taken_names[sec] ? saved[sec] : Node(sec);
|
|
|
|
|
|
-
|
|
|
- add_node(&saved[fir]);
|
|
|
- add_node(&saved[sec]);
|
|
|
+ try {
|
|
|
+ add_node(&saved[fir]);
|
|
|
+ }
|
|
|
+ catch (Graph_exception & ex) {}
|
|
|
+ try {
|
|
|
+ add_node(&saved[sec]);
|
|
|
+ }
|
|
|
+ catch (Graph_exception & ex) {}
|
|
|
|
|
|
add_edge(&saved[fir], &saved[sec]);
|
|
|
|
|
@@ -130,6 +123,70 @@ void Graph::read_file(string file_name)
|
|
|
}
|
|
|
|
|
|
}
|
|
|
+ input.close();
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+void Graph::write_graph(vector <vector <node_iterator> > ind)
|
|
|
+{
|
|
|
+ int size = ind.size();
|
|
|
+ for (int i = 0; i < size; i++) {
|
|
|
+
|
|
|
+ string c_name = "graph";
|
|
|
+ c_name += (i + 49);
|
|
|
+ c_name += ".txt";
|
|
|
+
|
|
|
+ ofstream out(c_name);
|
|
|
+ for (int j = 0; j < ind[i].size(); j++) {
|
|
|
+ Node * const cur = *ind[i][j];
|
|
|
+ node_iterator it = cur->neighboors.begin();
|
|
|
+ while (it != cur->neighboors.end()) {
|
|
|
+ Node* const neighb = *it;
|
|
|
+ out << cur->name << '\t' << neighb->name << '\n';
|
|
|
+ it++;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ out.close();
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+void Graph::find_independent_graphs()
|
|
|
+{
|
|
|
+ int size = nodes.size();
|
|
|
+ vector <vector <node_iterator> > independent;
|
|
|
+ set<Node*> working_nodes = nodes;
|
|
|
+
|
|
|
+
|
|
|
+ for (int i = 0; i <size; i++) {
|
|
|
+
|
|
|
+ set<Node*>::iterator it = working_nodes.begin(), compared = working_nodes.begin();
|
|
|
+ vector<node_iterator> tmp_nodes;
|
|
|
+ tmp_nodes.push_back(nodes.find(*it));
|
|
|
+
|
|
|
+ BFS fs(*this);
|
|
|
+ compared++;
|
|
|
+ while (compared != working_nodes.end()) {
|
|
|
+
|
|
|
+ if(fs.connected(*it, *compared))
|
|
|
+ tmp_nodes.push_back(nodes.find(*compared));
|
|
|
+ compared++;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ independent.push_back(tmp_nodes);
|
|
|
+
|
|
|
+ for (int i = 0; i < tmp_nodes.size(); i++) {
|
|
|
+ working_nodes.erase(working_nodes.find(*tmp_nodes[i]));
|
|
|
+ size--;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ write_graph(independent);
|
|
|
+
|
|
|
|
|
|
}
|
|
|
|