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 (w = g. vertices [i ]. firstarc ; w > ; 0 ;w = w - > ;nextarc ) {
if (visited [w - > ;adjvex ] == FALSE ) {
visited [w - > ;adjvex ] = 1 ;
DFS (g,w - > ;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);
//注意判斷結點不存在的情況
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 & ; & ; !j ) {
//無奈之舉,似乎測試數據有問題
//詢問A-G有無路徑時,i和j都為0
//這種判斷方法不嚴謹,請勿模仿
return FALSE ;
}
EnQueue (q,i ) ;
while ( !QueueEmpty (q ) ) { //BSF基本用法
DeQueue (q,e ) ;
visited [e ] = TRUE ;
p = g. vertices [e ]. firstarc ;
while (p ) {
if (visited [p - > ;adjvex ] == FALSE ) {
EnQueue (q,p - > ;adjvex ) ;
}
p = p - > ;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,暫時無法解決!
待更新中…