Chinaunix首页 | 论坛 | 博客
  • 博客访问: 633379
  • 博文数量: 54
  • 博客积分: 3812
  • 博客等级: 上校
  • 技术积分: 992
  • 用 户 组: 普通用户
  • 注册时间: 2007-04-16 20:53
文章分类

全部博文(54)

文章存档

2010年(10)

2009年(24)

2008年(20)

分类: C/C++

2009-03-24 15:40:59

    图算法的简单路径用于找到图中的两个顶点之间是否连通。可以通过下面的递归算法:
graph.h
 

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

Edge EDGE(int, int);

typedef struct graph *Graph;

Graph graphinit(int);

int graphinserte(Graph, Edge);

void graphremovee(Graph, Edge);

Graph graphcopy(Graph);

void graphdestroy(Graph);

void graphshow(Graph);

graph.c

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

struct graph
{
        int v;
        int e;
        int **adj;
};

Edge EDGE(int v, int w)
{
        Edge e;
        e.v = v;
        e.w = w;

        return e;
}

int **matrixint(int r, int c, int val)
{
        int i, j;

        int **t = (int**)malloc(r * sizeof(int*));
        if (t == NULL)
                return NULL;
        for (i = 0; i < r; i++)
        {
                t[i] = (int*)malloc(c * sizeof(int));
                if (t[i] == NULL)
                {
                        while (--i >= 0)
                                free(t[i]);
                        return NULL;
                }
        }

        for (i = 0; i < r; i++)
                for (j = 0; j < c; j++)
                        t[i][j] = val;
        return t;
}

Graph graphinit(int v)
{
        Graph g;

        g = (Graph)malloc(sizeof(*g));
        g->v = v;
        g->e = 0;
        g->adj = matrixint(v, v, 0);

        return g;
}

int graphinserte(Graph g, Edge e)
{
        int v = e.v;
        int w = e.w;

        if (g->adj[v][w] == 0)
                g->e++;
        g->adj[v][w] = 1;
        g->adj[w][v] = 1;
        return 0;
}

void graphremovee(Graph g, Edge e)
{
        int v = e.v;
        int w = e.w;

        if (g->adj[v][w] == 1)
                g->e--;
        g->adj[v][w] = 0;
        g->adj[w][v] = 0;
}

void graphshow(Graph g)
{
        int i, j;
        printf("%d vertices, %d edges\n", g->v, g->e);

        for (i = 0; i < g->v; i++)
        {
                printf("%2d: ", i);
                for (j = 0; j < g->v; j++)
                        if (g->adj[i][j] == 1)
                                printf(" %2d", j);
                printf("\n");
        }
}

void graphdestroy(Graph g)
{
        int i;

        for (i = 0; i < g->v; i++)
                free(g->adj[i]);
        free(g->adj);
}

simplepath.c

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

struct graph
{
        int v;
        int e;
        int **adj;
};

int pathR(Graph g, int v, int w, int *visited)
{
        int t;

        if (v == w)
                return 1;
        visited[v] = 1;

        for (t = 0; t < g->v; t++)
                if (g->adj[v][t] == 1)
                        if (visited[t] == 0)
                                if (pathR(g, t, w, visited))
                                {
                                        printf(" %d - %d ", t, v);
                                        return 1;
                                }
        return 0;
}

int graphpath(Graph g, int v, int w)
{
        int *visited;
        int t;

        visited = (int*)malloc(sizeof(int) * g->v);
        if (visited == NULL)
                return -1;

        for (t = 0; t < g->v; t++)
                visited[t] = 0;

        return pathR(g, v, w, visited);
}

main.c

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

int main()
{
        Graph g;
        int i, j;
        int ver = 8;
        int v1, v2;

        Edge e[] = {{0, 1},{0, 2}, {0, 5}, {0, 6}, {1, 2}, {2, 3}, {2, 4}, {3, 4}, {4, 5}, {4, 6}};
        j = sizeof(e) / sizeof(e[0]);

        g = graphinit(ver);
        if (g == NULL)
        {
                printf("init graph error\n");
                exit(1);
        }


        for (i = 0; i < j; i++)
                if (graphinserte(g, e[i]) < 0)
                {
                        printf("insert error\n");
                        break;
                }

        graphshow(g);

        scanf("%d %d", &v1, &v2);
        if (graphpath(g, v1, v2))
        {
                printf("\n%d-%d has path\n", v1, v2);
        }
        else
        {
                printf("%d-%d has no path\n", v1, v2);
        }

        graphdestroy(g);
        return 0;
}

 

gcc -Wall main.c simplepath.c graph.c -o main

./main

结果:

8 vertices, 10 edges
 0: 1 2 5 6
 1: 0 2
 2: 0 1 3 4
 3: 2 4
 4: 2 3 5 6
 5: 0 4
 6: 0 4
 7:
2 6
 6 - 4 4 - 5 5 - 0 0 - 2
2-6 has path

 

 

8 vertices, 10 edges
 0: 1 2 5 6
 1: 0 2
 2: 0 1 3 4
 3: 2 4
 4: 2 3 5 6
 5: 0 4
 6: 0 4
 7:
2 7
2-7 has no path

阅读(7110) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~