#include<iostream>
using namespace std;
const int DefaultVertices=30;//最大頂點數
class Graph
{
protected:
int maxVertices;
int numEdges;
int numVertices;
int getVertexPos(char vertex);
public:
Graph()
{
maxVertices=DefaultVertices;
numVertices=0;
numEdges=0;
}
~Graph(){}
bool GraphEmpty()const{return numVertices==0;}
int NumberOfVertices(){return numVertices;}
int NumberOfEdges(){return numEdges;}
// virtual char getValue(int i);
// virtual int getFirstNeighbor(int v);
// virtual int getNextNeighbor(int v,int w);
};
//template<class T>class Graphmtx;
// template<class T>istream& operator>>(istream& in,Graphmtx<T>& G);
//template<class T>ostream& operator<<(ostream& out,Graphmtx<T>& G);
class Graphmtx:public Graph
{
//friend istream& operator>>(istream& in,Graphmtx<T>& G);
//friend ostream& operator<<(ostream& out,Graphmtx<T>& G);
private:
char *VerticlesList;//頂點表
int **Edge;//鄰接矩陣
bool *visited;
int getVertexPos(char vertex)
{
for(int i=0;i<numVertices;i++)
if(VerticlesList[i]==vertex)
return i;
return -1;
}
public:
Graphmtx();//構造函數
~Graphmtx()
{
delete []visited;
delete []VerticlesList;
for (int i=0;i<numVertices;i++)
{
delete Edge[i];
}
delete []Edge;
}
char getValue(int i)//取頂點i的值,i不合理返回空
{
//return i>=0&&i<=numVertices?VerticlesList[i]:NULL;
if (i>=0&&i<=numVertices)
{
return VerticlesList[i];
}
else
{
cout<<"i不合理返回空"<<endl;
return NULL;
}
}
int getFirstNeighbor(int v);//取頂點v的第一個鄰接頂點
int getNextNeighbor(int v,int w);//取v的鄰接頂點w的下一鄰接頂點
//bool insertVertex(const char vertex);//插入頂點vertex
// bool removeVertex(int v);//刪去頂點v和所有與它相關聯的邊
// bool removeEdge(int v1,int v2);//在圖中刪去邊(v1,v2)
void DFS (const char& v);
void DFS ( int i, bool visited[]) ;
};
Graphmtx::Graphmtx()
{
char t,t1;
int i,j,k;
cout<<"input the number for vextices and edges:"<<endl;
cin>>numVertices>>numEdges;
visited=new bool[numVertices];
for (i=0;i<numVertices;i++)
{
visited[i]=false;
}
VerticlesList=new char [numVertices];//創建頂點表
Edge=(int**) new int *[numVertices];
for(i=0;i<numVertices;i++)
Edge[i]=new int[numVertices];//鄰接矩陣
for(i=0;i<numVertices;i++)
for(j=0;j<numVertices;j++)
Edge[i][j]=0;
cout<<"input vextices"<<endl;
for(i = 0; i <numVertices; i++)
cin>>VerticlesList[i];
cout<<"輸入有連接的兩個頂點:"<<endl;
for(k=0;k<numEdges;k++)
{
cin>>t>>t1;
i=getVertexPos(t);
j=getVertexPos(t1);
Edge[i][j]=1;
Edge[j][i]=1;
}
}
int Graphmtx::getFirstNeighbor(int i)
//給出頂點位置爲v的第一個鄰接頂點的位置,如果找不到,則函數返回-1
{
if(i!=-1)
{
for(int col=0;col<numVertices;col++)
if(Edge[i][col]==1&&visited[col]!=true)
return col;
}
return -1;
}
int Graphmtx::getNextNeighbor(int v,int w)
{
if (v!=-1&&w!=-1)
{
for(int j=w+1;j<numVertices;j++)
if(Edge[v][j]>0)
return j;
}
return -1;
}
void Graphmtx::DFS (const char& v) {
//從頂點v出發對圖G進行深度優先遍歷的主過程
int loc;
loc = getVertexPos(v);
DFS (loc, visited); //從頂點0開始深度優先搜索
}
void Graphmtx::DFS (int i, bool visited[]) {
cout << getValue(i) << ' '; //訪問頂點v
visited[i] = true; //作訪問標記
int w = getFirstNeighbor (i); //第一個鄰接頂點
while (w != -1) { //若鄰接頂點w存在
if ( !visited[w] )
DFS(w, visited);
//若w未訪問過, 遞歸訪問頂點w
w = getNextNeighbor (i, w); //下一個鄰接頂點
}
for (i=0;i<numVertices;i++)
{
if (visited[i]!=true)
DFS(getValue(i));
}
}
void main()
{
Graphmtx g;
char x=g.getValue(0);
g.DFS(x);
}
/*
#include <iostream>
#include <stdio.h>
#include <list>
#include <iterator>
#define MAX_VERTEX_NUM 100
int visited[MAX_VERTEX_NUM];
using namespace std;
typedef char v_type; //定點的數據類型
typedef struct ArcNode {
int adjvex;
struct ArcNode *nextarc;
int weight;
}ArcNode;
typedef struct VNode { //邊結點
v_type data;
ArcNode *firstarc;
}VNode,AdjList[MAX_VERTEX_NUM];
typedef struct {
AdjList vexs;
int vexnum,arcnum;
}ALGraph;
//-----------------判斷函數-------------------------
//用來選擇是否是有向圖
bool DirectedorNot() {
char d;
cout << "Is your graph directed or not?(Y/N):" ;
A: cin >> d;
if (d=='Y') return true;
else if (d=='N') return false;
else {
cout << "Please input correctly!" << endl ;
goto A;
}
}
//------------------位置確定函數---------------------
//用來確定u所處的位置
int LocateVex(ALGraph G, v_type u) {
int i;
for (i=0; i<G.vexnum; i++) {
if (u==G.vexs[i].data) return i;
}
if (i==G.vexnum) {
cout << "Error!" ;
exit (1);
}
}
//------------------圖的構造函數------------------------
void CreatALGraph(ALGraph &G) {
int i,j,w,k;
v_type v1,v2;
ArcNode *p;
bool flag = DirectedorNot();
cout << "Input vexnum & arcnum:" ;
cin >> G.vexnum >> G.arcnum ;
cout << "Input vertices" ;
for (i=0; i<G.vexnum; i++) { //初始化邊結點
cin >> G.vexs[i].data;
G.vexs[i].firstarc = NULL;
}
for (k=0; k<G.arcnum; k++) { //輸入兩定點和他們之間弧的權
cout << "Input arcs(v1, v2 & w):" ;
cin >> v1 >> v2 >> w ;
i = LocateVex(G, v1);
j = LocateVex(G, v2);
p = (ArcNode*)malloc(sizeof(ArcNode));
p->adjvex = j;
p->weight = w;
p->nextarc = G.vexs[i].firstarc;
G.vexs[i].firstarc = p;
if (!flag) { //如果是無向圖,執行此步驟
p=(ArcNode*)malloc(sizeof(ArcNode));
p->adjvex=i;
p->weight = w;
p->nextarc=G.vexs[j].firstarc;
G.vexs[j].firstarc=p;
}
}
}
//-------------------深度優先遍歷----------------------
void DFS(ALGraph G, int v) { //v代表從第v個頂點開始遍歷
ArcNode *p;
cout << G.vexs[v].data << ' ' ;
visited[v] = 1;
p = G.vexs[v].firstarc;
while (p) {
if (!visited[p->adjvex]) DFS(G, p->adjvex);
p = p->nextarc;
}
}
void DFSTraverse(ALGraph G) { //防止非連通圖不能全部遍歷到
int i;
for (i=0; i<G.vexnum; i++) {
visited[i] = 0;
}
for (i=0; i<G.vexnum; i++) {
if (!visited[i]) DFS(G, i);
}
}
//--------------------廣度優先遍歷-----------------------
void BFS(ALGraph G, int v) { //v代表從第v個定點開始遍歷
list<int> v_list; //定義一個鏈表
list<int>::iterator iter_front;
ArcNode *p;
cout << G.vexs[v].data << ' ' ;
visited[v] = 1;
v_list.push_back(v);
while (!v_list.empty()) {
iter_front = v_list.begin(); //先把值賦給迭代器,然後通過迭代器提領傳值給v
v = *iter_front;
v_list.pop_front();
p = G.vexs[v].firstarc;
while (p) {
if (!visited[p->adjvex]) {
cout << G.vexs[p->adjvex].data << ' ' ;
visited[p->adjvex] = 1;
v_list.push_back(p->adjvex);
}
p = p->nextarc;
}
}
}
void BFSTraverse(ALGraph G) {
int i;
for (i=0; i<G.vexnum; i++) {
visited[i] = 0;
}
for (i=0; i<G.vexnum; i++) {
if (!visited[i]) BFS(G, i);
}
}
//---------------------主函數-----------------------------
int main() {
ALGraph G;
CreatALGraph(G);
cout << "深度優先遍歷:" << endl;
DFSTraverse(G);
cout << endl;
cout << "廣度優先遍歷:" << endl;
BFSTraverse(G);
cout << endl;
system("pause");
return 0;
}*/