图的深度优先遍历和广度优先遍历--邻接矩阵法


//LinkQueue.h
#ifndef _LINKQUEUE_H_
#define _LINKQUEUE_H_

typedef void LinkQueue;

LinkQueue* LinkQueue_Create();

void LinkQueue_Destroy(LinkQueue* queue);

void LinkQueue_Clear(LinkQueue* queue);

int LinkQueue_Append(LinkQueue* queue, void* item);

void* LinkQueue_Retrieve(LinkQueue* queue);

void* LinkQueue_Header(LinkQueue* queue);

int LinkQueue_Length(LinkQueue* queue);

#endif

//LinkQueue.c
#include <malloc.h>
#include <stdio.h>
#include "LinkQueue.h"

typedef struct _tag_LinkQueueNode TLinkQueueNode;
struct _tag_LinkQueueNode
{
    TLinkQueueNode* next;
    void* item;
};

typedef struct _tag_LinkQueue
{
    TLinkQueueNode* front;
    TLinkQueueNode* rear;
    int length;
} TLinkQueue;

LinkQueue* LinkQueue_Create() // O(1)
{
    TLinkQueue* ret = (TLinkQueue*)malloc(sizeof(TLinkQueue));
    
    if( ret != NULL )
    {
        ret->front = NULL;
        ret->rear = NULL;
        ret->length = 0;
    }
    
    return ret;
}

void LinkQueue_Destroy(LinkQueue* queue) // O(n)
{
    LinkQueue_Clear(queue);
    free(queue);
}

void LinkQueue_Clear(LinkQueue* queue) // O(n)
{
    while( LinkQueue_Length(queue) > 0 )
    {
        LinkQueue_Retrieve(queue);
    }
}

int LinkQueue_Append(LinkQueue* queue, void* item) // O(1)
{
    TLinkQueue* sQueue = (TLinkQueue*)queue;
    TLinkQueueNode* node = (TLinkQueueNode*)malloc(sizeof(TLinkQueueNode));
    int ret = (sQueue != NULL ) && (item != NULL) && (node != NULL);
    
    if( ret )
    {
        node->item = item;
        
        if( sQueue->length > 0 )
        {
            sQueue->rear->next = node;
            sQueue->rear = node;
            node->next = NULL;
        }
        else
        {
            sQueue->front = node;
            sQueue->rear = node;
            node->next = NULL;
        }
        
        sQueue->length++;
    }
    
    if( !ret )
    {
        free(node);
    }
    
    return ret;
}

void* LinkQueue_Retrieve(LinkQueue* queue) // O(1)
{
    TLinkQueue* sQueue = (TLinkQueue*)queue;
    TLinkQueueNode* node = NULL;
    void* ret = NULL;
    
    if( (sQueue != NULL) && (sQueue->length > 0) )
    {
        node = sQueue->front;
        
        sQueue->front = node->next;
        
        ret = node->item;
        
        free(node);
        
        sQueue->length--;
        
        if( sQueue->length == 0 )
        {
            sQueue->front = NULL;
            sQueue->rear = NULL;
        }
    }
    
    return ret;
}

void* LinkQueue_Header(LinkQueue* queue) // O(1)
{
    TLinkQueue* sQueue = (TLinkQueue*)queue;
    void* ret = NULL;
    
    if( (sQueue != NULL) && (sQueue->length > 0) )
    {
        ret = sQueue->front->item;
    }
    
    return ret;
}

int LinkQueue_Length(LinkQueue* queue) // O(1)
{
    TLinkQueue* sQueue = (TLinkQueue*)queue;
    int ret = -1;
    
    if( sQueue != NULL )
    {
        ret = sQueue->length;
    }
    
    return ret;
}

//MGraph.h

#ifndef _MGRAPH_H_
#define _MGRAPH_H_

typedef void MGraph;
typedef void MVertex;
typedef void (MGraph_Printf)(MVertex*);

MGraph* MGraph_Create(MVertex** v,int n);

void MGraph_Destroy(MGraph* graph);

void MGraph_Clear(MGraph* graph);

int MGraph_AddEdge(MGraph* graph,int v1,int v2,int w);

int MGraph_RemoveEdge(MGraph* graph,int v1,int v2);

int MGraph_GetEdge(MGraph* graph,int v1,int v2);

int MGraph_TD(MGraph* graph,int v);

int MGraph_VertexCount(MGraph* graph);

int MGraph_EdgeCount(MGraph* graph);

void MGraph_DFS(MGraph* graph,int v,MGraph_Printf* pFunc);

void MGraph_BFS(MGraph* graph,int v,MGraph_Printf* pFunc);

void MGraph_Display(MGraph* graph,MGraph_Printf* pFunc);

#endif

//MGraph.c

#include "MGraph.h"
#include <malloc.h>
#include <stdio.h>
#include "LinkQueue.h"


typedef struct _tag_MGraph
{
	int count;
	MVertex** v;
	int** matrix;
}TMGraph;
static void recursive_dfs(MGraph* graph,int v,int visited[],MGraph_Printf* pFunc)
{
	TMGraph* tGraph = (TMGraph*)graph;

	pFunc(tGraph->v[v]);
	visited[v] = 1;
	printf(",");
	
	int i = 0;
	for(i=0;i<tGraph->count;i++)
	{
		if((tGraph->matrix[v][i]!=0)&&(!visited[i]))
		{
			recursive_dfs(tGraph,i,visited,pFunc);	
		}	
	}		
}
static void bfs(MGraph* graph,int v,int visited[],MGraph_Printf* pFunc)
{
	TMGraph* tGraph = (TMGraph*)graph;
	
	LinkQueue* queue = LinkQueue_Create();
	if(queue!=NULL)
	{
		LinkQueue_Append(queue,tGraph->v + v);  //	item is not 0;
		visited[v] = 1;

		while(LinkQueue_Length(queue)>0)
		{
			v = (MVertex**)LinkQueue_Retrieve(queue) - tGraph->v;
			pFunc(tGraph->v[v]);
			printf(",");
			
			int i = 0;
			for(i=0;i<tGraph->count;i++)
			{
				if((tGraph->matrix[v][i]!=0)&&(!visited[i]))
				{
					LinkQueue_Append(queue,tGraph->v + i);
					visited[i] = 1;	
				}	
			}		
		}
	}
}
MGraph* MGraph_Create(MVertex** v,int n)
{
	TMGraph* ret = NULL;
	
	if((v!=NULL)&&(n>0))
	{
		ret = (TMGraph*)malloc(sizeof(TMGraph));
		
		if(ret!=NULL)
		{
			int* p = NULL;
			ret->count = n;
			ret->v = (MVertex**)malloc(sizeof(MVertex*)*n);
			ret->matrix = (int**)malloc(sizeof(int*)*n*n);
			
			p = (int*)calloc(n*n,sizeof(int));
			if((ret->v!=NULL)&&(ret->matrix!=NULL)&&(p!=NULL))
			{
				int i = 0;
				for(i=0;i<n;i++)
				{
					ret->v[i] = v[i];          //¶þ¼¶Ö¸ÕëÓñäÁ¿Ãû£¨µØÖ·£©À´¸´ÖÆ£¬£¬²»ÄÜͨ¹ý³£Á¿ 
					ret->matrix[i] = p + i*n;  //¶þ¼¶Ö¸ÕëÓñäÁ¿Ãû£¨µØÖ·£©À´¸´ÖÆ£¬£¬²»ÄÜͨ¹ý³£Á¿ 
				}	
			}	
			else
			{
				free(p);
				free(ret->matrix);
				free(ret->v);
				free(ret);
				
				ret = NULL;	
			}
		}		
	}
	
	return ret;
}

