图
在线性表中,数据元素之间是被串起来的,仅有线性关系,每个数据元素只有一个直接前驱和一个直接后继。在树形结构中,数据元素之间有着明显的层次关系,并且每一层上的数据元素可能和下一层中多个元素相关,但只能和上一层中一个元素相关。图是一种较线性表和树更加复杂的数据结构。在图形结构中,结点之间的关系可以是任意的,图中任意两个数据元素之间都可能相关。
邻接矩阵用两个数组保存数据。一个一维数组存储图中顶点信息,一个二维数组存储图中边或弧的信息。
头文件及结构体
#include <stdio.h>
#include <malloc.h>
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(5 * sizeof(int *));
for (i = 0; i < paraSize; i++) {
resultPtr->connections[i] = (int *)malloc(5 * sizeof(int));
for (j = 0; j < 5; 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 (paraGraphPtr->connections[paraNode][i] && visitedPtr[i] == 0) {
depthFirstTranverse(paraGraphPtr, i);
}
}
}
void widthFirstTranverse(GraphPtr paraGraphPtr, int paraNode, int cnt) {
int tempNode[paraGraphPtr->numNodes];
int i, front = 0;
visitedPtr[paraNode] = 1;
tempNode[cnt] = paraNode;
while ((cnt + 1) != paraGraphPtr->numNodes) {
paraNode = tempNode[front];
for (i = 0; i < paraGraphPtr->numNodes; i++) {
if (visitedPtr[i])
continue;
if (paraGraphPtr->connections[paraNode][i] == 0)
continue;
visitedPtr[i] = 1;
cnt++;
tempNode[cnt] = i;
}
front++;
}
for (i = 0; i < paraGraphPtr->numNodes; i++) {
printf("%d\t", tempNode[i]);
}
}
广度优先遍历我用来一个数组来代替了队列的功能,相比起来在此处数组相对简洁一些。
测试函数和程序入口
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, 0);
}
int main() {
testGraphTranverse();
return 0;
}
输出结果
Preparing data
Data ready
num nodes = 5
Graph initialized
Depth first visit:
4 1 0 3 2
Width first visit:
4 1 2 0 3
邻接表
我们发现,当图中的边数相对于顶点较少时,邻接矩阵是对存储空间的极大浪费。我们可以考虑对边或弧使用链式存储的方式来避免空间浪费的问题。回忆树结构的孩子表示法,将结点存入数组,并对结点的孩子进行链式存储,不管有多少孩子,也不会存在空间浪费问题。
应用这种思路,我们把这种数组与链表相结合的存储方法称为邻接表(Adjacency List)。
邻接表的处理办法是这样。
1) 图中顶点用一个一维数组存储,当然也可以用单链表来存储,不过用数组可以较容易的读取顶点信息,更加方便。另外,对于顶点数组中,每个数据元素还需要存储指向第一个邻接点的指针,以便于查找该顶点的边信息。
2) 图中每个顶点vi的所有邻接点构成一个线性表,由于邻接点的个数不定,所以用单链表存储,无向图称为顶点vi的边表,有向图则称为以vi为弧尾的出边表。
下图是一个无向图的邻接表结构:
头文件及结构体
#include <stdio.h>
#include <malloc.h>
#define QUEUE_SIZE 10
#define true 1
#define false 0
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;
}
/**
* 判断队列是否满
*/
int 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("The node %d connects %d, ", i, p->column);
p = p->next;
}
printf("\r\n");
}
}
利用队列的广度优先搜索
/**
* 广度优先搜索
*/
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);
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 0;
}
输出结果
Preparing data
Data ready
This is the graph:
The node 0 connects 1, The node 0 connects 3,
The node 1 connects 0, The node 1 connects 2, The node 1 connects 4,
The node 2 connects 1, The node 2 connects 3, The node 2 connects 4,
The node 3 connects 0, The node 3 connects 2,
The node 4 connects 1, The node 4 connects 2,
width first
4 1 2 0 3
完整代码
#include <stdio.h>
#include <malloc.h>
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(5 * sizeof(int *));
for (i = 0; i < paraSize; i++) {
resultPtr->connections[i] = (int *)malloc(5 * sizeof(int));
for (j = 0; j < 5; 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 (paraGraphPtr->connections[paraNode][i] && visitedPtr[i] == 0) {
depthFirstTranverse(paraGraphPtr, i);
}
}
}
void widthFirstTranverse(GraphPtr paraGraphPtr, int paraNode, int cnt) {
int tempNode[paraGraphPtr->numNodes];
int i, front = 0;
visitedPtr[paraNode] = 1;
tempNode[cnt] = paraNode;
while ((cnt + 1) != paraGraphPtr->numNodes) {
paraNode = tempNode[front];
for (i = 0; i < paraGraphPtr->numNodes; i++) {
if (visitedPtr[i])
continue;
if (paraGraphPtr->connections[paraNode][i] == 0)
continue;
visitedPtr[i] = 1;
cnt++;
tempNode[cnt] = i;
}
front++;
}
for (i = 0; i < paraGraphPtr->numNodes; i++) {
printf("%d\t", tempNode[i]);
}
}
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, 0);
}
int main() {
testGraphTranverse();
return 0;
}
#include <stdio.h>
#include <malloc.h>
#define QUEUE_SIZE 10
#define true 1
#define false 0
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;
}
/**
* 判断队列是否满
*/
int 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("The node %d connects %d, ", i, p->column);
p = p->next;
}
printf("\r\n");
}
}
/**
* 广度优先搜索
*/
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);
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 0;
}