###### BWM Studio

The Blow Water Machines...

/// 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)

{

for(int i = 0; i < numOfVex; i++)

{

(head + i)->vertex = ' ';

}

}

//初始化一个edgeNode节点

{

}

edgeNode* newEdgeNode()

//用来分配空间

{

{

printf("分配空间出错！/n");

exit(1);

}

}

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++;

if (r == NULL)

{

}

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);

while (tempNode != NULL)

{

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;

do

{

while (tempedgeNode != NULL)

{

if ((graph + tempedgeNode->adjvex)->vertex == target)

{

printf("有从节点%c到节点%c的路径！/n", source, target);

return;

}

top++;

tempedgeNode = tempedgeNode->pNext;

}

while(tempedgeNode == NULL && top >= 0)

{

t = graph + stack[top];

top--;

}

}

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;

do

{

while (tempedgeNode != NULL)

{

if ((graph + tempedgeNode->adjvex)->vertex == target)

{

printf("有从节点%c到节点%c的路径！/n", source, target);

return;

}

rear++;

tempedgeNode = tempedgeNode->pNext;

}

while(tempedgeNode == NULL && front <= rear)

{

front++;

t = graph + stack[front];

}

}

while(front <= rear);

printf("没有从节点%c到节点%c的路径！/n", source, target);

}

void visit(vexNode* vex)

{

printf("%c", vex->vertex);

}

int _tmain(int argc, _TCHAR* argv[])

{

char ch = 'y';

char source = '/0';

char target = '/0';

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");

printf("/n还继续吗（请输入y以继续）？");

ch = getche();

printf("/n");

}

return 0;

}

2006-06-12 15:57:00

2006-05-26 16:28:00

2006-06-08 00:16:00

2006-05-26 17:39:00

2006-06-12 16:01:00

#### java编程思想（第四版）第二章练习题-10

2014-08-29 09:58:44

#### C++_Primer_Plus_第6版中文版第17章源代码

2015年06月23日 13KB 下载

#### opengl实验

2012年06月21日 2.24MB 下载

#### 学习C++ primer plus 第9章编程练习1

2017-02-01 13:56:37

#### 《c 和指针》第9章第12题——加密数据

2012-07-07 10:59:06