#include <iostream>
#include <deque>
#include <new>
using namespace std;
#define MAXSIZE 512
//点
#define INVALIDVALUE -1
struct Graph
{
int edge[MAXSIZE][MAXSIZE]; //边
char Point[MAXSIZE]; //点
int PointSize; //点数量
int edgeSize; //边数量
bool hasDirected; //是否有向图
bool hasWeight; //是否带权
Graph(){
CleanUp();
for (int i=0;i<MAXSIZE;i++)
{
/*for (int j = 0;j<MAXSIZE;j++)
{
edge[i][j] = INVALIDVALUE;
}*/
edge[i][i] = 1;
}
}
~Graph(){}
void CleanUp();
};
void Graph::CleanUp()
{
memset(edge,0,sizeof(edge));
memset(Point,0,sizeof(Point));
PointSize = 0;
edgeSize = 0;
hasDirected = false;
hasWeight = false;
}
Graph * CreateGraph(bool hasDirected,bool hasWeight)
{
Graph * graph = new(nothrow) Graph;
if (!graph)
{
cout<<"内存分配失败"<<endl;
return NULL;
}
cout<<"请输入点的数量"<<endl;
cin>>graph->PointSize;
if (graph->PointSize <= 0 || graph->PointSize >=MAXSIZE)
{
cout<<"点数量输入非法或者超过最大数量"<<endl;
return NULL;
}
for (int i=1;i<=graph->PointSize;i++)
{
cin>>graph->Point[i];
}
cout<<"请输入边的数量"<<endl;
cin>>graph->edgeSize;
if (graph->edgeSize <= 0 || graph->edgeSize >=MAXSIZE)
{
cout<<"边数量输入非法或者超过最大数量"<<endl;
return NULL;
}
int edgeA , edgeB , nWeight = 1;
for (int i=1;i<=graph->edgeSize;i++)
{
cin>>edgeA>>edgeB;
if (hasWeight)
{
cin>>nWeight;
}
graph->edge[edgeA][edgeB] = nWeight;
if (!hasDirected)
{
graph->edge[edgeB][edgeA] = nWeight;
}
}
graph->hasDirected = hasDirected;
graph->hasWeight = hasWeight;
return graph;
}
void Print(Graph *graph)
{
if (!graph)
{
return;
}
for (int i = 1;i<=graph->PointSize;i++)
{
for (int j = 1; j<= graph->PointSize;j++)
{
cout<<graph->edge[i][j]<<" ";
}
cout<<endl;
}
}
bool hasRoute(Graph *graph,int i,int j)
{
if (graph->edge[i][j] !=0)
{
return true;
}
return false;
}
void EveryDepthPrior(Graph *graph,int index,bool * visited)
{
cout<<"访问结点"<<index<<endl;
visited[index] = true;
for (int j = 1;j<=graph->PointSize;j++)
{
if (hasRoute(graph,index,j) && !visited[j])
{
EveryDepthPrior(graph,j,visited);
}
}
}
void DepthPrior(Graph *graph)
{
bool visited[MAXSIZE];
memset(visited,0,sizeof(visited));
//遍历 是为防止 非连通图
for (int i = 1;i<=graph->PointSize;i++)
{
if (!visited[i])
{
EveryDepthPrior(graph,i,visited);
}
}
}
deque<int> point;
void EveryScopePrior(Graph *graph,int index,bool * visited)
{
cout<<"访问结点"<<index<<endl;
visited[index] = true;
for (int j = 1;j<=graph->PointSize;j++)
{
if (hasRoute(graph,index,j) && !visited[j])
{
point.push_front(j);
}
}
if (point.size() > 0)
{
if (hasRoute(graph,index,point.back()) && !visited[point.back()])
{
EveryScopePrior(graph,point.back(),visited);
}
point.pop_back();
}
}
void ScopePrior(Graph *graph)
{
bool visited[MAXSIZE];
memset(visited,0,sizeof(visited));
//遍历 是为防止 非连通图
for (int i = 1;i<=graph->PointSize;i++)
{
if (!visited[i])
{
EveryScopePrior(graph,i,visited);
}
}
}
int main()
{
Graph * graph = CreateGraph(false,false);
Print(graph);
DepthPrior(graph);
ScopePrior(graph);
delete graph;
return 0;
}
#include <deque>
#include <new>
using namespace std;
#define MAXSIZE 512
//点
#define INVALIDVALUE -1
struct Graph
{
int edge[MAXSIZE][MAXSIZE]; //边
char Point[MAXSIZE]; //点
int PointSize; //点数量
int edgeSize; //边数量
bool hasDirected; //是否有向图
bool hasWeight; //是否带权
Graph(){
CleanUp();
for (int i=0;i<MAXSIZE;i++)
{
/*for (int j = 0;j<MAXSIZE;j++)
{
edge[i][j] = INVALIDVALUE;
}*/
edge[i][i] = 1;
}
}
~Graph(){}
void CleanUp();
};
void Graph::CleanUp()
{
memset(edge,0,sizeof(edge));
memset(Point,0,sizeof(Point));
PointSize = 0;
edgeSize = 0;
hasDirected = false;
hasWeight = false;
}
Graph * CreateGraph(bool hasDirected,bool hasWeight)
{
Graph * graph = new(nothrow) Graph;
if (!graph)
{
cout<<"内存分配失败"<<endl;
return NULL;
}
cout<<"请输入点的数量"<<endl;
cin>>graph->PointSize;
if (graph->PointSize <= 0 || graph->PointSize >=MAXSIZE)
{
cout<<"点数量输入非法或者超过最大数量"<<endl;
return NULL;
}
for (int i=1;i<=graph->PointSize;i++)
{
cin>>graph->Point[i];
}
cout<<"请输入边的数量"<<endl;
cin>>graph->edgeSize;
if (graph->edgeSize <= 0 || graph->edgeSize >=MAXSIZE)
{
cout<<"边数量输入非法或者超过最大数量"<<endl;
return NULL;
}
int edgeA , edgeB , nWeight = 1;
for (int i=1;i<=graph->edgeSize;i++)
{
cin>>edgeA>>edgeB;
if (hasWeight)
{
cin>>nWeight;
}
graph->edge[edgeA][edgeB] = nWeight;
if (!hasDirected)
{
graph->edge[edgeB][edgeA] = nWeight;
}
}
graph->hasDirected = hasDirected;
graph->hasWeight = hasWeight;
return graph;
}
void Print(Graph *graph)
{
if (!graph)
{
return;
}
for (int i = 1;i<=graph->PointSize;i++)
{
for (int j = 1; j<= graph->PointSize;j++)
{
cout<<graph->edge[i][j]<<" ";
}
cout<<endl;
}
}
bool hasRoute(Graph *graph,int i,int j)
{
if (graph->edge[i][j] !=0)
{
return true;
}
return false;
}
void EveryDepthPrior(Graph *graph,int index,bool * visited)
{
cout<<"访问结点"<<index<<endl;
visited[index] = true;
for (int j = 1;j<=graph->PointSize;j++)
{
if (hasRoute(graph,index,j) && !visited[j])
{
EveryDepthPrior(graph,j,visited);
}
}
}
void DepthPrior(Graph *graph)
{
bool visited[MAXSIZE];
memset(visited,0,sizeof(visited));
//遍历 是为防止 非连通图
for (int i = 1;i<=graph->PointSize;i++)
{
if (!visited[i])
{
EveryDepthPrior(graph,i,visited);
}
}
}
deque<int> point;
void EveryScopePrior(Graph *graph,int index,bool * visited)
{
cout<<"访问结点"<<index<<endl;
visited[index] = true;
for (int j = 1;j<=graph->PointSize;j++)
{
if (hasRoute(graph,index,j) && !visited[j])
{
point.push_front(j);
}
}
if (point.size() > 0)
{
if (hasRoute(graph,index,point.back()) && !visited[point.back()])
{
EveryScopePrior(graph,point.back(),visited);
}
point.pop_back();
}
}
void ScopePrior(Graph *graph)
{
bool visited[MAXSIZE];
memset(visited,0,sizeof(visited));
//遍历 是为防止 非连通图
for (int i = 1;i<=graph->PointSize;i++)
{
if (!visited[i])
{
EveryScopePrior(graph,i,visited);
}
}
}
int main()
{
Graph * graph = CreateGraph(false,false);
Print(graph);
DepthPrior(graph);
ScopePrior(graph);
delete graph;
return 0;
}
4982

被折叠的 条评论
为什么被折叠?



