图的遍历。

#include <stdio.h>
#include <malloc.h>
#include <stdbool.h>

#define QUEUE_SIZE 10

int* visitedPtr;

/**N
 * 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) % 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 -1;
     }//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(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
 
 /**
 * 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 paraStrat){
     //Use a queue to manage the pointers
     int i, j, tempNode;
     i = 0;
     QueuePtr tempQueuePtr = initQueue();
     printf("%d\t", paraStrat);
	visitedPtr[paraStrat] = 1;
	enqueue(tempQueuePtr, paraStrat);
	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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
创建:包括建立结点的函数CreatVex(Graph *G),以及GreatUDG(Graph *G) ,GreatUDN(Graph *G) ,GreatDG(Graph *G) GreatDN(Graph *G) 1提示用户输入的基本信息:顶点数,边数以及的基本类型; 2通过for循环语句提示用户输入顶点的值; 3Graph结构体类型包括:AdjList用来存储头结点的数组;int类型vexnum和arcnum,用来表示顶点数和边数的变量;int类型kind,用来存储的类型。边ArcNode结构包括:adjvex,ArcNode *nextarc,int info前者表示指向的结点的下标,后者表示指向结点的下一条边结点,最后的变量为边所带的权值信息; 4根据的类型决定是否要使用边中的info变量; 5提示用户按照正确的形式输入边的端点以及边上的权值信息; 遍历:包括DFSTraverse(Graph G,VertexType vex)以及DFS(Graph G,int v)两个主要的便历函数。前者第二个参数表示开始进行便历的顶点,后者的第二个参数表示对的下标为v的顶点访问。 1遍历前首先建立一个标志数组Visited[],长度为中结点的数目,用来表示是否访问过一结点,访问前全置为0; 2接收用户要求开始访问的顶点,通过函数Adjfound(Graph G,VertexType c)找到改点在的结点中的下标; 3若该下标对应的标志数组的值为0,访问该下标的firstArcNode结点,同时把该结点的在访问标志数组中的值设置为1;若该下标对应的标志数组的值为1,则进行第5步; 4继续进行操作2; 5在标志数组中查找仍为0的项,得到下标值再进行第1步操作;如果都访问过则遍历结束。 6退出程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值