前言
我们发现,当图中的边数相对于顶点较少时,邻接矩阵是对存储空间的极大浪费。我们可以考虑对边或弧使用链式存储的方式来避免空间浪费的问题。回忆树结构的孩子表示法,将结点存入数组,并对结点的孩子进行链式存储,不管有多少孩子,也不会存在空间浪费问题。
一、邻接表结构
1、 图中顶点用一个一维数组存储,当然也可以用单链表来存储,不过用数组可以较容易的读取顶点信息,更加方便。另外,对于顶点数组中,每个数据元素还需要存储指向第一个邻接点的指针,以便于查找该顶点的边信息。
2、 图中每个顶点vi的所有邻接点构成一个线性表,由于邻接点的个数不定,所以用单链表存储,无向图称为顶点vi的边表,有向图则称为以vi为弧尾的出边表。
无向图的邻接表:
有向图的邻接表:
有向图的逆邻接表:
带权值的网图得邻接表:
二、完整代码
#include <stdio.h>
#include <malloc.h>
#include <stdbool.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;
}
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 NULL;
}
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("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");
}
}
void widthFirstTranverse(AdjacencyListPtr paraListPtr, int paraStart)
{
printf("width first \r\n");
AdjacentNodePtr p;
int 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);
int tempNode,j;
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);
}
}
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 ++)
{
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;
}