# include <stdio.h>
# include <memory.h>
# include <malloc.h>
# define MAX 20 //最大頂點數
typedef struct VNODE
{
int num; //該邊所指的頂點的位置
struct VNODE *next; //指向下一條邊的指針
}arcnode; //表的結點
typedef struct LIST
{
char data; //頂點信息
arcnode * firstarc ; //指向第一條依附頂點的邊和弧指針
}ADJVEX[MAX]; //頭結點
typedef struct ALGRAPH
{
ADJVEX vertices;
bool visited[MAX]; //訪問標誌數組
int vexnum,arcnum; //圖的當前頂點數和弧數
}algraph;
void init_algraph (algraph &g) //初始化圖
{
memset(g.visited,false,sizeof(bool)*MAX); //訪問標誌數組置false,表示未訪問
g.arcnum = 0;
g.vexnum = 0;
}
int locatenode(algraph &g,char ch) //定位ch在頂點向量中的位置
{
int i;
for (i=0;i<g.vexnum && ch != g.vertices[i].data;++i)
;
return i;
}
void add_vex (algraph &g) //增加頂點
{
printf ("請輸入頂點個數\n");
scanf ("%d",&g.vexnum);
printf ("請輸入頂點信息\n");
for (int i=0;i<g.vexnum;++i)
{
scanf (" %c",&g.vertices[i].data); //構造頂點向量
g.vertices[i].firstarc = NULL;
}
}
void add_arc(algraph &g) //增加邊
{
arcnode *s,*p,*t;
printf ("請輸入邊的個數\n");
scanf ("%d",&g.arcnum);
printf ("請輸入邊的信息\n");
char ch1,ch2;
for (int k=0;k<g.arcnum;++k)
{
scanf (" %c %c",&ch1,&ch2);
int i = locatenode(g,ch1);
int j = locatenode(g,ch2); //確定ch1,ch2在頂點向量的位置
s = (arcnode *)malloc(sizeof(VNODE));
t = (arcnode *)malloc(sizeof(VNODE));
s->num = j; //該邊所指向的位置爲j
s->next = NULL;
if (!g.vertices[i].firstarc)
g.vertices[i].firstarc = s;
else
{
for (p=g.vertices[i].firstarc; p->next; p=p->next)
;
p->next = s;
}
t->num = i; //該邊所指向的位置爲i
t->next = NULL;
if (!g.vertices[j].firstarc)
g.vertices[j].firstarc = t;
else
{
for (p=g.vertices[j].firstarc; p->next; p=p->next)
;
p->next = t;
}
}
}
void creat_algraph(algraph &g) //構造鄰接鏈表
{
add_vex(g);
add_arc(g);
}
void print_algraph (algraph &g) //輸出圖
{
int i;
arcnode *p;
printf ("編號 頂點 鄰接結點\n");
for (i=0; i<g.vexnum; ++i)
{
printf ("%d %c ",i,g.vertices[i].data);
for (p=g.vertices[i].firstarc; p; p=p->next)
{
printf (" %d ",p->num);
}
printf ("\n");
}
}
void visit(algraph &g,int i)
{
printf ("%c ",g.vertices[i].data);
g.visited[i] = true;
}
void DFStraverse(algraph &g,int i) //深度優先搜索
{ //從第i個開始搜索
visit(g,i);
arcnode *p;
for (p=g.vertices[i].firstarc; p; p=p->next)
{
if (!g.visited[p->num])
{
DFStraverse(g,p->num);
}
}
}
void DFSearch(algraph &g,int i,int s,char path[])
{
arcnode *p;
g.visited[i] = true;
static int found = 0;
static int rear = 0; //路徑數組的尾指針
path[rear++] = g.vertices[i].data;
path[rear] = '\0';
for (p=g.vertices[i].firstarc; p && !found; p=p->next)
{
if (s == p->num) //找的目標結點
{
found = 1; //foun置1,退出
path[rear++] = g.vertices[p->num].data;
path[rear] = '\0';
}
else if (!g.visited[p->num])
DFSearch(g,p->num,s,path);
}
if (!found) //如果該節點的所有鄰接點都不是目標結點,並且鄰接點的鄰接點也不是目標結點,那麼他就不是路徑上的,退出路徑
{
path[--rear] = '\0';
}
}
void path_algraph(algraph &g,char ch1,char ch2,char path[]) //求兩點之間的一般路徑
{
int i = locatenode(g,ch1);
int s = locatenode(g,ch2);
DFSearch(g,i,s,path);
}
int main(void)
{
algraph g;
init_algraph(g);
creat_algraph(g);
print_algraph(g);
// DFStraverse(g,0);
char ch1,ch2;
scanf (" %c %c",&ch1,&ch2);
char *path;
path = (char *)malloc(sizeof(char)*MAX);
path_algraph(g,ch1,ch2,path);
int i=0;
while(path[i]) //輸出路徑
printf ("%c ",path[i++]);
return 0;
}
DFS--圖中兩點之間的路徑
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.