数据结构示例(七)——图

  • 格式:doc
  • 大小:36.00 KB
  • 文档页数:5

1、用邻接矩阵表示的图
const int MaxVertices = 10;
template <class T>
class Graph
{
private:
T Vertices[MaxVertices];
int Edge[MaxVertices][MaxVertices];
int n,e;
void DFS(int v, int visited[]); public:
Graph(int sz=MaxVertices);
T GetValue(int i);
int GetFirstNeighbor(int v);
int GetNextNeighbor(int v1, int v2);
void DFS();
void create();
void showMatrix();
};
template <class T>
Graph<T>::Graph(int sz)
{
for(int i=0;i<sz;i++)
for(int j=0;j<sz;j++)
Edge[i][j] = 0;
n = e = 0;
}
template <class T>
T Graph<T>::GetValue(int i)
{
if(i<0 || i>=n) exit(1);
return Vertices[i];
}
template <class T>
int Graph<T>::GetFirstNeighbor(int v) {
if(v<0 || v>=n) exit(1);
for(int i=0; i<n; i++)
if(Edge[v][i]==1) return i;
return -1;
}
template <class T>
int Graph<T>::GetNextNeighbor(int v1, int v2) {
if(v1<0 || v1>=n || v2<0 || v2>=n ) exit(1); for(int i=v2+1; i<n; i++)
if(Edge[v1][i]==1) return i;
return -1;
}
template <class T>
void Graph<T>::DFS(int v, int visited[])
{
cout << GetValue(v)<<' ';
visited[v] = 1;
int w = GetFirstNeighbor(v);
while(w!=-1)
{
if(!visited[w]) DFS(w,visited);
w = GetNextNeighbor(v,w);
}
}
template <class T>
void Graph<T>::DFS()
{
int visited[MaxVertices]={0};
for(int i=0;i<n;i++)
if(!visited[i]) DFS(i,visited);
}
template <class T>
void Graph<T>::create()
{
int i,j,k;
cout<<"请输入顶点数和边数:";
cin>>n>>e;
cout<<"请输入"<<n<<"个顶点的值:"; for(i=0; i<n; i++)
cin>>Vertices[i];
for (i=0; i<n; i++ )
for (j=0; j<n; j++)
Edge[i][j]=0;
cout<<"请输入"<<e<<"条边:";
for (k=1; k<=e; k++)
{ cin>>i>>j;
Edge[i][j]=1;
Edge[j][i]=1;
}
}
template <class T>
void Graph<T>::showMatrix()
{
cout<<setw(3)<<' ';
for (int i=0; i<n; i++ )
cout<<setw(3)<<Vertices[i]; cout<<endl;
for (int i=0; i<n; i++ )
{
cout<<setw(3)<<Vertices[i];
for (int j=0; j<n; j++)
cout<<setw(3)<<Edge[i][j];
cout<<endl;
}
}
2、用邻接表表示的图
const int MaxVertices = 10;
struct Edge
{ int dest;
Edge *next;
};
template <class T>
struct Vertex
{ T data;
Edge *adj;
}
template <class T>
class Graph
{
private:
vertex<T> Vertices[MaxVertices];
int n,e;
void DFS(int v, int visited[]);
public:
Graph(int sz=MaxVertices);
T GetValue(int i);
int GetFirstNeighbor(int v);
int GetNextNeighbor(int v1, int v2);
void DFS();
};
template <class T>
Graph<T>::Graph()
{
for(int i=0;i<MaxVertices;i++)
V ertices[i].adj = NULL;
n = e = 0;
}
T Graph<T>::GetValue(int i)
{
if(i<0 || i>=n) exit(1);
return Vertices[i].data;
}
template <class T>
int Graph<T>::GetFirstNeighbor(int v)
{
if(v<0 || v>=n) exit(1);
Edge *p = Vertices[v].adj;
if(p!=NULL) return p->dest;
else return -1;
}
template <class T>
int Graph<T>::GetNextNeighbor(int v1, int v2); {
if(v1<0 || v1>=n || v2<0 || v2>=n ) exit(1); while(p!=NULL)
if(p->dest == v2 && p->next!=NULL)
return p->next->dest;
else return -1;
}
template <class T>
void Graph<T>::DFS(int v, int visited[])
{
cout << GetValue(v)<<' '; visited[v] = 1;
int w = GetFirstNeighbor(v); while(w!=-1)
{
if(!visited[w]) DFS(w,visited);
w = GetNextNeighbor(v,w); }
}
template <class T>
void Graph<T>::DFS()
{
int visited[MaxVertices]={0}; for(int i=0;i<n;i++)
if(!visited[i]) DFS(i,visited); }
作者:Leida。