• 图的遍历(邻居(数组+队列)实例


    1.#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"};
        MGraph* graph = MGraph_Create(v, 6);
        
        MGraph_AddEdge(graph, 0, 1, 1);
        MGraph_AddEdge(graph, 0, 2, 1);
        MGraph_AddEdge(graph, 0, 3, 1);
        MGraph_AddEdge(graph, 1, 5, 1);
        MGraph_AddEdge(graph, 1, 4, 1);
        MGraph_AddEdge(graph, 2, 1, 1);
        MGraph_AddEdge(graph, 3, 4, 1);
        MGraph_AddEdge(graph, 4, 2, 1);
        //打印顶点与边的信息
        MGraph_Display(graph, print_data);
        
        MGraph_DFS(graph, 0, print_data);
        MGraph_BFS(graph, 0, print_data);
        
        MGraph_Destroy(graph);
        
        return 0;
    }

    2.
    #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);
    //v点的度数
    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

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

    /* 邻居矩阵法 */
    typedef struct _tag_MGraph
    {
        int count;
        MVertex** v;
        int** matrix;
    } TMGraph;
    //深度优先遍历算法实现
    static void recursive_dfs(TMGraph* graph, int v, int visited[], MGraph_Printf* pFunc)
    {
        int i = 0;
        
        pFunc(graph->v[v]);
        
        visited[v] = 1;
        
        printf(", ");
        
        for(i=0; i<graph->count; i++)
        {
            if( (graph->matrix[v][i] != 0) && !visited[i] )
            {
                recursive_dfs(graph, i, visited, pFunc);
            }
        }
    }
    //广度优先遍历算法实现
    static void bfs(TMGraph* graph, int v, int visited[], MGraph_Printf* pFunc)
    {
        LinkQueue* queue = LinkQueue_Create();
        
        if( queue != NULL )
        {
            LinkQueue_Append(queue, graph->v + v);
            
            visited[v] = 1;
            
            while( LinkQueue_Length(queue) > 0 )
            {
                int i = 0;
                
                v = (MVertex**)LinkQueue_Retrieve(queue) - graph->v;
                
                pFunc(graph->v[v]);
                
                printf(", ");
                
                for(i=0; i<graph->count; i++)
                {
                    if( (graph->matrix[v][i] != 0) && !visited[i] )
                    {
                        LinkQueue_Append(queue, graph->v + i);
                        
                        visited[i] = 1;
                    }
                }
            }
        }
        
        LinkQueue_Destroy(queue);
    }
    //创建图
    MGraph* MGraph_Create(MVertex** v, int n)  // O(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);
                
                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) // O(1)
    {
        TMGraph* tGraph = (TMGraph*)graph;
        
        if( tGraph != NULL )
        {
            free(tGraph->v);
            free(tGraph->matrix[0]);
            free(tGraph->matrix);
            free(tGraph);
        }
    }

    void MGraph_Clear(MGraph* graph) // O(n*n)
    {
        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) // O(1)
    {
        TMGraph* tGraph = (TMGraph*)graph;
        int ret = (tGraph != NULL);
        
        ret = ret && (0 <= v1) && (v1 < tGraph->count);
        ret = ret && (0 <= v2) && (v2 < tGraph->count);
        ret = ret && (0 <= w);
        
        if( ret )
        {
            tGraph->matrix[v1][v2] = w;
        }
        
        return ret;
    }

    int MGraph_RemoveEdge(MGraph* graph, int v1, int v2) // O(1)
    {
        int ret = MGraph_GetEdge(graph, v1, v2);
        
        if( ret != 0 )
        {
            ((TMGraph*)graph)->matrix[v1][v2] = 0;
        }
        
        return ret;
    }

    int MGraph_GetEdge(MGraph* graph, int v1, int v2) // O(1)
    {
        TMGraph* tGraph = (TMGraph*)graph;
        int condition = (tGraph != NULL);
        int ret = 0;
        
        condition = condition && (0 <= v1) && (v1 < tGraph->count);
        condition = condition && (0 <= v2) && (v2 < tGraph->count);
        
        if( condition )
        {
            ret = tGraph->matrix[v1][v2];
        }
        
        return ret;
    }
    //顶点
    int MGraph_TD(MGraph* graph, int v) // O(n)
    {
        TMGraph* tGraph = (TMGraph*)graph;
        int condition = (tGraph != NULL);
        int ret = 0;
        
        condition = condition && (0 <= v) && (v < tGraph->count);
        
        if( condition )
        {
            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) // O(1)
    {
        TMGraph* tGraph = (TMGraph*)graph;
        int ret = 0;
        
        if( tGraph != NULL )
        {
            ret = tGraph->count;
        }
        
        return ret;
    }
    //返回边数
    int MGraph_EdgeCount(MGraph* graph) // O(n*n)
    {
        TMGraph* tGraph = (TMGraph*)graph;
        int ret = 0;
        
        if( tGraph != 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* visited = NULL;
        int condition = (tGraph != NULL);
        
        condition = condition && (0 <= v) && (v < tGraph->count);
        condition = condition && (pFunc != NULL);
        condition = condition && ((visited = (int*)calloc(tGraph->count, sizeof(int))) != NULL);
        
        if( condition )
        {
            int i = 0;
            
            recursive_dfs(tGraph, v, visited, pFunc);
            
            for(i=0; i<tGraph->count; i++)
            {
                if( !visited[i] )
                {
                    recursive_dfs(tGraph, i, visited, pFunc);
                }
            }
            
            printf(" ");
        }
        
        free(visited);
    }
    //广度优先递归
    void MGraph_BFS(MGraph* graph, int v, MGraph_Printf* pFunc)
    {
        TMGraph* tGraph = (TMGraph*)graph;
        int* visited = NULL;
        int condition = (tGraph != NULL);
        
        condition = condition && (0 <= v) && (v < tGraph->count);
        condition = condition && (pFunc != NULL);
        condition = condition && ((visited = (int*)calloc(tGraph->count, sizeof(int))) != NULL);
        
        if( condition )
        {
            int i = 0;
            
            bfs(tGraph, v, visited, pFunc);
            
            for(i=0; i<tGraph->count; i++)
            {
                if( !visited[i] )
                {
                    bfs(tGraph, i, visited, pFunc);
                }
            }
            
            printf(" ");
        }
        
        free(visited);
    }
    //打印顶点与边的信息
    void MGraph_Display(MGraph* graph, MGraph_Printf* pFunc) // O(n*n)
    {
        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(" ");
            //打印边
            for(i=0; i<tGraph->count; i++)
            {
                for(j=0; j<tGraph->count; j++)
                {
                    if( tGraph->matrix[i][j] != 0 )
                    {
                        printf("<");
                        pFunc(tGraph->v[i]);
                        printf(", ");
                        pFunc(tGraph->v[j]);
                        printf(", %d", tGraph->matrix[i][j]);
                        printf(">");
                        printf(" ");
                    }
                }
            }
            
            printf(" ");
        }
    }

    4.#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


    5.#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;
    }


  • 相关阅读:
    微信朋友圈怎么发文字?如何只发文字和表情?
    微信将推指纹支付 "指付通"会与Touch ID整合吗
    微信公众平台上传图片很卡 微信整合京东的关系?
    微信公众平台可以修改名称吗?微信认证时可以改名!
    怎样制作漂亮的微信二维码?用在线二维码生成器!
    微信网页版APP
    织梦channelid是什么?dede channel typeid有什么区别
    微信消息如何添加文字链接?【微信公众平台技巧】
    为什么在有些文章末尾加一张收录截图?
    5步教你设置微信自定义菜单【微信公众平台技巧】
  • 原文地址:https://www.cnblogs.com/wxb20/p/6163851.html
Copyright © 2020-2023  润新知