void MGraph_Destroy(MGraph* graph)
{
	TMGraph* tGraph = (TMGraph*)graph;
	if(tGraph!=NULL)
	{
		free(tGraph->v);
		free(tGraph->matrix[0]);  //ÕâÁ½ÌõÓï¾äµÄ˳ÐòºÜÖØÒª 
		free(tGraph->matrix);
		free(tGraph);	
	}	
}
void MGraph_Clear(MGraph* graph)
{
	TMGraph* tGraph = (TMGraph*)graph;
	if(tGraph!=NULL)
	{
		int i = 0;
		int j = 0;
		
		for(i=0;i<tGraph->count;i++)
		{
			for(j=0;j<tGraph->count;j++)
			{
				tGraph->matrix[i][j] = 0;	
			}	
		}
	}
}
int MGraph_AddEdge(MGraph* graph,int v1,int v2,int w)
{
	int ret = 0;
	TMGraph* tGraph = (TMGraph*)graph;
	ret = (tGraph!=NULL) && (v1>=0) && (v1<tGraph->count);
	ret = ret && (v2>=0) && (v2<tGraph->count);
	ret = ret && (w>=0);
	if(ret)
	{
		tGraph->matrix[v1][v2] = w;	
	}
	return ret;
}

int MGraph_RemoveEdge(MGraph* graph,int v1,int v2)
{
	int ret = 0;
	TMGraph* tGraph = (TMGraph*)graph;
	if((tGraph!=NULL)&&(v1>=0)&&(v1<tGraph->count)&&(v2>=0)&&(v2<tGraph->count))
	{
		ret = tGraph->matrix[v1][v2];
		tGraph->matrix[v1][v2] = 0;	
	}
	
	return ret;
}
int MGraph_GetEdge(MGraph* graph,int v1,int v2)
{
	int ret = 0;
	TMGraph* tGraph = (TMGraph*)graph;
	if((tGraph!=NULL)&&(v1>=0)&&(v1<tGraph->count)&&(v2>=0)&&(v2<tGraph->count))
	{
		ret = tGraph->matrix[v1][v2];
		//tGraph->matrix[v1][v2] = 0;	
	}
	
	return ret;
}

int MGraph_TD(MGraph* graph,int v)
{
	int ret = 0;
	TMGraph* tGraph = (TMGraph*)graph;
	if((tGraph!=NULL)&&(v>=0)&&(v<tGraph->count))
	{
		int i = 0;
		for(i=0;i<tGraph->count;i++)
		{
			if(tGraph->matrix[v][i] != 0)   //³ö¶È	
			{
				ret++;	
			}
			if(tGraph->matrix[i][v] != 0)  //Èë¶È 
			{
				ret++;	
			}
		}
	}
	
	return ret;
}

int MGraph_VertexCount(MGraph* graph)
{
	int ret = 0;
	TMGraph* tGraph = (TMGraph*)graph;
	if(tGraph!=NULL)
	{
		ret = tGraph->count;	
	}
	
	return ret;
}

int MGraph_EdgeCount(MGraph* graph)
{
	int ret = 0;
	TMGraph* tGraph = (TMGraph*)graph;
	if(graph!=NULL)
	{
		int i = 0;
		int j = 0;
		for(i=0;i<tGraph->count;i++)
		{
			for(j=0;j<tGraph->count;j++)
			{
				if(tGraph->matrix[i][j]!=0)
				{
					ret++;	
				}	
			}	
		}	
	}
	return ret;
}

void MGraph_DFS(MGraph* graph,int v,MGraph_Printf* pFunc)
{
	TMGraph* tGraph = (TMGraph*)graph;
	int condition = 0;
	int* visited = NULL;
	visited = (int*)calloc(tGraph->count,sizeof(int));
	
	condition = (tGraph!=NULL)&&(pFunc!=NULL);
	condition = condition && (v>=0)&&(condition<tGraph->count);
	condition = condition && (visited!=NULL);
	
	if(condition)
	{
		recursive_dfs(tGraph,v,visited,pFunc);
		int i = 0;
		for(i=0;i<tGraph->count;i++)
		{
			if(!visited[i])
			{
				recursive_dfs(tGraph,i,visited,pFunc);	
			}		
		}	
		printf("\n");
	}
	free(visited);
}

