1.代码:
先上代码,废话少说。
/**
* Adjacency list for directed graph.
*
* @author Fan Min minfanphd@163.com.
*/
#include <stdio.h>
#include <malloc.h>
#define QUEUE_SIZE 10
/*************** Copied code begins *****************/
int* visitedPtr;
/**
* The structure of a graph.
*/
typedef struct Graph{
int** connections;
int numNodes;
} *GraphPtr;
/**
* Initialize a graph.
*/
GraphPtr initGraph(int paraSize, int** paraData) {
int i, j;
GraphPtr resultPtr = (GraphPtr)malloc(sizeof(struct 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
/**
* 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){
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;
}//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
/*************** Copied code ends *****************/
/**
* Aajacent node.
*/
typedef struct AdjacencyNode {
int column;
AdjacencyNode* next;
}AdjacencyNode, *AdjacentNodePtr;
/**
* Aajacent list.
*/
typedef struct AdjacencyList {
int numNodes;
AdjacencyNode* headers;
}AdjacencyList, *AdjacencyListPtr;
/**
* Construct an adjacent list.
*/
AdjacencyListPtr graphToAdjacentList(GraphPtr paraPtr) {
//Allocate space.
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));
//Fill the data.
for (i = 0; i < tempNum; i ++) {
//Initialize headers.
p = &(resultPtr->headers[i]);
p->column = -1;
p->next = NULL;
for (j = 0; j < tempNum; j ++) {
if (paraPtr->connections[i][j] > 0) {
//Create a new node.
q = (AdjacentNodePtr)malloc(sizeof(struct AdjacencyNode));
q->column = j;
q->next = NULL;
//Link.
p->next = q;
p = q;
}//Of if
}//Of for j
}//Of for i
return resultPtr;
}//Of graphToAdjacentList
/**
* Print an adjacent list.
*/
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;
}//Of while
printf("\r\n");
}//Of for i
}//Of printAdjacentList
/**
* Width first tranverse.
*/
void widthFirstTranverse(AdjacencyListPtr paraListPtr, int paraStart){
printf("width first \r\n");
//Use a queue to manage the pointers
int i, j, tempNode;
AdjacentNodePtr p;
i = 0;
//Initialize data
visitedPtr = (int*) malloc(paraListPtr->numNodes * sizeof(int));
for (i = 0; i < paraListPtr->numNodes; i ++) {
visitedPtr[i] = 0;
}//Of for i
QueuePtr tempQueuePtr = initQueue();
printf("%d\t", paraStart);
visitedPtr[paraStart] = 1;
enqueue(tempQueuePtr, paraStart);
// printf("After enqueue\r\n");
while (!isQueueEmpty(tempQueuePtr)) {
// printf("First while\r\n");
tempNode = dequeue(tempQueuePtr);
for (p = &(paraListPtr->headers[tempNode]); p != NULL; p = p->next) {
j = p->column;
// printf("j = %d \r\n", j);
if (visitedPtr[j])
continue;
printf("%d\t", j);
visitedPtr[j] = 1;
enqueue(tempQueuePtr, j);
}//Of for
}//Of while
printf("\r\n");
}//Of widthFirstTranverse
/**
* Test graph tranverse.
*/
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);
AdjacencyListPtr tempListPtr = graphToAdjacentList(tempGraphPtr);
printAdjacentList(tempListPtr);
widthFirstTranverse(tempListPtr, 4);
}//Of testGraphTranverse
/**
* Entrance.
*/
int main(){
testGraphTranverse();
return 1;
}//Of main
2.运行结果:
3.代码说明:
邻接表是图的一种常用表示方法,它由一个链表数组组成。数组中每个元素表示一个节点,而链表则表示该节点与其它节点的关系。具体来说,每个节点的链表包含了该节点所连出去的所有边所对应的目标节点,这些目标节点被称为该节点的邻居节点。
邻接表的优点在于它可以节省存储空间,因为只需要存储有边的节点即可,而没有边的节点可以省略。缺点在于它不太便于查找节点的入度。此外,邻接表还可以使用优先队列实现Dijkstra最短路径算法。
广度优先遍历(BFS)是一种从起始节点开始逐层访问节点的遍历方式。广度优先遍历需要借助队列数据结构来实现。从起始节点开始,先将其入队,然后从队头开始,依次将队列中的节点出队,并将其未访问过的邻居节点入队。直到队列为空时,遍历结束。在进行BFS遍历时,需要使用一个标记数组记录每个节点是否被访问过,防止重复访问。
在使用邻接表表示图时,进行广度优先遍历的方法是首先将起始节点加入队列,然后从队头开始出队,再遍历出队节点的所有邻居节点,并将未被访问的节点加入队列。重复以上过程,直到队列为空。每遍历一个节点,就将它标记为已访问,以避免重复遍历。