数据结构之邻接表及广度优先遍历

一、邻接表的概念

邻接表是图的一种最主要存储结构(相当于图的压缩存储),用来描述图上的每一个点。图的邻接表存储方法跟树的孩子链表示法相类似,是一种顺序分配和链式分配相结合的存储结构。如这个表头结点所对应的顶点存在相邻顶点,则把相邻顶点依次存放于表头结点所指向的单向链表中。表结点存放的是邻接顶点在数组中的索引。对于无向图来说,使用邻接表进行存储也会出现数据冗余,表头结点A所指链表中存在一个指向C的表结点的同时,表头结点C所指链表也会存在一个指向A的表结点。

广度优先搜索遍历类似于树的按层次遍历,对于用邻接表做存储结构的图,从某个给定 顶点出发的图的遍历得到的访问结点顶点次序,随建立的邻接表的不同而可能不同

二、代码步骤

1、创建图

2、图的初始化

3、创建链队列

4、队列的初始化

5、判断队列是否为空

6、入队

7、出队

8、邻接表的结构体定义

9、定义遍历数组

10、将图转为邻接表

11、打印邻接表

12、广度遍历

13、测试代码

14、程序入口

15、运行结果


三、代码功能

1、创建图

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

2、图的初始化

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

3、创建链队列

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

4、队列的初始化

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

5、判断队列是否为空

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

	return false;
}

6、入队

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

7、出队

int dequeue(QueuePtr paraQueuePtr)
{
	if (isQueueEmpty(paraQueuePtr)) 
	{
		printf("Error, empty queue\r\n");
		return NULL;
	}
	paraQueuePtr->front = (paraQueuePtr->front + 1) % QUEUE_SIZE;

	return paraQueuePtr->nodes[paraQueuePtr->front];
}

8、邻接表的结构体定义

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

/**
 * Aajacent list.
 */
typedef struct AdjacencyList 
{
	int numNodes;
	AdjacencyNode* headers;
}AdjacencyList, *AdjacencyListPtr;

9、定义遍历数组

int* visitedPtr;

10、将图转为邻接表

AdjacencyListPtr graphToAdjacentList(GraphPtr paraPtr) 
{
	//Allocate space.
	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));
	
	//Fill the data.
	for (i = 0; i < tempNum; i ++)
	{
		//Initialize headers.
		p = &(resultPtr->headers[i]);
		p->column = -1;
		p->next = NULL;

		for (j = 0; j < tempNum; j ++) 
		{
			if (paraPtr->connections[i][j] > 0) 
			{
				//Create a new node.
				q = (AdjacentNodePtr)malloc(sizeof(struct AdjacencyNode));
				q->column = j;
				q->next = NULL;
				p->next = q;
				p = q;
			}
		}
	}
	return resultPtr;
}

11、打印邻接表

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;
		}
		printf("\r\n");
	}
}

12、广度遍历

void widthFirstTranverse(AdjacencyListPtr paraListPtr, int paraStart)
{
	printf("width first \r\n");
	//Use a queue to manage the pointers
	int i, j, tempNode;
	AdjacentNodePtr p;
	i = 0;

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

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

		for (p = &(paraListPtr->headers[tempNode]); p != NULL; p = p->next) 
		{
			j = p->column;
			if (visitedPtr[j]) 
				continue;

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

13、测试代码

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);
	AdjacencyListPtr tempListPtr = graphToAdjacentList(tempGraphPtr);

	printAdjacentList(tempListPtr);

	widthFirstTranverse(tempListPtr, 4);
}

14、程序入口

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

15、运行结果

Preparing data
Data ready
This is the graph:
1, 3,
0, 2, 4,
1, 3, 4,
0, 2,
1, 2,
width first
4       1       2       0       3

四、整体代码


#include 
#include 
#define QUEUE_SIZE 10


int* visitedPtr;

/**
 * The structure of a graph.
 */
typedef struct Graph
{
	int** connections;
	int numNodes;
} *GraphPtr;

/**
 * Initialize a graph.
 */
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;
}//Of initGraph

/**
 * A queue with a number of indices.
 */
typedef struct GraphNodeQueue
{
	int* nodes;
	int front;
	int rear;
}GraphNodeQueue, *QueuePtr;

/**
 * Initialize the queue.
 */
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;
}

/**
 * Is the queue empty?
 */
bool isQueueEmpty(QueuePtr paraQueuePtr)
{
	if ((paraQueuePtr->front + 1) % QUEUE_SIZE == paraQueuePtr->rear) 
	{
		return true;
	}

	return false;
}

/**
 * Add a node to the queue.
 */
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;
}

/**
 * Remove an element from the queue and return.
 */
int dequeue(QueuePtr paraQueuePtr)
{
	if (isQueueEmpty(paraQueuePtr)) 
	{
		printf("Error, empty queue\r\n");
		return NULL;
	}
	paraQueuePtr->front = (paraQueuePtr->front + 1) % QUEUE_SIZE;

	return paraQueuePtr->nodes[paraQueuePtr->front];
}

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

/**
 * Aajacent list.
 */
typedef struct AdjacencyList 
{
	int numNodes;
	AdjacencyNode* headers;
}AdjacencyList, *AdjacencyListPtr;

/**
 * Construct an adjacent list.
 */
AdjacencyListPtr graphToAdjacentList(GraphPtr paraPtr) 
{
	//Allocate space.
	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));
	
	//Fill the data.
	for (i = 0; i < tempNum; i ++)
	{
		//Initialize headers.
		p = &(resultPtr->headers[i]);
		p->column = -1;
		p->next = NULL;

		for (j = 0; j < tempNum; j ++) 
		{
			if (paraPtr->connections[i][j] > 0) 
			{
				//Create a new node.
				q = (AdjacentNodePtr)malloc(sizeof(struct AdjacencyNode));
				q->column = j;
				q->next = NULL;
				p->next = q;
				p = q;
			}
		}
	}
	return resultPtr;
}

/**
 * Print an adjacent list.
 */
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;
		}
		printf("\r\n");
	}
}

/**
 * Width first tranverse.
 */
void widthFirstTranverse(AdjacencyListPtr paraListPtr, int paraStart)
{
	printf("width first \r\n");
	//Use a queue to manage the pointers
	int i, j, tempNode;
	AdjacentNodePtr p;
	i = 0;

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

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

		for (p = &(paraListPtr->headers[tempNode]); p != NULL; p = p->next) 
		{
			j = p->column;
			if (visitedPtr[j]) 
				continue;

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

/**
 * Test graph tranverse.
 */
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);
	AdjacencyListPtr tempListPtr = graphToAdjacentList(tempGraphPtr);

	printAdjacentList(tempListPtr);

	widthFirstTranverse(tempListPtr, 4);
}

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

你可能感兴趣的:(数据结构,宽度优先,链表,c语言)