B00015 C + + implementation of graph classes

Source: Internet
Author: User

Code from: github-obscure76/graph:c++ graphs.

The contents of the Graph.h file are as follows:

#include <iostream> #include <cstring> #include <algorithm> #include <list> #include <set > #include <map> #include <queue> #include <climits> #define MAXSIZE 100using namespace Std;class    vertex{private:pair<int, list<vertex> > V;        Public:vertex () {};        Vertex (int);        BOOL Addadjvertex (int);        friend BOOL operator== (Vertex const &, Vertex const &);        friend BOOL operator< (Vertex const &, Vertex const &); friend BOOL Operator> (Vertex const &, Vertex const &);};        Class edge{Private:int weight;        int src;    int DST;        Public:edge () {};        Edge (int, int, int);        ~edge () {};        friend BOOL operator== (Edge const &, Edge const &);        friend BOOL operator< (Edge const &, Edge const &);        friend BOOL Operator> (Edge const &, Edge const &);        int Getweight ();     int GetSource ();   int getdest ();};        Class graph{Private:map<int, list<int> > vertices;        List<edge> edges;    Set<int> visited;        Public:graph ();        BOOL Addvertex (int);        BOOL Addedge (int, int, int);        BOOL Deletevertex (int);        void print ();        void BFs ();        void Dfstraversal ();        void Dfs (int, list<int>);        void pathsum (int);        void Findpath (int, int, char *, int, int);        void Bellman ();        List<int> Topologicalsort (); void Topo ();};

The contents of the Graph.cpp file are as follows:

#include <graph.h>vertex::vertex (int v) {}bool Vertex::addadjvertex (int v) {}list<int> Order;bool operator = = (Vertex const &AMP;V1, Vertex const &AMP;V2) {return true;} BOOL operator< (Vertex const &AMP;V1, Vertex const &AMP;V2) {return true;} BOOL Operator> (Vertex const &AMP;V1, Vertex const &AMP;V2) {return true;}    Edge::edge (int A, int s1, int s2) {weight = A;    src = S1; DST = s2;} int Edge::getweight () {return weight;} int Edge::getsource () {return src;} int Edge::getdest () {return DST;}  BOOL operator== (Edge const &AMP;E1, Edge Const & E2) {if (E1.weight==e2.weight && e1.src==e2.src &&    E1.DST = = E2.DST) return true; return false;}    Graph::graph () {}bool Graph::addvertex (int v) {list<int> adjlist;    Vertices.insert (Pair<int, list<int> > (v, adjlist));    }bool graph::addedge (int wt, int s, int d) {edges.push_back (Edge (WT, S, D));    Map<int, list<int> >::iterator sit = Vertices.find (s); if (sit!=Vertices.end ()) Sit->second.push_back (d);        else {list<int> adjlist;        Adjlist.push_back (d);    Vertices.insert (Pair<int, list<int> > (S, adjlist));    } sit = Vertices.find (d);        if (sit = = Vertices.end ()) {list<int> adjList1;    Vertices.insert (Pair<int, list<int> > (d, AdjList1));    }}bool graph::d eletevertex (int v) {}void Graph::p rint () {map<int, list<int> >::iterator it;        for (it = Vertices.begin (); It!=vertices.end (); it++) {cout<< "Node" <<it->first<< '-'; for (List<int>::iterator lit = It->second.begin (); Lit!=it->second.end (); lit++) cout<<*lit<        < ', ';    cout<<endl;    }}void Graph::bfs () {queue<int> q;    Visited.clear ();    Q.push (Vertices.begin ()->first);    Visited.insert (Vertices.begin ()->first);    cout<< "\ n BFS traversal:"; while (Q.size () > 0) {int v = Q.front();        Q.pop ();        list<int> adjlist = Vertices.find (v)->second;        List<int>::iterator it;        Set<int>::iterator sit;            for (it = Adjlist.begin (); It!=adjlist.end (); it++) {sit = Visited.find (*it);            if (Sit!=visited.end ()) continue;            Visited.insert (*it);        Q.push (*it);    } cout<<v<< "";    }}void Graph::d fstraversal () {visited.clear ();    cout<< "\NDFS traversal:";    Map<int, list<int> >::iterator it;            for (it = Vertices.begin (); It!=vertices.end (); it++) {if (Visited.find (it->first)! = Visited.end ())        Continue        Visited.insert (It->first);    DFS (It->first, it->second); } Cout<<endl;}    void Graph::d fs (int v, list<int> adj) {list<int>::iterator lit;        for (lit = Adj.begin (); Lit!=adj.end (); lit++) {if (Visited.find (*lit)! = Visited.end ()) continue; ViSited.insert (*lit);    DFS (*lit, Vertices.find (*lit)->second);    } cout<<v<< '; Order.push_front (v);}    void Graph::p athsum (int sum) {char *str;    str = (char *) malloc (100);    memset (str, 0, 100);    Visited.clear ();    Map<int, list<int> >::iterator it;    it = Vertices.begin (); Findpath (sum, 0, str, 0, it->first);}    void Graph::findpath (int sum, int cval, char *str, int in, int curr) {if (Curr+cval > Sum) return;        if (Cval+curr = = sum) {Str[in] = ' 0 ' + curr;        Str[in+1] = ' + ';        cout<<str<<endl;        Visited.insert (Curr);    if (Visited.find (Curr) ==visited.end ()) Findpath (sum, 0, str, 0, Curr);        } else {cval + = Curr;        cout<< "Cval Now" <<cval<< ' t ';        Str[in] = ' 0 ' +curr;        Map<int, list<int> >::iterator it;        it = Vertices.find (Curr);        if (It==vertices.end ()) return; list<int> adj = It-> second;        for (List<int>::iterator lit = Adj.begin (); Lit!=adj.end (); lit++) Findpath (sum, Cval, str, in+1, *lit); for (List<int>::iterator lit = Adj.begin (); Lit!=adj.end (); lit++) {if (Visited.find (*lit) ==visi                Ted.end ()) {Visited.insert (*lit);            Findpath (sum, 0, str, 0, *lit);    }}}}void Graph::bellman () {order.clear ();    Dfstraversal ();    Map<int, int> VD;    Map<int, list<int> >::iterator it;    int vlen = Vertices.size ();    for (it = Vertices.begin (); It!=vertices.end (); it++) Vd.insert (Pair<int, int> (It->first, Int_max));    Vd.find (Vertices.begin ()->first)->second = 0;    List<int>::iterator lit;        For (Lit=order.begin (); Lit!=order.end (); lit++) {list<edge>::iterator it;            for (it = Edges.begin (); It!=edges.end (); it++) {int src = It->getsource (); int dst = It->getdest ();            int wt = It->getweight ();            if (src!=*lit) continue; if (Vd.find (DST)->second > Vd.find (src)->second+wt) vd.find (DST)->second = Vd.find (src)->seco        ND+WT;    }} map<int, Int>::iterator mit; for (mit = Vd.begin (); Mit!=vd.end (), mit++) cout<<mit->first<< "at Dist" <<mit->second<&lt ; Endl;}    List<int> Graph::topologicalsort () {order.clear ();    Dfstraversal ();    for (List<int>::iterator it = Order.begin (); It!=order.end (); it++) cout<<*it; return order;} void Graph::topo () {order.push_back (4);}    int main () {Graph g;    G.addedge (1,1,2);    G.addedge (1,1,3);    G.addedge (1,2,4);    G.addedge (1,2,5);    G.addedge (1,3,6);    G.addedge (1,3,8);    G.print ();    G.bfs ();    G.dfstraversal ();    G.pathsum (8);    cout<<endl;    G.topologicalsort ();    cout<<endl;    G.bellman (); return 0;}

B00015 C + + implementation of graph classes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.