Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Kuat Hadianto
- // 1.7.2015
- #include <iostream>
- #include <map>
- #include <vector>
- using namespace std;
- // Delete node from a graph
- map<int, vector<int> > deleteNode(map<int, vector<int> > graph, int toBeDeleted) {
- map<int, vector<int> > output;
- // Create a new graph without including the node "toBeDeleted"
- for (map<int, vector<int> >::iterator it = graph.begin(); it != graph.end(); it++) {
- if (it->first != toBeDeleted) {
- for (int i = 0; i < it->second.size(); i++) {
- if (it->second[i] != toBeDeleted) {
- output[it->first].push_back(it->second[i]);
- }
- }
- }
- }
- return output;
- }
- // Print graph
- void print(map<int, vector<int> > graph) {
- for (map<int, vector<int> >::iterator it = graph.begin(); it != graph.end(); it++) {
- cout << it->first << ": ";
- for (int i = 0; i < it->second.size(); i++) {
- cout << it->second[i] << " ";
- }
- cout << endl;
- }
- }
- int main() {
- map<int, vector<int> > graph;
- graph[1].push_back(2);
- graph[1].push_back(3);
- graph[2].push_back(1);
- graph[2].push_back(3);
- graph[2].push_back(4);
- graph[3].push_back(1);
- graph[3].push_back(2);
- graph[4].push_back(2);
- map<int, vector<int> > afterDelete = deleteNode(graph, 3);
- print(graph);
- cout << endl;
- print(afterDelete);
- system("PAUSE");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement