anyview 数据结构习题集 第7章答案


7.22③ 试基于图的深度优先搜索策略写一算法,
判别以邻接表方式存储的有向图中是否存在由顶
点vi到顶点vj的路径(i≠j)。 注意:算法中涉及
的图的基本操作必须在此存储结构上实现。

实现下列函数:
Status DfsReachable(ALGraph g, int i, int j);
/* Judge if it exists a path from vertex ‘i’ to */
/* vertex ‘j’ in digraph ‘g’. */
/* Array ‘visited[]‘ has been initialed to ‘false’.*/

图的邻接表以及相关类型和辅助变量定义如下:
Status visited[MAX_VERTEX_NUM];
typedef char VertexType;
typedef struct ArcNode {
int adjvex;
struct ArcNode *nextarc;
} ArcNode;

typedef struct VNode {
VertexType data;
ArcNode *firstarc;
} VNode, AdjList[MAX_VERTEX_NUM];

typedef struct {
AdjList vertices;
int vexnum, arcnum;
} ALGraph;

//这道题思路很简单,就是简单的深度优先搜索,只不过不用搜索整个图
//只要遍历完含有顶点i的连通子图,然后判断顶点j是否被访问过就可以知道他们
//是否存在路径
int DFS (ALGraph g, int i ) {
ArcNode   *w ;
for (= g. vertices [i ]. firstarc ; w  &gt ;  0  ;= w - &gt ;nextarc ) {
if (visited [w - &gt ;adjvex ]  == FALSE ) {
visited [w - &gt ;adjvex ]  =  1 ;
DFS (g,w - &gt ;adjvex ) ;
}
}
return TRUE ;
}
Status DfsReachable (ALGraph g,  int i,  int j )
/* Judge if it exists a path from vertex 'i' to    */
/* vertex 'j' in digraph 'g'.                      */
/* Array 'visited[]' has been initialed to 'false'.*/
{
int n ;
DFS (g,i ) ;
if (visited [j ]  ! = FALSE ) {
return TRUE ;
}
else {
return FALSE ;
}
}

7.23③ 同7.22题要求。试基于图的广度优先搜索策略写一算法。

实现下列函数:
Status BfsReachable(ALGraph g, int i, int j);
/* Determine whether it exists path from vertex i to */
/* vertex j in digraph g with Breadth_First Search. */
/* Array ‘visited’ has been initialed to ‘false’. */

图的邻接表以及相关类型和辅助变量定义如下:
Status visited[MAX_VERTEX_NUM];
typedef char VertexType;
typedef struct ArcNode {
int adjvex;
struct ArcNode *nextarc;
} ArcNode;

typedef struct VNode {
VertexType data;
ArcNode *firstarc;
} VNode, AdjList[MAX_VERTEX_NUM];

typedef struct {
AdjList vertices;
int vexnum, arcnum;
} ALGraph;

Status InitQueue(Queue &q);
Status EnQueue(Queue &q, int e);
Status DeQueue(Queue &q, int &e);
Status QueueEmpty(Queue q);
Status GetFront(Queue q, int &e);

//此题与22题差别不大,同样是使用BFS遍历连通子图再判断
//注意判断结点不存在的情况
Status BfsReachable (ALGraph g,  int i,  int j )
/* Determine whether it exists path from vertex i to */
/* vertex j in digraph g with Breadth_First Search.  */
/* Array 'visited' has been initialed to 'false'.    */
{
ArcNode  *p ;
Queue q ;
int e,n ;
InitQueue (q ) ;
if ( !i &amp ; &amp ; !j ) {
//无奈之举,似乎测试数据有问题
//询问A-G有无路径时,i和j都为0
//这种判断方法不严谨,请勿模仿
return FALSE ;
}
EnQueue (q,i ) ;
while ( !QueueEmpty (q ) ) { //BSF基本用法
DeQueue (q,e ) ;
visited [e ]  = TRUE ;
= g. vertices [e ]. firstarc ;
while (p ) {
if (visited [p - &gt ;adjvex ]  == FALSE ) {
EnQueue (q,p - &gt ;adjvex ) ;
}
= p - &gt ;nextarc ;
}
}
if (visited [j ]  ! = FALSE ) { //顶点j已被访问
return TRUE ;
}
else {
return FALSE ;
}
}

7.24③ 试利用栈的基本操作编写,按深度优先搜索策略
遍历一个强连通图的非递归形式的算法。算法中不规定具
体的存储结构,而将图Graph看成是一种抽象的数据类型。

实现下列函数:
void Traverse(Graph dig, VertexType v0, void(*visit)(VertexType));
/* Travel the digraph ‘dig’ with Depth_First Search. */

图以及相关类型、函数和辅助变量定义如下:
Status visited[MAX_VERTEX_NUM];
int LocateVex(Graph g, VertexType v);
VertexType GetVex(Graph g, int i);
int FirstAdjVex(Graph g, int v);
int NextAdjVex(Graph g, int v, int w);
void visit(char v);

Status InitStack(SStack &s);
Status Push(SStack &s, SElemType x);
Status Pop(SStack &s, SElemType &x);
Status StackEmpty(SStack s);
Status GetTop(SStack s, SElemType &e);
这道题测试数据似乎有问题

在内村里面H明明没有指向结点,而测试例子和答案竟然给出H – A,暂时无法解决!
待更新中…


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM