打印两点间的所有路径

来源:互联网 发布:拐弯网络大电影 编辑:程序博客网 时间:2024/05/21 11:17

有如下的图,该如何打印起始节点和终点的所有路径呢?

 

考虑到路径枚举的回溯,所以想到用栈来实现之。奥妙之处:需要给每个入栈的节点添加标记printed,而出站的时候,如何设置标记就是关键。已在代码中体现。


文件in.txt

12 200 1 0 20 31 42 42 53 43 54 6 4 7 5 65 75 86 96 107 97 108 109 1110 110 11


 

/* * main.c */#include "szl_stack_print_paths.h" #include <stdio.h>#include <stdlib.h> /* malloc () free () */#include <string.h> /* memset () */#include <assert.h>#define GRAPH_FILE  "in.txt"int main (int argc ,char ** argv){    int * * digraph = NULL;    int i;    int n,m; /* verteices and edges */    int s,d; /* the source node and the destination node */    int u,v; /* vertices of an edge */    if( 1 < argc){        freopen(argv[1], "r", stdin);    }    else{        ;#ifdef DEBUG        freopen (GRAPH_FILE, "r", stdin);        printf ("%s is open \n", GRAPH_FILE);#endif    }    assert (NULL != stdin);    fscanf (stdin, "%d%d", &n, &m);    assert (1 < n && 0 < m);    fprintf (stdout, "n=%d m=%d\n", n, m);        digraph = (int * *) malloc(sizeof (int *) * n);    assert (NULL != digraph);    for (i=0; i<n; i++){        digraph[i] = (int *)malloc (sizeof (int) * n);        assert (NULL != digraph[i]);    }    for (i=0; i<n; i++){        memset (digraph[i], 0, sizeof (int) * n);    }        for (i=0; i<m; i++){        fscanf (stdin, "%d%d", &u, &v);        fprintf (stdout, "u=%d v=%d\n", u, v);        digraph[u][v] = 1;    }    fscanf (stdin,"%d%d",&s,&d);    fprintf (stdout, "s=%d d=%d\n", s, d);    szl_stack_print_paths (digraph, n, s, d);    fclose (stdin);    for (i=0; i<n; i++){        free (digraph[i]);    }    free (digraph);    return 0;}


 

/*  * szl_stack.h  */#ifndef SZL_STACK_H#define SZL_STACK_H#include <stdio.h>#include <stdlib.h>#include <string.h>#include <assert.h>struct szl_stack_node;struct szl_stack;struct szl_stack * szl_stack_malloc (struct szl_stack * * p_st);int  szl_stack_is_empty   (struct szl_stack * st          );int  szl_stack_pop        (struct szl_stack * st, int * pv);int  szl_stack_push       (struct szl_stack * st, int v   );int  szl_stack_top        (struct szl_stack * st, int * pv);void szl_stack_make_empty (struct szl_stack * st          );void szl_stack_free       (struct szl_stack * * p_st      );#endif


 

/* * szl_stack_print.h */#ifndef SZL_STACK_PRINT_H#define SZL_STACK_PRINT_H#include "szl_stack.h"/* print the elements of a stack from the stack bottom to stack top */void szl_stack_print (struct szl_stack * st);#endif


 

/* * szl_stack_print_paths.h */#ifndef SZL_STACK_PRINT_PATHS_H#define SZL_STACK_PRINT_PATHS_H/* print all the paths from vertex s to d in digraph with n vertices */void szl_stack_print_paths (int * * digraph, int n, int s, int d);#endif


 

/*  * szl_stack.c  */#include "szl_stack.h"#include <stdio.h>#include <assert.h>#include <stdlib.h>struct szl_stack_node{    int value;    struct szl_stack_node * next;};struct szl_stack{    struct szl_stack_node * top;};struct szl_stack * szl_stack_malloc (struct szl_stack * * p_st){    assert (NULL == * p_st);    struct szl_stack * r_st = (struct szl_stack *) malloc (sizeof (struct szl_stack));    assert (NULL != r_st);    r_st->top = NULL;    return (* p_st = r_st);}int szl_stack_is_empty (struct szl_stack * st){    assert (NULL != st);    return (NULL == st->top) ? 1 : 0;}int szl_stack_push (struct szl_stack * st, int v){    struct szl_stack_node * stack_node_p = NULL;    assert (NULL != st);    stack_node_p = (struct szl_stack_node *) malloc (sizeof (struct szl_stack_node));    assert (NULL != stack_node_p);    stack_node_p->value = v;    stack_node_p->next = st->top;    st->top = stack_node_p;    return v;}int szl_stack_pop (struct szl_stack * st, int * pv){    struct szl_stack_node * stack_node_p = NULL;    assert (NULL != st);    assert (NULL != pv);    assert (!szl_stack_is_empty (st));    stack_node_p = st->top;    st->top = stack_node_p->next;    *pv = stack_node_p->value;        free (stack_node_p);    stack_node_p = NULL;    return *pv;}int szl_stack_top (struct szl_stack * st, int * pv){    assert (NULL != st);    assert (NULL != pv);    assert (!szl_stack_is_empty (st));    *pv = st->top->value;        return *pv;}void szl_stack_make_empty (struct szl_stack * st){    struct szl_stack_node * stack_node_p = NULL;    assert (NULL != st);    if (!szl_stack_is_empty (st)){        while (NULL != st->top){            stack_node_p = st->top;            st->top = st->top->next;            free (stack_node_p);         }    }}void szl_stack_free (struct szl_stack * * p_st){    assert (NULL != * p_st);    if (!szl_stack_is_empty (* p_st)){        szl_stack_make_empty (* p_st);    }    free (* p_st);    * p_st = NULL;}


 

/* * szl_stack_print.c */#include "szl_stack.h"#include "szl_stack_print.h"/* print the elements of a stack from the stack bottom to stack top */void szl_stack_print (struct szl_stack * st){    int v;    struct szl_stack * st2 = NULL;    szl_stack_malloc (&st2);    while (!szl_stack_is_empty (st)){        szl_stack_pop  (st, &v);        szl_stack_push (st2, v);    }    while (!szl_stack_is_empty (st2)){        szl_stack_pop  (st2, &v);        printf ("%d  ", v);        szl_stack_push (st, v);    }    puts ("");    szl_stack_make_empty (st2);    szl_stack_free (&st2);}


 

/*  * szl_stack_print_paths.c */#include "szl_stack.h"#include "szl_stack_print.h"#include "szl_stack_print_paths.h"/* print all the paths from vertex s to d in digraph with n vertices */void szl_stack_print_paths (int * * digraph, int n, int s, int d){    int i;    int * printed = NULL;    struct szl_stack * st = NULL;    printed = (int *) malloc (sizeof (int) * n);    assert (NULL != printed);    szl_stack_malloc (&st);    assert (NULL != st);    memset (printed, 0, sizeof (int) * n);    printed[s] = 1;    szl_stack_push (st, s);    while (!szl_stack_is_empty (st)){        int all_printed = 0;        int t;        int c;        szl_stack_top (st, &t);        if (d == t){            szl_stack_print (st);            szl_stack_pop (st, &t);            printed[t] = 1;        }        szl_stack_top (st, &t);        all_printed = 1;        for (i=0; i<n; i++){            if (1 == digraph[t][i]){               if (!printed[i]){                   all_printed = 0;                   c = i;                   break;                }            }        }        if (all_printed){            for (i=0; i<n; i++){                if (1 == digraph[t][i]){                    printed[i] = 0;                }            }            szl_stack_pop (st, &t);        }        else{            szl_stack_push (st, c);            printed[c] = 1;        }    }           szl_stack_make_empty (st);    szl_stack_free (&st);    free (printed);}



编译运行之

in.txt is open n=12 m=20u=0 v=1u=0 v=2u=0 v=3u=1 v=4u=2 v=4u=2 v=5u=3 v=4u=3 v=5u=4 v=6u=4 v=7u=5 v=6u=5 v=7u=5 v=8u=6 v=9u=6 v=10u=7 v=9u=7 v=10u=8 v=10u=9 v=11u=10 v=11s=0 d=110  1  4  6  9  11  0  1  4  6  10  11  0  1  4  7  9  11  0  1  4  7  10  11  0  2  4  6  9  11  0  2  4  6  10  11  0  2  4  7  9  11  0  2  4  7  10  11  0  2  5  6  9  11  0  2  5  6  10  11  0  2  5  7  9  11  0  2  5  7  10  11  0  2  5  8  10  11  0  3  4  6  9  11  0  3  4  6  10  11  0  3  4  7  9  11  0  3  4  7  10  11  0  3  5  6  9  11  0  3  5  6  10  11  0  3  5  7  9  11  0  3  5  7  10  11  0  3  5  8  10  11  

 

将图略作修改:


其结果为:

0  1  4  6  9  11  0  1  4  6  10  11  0  1  4  7  9  11  0  1  4  7  10  11  0  1  4  7  14  11  0  1  13  6  9  11  0  1  13  6  10  11  0  1  13  7  9  11  0  1  13  7  10  11  0  1  13  7  14  11  0  2  4  6  9  11  0  2  4  6  10  11  0  2  4  7  9  11  0  2  4  7  10  11  0  2  4  7  14  11  0  2  5  6  9  11  0  2  5  6  10  11  0  2  5  7  9  11  0  2  5  7  10  11  0  2  5  7  14  11  0  2  5  8  10  11  0  2  5  8  14  11  0  3  4  6  9  11  0  3  4  6  10  11  0  3  4  7  9  11  0  3  4  7  10  11  0  3  4  7  14  11  0  3  5  6  9  11  0  3  5  6  10  11  0  3  5  7  9  11  0  3  5  7  10  11  0  3  5  7  14  11  0  3  5  8  10  11  0  3  5  8  14  11  0  3  12  7  9  11  0  3  12  7  10  11  0  3  12  7  14  11  0  3  12  8  10  11  0  3  12  8  14  11  

相应的输入文件in.txt应为:

15 290 1 0 20 31 41 132 42 53 43 53 124 6 4 7 5 65 75 86 96 107 97 107 148 108 149 1110 1113 613 712 7 12 814 110 11