C++演算法從入門到放棄-無向圖(1)

來源:互聯網
上載者:User

April 8, 2016

API 圖的表示 圖的資料結構 實現

API

class Graph{private:    int _v;    int _e;    std::map<int, std::vector<int>> adjMartix;public:    using Iterator = std::map<int, std::vector<int>>::iterator;    using Const_Iterator = std::map<int, std::vector<int>>::const_iterator;    Graph() = delete;    Graph(int V):_v(V), _e(0)    {        std::vector<int> temp;        for(int i = 0; i < V; ++i){            this->adjMartix.insert(std::pair<int, std::vector<int>>(i, temp));        }    }    Graph(const Graph& other);    Graph& operator=(const Graph& other);    int V() { return _e; }    int E() { return _e; }    void addEdge(int v, int w);    std::vector<int> adj(int v);    int degree(int v);    int maxDegree();    double avgDegree();    int numberOfselfLoops();    std::string toString(std::ostream& os);};
圖的表示 鄰接矩陣。不能滿足儲存平行邊的需求,放棄。
bool edge[M][N];
邊的數組。如果要找出和某一頂點相鄰的所有頂點那麼就要遍整個數組,放棄。
struct Edge{    int v;    int w;};std::vector<Edge> allEdge;
鄰接表數組滿足需求而且空間要求僅為頂點數的兩倍(不考慮順序容器的開銷)。
std::map<int,std::vector<int>> allEdge;
圖的資料結構

使用鄰接表數組表示的圖有如下特點: 使用的空間和V+E成正比 添加一條邊所需要的時間為常數 遍曆頂點v的所有相鄰頂點所需的時間和v的度數成正比(處理每個相鄰頂點所需要的時間為常數) 實現

Graph::Graph(const Graph & other){    this->_v = other._v;    this->_e = other._e;    this->adjMartix = other.adjMartix;}Graph & Graph::operator=(const Graph & other){    this->_v = other._v;    this->_e = other._e;    this->adjMartix = other.adjMartix;    return *this;}void Graph::addEdge(int v, int w){    Graph::Iterator it = this->adjMartix.find(v);    (*it).second.push_back(w);    ++(this->_e);}std::vector<int> Graph::adj(int v){    return (this->adjMartix).find(v)->second;}int Graph::degree(int v){    Graph::Const_Iterator cit = this->adjMartix.find(v);    return (*cit).second.size();}int Graph::maxDegree(){    unsigned max = 0;    for each (auto var in this->adjMartix)    {        if(var.second.size() > max)            max = var.second.size();    }    return max;}double Graph::avgDegree(){    return E() / V()*2.0;}int Graph::numberOfselfLoops(){    int count = 0;    for(int v = 0; v < V(); v++){        for each (auto var in adj(v))        {            if(v == var)                ++count;        }    }    return count / 2;}std::string Graph::toString(std::ostream& os){    std::stringstream ss;    ss << this->_v << " vertices. " << this->_e << " edges\n";    for(int v = 0; v < (this->_v); ++v){        ss << v << ": ";        for each (auto var in this->adj(v))        {            ss << var << " ";        }        ss << "\n";    }    os << ss.str();    return ss.str();}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.