在线性表,数据元素之间是被串起来的,仅有线性关系,每个数据元素只有一个直接前驱和一个直接后继,即存在一对一的关系。在树形结构中,数据元素之间有着明显的层次关系,并且每一层上的数据元素可能和下一层中多个元素相关,但只能和上一层中一个元素相关,即存在一对多的关系。图是一种较线性表和数更加复杂的数据结构。在图结构中,结点之间的关系可以是任意的,图中任意两个数据元素之间都可能相关,图存在多对多的关系。
图(Graph)是由顶点的有穷非空集合和顶点之间边的集合组成,通常表示为:G(V, E),其中,G 表示一个图,V 是图 G 中顶点的集合,E 是图 G 中边的集合。
对于图的定义,我们需要明确几个注意的地方。
- 线性表中我们把数据元素叫元素,树中将数据元素叫结点,在图中数据元素我们则称之为顶点(Vertex)。
- 在图结构中,不允许没有顶点,在定义中,若 V 是顶点的集合,则强调了顶点集合 V 有穷非空。但是允许边集 E 为空。
老师的代码
#include <stdio.h>
#include <malloc.h>
#define QUEUE_SIZE 10
int* visitedPtr;
/**
* 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;
}//Of initQueue
/**
* Is the queue empty?
*/
bool isQueueEmpty(QueuePtr paraQueuePtr){
if ((paraQueuePtr->front + 1) % QUEUE_SIZE == paraQueuePtr->rear) {
return true;
}//Of if
return false;
}//Of isQueueEmpty
/**
* Add a node to the queue.
*/
void enqueue(QueuePtr paraQueuePtr, int paraNode){
//printf("front = %d, rear = %d.\r\n", paraQueuePtr->front, paraQueuePtr->rear);
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;
//printf("enqueue %d ends.\r\n", paraNode);
}//Of enqueue
/**
* Remove an element from the queue and return.
*/
int dequeue(QueuePtr paraQueuePtr){
if (isQueueEmpty(paraQueuePtr)) {
printf("Error, empty queue\r\n");
return NULL;
}//Of if
paraQueuePtr->front = (paraQueuePtr->front + 1) % QUEUE_SIZE;
//printf("dequeue %d ends.\r\n", paraQueuePtr->nodes[paraQueuePtr->front]);
return paraQueuePtr->nodes[paraQueuePtr->front];
}//Of dequeue
/**
* The structure of a graph.
*/
typedef struct Graph{
int** connections;
int numNodes;
} *GraphPtr;
//void deepFirst(GraphPtr paraGraphPtr, int paraNode);
GraphPtr initGraph(int paraSize, int** paraData) {
int i, j;
GraphPtr resultPtr = (GraphPtr)malloc(sizeof(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;
}//Of initGraph
/**
* Initialize the tranverse.
*/
void initTranverse(GraphPtr paraGraphPtr) {
int i;
//Initialize data
visitedPtr = (int*) malloc(paraGraphPtr -> numNodes * sizeof(int));
for (i = 0; i < paraGraphPtr -> numNodes; i ++) {
visitedPtr[i] = 0;
}//Of for i
}//Of initTranverse
/**
* Depth first tranverse.
*/
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);
}//Of if
}//Of if
}//Of for i
}//Of depthFirstTranverse
/**
* Width first tranverse.
*/
void widthFirstTranverse(GraphPtr paraGraphPtr, int paraStart){
//Use a queue to manage the pointers
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;
//For output.
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);
}//Of for j
}//Of while
}//Of widthFirstTranverse
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));
}//Of for i
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);
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);
}//Of testGraphTranverse
int main(){
testGraphTranverse();
return 1;
}//Of main
运行结果
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 Press any key to continue
我的代码
#include "stdio.h"
#include "stdlib.h"
#include "math.h"
#include "time.h"
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef int Status; /* Status是函数的类型,其值是函数结果状态代码,如OK等 */
typedef int Boolean; /* Boolean是布尔类型,其值是TRUE或FALSE */
typedef char VertexType; /* 顶点类型应由用户定义 */
typedef int EdgeType; /* 边上的权值类型应由用户定义 */
#define MAXSIZE 9 /* 存储空间初始分配量 */
#define MAXEDGE 15
#define MAXVEX 9
typedef struct
{
VertexType vexs[MAXVEX]; /* 顶点表 */
EdgeType arc[MAXVEX][MAXVEX];/* 邻接矩阵,可看作边表 */
int numVertexes, numEdges; /* 图中当前的顶点数和边数 */
}MGraph;
/* 用到的队列结构与函数********************************** */
/* 循环队列的顺序存储结构 */
typedef struct
{
int data[MAXSIZE];
int front; /* 头指针 */
int rear; /* 尾指针,若队列不空,指向队列尾元素的下一个位置 */
}Queue;
/* 初始化一个空队列Q */
Status InitQueue(Queue *Q)
{
Q->front=0;
Q->rear=0;
return OK;
}
/* 若队列Q为空队列,则返回TRUE,否则返回FALSE */
Status QueueEmpty(Queue Q)
{
if(Q.front==Q.rear) /* 队列空的标志 */
return TRUE;
else
return FALSE;
}
/* 若队列未满,则插入元素e为Q新的队尾元素 */
Status EnQueue(Queue *Q,int e)
{
if ((Q->rear+1)%MAXSIZE == Q->front) /* 队列满的判断 */
return ERROR;
Q->data[Q->rear]=e; /* 将元素e赋值给队尾 */
Q->rear=(Q->rear+1)%MAXSIZE;/* rear指针向后移一位置, */
/* 若到最后则转到数组头部 */
return OK;
}
/* 若队列不空,则删除Q中队头元素,用e返回其值 */
Status DeQueue(Queue *Q,int *e)
{
if (Q->front == Q->rear) /* 队列空的判断 */
return ERROR;
*e=Q->data[Q->front]; /* 将队头元素赋值给e */
Q->front=(Q->front+1)%MAXSIZE; /* front指针向后移一位置, */
/* 若到最后则转到数组头部 */
return OK;
}
/* ****************************************************** */
void CreateMGraph(MGraph *G)
{
int i, j;
G->numEdges=15;
G->numVertexes=9;
/* 读入顶点信息,建立顶点表 */
G->vexs[0]='A';
G->vexs[1]='B';
G->vexs[2]='C';
G->vexs[3]='D';
G->vexs[4]='E';
G->vexs[5]='F';
G->vexs[6]='G';
G->vexs[7]='H';
G->vexs[8]='I';
for (i = 0; i < G->numVertexes; i++)/* 初始化图 */
{
for ( j = 0; j < G->numVertexes; j++)
{
G->arc[i][j]=0;
}
}
G->arc[0][1]=1;
G->arc[0][5]=1;
G->arc[1][2]=1;
G->arc[1][8]=1;
G->arc[1][6]=1;
G->arc[2][3]=1;
G->arc[2][8]=1;
G->arc[3][4]=1;
G->arc[3][7]=1;
G->arc[3][6]=1;
G->arc[3][8]=1;
G->arc[4][5]=1;
G->arc[4][7]=1;
G->arc[5][6]=1;
G->arc[6][7]=1;
for(i = 0; i < G->numVertexes; i++)
{
for(j = i; j < G->numVertexes; j++)
{
G->arc[j][i] =G->arc[i][j];
}
}
}
Boolean visited[MAXVEX]; /* 访问标志的数组 */
/* 邻接矩阵的深度优先递归算法 */
void DFS(MGraph G, int i)
{
int j;
visited[i] = TRUE;
printf("%c ", G.vexs[i]);/* 打印顶点,也可以其它操作 */
for(j = 0; j < G.numVertexes; j++)
if(G.arc[i][j] == 1 && !visited[j])
DFS(G, j);/* 对为访问的邻接顶点递归调用 */
}
/* 邻接矩阵的深度遍历操作 */
void DFSTraverse(MGraph G)
{
int i;
for(i = 0; i < G.numVertexes; i++)
visited[i] = FALSE; /* 初始所有顶点状态都是未访问过状态 */
for(i = 0; i < G.numVertexes; i++)
if(!visited[i]) /* 对未访问过的顶点调用DFS,若是连通图,只会执行一次 */
DFS(G, i);
}
/* 邻接矩阵的广度遍历算法 */
void BFSTraverse(MGraph G)
{
int i, j;
Queue Q;
for(i = 0; i < G.numVertexes; i++)
visited[i] = FALSE;
InitQueue(&Q); /* 初始化一辅助用的队列 */
for(i = 0; i < G.numVertexes; i++) /* 对每一个顶点做循环 */
{
if (!visited[i]) /* 若是未访问过就处理 */
{
visited[i]=TRUE; /* 设置当前顶点访问过 */
printf("%c ", G.vexs[i]);/* 打印顶点,也可以其它操作 */
EnQueue(&Q,i); /* 将此顶点入队列 */
while(!QueueEmpty(Q)) /* 若当前队列不为空 */
{
DeQueue(&Q,&i); /* 将队对元素出队列,赋值给i */
for(j=0;j<G.numVertexes;j++)
{
/* 判断其它顶点若与当前顶点存在边且未访问过 */
if(G.arc[i][j] == 1 && !visited[j])
{
visited[j]=TRUE; /* 将找到的此顶点标记为已访问 */
printf("%c ", G.vexs[j]); /* 打印顶点 */
EnQueue(&Q,j); /* 将找到的此顶点入队列 */
}
}
}
}
}
}
int main(void)
{
MGraph G;
CreateMGraph(&G);
printf("\n深度遍历:");
DFSTraverse(G);
printf("\n广度遍历:");
BFSTraverse(G);
return 0;
}