数据结构15:图的邻接表存储

邻接表

1.定义

邻接表相当于图的压缩存储,当一个图是稀疏图(即边数较少)时,用矩阵存储会导致大量空间的浪费,而用邻接表则可以很好地解决这个问题。

邻接表是顺序和链式存储相结合的存储方式,用一个数组来存储图的每个结点,将结点作为表头。其他邻接点作为边表,与表头构成单链表。

2.基本操作

1.定义表头结点

//表头结点 
typedef struct AdjacencyList
{
	int numNodes;
	AdjacencyNode* headers;
}AdjacencyList, *AdjacencyListPtr;

2.定义边表结点

//边表结点 
typedef struct AdjacencyNode
{
	int column;
	struct AdjacencyNode* next;
} AdjacencyNode, *AdjacentNodePtr;

3.创建邻接表

 

//创建邻接表 
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;
			}
		}
	}
	return resultPtr;
}

4.打印邻接表

//打印邻接表 
void printAdjacentList(AdjacencyListPtr paraPtr)
{
	int i;
	AdjacentNodePtr p;
	int tempNum = paraPtr->numNodes;

	printf("图的邻接表存储:\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");
	}
}

3.广度优先遍历

//广度优先遍历 
void widthFirstTranverse(AdjacencyListPtr paraListPtr, int paraStart)
{
	printf("广度优先遍历:\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 ", 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 ", j);
			visitedPtr[j] = 1;
			enqueue(tempQueuePtr, j);
		}
	}
	printf("\r\n");
}

总代码

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

//队列 
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;
}//Of isQueueEmpty

//入队 
void enqueue(QueuePtr paraQueuePtr, int paraNode){
	
	if((paraQueuePtr->rear + 1) % QUEUE_SIZE == paraQueuePtr->front % QUEUE_SIZE)
	{
		printf("队满,不能入队.\r\n", paraNode);
		return;
	}
	paraQueuePtr->nodes[paraQueuePtr->rear] = paraNode;
	paraQueuePtr->rear = (paraQueuePtr->rear + 1) % QUEUE_SIZE;
}

//出队 
int dequeue(QueuePtr paraQueuePtr)
{
	if(isQueueEmpty(paraQueuePtr))
	{
		printf("队空,不能出队.\r\n");
		return -1;
	}
	paraQueuePtr->front = (paraQueuePtr->front + 1) % QUEUE_SIZE;
	
	return paraQueuePtr->nodes[paraQueuePtr->front];
}

//边表结点 
typedef struct AdjacencyNode
{
	int column;
	struct 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;
			}
		}
	}
	return resultPtr;
}

//打印邻接表 
void printAdjacentList(AdjacencyListPtr paraPtr)
{
	int i;
	AdjacentNodePtr p;
	int tempNum = paraPtr->numNodes;

	printf("图的邻接表存储:\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");
	}
}

//广度优先遍历 
void widthFirstTranverse(AdjacencyListPtr paraListPtr, int paraStart)
{
	printf("广度优先遍历:\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 ", 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 ", j);
			visitedPtr[j] = 1;
			enqueue(tempQueuePtr, j);
		}
	}
	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("图的矩阵形式:\n");
	for(i = 0;i < 5;i ++)
	{
		for(j = 0;j < 5;j ++)
		{
			printf("%d ",myGraph[i][j]);
		}
		printf("\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];
		}
	}
	GraphPtr tempGraphPtr = initGraph(5, tempPtr);
	AdjacencyListPtr tempListPtr = graphToAdjacentList(tempGraphPtr);
	printAdjacentList(tempListPtr);
	widthFirstTranverse(tempListPtr, 4);
}


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

 运行结果

图的矩阵形式:
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
图的邻接表存储:
1  3
0  2  4
1  3  4
0  2
1  2
广度优先遍历:
4 1 2 0 3

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值