数据结构第十一周作业(2)——图的遍历&邻接表及广度优先遍历

一.图的遍历

1.代码

#include <stdio.h>
#include <malloc.h>
#include <stdbool.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;
    }

    return false;
}

void enqueue(QueuePtr paraQueuePtr, int paraNode) {
    if ((paraQueuePtr->rear + 1) % QUEUE_SIZE == paraQueuePtr->front % QUEUE_SIZE) {
        printf("Error, trying to enqueue %d. queue full.\r\n", paraNode);
        return;
    }//of if
    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 -1;
    }

    paraQueuePtr->front = (paraQueuePtr->front + 1) % QUEUE_SIZE;

    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(struct Graph));
    resultPtr->numNodes = paraSize;

    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 ++) {
            tempPtr[i][j] = myGraph[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;
}

2.运行结果

3.心得体会

①.图是数据结构中最难的部分,综合性较强

二.邻接表及广度优先遍历

1.代码

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

int* visitedPtr;

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


GraphPtr initGraph(int paraSize, int** paraData) {
	int i, j;
	GraphPtr resultPtr = (GraphPtr)malloc(sizeof(struct 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];
		}//Of for j
	}//Of for i
	
	return resultPtr;
}

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

	return false;
}

void enqueue(QueuePtr paraQueuePtr, int paraNode) {
	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 -1;
	}

	paraQueuePtr->front = (paraQueuePtr->front + 1) % QUEUE_SIZE;

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

typedef struct AdjacencyNode {
	int column;
	AdjacencyNode* next;
}AdjacencyNode, *AdjacentNodePtr;

typedef struct AdjacencyList {
	int numNodes;
	AdjacencyNode* headers;
}AdjacencyList, *AdjacencyListPtr;

AdjacencyListPtr graphToAdjacentList(GraphPtr paraPtr) {
	int i, j, tempNum;
	AdjacentNodePtr p, q;
	tempNum = paraPtr->numNodes;
	AdjacencyListPtr resultPtr = (AdjacencyListPtr)malloc(sizeof(struct AdjacencyList));
	resultPtr->numNodes = tempNum;
	resultPtr->headers = (AdjacencyNode*)malloc(tempNum * sizeof(struct AdjacencyNode));
	
	for (i = 0; i < tempNum; i ++) {
		p = &(resultPtr->headers[i]);
		p->column = -1;
		p->next = NULL;

		for (j = 0; j < tempNum; j ++) {
			if (paraPtr->connections[i][j] > 0) {
				q = (AdjacentNodePtr)malloc(sizeof(struct AdjacencyNode));
				q->column = j;
				q->next = NULL;

				p->next = q;
				p = q;
			}//Of if
		}//Of for j
	}//Of for i

	return resultPtr;
}

void printAdjacentList(AdjacencyListPtr paraPtr) {
	int i;
	AdjacentNodePtr p;
	int tempNum = paraPtr->numNodes;

	printf("This is the graph:\r\n");
	for (i = 0; i < tempNum; i ++) {
		p = paraPtr->headers[i].next;
		while (p != NULL) {
			printf("%d, ", p->column);
			p = p->next;
		}//Of while
		printf("\r\n");
	}//Of for i
}

void widthFirstTranverse(AdjacencyListPtr paraListPtr, int paraStart) {
	printf("width first \r\n");
	int i, j, tempNode;
	AdjacentNodePtr p;
	i = 0;

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

	QueuePtr tempQueuePtr = initQueue();
	printf("%d\t", paraStart);
	visitedPtr[paraStart] = 1;
	enqueue(tempQueuePtr, paraStart);
	//printf("After enqueue\r\n");
	while (!isQueueEmpty(tempQueuePtr)) {
		//printf("First while\r\n");
		tempNode = dequeue(tempQueuePtr);

		for (p = &(paraListPtr->headers[tempNode]); p != NULL; p = p->next) {
			j = p->column;
			//printf("j = %d \r\n", j);
			if (visitedPtr[j]) 
				continue;

			printf("%d\t", j);
			visitedPtr[j] = 1;
			enqueue(tempQueuePtr, j);
		}//of for
	}//Of while
	printf("\r\n");
}


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]);
		}//Of for j
	}//Of for i
 
	printf("Data ready\r\n");
	
	GraphPtr tempGraphPtr = initGraph(5, tempPtr);
	AdjacencyListPtr tempListPtr = graphToAdjacentList(tempGraphPtr);

	printAdjacentList(tempListPtr);

	widthFirstTranverse(tempListPtr, 4);
}


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

2.运行结果

3.心得体会

①.使用邻接表可以有效地表示稀疏图,因为它只存储了实际存在的边,而不会浪费额外的空间。同时,邻接表也使得查找某个节点的邻居节点变得简单高效,只需遍历该节点对应的链表即可;

邻接矩阵

邻接矩阵(Adjacency Matrix)是图论中用于表示图中顶点之间相邻关系的矩阵。在无向图中,如果顶点 i 和顶点 j 之间存在一条边,那么邻接矩阵的第 i 行第 j 列的元素(以及第 j 行第 i 列的元素)为 1,否则为 0。在有向图中,邻接矩阵的第 i 行第 j 列的元素为 1 表示存在一条从顶点 i 到顶点 j 的边。

假设我们有一个包含 4 个顶点的无向图,顶点集合为 {A, B, C, D},边集合为 {(A, B), (A, C), (B, C), (C, D)}。该图的邻接矩阵可以表示为

邻接表

邻接表(Adjacency List)是图论中另一种用于表示图中顶点之间相邻关系的常用数据结构。与邻接矩阵不同,邻接表不会为每一对顶点都分配存储空间,而是仅为图中的每一个顶点分配一个链表(或其他动态集合结构),链表中包含与该顶点相邻的所有顶点。

假设我们有一个包含 4 个顶点的无向图,顶点集合为 {A, B, C, D},边集合为 {(A, B), (A, C), (B, C), (C, D)}。该图的邻接表可以表示为

深度优先遍历(栈实现)

广度优先遍历(队列实现)

最终,广度优先遍历的访问顺序为:A, B, C, D, E

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值