?? graph.h
字號:
#include "stdio.h"
#include "assert.h"
#include "sqlist.h"
#include "minspantree.h"
extern const int MaxNumEdges;
extern const int MaxNumVertices;
template <class NameType, class DistType> class Graph { //圖的類定義
private:
SeqList<NameType> VerticesList; //頂點表
// SeqList<NameType> VerticesList ( MaxNumVertices ); //頂點表
DistType Edge[MaxNumVertices][MaxNumVertices]; //鄰接矩陣
int CurrentEdges; //當前邊數
int NumVertices; //當前頂點數
int MaxNumVertices; //最大頂點個數
int FindVertex ( SeqList<NameType> & L, NameType & vertex )
{ return L.Find (vertex); } //在頂點表L中搜索頂點vertex
int GetVertexPos ( NameType & vertex )
{ return FindVertex (VerticesList, vertex ); }
//給出頂點vertex在圖中的位置
public:
Graph ( const int sz=MaxNumVertices ); //構造函數
int GraphEmpty ( ) const { return VerticesList.IsEmpty ( ); } //判圖空否
int GraphFull ( ) const { return VerticesList.IsFull ( ) || CurrentEdges ==MaxNumEdges; }
int NumberOfVertices ( ) { return VerticesList.Length(); } //返回當前頂點數
int NumberOfEdges ( ) { return CurrentEdges; } //返回當前邊數
NameType GetValue ( const int i ) //取頂點i的值, i不合理則返回空
{ return VerticesList.Get(i); }
DistType GetWeight ( const int v1, const int v2 ); //給出以頂點v1和v2為兩端點的邊上的權值
int GetFirstNeighbor ( const int v ); //給出頂點位置為v的第一個鄰接頂點的位置
int GetNextNeighbor ( const int v1, const int v2 ); //給出頂點位置v1的某鄰接頂點v2的下一個鄰接頂點
void InsertVertex ( NameType & vertex ); //插入一個頂點vertex, 該頂點沒有入邊
void InsertEdge ( const int v1, const int v2, DistType weight ); //插入一條邊(v1, v2), 該邊上的權值為weight
void RemoveVertex ( const int v ); //在圖中刪去頂點vertex和所有與它相關聯的邊
void RemoveEdge ( const int v1, const int v2 ); //在圖中刪去邊(v1,v2)
void Prim ( MinSpanTree &T ) ;
void DFS ( );
void DFS ( const int v, int visited [ ] );
void BFS ( int v );
MinSpanTree& Kruskal ( MinSpanTree &T ); //求最小生成樹算法
friend istream& operator >>(istream& , Graph&);
friend ostream& operator <<(ostream& , Graph&);
};
template <class NameType, class DistType> Graph<NameType, DistType>::Graph ( const int sz ) :MaxNumVertices(sz),CurrentEdges(0),NumVertices(0){
//構造函數
for ( int i=0; i<sz; i++ ) //鄰接矩陣初始化
for ( int j=0; j<sz; j++ ) Edge[i][j] = 0;
// CurrentEdges = 0; //圖中當前邊數初始化
};
template <class NameType, class DistType>
DistType Graph<NameType, DistType>::GetWeight ( const int v1, const int v2 )
{
//給出以頂點v1和v2為兩端點的邊上的權值
if ( v1 != -1 && v2 != -1 ) return Edge[v1][v2];
else return NULL; //帶權圖中權值為0, 表示無權值
};
template <class NameType, class DistType>
int Graph<NameType, DistType>::GetFirstNeighbor ( const int v ) {
//給出頂點位置為v的第一個鄰接頂點的位置, 如果找不到, 則函數返回-1。
if ( v != -1 ) {
for ( int col=0; col<VerticesList.Length(); col++ ) if ( Edge[v][col] > 0 ) return col;
}
return -1;
};
template <class NameType, class DistType>
int Graph<NameType, DistType>::GetNextNeighbor ( const int v1, const int v2 ) {
//給出頂點v1的某鄰接頂點v2的下一個鄰接頂點
if ( v1 != -1 && v2 != -1 )
{
for ( int col=v2+1; col<VerticesList.Length(); col++ )
if ( Edge[v1][col] > 0 ) return col;
}
return -1;
};
template <class NameType, class DistType>
void Graph<NameType, DistType>::InsertVertex ( NameType & vertex ) //插入一個頂點vertex, 該頂點沒有入邊
{
assert (VerticesList.Insert ( vertex, VerticesList.Length() ));
};
template <class NameType, class DistType>
void Graph<NameType, DistType>:: InsertEdge ( const int v1, const int v2, DistType weight ) //插入一條邊(v1, v2), 該邊上的權值為weight
{
CurrentEdges++;
Edge[v1][v2]=weight;
};
template <class NameType, class DistType>
void Graph< NameType, DistType>::DFS ( ) { //對連通圖進行深度優先搜索的主過程
int *visited = new int [NumVertices]; //創建輔助數組
for ( int i=0; i<NumVertices; i++ ) visited [i] = 0; //輔助數組初始化
for ( i=0; i<NumVertices; i++)
if (!visited[i]) DFS (i ,visited); //從頂點0開始深度優先搜索
delete [ ] visited;
};
template <class NameType, class DistType>
void Graph< NameType, DistType>::DFS ( const int v, int visited [ ] ) { //子過程
//從頂點位置v出發, 以深度優先的次序訪問所有可讀入的尚未訪問過的頂點。算法中用到一個輔助數組
// visited, 對已訪問過的頂點作訪問標記。
cout << GetValue (v) << ' '; //訪問該頂點的數據
visited[v] = 1; //訪問標志改為已訪問過
int w = GetFirstNeighbor (v); //找頂點v的第一個鄰接頂點w
while ( w != -1 ) { //有鄰接頂點
if ( !visited[w] ) DFS ( w, visited ); //若未訪問過, 從w遞歸訪問
w = GetNextNeighbor ( v, w ); //找頂點v的下一個鄰接頂點
}
}
template <class NameType, class DistType>
istream& operator >>(istream& is, Graph<NameType,DistType>& g)
{
int n,e,k,j;
NameType head,tail,name;
DistType weight;
cout<<"please input the number of vertex"<<endl;
is >>g.NumVertices; //輸入頂點個數
cout<<"please input the name of vertex"<<endl;
for ( int i=0; i<g.NumVertices; i++)
{
is >> name;
g.InsertVertex ( name );
} //依次輸入頂點, 插入圖中
cout<<"please input the number of edges"<<endl;
is >>e; //輸入邊數
cout<<"please input edge"<<endl;
for ( i=0; i<e; i++) { //依次輸入邊信息
is >> tail >> head >> weight; //輸入各邊
k = g.GetVertexPos ( tail ); j = g.GetVertexPos ( head ); //取兩頂點位置
g.InsertEdge ( k, j, weight ); //插入圖中
}
return is;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -