第1关:习题2 DFS非递归
//算法6.2 采用邻接表表示法创建无向图
#include <iostream>
#include <stack>
using namespace std;
#define MVNum 100 //最大顶点数
#define OK 1
#define ERROR 0
bool visited[100];
typedef char VerTexType; //顶点信息
typedef int OtherInfo; //和边相关的信息
//- - - - -图的邻接表存储表示- - - - -
typedef struct ArcNode{ //边结点
int adjvex; //该边所指向的顶点的位置
struct ArcNode *nextarc; //指向下一条边的指针
OtherInfo info; //和边相关的信息
}ArcNode;
typedef struct VNode{
VerTexType data; //顶点信息
ArcNode *firstarc; //指向第一条依附该顶点的边的指针
}VNode, AdjList[MVNum]; //AdjList表示邻接表类型
typedef struct{
AdjList vertices; //邻接表
int vexnum, arcnum; //图的当前顶点数和边数
}ALGraph;
int LocateVex(ALGraph G , VerTexType v)
{
//确定点v在G中的位置
for(int i = 0; i < G.vexnum; ++i)
if(G.vertices[i].data == v)
return i;
return -1;
}//LocateVex
int CreateUDG(ALGraph &G)
{
//采用邻接表表示法,创建无向图G
int i , k;
cin >> G.vexnum >> G.arcnum; //输入总顶点数,总边数
for(i = 0; i < G.vexnum; ++i)
{ //输入各点,构造表头结点表
cin >> G.vertices[i].data; //输入顶点值
G.vertices[i].firstarc=NULL; //初始化表头结点的指针域为NULL
}//for
for(k = 0; k < G.arcnum;++k)
{ //输入各边,构造邻接表
VerTexType v1 , v2;
int i , j;
cin >> v1 >> v2; //输入一条边依附的两个顶点
i = LocateVex(G, v1); j = LocateVex(G, v2);
//确定v1和v2在G中位置,即顶点在G.vertices中的序号
ArcNode *p2=new ArcNode; //生成另一个对称的新的边结点*p2
p2->adjvex=j; //邻接点序号为i
p2->nextarc= G.vertices[i].firstarc;
G.vertices[i].firstarc=p2;
//将新结点*p2插入顶点vj的边表头部
}//for
return OK;
}//CreateUDG
void DFS(ALGraph G, VerTexType v)
{
/************************Begin****************/
int k = LocateVex(G,v);
stack <int> s;
visited[k] = true;
s.push(k);
while(!s.empty())
{
int t = s.top();
cout << G.vertices[t].data << ' ';
s.pop();
ArcNode *p = G.vertices[t].firstarc;
while(p)
{
int j = p -> adjvex;
if(!visited[j])
{
visited[j] = true;
s.push(j);
}
p = p -> nextarc;
}
}
/***********************End******************/
}
void show(ALGraph G)
{
for(int i = 0 ; i < G.vexnum ; ++i)
{
VNode temp = G.vertices[i];
ArcNode *p = temp.firstarc;
if(p == NULL)
{
cout << G.vertices[i].data;
cout << endl;
}
else
{
cout << temp.data;
while(p)
{
cout << "->";
cout << G.vertices[p->adjvex].data;
p = p->nextarc;
}
}
cout << endl;
}
}
int main()
{
ALGraph G;
CreateUDG(G);
VerTexType v;
cin >> v;
DFS(G, v);
return 0;
}//main
第2关:习题3 最短路径-邻接矩阵表示
#include <iostream>
using namespace std;
#define MaxInt 32767 //表示极大值,即∞
#define MVNum 100 //最大顶点数
#define OK 1
#define ERROR 0
typedef char VerTexType; //假设顶点的数据类型为字符型
typedef int ArcType; //假设边的权值类型为整型
int D[MVNum];
bool S[MVNum];
int Path[MVNum];
//- - - - -图的邻接矩阵存储表示- - - - -
typedef struct{
VerTexType vexs[MVNum]; //顶点表
ArcType arcs[MVNum][MVNum]; //邻接矩阵
int vexnum,arcnum; //图的当前点数和边数
}AMGraph;
int LocateVex(AMGraph G , VerTexType v){
//确定点v在G中的位置
for(int i = 0; i < G.vexnum; ++i)
if(G.vexs[i] == v)
return i;
return -1;
}//LocateVex
int CreateUDN(AMGraph &G){
//采用邻接矩阵表示法,创建无向网G
int i , j , k, w;
cin >> G.vexnum >> G.arcnum; //输入总顶点数,总边数
for(i = 0; i < G.vexnum; ++i){
cin >> G.vexs[i]; //依次输入点的信息
}
for(i = 0; i < G.vexnum; ++i) //初始化邻接矩阵,边的权值均置为极大值MaxInt
for(j = 0; j < G.vexnum; ++j)
G.arcs[i][j] = MaxInt;
for(k = 0; k < G.arcnum;++k){ //构造邻接矩阵
VerTexType v1 , v2;
cin >> v1 >> v2 >> w; //输入一条边依附的顶点及权值
i = LocateVex(G, v1); j = LocateVex(G, v2); //确定v1和v2在G中的位置,即顶点数组的下标
G.arcs[i][j] = w; //边<v1, v2>的权值置为w
}//for
return OK;
}//CreateUDN
int ShortestPathMax(AMGraph G, VerTexType v)
{
/*************************Begin*********************/
int k = LocateVex(G,v);
int n = G.vexnum,i,w,t;
for(t = 0;t < n; ++ t )
{
S[t] = false;
D[t] = G.arcs[k][t];
if(D[t]< MaxInt) Path[t] = k;
else Path [t]=-1;
}
S[k]=true;
D[k]=0;
for(i=1;i<n; ++i)
{
int min= MaxInt;
for(w=0;w<n; ++w)
if(!S[w]&&D[w]<min)
{t=w; min=D[w];}
S[t]=true;
for(w=0;w<n; ++w)
if(!S[w]&&(D[t]+G.arcs[t][w]<D[w])){
D[w]=D[t]+G.arcs[t][w];
Path [w]=t;
}
}
int Max=D[0];
int m=0;
for(i=1;i<n;i++)
if(Max<D[i]) m = i;
return m - 1;
/*************************End************************/
}
int main(){
AMGraph G;
CreateUDN(G);
VerTexType v;
cin >> v;
int p = ShortestPathMax(G, v);
cout << G.vexs[p];
return 0;
}//main
第3关:习题4 有向图邻接表表示深搜路径
#include <iostream>
#include <stack>
using namespace std;
#define MVNum 100 //最大顶点数
#define OK 1
#define ERROR 0
typedef char VerTexType; //顶点信息
typedef int OtherInfo; //和边相关的信息
//- - - - -图的邻接表存储表示- - - - -
typedef struct ArcNode{ //边结点
int adjvex; //该边所指向的顶点的位置
struct ArcNode *nextarc; //指向下一条边的指针
OtherInfo info; //和边相关的信息
}ArcNode;
typedef struct VNode{
VerTexType data; //顶点信息
ArcNode *firstarc; //指向第一条依附该顶点的边的指针
}VNode, AdjList[MVNum]; //AdjList表示邻接表类型
typedef struct{
AdjList vertices; //邻接表
int vexnum, arcnum; //图的当前顶点数和边数
}ALGraph;
int LocateVex(ALGraph G , VerTexType v)
{
//确定点v在G中的位置
for(int i = 0; i < G.vexnum; ++i)
if(G.vertices[i].data == v)
return i;
return -1;
}//LocateVex
int CreateUDG(ALGraph &G)
{
//采用邻接表表示法,创建无向图G
int i , k;
cin >> G.vexnum >> G.arcnum; //输入总顶点数,总边数
for(i = 0; i < G.vexnum; ++i)
{ //输入各点,构造表头结点表
cin >> G.vertices[i].data; //输入顶点值
G.vertices[i].firstarc=NULL; //初始化表头结点的指针域为NULL
}//for
for(k = 0; k < G.arcnum;++k)
{ //输入各边,构造邻接表
VerTexType v1 , v2;
int i , j;
cin >> v1 >> v2; //输入一条边依附的两个顶点
i = LocateVex(G, v1); j = LocateVex(G, v2);
//确定v1和v2在G中位置,即顶点在G.vertices中的序号
ArcNode *p2=new ArcNode; //生成另一个对称的新的边结点*p2
p2->adjvex=j; //邻接点序号为i
p2->nextarc= G.vertices[i].firstarc;
G.vertices[i].firstarc=p2;
//将新结点*p2插入顶点vj的边表头部
}//for
return OK;
}//CreateUDG
int flag = 0; //注意flag
bool visited[MVNum];
void PathDFS(ALGraph G, VerTexType v, VerTexType w)
{
/**********************Begin********************/
int i = LocateVex(G,v),j = LocateVex(G,w);
int level = 0;
if(i==j) flag = 1;
else
{
visited[i]=1;
for(ArcNode *p = G.vertices[i].firstarc;p;p=p->nextarc,level -- )
{
level++;
int k = p->adjvex;
if(!visited[k]) PathDFS(G,G.vertices[k].data,w);
}
}
if(level == 1) flag = 1;
/**********************End**********************/
}
void show(ALGraph G)
{
for(int i = 0 ; i < G.vexnum ; ++i)
{
VNode temp = G.vertices[i];
ArcNode *p = temp.firstarc;
if(p == NULL)
{
cout << G.vertices[i].data;
cout << endl;
}
else
{
cout << temp.data;
while(p)
{
cout << "->";
cout << G.vertices[p->adjvex].data;
p = p->nextarc;
}
}
cout << endl;
}
}
int main()
{
ALGraph G;
CreateUDG(G);
VerTexType m, n;
cin >> m >> n;
PathDFS(G, m, n);
cout << flag;
return 0;
}//main
第4关:习题5 无向图邻接表表示长度为k的路径
#include <iostream>
#include <stack>
using namespace std;
#define MVNum 100 //最大顶点数
#define OK 1
#define ERROR 0
typedef char VerTexType; //顶点信息
typedef int OtherInfo; //和边相关的信息
//- - - - -图的邻接表存储表示- - - - -
typedef struct ArcNode{ //边结点
int adjvex; //该边所指向的顶点的位置
struct ArcNode *nextarc; //指向下一条边的指针
OtherInfo info; //和边相关的信息
}ArcNode;
typedef struct VNode{
VerTexType data; //顶点信息
ArcNode *firstarc; //指向第一条依附该顶点的边的指针
}VNode, AdjList[MVNum]; //AdjList表示邻接表类型
typedef struct{
AdjList vertices; //邻接表
int vexnum, arcnum; //图的当前顶点数和边数
}ALGraph;
int LocateVex(ALGraph G , VerTexType v)
{
//确定点v在G中的位置
for(int i = 0; i < G.vexnum; ++i)
if(G.vertices[i].data == v)
return i;
return -1;
}//LocateVex
int CreateUDG(ALGraph &G)
{
//采用邻接表表示法,创建无向图G
int i , k;
cin >> G.vexnum >> G.arcnum; //输入总顶点数,总边数
for(i = 0; i < G.vexnum; ++i)
{ //输入各点,构造表头结点表
cin >> G.vertices[i].data; //输入顶点值
G.vertices[i].firstarc=NULL; //初始化表头结点的指针域为NULL
}//for
for(k = 0; k < G.arcnum;++k)
{ //输入各边,构造邻接表
VerTexType v1 , v2;
int i , j;
cin >> v1 >> v2; //输入一条边依附的两个顶点
i = LocateVex(G, v1); j = LocateVex(G, v2);
//确定v1和v2在G中位置,即顶点在G.vertices中的序号
ArcNode *p2=new ArcNode; //生成另一个对称的新的边结点*p2
p2->adjvex=j; //邻接点序号为i
p2->nextarc= G.vertices[i].firstarc;
G.vertices[i].firstarc=p2;
//将新结点*p2插入顶点vj的边表头部
ArcNode *p1=new ArcNode; //生成另一个对称的新的边结点*p2
p1->adjvex=i; //邻接点序号为i
p1->nextarc= G.vertices[j].firstarc;
G.vertices[j].firstarc=p1;
//将新结点*p1插入顶点vi的边表头部
}//for
return OK;
}//CreateUDG
bool visited[MVNum];
int PathLenK(ALGraph G, VerTexType v, VerTexType w, int k)
{
/**********************Begin*******************/
int i = LocateVex(G,v),j = LocateVex(G,w);
if(i==j&&k==0) return 1;
else if(k>0)
{
visited[i]=1;
for(ArcNode *p=G.vertices[i].firstarc;p;p=p->nextarc)
{
int l = p->adjvex;
if(!visited[l] && PathLenK(G,G.vertices[l].data,w,k-1)) return 1;
}
visited[i]=0;
}
return 0;
/**********************End*******************/
}
void show(ALGraph G)
{
for(int i = 0 ; i < G.vexnum ; ++i)
{
VNode temp = G.vertices[i];
ArcNode *p = temp.firstarc;
if(p == NULL)
{
cout << G.vertices[i].data;
cout << endl;
}
else
{
cout << temp.data;
while(p)
{
cout << "->";
cout << G.vertices[p->adjvex].data;
p = p->nextarc;
}
}
cout << endl;
}
}
int main()
{
ALGraph G;
CreateUDG(G);
int k;
VerTexType m, n;
cin >> m >> n >> k;
cout << PathLenK(G, m, n, k);
return 0;
}//main