#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;
}*/