C++實現有向圖的鄰接表表示
阿新 • • 發佈:2020-04-27
本文例項為大家分享了C++有向圖的鄰接表表示,供大家參考,具體內容如下
一、思路:
有向圖的插入有向邊、刪除邊、刪除頂點和無向圖的有區別。其他的和無向圖的類似。
1.插入有向邊<e1,e2>
只需要插入<e1,e2>邊就行,不需要插入對稱邊<e2,e1>
2.刪除邊<e1,e2>:
只需要刪除<e1,e2>邊就行,不需要仔找對稱邊<e2,e1>進行刪除。
3.刪除頂點v:
首先,要在鄰接表中刪除以v為頭的邊<v,w>;
同時,也要在鄰接表中刪除以v為尾的邊<k,v>,不能通過對稱邊來找,只能一個個頂點找,浪費時間。
二、實現程式:
1.DirectedGraph.h:有向圖
#ifndef DirectedGraph_h #define DirectedGraph_h #include <iostream> using namespace std; const int DefaultVertices = 30; template <class T,class E> struct Edge { // 邊結點的定義 int dest; // 邊的另一頂點位置 E cost; // 表上的權值 Edge<T,E> *link; // 下一條邊鏈指標 }; template <class T,class E> struct Vertex { // 頂點的定義 T data; // 頂點的名字 Edge<T,E> *adj; // 邊連結串列的頭指標 }; template <class T,class E> class Graphlnk { public: const E maxValue = 100000; // 代表無窮大的值(=∞) Graphlnk(int sz=DefaultVertices); // 建構函式 ~Graphlnk(); // 解構函式 void inputGraph(); // 建立鄰接表表示的圖 void outputGraph(); // 輸出圖中的所有頂點和邊資訊 T getValue(int i); // 取位置為i的頂點中的值 E getWeight(int v1,int v2); // 返回邊(v1, v2)上的權值 bool insertVertex(const T& vertex); // 插入頂點 bool insertEdge(int v1,int v2,E weight); // 插入邊 bool removeVertex(int v); // 刪除頂點 bool removeEdge(int v1,int v2); // 刪除邊 int getFirstNeighbor(int v); // 取頂點v的第一個鄰接頂點 int getNextNeighbor(int v,int w); // 取頂點v的鄰接頂點w的下一鄰接頂點 int getVertexPos(const T vertex); // 給出頂點vertex在圖中的位置 int numberOfVertices(); // 當前頂點數 private: int maxVertices; // 圖中最大的頂點數 int numEdges; // 當前邊數 int numVertices; // 當前頂點數 Vertex<T,E> * nodeTable; // 頂點表(各邊連結串列的頭結點) }; // 建構函式:建立一個空的鄰接表 template <class T,class E> Graphlnk<T,E>::Graphlnk(int sz) { maxVertices = sz; numVertices = 0; numEdges = 0; nodeTable = new Vertex<T,E>[maxVertices]; // 建立頂點表陣列 if(nodeTable == NULL) { cerr << "儲存空間分配錯誤!" << endl; exit(1); } for(int i = 0; i < maxVertices; i++) nodeTable[i].adj = NULL; } // 解構函式 template <class T,E>::~Graphlnk() { // 刪除各邊連結串列中的結點 for(int i = 0; i < numVertices; i++) { Edge<T,E> *p = nodeTable[i].adj; // 找到其對應連結串列的首結點 while(p != NULL) { // 不斷地刪除第一個結點 nodeTable[i].adj = p->link; delete p; p = nodeTable[i].adj; } } delete []nodeTable; // 刪除頂點表陣列 } // 建立鄰接表表示的圖 template <class T,class E> void Graphlnk<T,E>::inputGraph() { int n,m; // 儲存頂點樹和邊數 int i,j,k; T e1,e2; // 頂點 E weight; // 邊的權值 cout << "請輸入頂點數和邊數:" << endl; cin >> n >> m; cout << "請輸入各頂點:" << endl; for(i = 0; i < n; i++) { cin >> e1; insertVertex(e1); // 插入頂點 } cout << "請輸入圖的各邊的資訊:" << endl; i = 0; while(i < m) { cin >> e1 >> e2 >> weight; j = getVertexPos(e1); k = getVertexPos(e2); if(j == -1 || k == -1) cout << "邊兩端點資訊有誤,請重新輸入!" << endl; else { insertEdge(j,k,weight); // 插入邊 i++; } } // while } // 輸出有向圖中的所有頂點和邊資訊 template <class T,E>::outputGraph() { int n,m,i; T e1,e2; // 頂點 E weight; // 權值 Edge<T,E> *p; n = numVertices; m = numEdges; cout << "圖中的頂點數為" << n << ",邊數為" << m << endl; for(i = 0; i < n; i++) { p = nodeTable[i].adj; while(p != NULL) { e1 = getValue(i); // 有向邊<i,p->dest> e2 = getValue(p->dest); weight = p->cost; cout << "<" << e1 << "," << e2 << "," << weight << ">" << endl; p = p->link; // 指向下一個鄰接頂點 } } } // 取位置為i的頂點中的值 template <class T,class E> T Graphlnk<T,E>::getValue(int i) { if(i >= 0 && i < numVertices) return nodeTable[i].data; return NULL; } // 返回邊(v1, v2)上的權值 template <class T,class E> E Graphlnk<T,E>::getWeight(int v1,int v2) { if(v1 != -1 && v2 != -1) { Edge<T,E> *p = nodeTable[v1].adj; // v1的第一條關聯的邊 while(p != NULL && p->dest != v2) { // 尋找鄰接頂點v2 p = p->link; } if(p != NULL) return p->cost; } return maxValue; // 邊(v1,v2)不存在,就存放無窮大的值 } // 插入頂點 template <class T,class E> bool Graphlnk<T,E>::insertVertex(const T& vertex) { if(numVertices == maxVertices) // 頂點表滿,不能插入 return false; nodeTable[numVertices].data = vertex; // 插入在表的最後 numVertices++; return true; } // 插入邊 template <class T,E>::insertEdge(int v1,E weight) { if(v1 >= 0 && v1 < numVertices && v2 >= 0 && v2 < numVertices) { Edge<T,E> *p = nodeTable[v1].adj; // v1對應的邊連結串列頭指標 while(p != NULL && p->dest != v2) // 尋找鄰接頂點v2 p = p->link; if(p != NULL) // 已存在該邊,不插入 return false; p = new Edge<T,E>; // 建立新結點 p->dest = v2; p->cost = weight; p->link = nodeTable[v1].adj; // 鏈入v1邊連結串列 nodeTable[v1].adj = p; numEdges++; return true; } return false; } // 有向圖刪除頂點較麻煩 template <class T,E>::removeVertex(int v) { if(numVertices == 1 || v < 0 || v > numVertices) return false; // 表空或頂點號超出範圍 Edge<T,E> *p,*s; // 1.清除頂點v的邊連結串列結點w 邊<v,w> while(nodeTable[v].adj != NULL) { p = nodeTable[v].adj; nodeTable[v].adj = p->link; delete p; numEdges--; // 與頂點v相關聯的邊數減1 } // while結束 // 2.清除<w,v>,與v有關的邊 for(int i = 0; i < numVertices; i++) { if(i != v) { // 不是當前頂點v s = NULL; p = nodeTable[i].adj; while(p != NULL && p->dest != v) {// 在頂點i的連結串列中找v的頂點 s = p; p = p->link; // 往後找 } if(p != NULL) { // 找到了v的結點 if(s == NULL) { // 說明p是nodeTable[i].adj nodeTable[i].adj = p->link; } else { s->link = p->link; // 儲存p的下一個頂點資訊 } delete p; // 刪除結點p numEdges--; // 與頂點v相關聯的邊數減1 } } } numVertices--; // 圖的頂點個數減1 nodeTable[v].data = nodeTable[numVertices].data; // 填補,此時numVertices,比原來numVertices小1,所以,這裡不需要numVertices-1 nodeTable[v].adj = nodeTable[numVertices].adj; // 3.要將填補的頂點對應的位置改寫 for(int i = 0; i < numVertices; i++) { p = nodeTable[i].adj; while(p != NULL && p->dest != numVertices) // 在頂點i的連結串列中找numVertices的頂點 p = p->link; // 往後找 if(p != NULL) // 找到了numVertices的結點 p->dest = v; // 將鄰接頂點numVertices改成v } return true; } // 刪除邊 template <class T,E>::removeEdge(int v1,E> * p = nodeTable[v1].adj,*q = NULL; while(p != NULL && p->dest != v2) { // v1對應邊連結串列中找被刪除邊 q = p; p = p->link; } if(p != NULL) { // 找到被刪除邊結點 if(q == NULL) // 刪除的結點是邊連結串列的首結點 nodeTable[v1].adj = p->link; else q->link = p->link; // 不是,重新連結 delete p; return true; } } return false; // 沒有找到結點 } // 取頂點v的第一個鄰接頂點 template <class T,class E> int Graphlnk<T,E>::getFirstNeighbor(int v) { if(v != -1) { Edge<T,E> *p = nodeTable[v].adj; // 對應連結串列第一個邊結點 if(p != NULL) // 存在,返回第一個鄰接頂點 return p->dest; } return -1; // 第一個鄰接頂點不存在 } // 取頂點v的鄰接頂點w的下一鄰接頂點 template <class T,E>::getNextNeighbor(int v,int w) { if(v != -1) { Edge<T,E> *p = nodeTable[v].adj; // 對應連結串列第一個邊結點 while(p != NULL && p->dest != w) // 尋找鄰接頂點w p = p->link; if(p != NULL && p->link != NULL) return p->link->dest; // 返回下一個鄰接頂點 } return -1; // 下一個鄰接頂點不存在 } // 給出頂點vertex在圖中的位置 template <class T,E>::getVertexPos(const T vertex) { for(int i = 0; i < numVertices; i++) if(nodeTable[i].data == vertex) return i; return -1; } // 當前頂點數 template <class T,E>::numberOfVertices() { return numVertices; } #endif /* DirectedGraph_h */
2.main.cpp
/* 測試資料: 5 7 0 1 2 3 4 0 1 10 0 3 30 0 4 100 1 2 50 2 4 10 3 2 20 3 4 60 */ #include "DirectedGraph.h" int main(int argc,const char * argv[]) { Graphlnk<char,int> st; // 宣告物件 bool finished = false; int choice; char e1,e2,next; int weight; while(!finished) { cout << "[1]建立基於鄰接表的有向圖" << endl; cout << "[2]輸出圖的所有頂點和邊資訊" << endl; cout << "[3]取頂點v的第一個鄰接頂點" << endl; cout << "[4]取v的鄰接頂點w的下一個鄰接頂點" << endl; cout << "[5]插入頂點" << endl; cout << "[6]插入邊" << endl; cout << "[7]刪除頂點" << endl; cout << "[8]刪除邊" << endl; cout << "[9]退出" << endl; cout << "請輸入選擇[1-9]:"; cin >> choice; switch(choice) { case 1: st.inputGraph(); break; case 2: st.outputGraph(); break; case 3: cout << "請輸入頂點:"; cin >> e1; e2 = st.getValue(st.getFirstNeighbor(st.getVertexPos(e1))); if(e2) cout << "頂點" << e1 << "的第一個鄰接頂點為:" << e2 << endl; else cout << "頂點" << e1 << "沒有鄰接頂點!" << endl; break; case 4: cout << "請輸入頂點v和鄰接頂點w:"; cin >> e1 >> e2; next = st.getValue(st.getNextNeighbor(st.getVertexPos(e1),st.getVertexPos(e2))); if(next) cout << "頂點" << e1 << "的鄰接頂點" << e2 << "的下一個鄰接頂點為:" << next << endl; else cout << "頂點" << e1 << "的鄰接頂點" << e2 << "沒有下一個鄰接頂點!" << endl; break; case 5: cout << "請輸入要插入的頂點:"; cin >> e1; if(st.insertVertex(e1)) cout << "插入成功!" << endl; else cout << "表已滿,插入失敗!" << endl; break; case 6: cout << "請輸入要插入的邊的資訊:" << endl; cin >> e1 >> e2 >> weight; st.insertEdge(st.getVertexPos(e1),st.getVertexPos(e2),weight); break; case 7: cout << "請輸入要刪除的頂點:"; cin >> e1; if(st.removeVertex(st.getVertexPos(e1))) cout << "頂點" << e1 << "已刪除!" << endl; else cout << "頂點" << e1 << "不在圖中!" << endl; break; case 8: cout << "請輸入要刪除的邊的兩個端點:" << endl; cin >> e1 >> e2; st.removeEdge(st.getVertexPos(e1),st.getVertexPos(e2)); break; case 9: finished = true; break; default: cout << "選擇輸入錯誤,請重新輸入!" << endl; } } return 0; }
測試結果:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。