Saturday, June 6, 2015

BFS and DFS with MultiMap in C++


#include <iostream>
#include <stack>
#include <queue>
#include <map>
#include <string>

using namespace std;

template <typename type>
class graph {
    private:
        multimap<type, type> adj;
        
    public:
        void insert(type, type);
        void DFS(type);
        void BFS(type);
        void display();
};

template <typename type>
void graph<type>::insert(type e1, type e2) {
    adj.insert(make_pair(e1,e2));
    // for un-directed graphs
    adj.insert(make_pair(e2,e1));
    
}
template <typename type>
void graph<type>::display() {
    typename multimap<type, type>::iterator i;

    for(i = adj.begin(); i != adj.end();i = adj.upper_bound(i->first)) { 
        cout<<i->first;
        pair<typename multimap<type, type>::iterator, typename multimap<type, type>::iterator> val;
        val = adj.equal_range(i->first);
        typename multimap<type, type>::iterator j;
        for(j = val.first; j != val.second; j++) {
            cout<<"->"<<j->second;
        }
        cout<<endl;
    }
}

template <typename type>
void graph<type>::DFS(type dd) {
    map<type, bool> visited;
    typename multimap<type, type>::iterator i;
    stack<type> ss;
    type v;
    
    for (i = adj.begin(); i != adj.end();i = adj.upper_bound(i->first)) {
        visited[i->first] = false;
    }
    cout<<"DFS : ";
    ss.push(dd);
    while (!ss.empty()) {
        v = ss.top();
        ss.pop();
        if (visited[v]) continue;
        cout<<v<<"->";
        visited[v] = true;
        pair<typename multimap<type, type>::iterator, typename multimap<type, type>::iterator> val;
        val = adj.equal_range(v);
        for(i = val.first; i != val.second; i++) {
            ss.push(i->second);
        }
    }
    cout<<"NULL"<<endl;
}

template <typename type>
void graph<type>::BFS(type dd) {
    map<type, bool> visited;
    typename multimap<type, type>::iterator i;
    queue<type> qq;
    type v;
    
    for (i = adj.begin(); i != adj.end();i = adj.upper_bound(i->first)) {
        visited[i->first] = false;
    }
    cout<<"BFS : ";
    qq.push(dd);
    cout<<dd<<"->";
    visited[dd] = true;
    while (!qq.empty()) {
        v = qq.front();
        qq.pop();
        pair<typename multimap<type, type>::iterator, typename multimap<type, type>::iterator> val;
        val = adj.equal_range(v);
        for(i = val.first; i != val.second; i++) {
            if (visited[i->second]) continue;
            qq.push(i->second);
            cout<<i->second<<"->";
            visited[i->second] = true;
        }
    }
    cout<<"NULL"<<endl;
}


int main() {
    graph<int> g;
    
    g.insert(1,2); g.insert(1,7); g.insert(1,8);
    g.insert(2,3); g.insert(2,6);
    g.insert(3,4); g.insert(3,5);
    g.insert(8,9); g.insert(8,12);
    g.insert(9,10); g.insert(9,11);
    g.display();
    g.DFS(1);
    g.BFS(1);
    return 0;
}

-----


++++ Output ++++
1->2->7->8
2->1->3->6
3->2->4->5
4->3
5->3
6->2
7->1
8->1->9->12
9->8->10->11
10->9
11->9
12->8
DFS : 1->8->12->9->11->10->7->2->6->3->5->4->NULL
BFS : 1->2->7->8->3->6->9->12->4->5->10->11->NULL

--------------------------------
Process exited after 0.02219 seconds with return value 0
Press any key to continue . . .

No comments:

Post a Comment