图的学习——图的遍历

1.图的定义

”“

   图是一种比线性表和树更为复杂的数据结构。 它是由顶点的有穷非空集合和顶点之间边的集合组成,通常表示为:G(V,E),其中,G表示一个图,V是图G中顶点的集合,E是图G中边的集合 。图结构,是研究数据元素之间的多对多的关系。在这种结构中,任意两个元素之间可能存在关系。即结点之间的关系可以是任意的,图中任意元素之间都可能相关。 图是由顶点的有穷非空集合和顶点之间边的集合组成,通常表示为:G(V,E),其中,G表示一个图,V是图G中顶点的集合,E是图G中边的集合。

""

2.代码实现

#include <stdio.h>
#include <malloc.h>
#define QUEUE_SIZE 10

int* visitedPtr;

typedef struct GraphNodeQueue{
	int* nodes;
	int front;
	int rear;
}GraphNodeQueue, *QueuePtr;


QueuePtr initQueue(){
	QueuePtr resultQueuePtr = (QueuePtr)malloc(sizeof(struct GraphNodeQueue));
	resultQueuePtr->nodes = (int*)malloc(QUEUE_SIZE * sizeof(int));
	resultQueuePtr->front = 0;
	resultQueuePtr->rear = 1;
	return resultQueuePtr;
}


bool isQueueEmpty(QueuePtr paraQueuePtr){
	if ((paraQueuePtr->front + 1) % QUEUE_SIZE == paraQueuePtr->rear) {
		return true;
	}//Of if
	return false;
}

void enqueue(QueuePtr paraQueuePtr, int paraNode){
	printf("front = %d, rear = %d.\r\n", paraQueuePtr->front, paraQueuePtr->rear);
	if ((paraQueuePtr->rear + 1) % QUEUE_SIZE == paraQueuePtr->front % QUEUE_SIZE) {
		printf("Error, trying to enqueue %d. queue full.\r\n", paraNode);
		return;
	}
	paraQueuePtr->nodes[paraQueuePtr->rear] = paraNode;
	paraQueuePtr->rear = (paraQueuePtr->rear + 1) % QUEUE_SIZE;
}


int dequeue(QueuePtr paraQueuePtr){
	if (isQueueEmpty(paraQueuePtr)) {
		printf("Error, empty queue\r\n");
		return NULL;
	}

	paraQueuePtr->front = (paraQueuePtr->front + 1) % QUEUE_SIZE;
	printf("dequeue %d ends.\r\n", paraQueuePtr->nodes[paraQueuePtr->front]);
	return paraQueuePtr->nodes[paraQueuePtr->front];
}

typedef struct Graph{
	int** connections;
	int numNodes;
} *GraphPtr;


GraphPtr initGraph(int paraSize, int** paraData) {
	int i, j;
	GraphPtr resultPtr = (GraphPtr)malloc(sizeof(Graph));
	resultPtr -> numNodes = paraSize;
	resultPtr -> connections = (int**)malloc(paraSize * paraSize * sizeof(int));
	resultPtr -> connections = (int**)malloc(paraSize * sizeof(int*));
	for (i = 0; i < paraSize; i ++) {
		resultPtr -> connections[i] = (int*)malloc(paraSize * sizeof(int));
		for (j = 0; j < paraSize; j ++) {
			resultPtr -> connections[i][j] = paraData[i][j];
		}
	}
	return resultPtr;
}

void initTranverse(GraphPtr paraGraphPtr) {
	int i;
	visitedPtr = (int*) malloc(paraGraphPtr -> numNodes * sizeof(int));
	for (i = 0; i < paraGraphPtr -> numNodes; i ++) {
		visitedPtr[i] = 0;
	}
}

void depthFirstTranverse(GraphPtr paraGraphPtr, int paraNode) {
	int i;
	
	visitedPtr[paraNode] = 1;
	printf("%d\t", paraNode);
	
	for (i = 0; i < paraGraphPtr -> numNodes; i ++) {
		if (!visitedPtr[i]){ 
			if (paraGraphPtr -> connections[paraNode][i]) {
				depthFirstTranverse(paraGraphPtr, i);
			}
		}
	}
}

void widthFirstTranverse(GraphPtr paraGraphPtr, int paraStart){
	int i, j, tempNode;
	i = 0;
	QueuePtr tempQueuePtr = initQueue();
	printf("%d\t", paraStart);
	visitedPtr[paraStart] = 1;
	enqueue(tempQueuePtr, paraStart);
	while (!isQueueEmpty(tempQueuePtr)) {
		tempNode = dequeue(tempQueuePtr);
		visitedPtr[tempNode] = 1;
		
		i ++;

		for (j = 0; j < paraGraphPtr->numNodes; j ++) {
			if (visitedPtr[j]) 
				continue;

			if (paraGraphPtr->connections[tempNode][j] == 0)
				continue;
			
			printf("%d\t", j);
			visitedPtr[j] = 1;
			enqueue(tempQueuePtr, j);
		}
	}
}

void testGraphTranverse() {
	int i, j;
	int myGraph[5][5] = { 
		{0, 1, 0, 1, 0},
		{1, 0, 1, 0, 1}, 
		{0, 1, 0, 1, 1}, 
		{1, 0, 1, 0, 0}, 
		{0, 1, 1, 0, 0}};
	int** tempPtr;
	printf("Preparing data\r\n");
		
	tempPtr = (int**)malloc(5 * sizeof(int*));
	for (i = 0; i < 5; i ++) {
		tempPtr[i] = (int*)malloc(5 * sizeof(int));
	}
	 
	for (i = 0; i < 5; i ++) {
		for (j = 0; j < 5; j ++) {
			printf("i = %d, j = %d, ", i, j);
			printf("%d\r\n", tempPtr[i][j]);
			tempPtr[i][j] = myGraph[i][j];
			printf("i = %d, j = %d, %d\r\n", i, j, tempPtr[i][j]);
		}
	}
	printf("Data ready\r\n");
	
	GraphPtr tempGraphPtr = initGraph(5, tempPtr);
	printf("num nodes = %d \r\n", tempGraphPtr -> numNodes);
	printf("Graph initialized\r\n");

	printf("Depth first visit:\r\n");
	initTranverse(tempGraphPtr);
	depthFirstTranverse(tempGraphPtr, 4);

	printf("\r\nWidth first visit:\r\n");
	initTranverse(tempGraphPtr);
	widthFirstTranverse(tempGraphPtr, 4);
}

int main(){
	testGraphTranverse();
	return 1;
}

3.深度优先遍历

    深度优先搜索 就是从一个顶点开始,沿着一条路径一直搜索,直到到达该路径的最后一个结点,然后回退到之前经过但未搜索过的路径继续搜索,直到所有路径和结点都被搜索完毕同样的,概念比较抽象,我们用例子模拟一遍深度优先搜索,如图所示,先随便沿着一条路径一直搜索,图中路径为 1 -> 2 -> 3 -> 6 -> 9 -> 8 -> 5 -> 4

 

当搜索到 4时,发现查找不到未被访问的顶点了,因此 4就是这条路径的最后一个顶点,此时我们要往后倒退,找寻别的每搜索过的路径.此时发现当往后退到 8 时,有一条路径上还有未被搜索过的顶点,即 8 -> 7.沿着这条路径一直找到了最后一个顶点—— 7,此时我们要继续往后退,看看还有没有别的路径没走过并且有未搜索过的顶点,发现此时所有顶点都被访问过了,也就意味着一个完整的深度优先搜索就完成了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值