图的定义
图是由顶点的有穷非空集合和顶点之间边的集合组成,表示为G(V,E),其中G表示一个图,V是图G中顶点的集合,E是图G中边的集合。
图遍历
图遍历,别称是图的遍历,是一个数理学术语,是指数据结构中的内容。指的是从图中的任一顶点出发,对图中的所有顶点访问一次且只访问一次。
图的遍历方法目前有深度优先搜索法和广度(宽度)优先搜索法两种算法。
深度优先
深度优先搜索法是树的先根遍历的推广,它的基本思想是:从图G的某个顶点v0出发,访问v0,然后选择一个与v0相邻且没被访问过的顶点vi访问,再从vi出发选择一个与vi相邻且未被访问的顶点vj进行访问,依次继续。如果当前被访问过的顶点的所有邻接顶点都已被访问,则退回到已被访问的顶点序列中最后一个拥有未被访问的相邻顶点的顶点w,从w出发按同样的方法向前遍历,直到图中所有顶点都被访问。其递归算法如下:
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);
}
}
}
}
广度优先
图的广度优先搜索是树的按层次遍历的推广,它的基本思想是:首先访问初始点vi,并将其标记为已访问过,接着访问vi的所有未被访问过的邻接点vi1,vi2,…, vi t,并均标记已访问过,然后再按照vi1,vi2,…, vi t的次序,访问每一个顶点的所有未被访问过的邻接点,并均标记为已访问过,依次类推,直到图中所有和初始点vi有路径相通的顶点都被访问过为止。其非递归算法如下:
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);
}
}
}
总代码
#include <stdio.h>
#include <malloc.h>
#include <stdbool.h>
#define QUEUE_SIZE 10
int* visitedPtr;//全局变量
typedef struct GraphNodeQueue{
int* nodes;
int front;
int rear;
}GraphNodeQueue,*QueuePtr;
QueuePtr initQueue (){
QueuePtr resultPtr = (QueuePtr)malloc(sizeof(struct GraphNodeQueue));
resultPtr->nodes = (int*)malloc(QUEUE_SIZE*sizeof(int));
resultPtr->front = 0;
resultPtr->rear = 1;
return resultPtr;
}//队列初始化
//空队列判断
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("错误,队满\r\n");
return;
}
paraQueuePtr->nodes[paraQueuePtr->rear] = paraNode;
paraQueuePtr->rear = (paraQueuePtr->rear+1)%QUEUE_SIZE;
}
int dequeue(QueuePtr paraQueuePtr){
if(isQueueEmpty(paraQueuePtr)){
printf("队列为空\r\n");
return NULL;
}
paraQueuePtr->front = (paraQueuePtr->front + 1) % QUEUE_SIZE;
return paraQueuePtr->nodes[paraQueuePtr->front];
}
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;
}
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 (!visitedPtr[i]){
if (paraGraphPtr -> connections[paraNode][i]) {
depthFirstTranverse(paraGraphPtr, i);
}
}
}
}
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("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);
}
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