void MGraph_BFS(MGraph* graph,int v,MGraph_Printf* pFunc)
{
	TMGraph* tGraph = (TMGraph*)graph;
	int condition = 0;
	int* visited = NULL;
	visited = (int*)calloc(tGraph->count,sizeof(int));
	
	condition = (tGraph!=NULL)&&(pFunc!=NULL);
	condition = condition&&(v>=0)&&(v<tGraph->count);
	condition = condition&&(visited!=NULL);
	
	if(condition)
	{
		bfs(tGraph,v,visited,pFunc);
		int i = 0;
		for(i=0;i<tGraph->count;i++)
		{
			if(!visited[i])
			{
				bfs(tGraph,i,visited,pFunc);	
			}
				
		}
		printf("\n");
	}
	free(visited);
}

void MGraph_Display(MGraph* graph,MGraph_Printf* pFunc)
{
	TMGraph* tGraph = (TMGraph*)graph;
	if((tGraph!=NULL)&&(pFunc!=NULL))
	{
		int i = 0;
		int j = 0;
		for(i=0;i<tGraph->count;i++)
		{
			printf("%d,",i);
			pFunc(tGraph->v[i]);
			printf(" ");	
		}
				
		printf("\n");
		
		for(i=0;i<tGraph->count;i++)
		{
			for(j=0;j<tGraph->count;j++)
			{
				if(tGraph->matrix[i][j]!=0)
				{
					printf("< ");
					printf(tGraph->v[i]);  //Vertex 1
					printf(",");
					printf(tGraph->v[j]);  //Vertex 2
					printf(", %d",tGraph->matrix[i][j]);
					printf(" >");
					printf(" ");	
				}	
			}	
		}
		printf("\n");
	}
}

//main.c

/*---------------------
*time:2016-4-13
----------------------*/
#include <stdio.h>
#include <stdlib.h>
#include "MGraph.h"

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
void print_data(MVertex* v)
{
	printf("%s",(char*)v);
}
int main(int argc, char *argv[]) 
{
	MVertex* v[] = {"A","B","C","D","E","F"};  //Ö¸ÕëÊý×飬[]µÄÓÅÏȼ¶±È*¸ß£¬Ö¸ÕëÊý×éµÄÿ¸öÔªËØΪָÕëÀàÐÍ£¬Í¨³£À´´æ·Å×Ö·û´® 
	//MVertex** v = {"A","B","C","D","E","F"};   //²»ÖªµÀΪɶ²»ÐÐ £»Ö»ÄܰѱäÁ¿Ãû£¨µØÖ·£©¸³¸øÖ¸Õ룬²»Äܰѳ£Á¿¸³¸øÖ¸Õë 
	MGraph* graph = MGraph_Create(v,6);
	
	MGraph_AddEdge(graph,0,1,1);
	MGraph_AddEdge(graph,0,2,1);
	MGraph_AddEdge(graph,1,2,1);
	MGraph_AddEdge(graph,1,3,1);
	
	MGraph_AddEdge(graph,2,4,1);
	MGraph_AddEdge(graph,3,2,1);
	MGraph_AddEdge(graph,3,5,1);

	
	printf("A Degree : %d\n",MGraph_TD(graph,0));
	
	printf("Vertex Count is : %d\n",MGraph_VertexCount(graph));
	
	printf("Edge Count : %d\n",MGraph_EdgeCount(graph));
	
	MGraph_Display(graph,print_data);
	
	printf("MGraph_DFS: ");
	MGraph_DFS(graph,0,print_data);
	
	printf("MGraph_BFS: ");
	MGraph_BFS(graph,0,print_data);
	
	MGraph_Destroy(graph);
	return 0;
}

1

    原文作者:数据结构之图
    原文地址: https://blog.csdn.net/daa20/article/details/51159738
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