One Day One Step之图论(1)



    没错,我又回来了!开学就是麻烦,搞得我都没什么时间写博客了!不过,这几天我也没有闲着,虽然没有做题,但是在图书馆借了一本神级的的书,在网上买的书也终于到了!为什么是神级的呢?以为这本书写的代码真的是很好很好!所以,我接下来的代码虽然都是自己写的,但是基本思路都是从这本书上学到的,也能够与大家分享一下哦!

    图论的基本知识我在这里就不说了,书上都有的!就说几点:图的表示方式有几种,总结起来:如果图比较的稠密,那么邻接矩阵(adjacency matrix)是一种不错的表示方式;但是图比较的稀疏,那么邻接表(adjacencylists)就是一种理想而且节约的表达方式!不展开细说,接下来代码说话:

邻接矩阵的实现:

#include <stdio.h>
#include <stdlib.h>

typedef struct
{
    int v;
    int w;
}Edge;

struct graph
{
    int vertex;
    int edge;
    int **adj;
};

typedef struct graph * Graph;

int ** MatrixInit( int row, int col, int val )
{
    int i, j;
    int **t = malloc( row * sizeof( int * ) );

    for( i = 0; i < row; i++ )
    {
        for( j = 0; j < col; j++ )
        {
            t[i][j] = val;
        }
    }

    return t;
}

Graph GraphIni( int numofvertex )
{
    Graph G = ( Graph ) malloc( sizeof( struct graph ) );

    G->vertex = numofvertex;
    G->edge = 0;
    G->adj = MatrixInit( numofvertex, numofvertex, 0 );

    return G;
}

void GraphInsetEdge( Graph G, Edge e )
{
    int v = e.v;
    int w = e.w;

    if( G->adj[v][w] == 0 )
    {
        G->edge++;
    }
    G->adj[v][w] = 1;
    G->adj[w][v] = 1;
}

void GraphRemoveEdge( Graph G, Edge e )
{
    int v = e.v;
    int w = e.w;

    if( G->adj[v][w] == 1 )
    {
        G->edge--;
    }
    G->adj[v][w] = 0;
    G->adj[w][v] = 0;
}

int main()
{
    printf("Hello world!\n");
    return 0;
}

/*
**在这个实现中,Graph是头指针,指向一个顶点数组adj
*/


下面是邻接表的实现,十分的优美!
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#define maxV 10
#define maxE 45

typedef struct node * link;
short int visited[maxV] = {0};

typedef struct
{
    int v;
    int w;
}Edge;

struct node
{
    int v;
    link next;
};

typedef struct graph
{
    int vertex;
    int edge;
    link * adj;
}graph;

typedef struct graph * Graph;

link New( int v, link next )
{
    link x = ( link ) malloc( sizeof( struct node ) );

    x->v = v;
    x->next = next;

    return x;
}

Graph GraphInit( int numofvertex )
{
    int i;

    Graph G = ( graph * ) malloc( sizeof( graph ) );
    G->vertex = numofvertex;
    G->edge = 0;
    G->adj = malloc( numofvertex * sizeof( link ) );
    for( i = 0; i < numofvertex; i++ )
    {
        G->adj[i] = NULL;
    }

    return G;
}

void GraphInsetEdge( Graph G, Edge e )
{
    int v = e.v;
    int w = e.w;

    G->adj[v] = New( w, G->adj[v] );
    G->adj[w] = New( v, G->adj[w] );
    G->edge++;
}

void DFS( Graph G, int vertex )
{
    link Tmp;

    visited[vertex] = 1;
    printf( "%d ", vertex );
    for( Tmp = G->adj[vertex]; Tmp != NULL; Tmp = Tmp->next )
    {
        if( visited[Tmp->v] == 0 )
        {
            DFS( G, Tmp->v );
        }
    }
}

int main()
{
    Graph G = GraphInit( maxV );
    Edge e[maxE];
    for( int i = 0; i < maxE; i++ )
    {
        scanf( "%d%d", &e[i].v, &e[i].w );
        GraphInsetEdge( G, e[i] );
    }
    int vertex;
    printf( "Print the point to search :");
    scanf( "%d", &vertex );
    DFS( G, vertex );
    return 0;
}

最后主函数是一个深度优先搜索!

这个系列我会一直写,书会一直看,不定时更新!好的,就这样!

猜你喜欢

转载自blog.csdn.net/u011564456/article/details/20076993
one