/// 10_16 利用图的深度优先搜索和广度优先搜索各写一个算法,
/// 判别以邻接表方式表示的有向图中是否存在由顶点
/// vi到顶点vj的路径(i!=j)
#include "stdafx.h"
#include <stdlib.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#include "Chapter10.h"
void initializeGraph(vexNode* head, const int numOfVex)
//初始化一个以head为头节点,长度为numOfVex的邻接表
{
for(int i = 0; i < numOfVex; i++)
{
(head + i)->vertex = ' ';
(head + i)->pLink = NULL;
}
}
void initializeEdgeNode(edgeNode* head)
//初始化一个edgeNode节点
{
head->adjvex = -1;
head->pNext = NULL;
}
edgeNode* newEdgeNode()
//用来分配空间
{
edgeNode* head = NULL;
head = (edgeNode*)malloc(sizeof(edgeNode));
if(head == NULL)
{
printf("分配空间出错!/n");
exit(1);
}
initializeEdgeNode(head);
return head;
}
vexNode* constructGraph(const int size)
//生成邻接表的函数
{
char ch = 'A';
int tempVex = 0;
srand((unsigned)time(NULL));
vexNode* vexes = (vexNode*)malloc(sizeof(vexNode) * size);
initializeGraph(vexes, size);
edgeNode* r = NULL;
edgeNode* s = NULL;
int totalEdges = 0;
for(int i = 0; i < size; i++, ch++)
{
r = NULL;
(vexes + i)->vertex = (char)('a' + i);
for (int j = 0; j < size; j++)
{
if (j == i)
{
continue;
}
//避免生成与自己相连的边
if ((tempVex = (rand() % 4) )== 1)
//随机地生成与这个顶点相连的边
{
s = newEdgeNode();
totalEdges++;
s->adjvex = j;
if (r == NULL)
{
(vexes + i)->pLink = s;
}
else
{
r->pNext = s;
}
r = s;
}
}
}
printf("已生成邻接表,顶点数为%d,边数为%d。/n", NUM_OF_VEX, totalEdges);
printEdges(vexes, size);
return vexes;
}
void printEdges(vexNode* vexes, const int size)
//显示所有的顶点和边
{
printf("邻接表的信息为:/n");
printf("顶点/t相邻的顶点/n");
edgeNode* tempNode = NULL;
for (int i = 0; i < size; i++)
{
printf("%c/t", (vexes + i)->vertex);
tempNode = (vexes + i)->pLink;
while (tempNode != NULL)
{
printf("%c/t", tempNode->adjvex + 'a');
tempNode = tempNode->pNext;
}
printf("/n");
}
}
vexNode* searchNode(vexNode* vexes, const int size, const char data)
//在以vexes为首,大小为size的邻接表中查找vertex为data的节点的函数
{
vexNode* t = vexes;
vexNode* s = NULL;
for (int i = 0; (i < size) && (t->vertex != data); i++)
{
t++;
}
if (t->vertex == data)
{
s = t;
}
else
{
return NULL;
}
return s;
}
void findPathDepth(vexNode* graph, const int size, const char source, const char target)
//在先序遍历的基础上作的查找从source到target的路径的程序,深度优先
{
int stack[NUM_OF_VEX * NUM_OF_VEX] = {NULL};
int top = -1;
int i = 0; //循环计数器
vexNode* s = NULL;
vexNode* t = graph;
vexNode* r = NULL;
s = searchNode(graph, size, source);
r = searchNode(graph, size, target);
//以上将s和r调整到source和target对应的位置
printf("进行深度优先查找……/n");
if (s == NULL || r == NULL)
{
printf("空的起始/终结节点,不进行路径查找!/n");
return;
}
t = s;
edgeNode* tempedgeNode = s->pLink;
do
{
while (tempedgeNode != NULL)
{
if ((graph + tempedgeNode->adjvex)->vertex == target)
{
printf("有从节点%c到节点%c的路径!/n", source, target);
return;
}
top++;
stack[top] = tempedgeNode->adjvex; //s的临接顶点的位置入栈
tempedgeNode = tempedgeNode->pNext;
}
while(tempedgeNode == NULL && top >= 0)
{
t = graph + stack[top];
top--;
tempedgeNode = t->pLink;
}
}
while(top >= 0 && top < 64);
printf("没有从节点%c到节点%c的路径!/n", source, target);
}
void findPathWidth(vexNode* graph, const int size, const char source, const char target)
//在先序遍历的基础上作的查找从source到target的路径的程序,广度优先
{
int stack[NUM_OF_VEX * NUM_OF_VEX] = {NULL};
int front = 0;
int rear = -1;
int i = 0; //循环计数器
vexNode* s = NULL;
vexNode* t = graph;
vexNode* r = NULL;
s = searchNode(graph, size, source);
r = searchNode(graph, size, target);
//以上将s和r调整到source和target对应的位置
printf("进行广度优先查找……/n");
if (s == NULL || r == NULL)
{
printf("空的起始/终结节点,不进行路径查找!/n");
return;
}
t = s;
edgeNode* tempedgeNode = s->pLink;
do
{
while (tempedgeNode != NULL)
{
if ((graph + tempedgeNode->adjvex)->vertex == target)
{
printf("有从节点%c到节点%c的路径!/n", source, target);
return;
}
rear++;
stack[rear] = tempedgeNode->adjvex; //s的临接顶点的位置入队
tempedgeNode = tempedgeNode->pNext;
}
while(tempedgeNode == NULL && front <= rear)
{
front++;
t = graph + stack[front];
tempedgeNode = t->pLink;
}
}
while(front <= rear);
printf("没有从节点%c到节点%c的路径!/n", source, target);
}
void visit(vexNode* vex)
{
printf("%c", vex->vertex);
}
int _tmain(int argc, _TCHAR* argv[])
{
vexNode* head = NULL;
char ch = 'y';
char source = '/0';
char target = '/0';
head = constructGraph(NUM_OF_VEX);
while(ch == 'y')
{
printf("请输入一个从%c到%c的字母作为起始节点:", 'a', 'a' + NUM_OF_VEX - 1);
source = getche();
printf("/n请输入一个从%c到%c的字母作为结束节点:", 'a', 'a' + NUM_OF_VEX - 1);
target = getche();
printf("/n");
findPathDepth(head, NUM_OF_VEX, source, target);
findPathWidth(head, NUM_OF_VEX, source, target);
printf("/n还继续吗(请输入y以继续)?");
ch = getche();
printf("/n");
}
return 0;
}