implement a undirected graph use adjacent list
来源:互联网 发布:国家数据安全标准 编辑:程序博客网 时间:2024/05/21 17:24
I had wire a undirectded graph use adjacent matrix,have a look ^_^ ...
http://blog.csdn.net/Non_Recursive/archive/2008/11/21/3345183.aspx
and this a undirected graph represent by adjacent list..
- //AdjListGraph.h
- #include<iostream>
- using namespace std;
- const int maxVertex = 1000; //infinity number
- const int maxWeight = 10000; //infinity number
- const int defaultVertices = 30; //default the biggest number of vertices (=n)
- template<typename T,typename E>
- struct Edge //define Edge node
- {
- int dest; //the other vertex of edge
- E cost; //the weight of edge
- Edge<T,E>* link; //the next pointer link to edge
- Edge(){};
- Edge(int num,E weight) : dest(num),cost(weight),link(NULL){}
- bool operator !=(Edge<T,E>& R)const
- {
- return (dest != R.dest) ? true : false;
- }
- };
- template<typename T,typename E>
- struct Vertex //define vertex
- {
- T data; //name of vertex
- Edge<T,E> *head; //Edge List's head pointer
- };
- template<typename T,typename E>
- class AdjListGraph
- {
- Vertex<T,E>* NodeTable; //node table
- int maxVertices;
- int numEdges,numVertices; //current number of edge and vertex
- T *VerticesList; //vertex table
- //E **Edge; //Adjacent matrix
- int GetVertexPos(const T vertex)
- {
- for (int i=0;i<numVertices;i++)
- if (NodeTable[i].data == vertex) return i;
- return -1;
- }
- friend istream& operator >>(istream& in,AdjListGraph<T,E>& G);
- friend ostream& operator <<(ostream& out,AdjListGraph<T,E>& G);
- public:
- AdjListGraph(int sz=defaultVertices);
- ~AdjListGraph();
- T GetValue(int i)
- {
- return (i>=0 && i<numVertices) ? NodeTable[i].data : 0;
- }
- E GetWeight(int v1,int v2);
- bool InsertVertex(const T& vertex);
- bool RemoveVertex(int v);
- bool InsertEdge(int v1,int v2,E cost);
- bool RemoveEdge(int v1,int v2);
- int GetFirstNeighbor(int v);
- int GetNextNeighbor(int v,int w);
- };
- //AdjListGraph.cpp
- #include "AdjListGraph.h"
- template<typename T,typename E>
- AdjListGraph<T,E>::AdjListGraph(int sz)
- {
- maxVertices = sz,numVertices = numEdges = 0;
- NodeTable = new Vertex<T,E>[maxVertices]; //create node table array
- if (NodeTable == NULL) exit(1); //allocation error
- for(int i=0;i<maxVertices;i++)
- NodeTable[i].head = NULL;
- }
- template<typename T,typename E>
- AdjListGraph<T,E>::~AdjListGraph()
- {
- for (int i=0;i<numVertices;i++)
- {
- Edge<T,E>* p = NodeTable[i].head; //find the head node
- while (p != NULL)
- {
- NodeTable[i].head = p->link; //delete first node
- delete p;
- p = NodeTable[i].head;
- }
- }
- delete []NodeTable; //delete vertex table array
- }
- template<typename T,typename E>
- int AdjListGraph<T,E>::GetFirstNeighbor(int v)
- {
- if (v != -1)
- {
- Edge<T,E>* p = NodeTable[v].head; //the first edge node
- if (p != NULL) return p->dest; //return the first adjacent node
- }
- return -1; //there is not first adjacent node
- }
- template<typename T,typename E>
- int AdjListGraph<T,E>::GetNextNeighbor(int v,int w)
- {
- //find w's adjacent node,which 'w' is a adjacent node of 'v'
- if (v != -1)
- {
- Edge<T,E>* p = NodeTable[v].head; //对应于边链表第一个边结点...
- while (p != NULL && p->dest != w) //try to find v's adjacent node 'w'
- p = p->link;
- if (p != NULL && p->link != NULL)
- return p->link->dest; //return nest adjacent node
- }
- return -1; //there is not "next adjacent node"
- }
- template<typename T,typename E>
- E AdjListGraph<T,E>::GetWeight(int v1,int v2)
- {
- if ((v1 != -1) && (v2 != -1))
- {
- Edge<T,E>* p = NodeTable[v1].head; //first edge related to 'v1'
- while (p!=NULL && p->dest!=v2) p = p->link;
- if (p != NULL) return p->cost;
- }
- return 0;
- }
- template<typename T,typename E>
- bool AdjListGraph<T,E>::InsertVertex(const T& vertex)
- {
- if (numVertices == maxVertex) return false; //node table have full
- NodeTable[numVertices++].data = vertex;
- return true;
- }
- template<typename T,typename E>
- bool AdjListGraph<T,E>::RemoveVertex(int v)
- {
- if (numVertices==1 || v<0 || v>=maxVertices) return false; //empty table or ...
- Edge<T,E>* p,*s,*t;
- while (NodeTable[v].head != NULL)
- {
- p = NodeTable[v].head;
- int k = p->dest;
- s = NodeTable[k].head ; //p->dest
- t = NULL;
- while (s!=NULL && s->dest!=v)
- {
- t = s;
- s = s->link;
- }
- if (s != NULL)
- {
- if (t ==NULL) NodeTable[k].head = s->link;
- else t->link = s->link;
- delete s;
- }
- NodeTable[v].head = p->link;
- delete p;
- numEdges--;
- }
- numVertices--;
- NodeTable[v].data = NodeTable[numVertices].data; //fill
- p = NodeTable[v].head = NodeTable[numVertices].head;
- while (p!=NULL)
- {
- s = NodeTable[p->dest].head;
- while(s!=NULL)
- if (s->dest == numVertices)
- {
- s->dest = v;
- break;
- }
- else s = s->link;
- }
- return true;
- }
- template<typename T,typename E>
- bool AdjListGraph<T,E>::InsertEdge(int v1,int v2,E weight)
- {
- if ((v1>=0 && v1<numVertices) && (v2>=0 && v2<numVertices))
- {
- Edge<T,E>* q,*p = NodeTable[v1].head;
- while (p!=NULL && p->dest!=v2) p = p->link;
- if (p == NULL) return false; //
- p = new Edge<T,E>;
- q = new Edge<T,E>;
- p->dest = v2;
- p->cost = weight;
- p->link = NodeTable[v1].head; //link to 'v1' edgeLinkNode
- NodeTable[v1].head = p;
- q->dest = v1;
- q->weight = weight;
- q->link = NodeTable[v2].head; //link to 'v2' edgeLinkNode
- NodeTable[v2].head = q;
- numEdges++;
- return true;
- }
- return 0;
- }
- template<typename T,typename E>
- bool AdjListGraph<T,E>::RemoveEdge(int v1,int v2)
- {
- if ((v1 != -1) && (v2 != -1))
- {
- Edge<T,E>* p = NodeTable[v1].head,*q = NULL,*s = p;
- while (p!=NULL && p->dest!=v2) //v1对应边中找删除边
- {
- q = p;
- p = p->link;
- }
- if (p!=NULL) //fimd the edge prepare to delete
- {
- if (p == s)NodeTable[v1].head = p->link; //first node of edgeLinkTable
- else q->link = p->link; //else reconnection
- delete p;
- }
- else return false; //can't find the edge to remove
- p = NodeTable[v2].head;
- q = NULL,s = p; //v2对应边中找删除边
- while (p->dest != v1)
- {
- q = p;
- p = p->link;
- }
- if (p == s)NodeTable[v2].head = p->link;
- else q->link = p->link;
- delete p;
- return true;
- }
- return false;
- }
- template<typename T,typename E>
- istream& operator >>(istream& in,AdjListGraph<T,E>& G)
- {
- int n,m;
- T v1,v2;
- E weight;
- cout<<"please the numbers of vertex and edge:";
- cin>>n>>m; //input vertices number,edges number;
- for (int i=0;i<n;i++) //create data of vertex table
- {
- in>>v1;
- G.InsertVertex(v1);
- }
- int j=0;
- while (j<m)
- {
- in>>v1>>v2>>weight; //input information
- int pos1 = G.GetVertexPos(v1);
- int pos2 = G.GetVertexPos(v2);
- if (pos1==-1 || pos2==-1) cout<<"Error,please input again"<<endl;
- else j++,G.InsertEdge(pos1,pos2,weight);
- }
- return in;
- }
- template<typename T,typename E>
- ostream& operator <<(ostream& out,AdjListGraph<T,E>& G)
- {
- //out all the information of graph's vertices and edges
- int n = G.NumberOfVertices(),m = G.NumberOfEdges();
- cout<<"number of vertices is "<<n<<", number of edges if "<<m<<endl;
- for (int i=0;i<n;i++)
- for (int j=i+1;j<n;j++)
- {
- int w = G.GetWeight(i,j);
- if (w>0 && w<maxWeight)
- {
- int e1 = G.GetValues(i),e2 = G.GetValue(j);
- out<<"("<<e1<<","<<e2<<","<<w<<")"<<endl;
- }
- }
- return out;
- }
- //main.cpp
- #include "AdjListGraph.cpp"
- int main(void) //a simple test
- {
- AdjListGraph<int,int> AdjListG(10);
- return 0;
- }
- implement a undirected graph use adjacent list
- Implement a undirected graph,represent by a adjacent matrix
- Practice_5: Adjacent List Graph
- HDU5333 Undirected Graph
- HDU 5333 Undirected Graph
- Undirected Graph BFS/DFS
- judge loop in undirected graph
- hdu 5333 Undirected Graph (LCT)
- connect components in undirected graph
- Connected Component in Undirected Graph
- Geeks - Union-Find Algorithm - Detect Cycle in a an Undirected Graph算法
- Use List to implement the Video play list
- Sicily 4378. connect components in undirected graph
- Detect cycle in an undirected graph
- Sicily 1002. connected components in undirected graph
- 【HDU】5333 Undirected Graph【LCT+BIT】
- 【动态树】 HDOJ 5333 Undirected Graph
- HDU 5333 Undirected Graph LCT+BIT
- Tuning Up ADO.NET Connection Pooling in ASP.NET Applications
- 开通博客前言
- 反射
- 蛇形矩阵的各种情形
- 第一期
- implement a undirected graph use adjacent list
- 【seo知识】用户的搜索意图(intention of SE users)
- 对父类和接口的猜想
- linux相关的网络配置文件
- JavaScript中 使用正则表达式 取得字符串所占字节数
- 社交时人人都对你刮目相看
- 解决模态窗体中出现服务器端交互就弹出新窗口的方案
- event.keycode值大全
- 《信息网络传播权保护条例》