首页 > 代码库 > 数据结构——图——最短路径D&F算法

数据结构——图——最短路径D&F算法

一、Dijkstra算法(贪心地求最短距离的算法)

  在此算法中,我按照自己的理解去命名,理解起来会轻松一些。

  

#define MAXSIZE 100
#define UNVISITED 0
#define VISITED 1
#define INFINITY 66666

typedef struct tool {
    int visited[MAXSIZE];
    /*是否已访问的数组,visited[i]表示顶点i已经访问,也就是到顶点i的最短距离已求出*/
    int known_shortest_distance[MAXSIZE];
    /*已知的最短距离数组。known_shortest_distance[i]代表从V0到Vi的最短距离*/
    int previous_vertex[MAXSIZE];
    /*previous_vertex[i]=j,代表:在从V0到Vi的最短路径上,顶点i的前一个顶点是顶点j*/
} Tool;

typedef struct gragh {
    int num_of_vertexes;
    int adjacent_matrix[MAXSIZE][MAXSIZE];
} Gragh;

void init_tool(Gragh g, Tool &t) {
    int i = 0;
    for(i = 0; i < g.num_of_vertexes; i++) {
        t.visited[i] = UNVISITED;
        t.known_shortest_distance[i] = g.adjacent_matrix[0][i];
        t.previous_vertex[i] = 0;
    }
}

void dijstra_shortest_distance(Gragh g, Tool &t) {
    int i, j, x; //循环变量
    int min_distance, x_index;
    /*min_distance存储的是当前已知的最短距离数组中从V0到Vx最小的距离;
    x_index存储的Vx的下标*/
    init_tool(g, t);
    for(i = 0; i < g.num_of_vertexes; i++) {
        min_distance = INFINITY;
        for(x = 0; x < g.num_of_vertexes; x++) {
            if(!t.visited[x] && t.known_shortest_distance[x] < min_distance) {
                min_distance = t.known_shortest_distance[x];
                x_index = x;
            }
        }
        /*通过上面一个循环,最短距离数组中的最小值就是min_distance;
        该顶点的下标就是x_index;
        在当前没有访问且以V0为起点距离最短的顶点中,
        这个点的下标就是x_index.*/
        t.visited[x_index] = VISITED;

        for(j = 0; j < g.num_of_vertexes; j++) {
            if(!t.visited[j] && (min_distance + g.adjacent_matrix[x_index][j] < t.known_shortest_distance[j])) {
                t.known_shortest_distance[j] = min_distance + g.adjacent_matrix[x_index][j];
                t.previous_vertex[j] = x_index;
            }
        }
    }

}

 

 

一、Floyd算法(不管三七二十一,先把整个图中任意两点的最短距离求出再说)

 

typedef struct floyd_tool {
    int known_shortest_distance[MAXSIZE][MAXSIZE];
    /*已知的最短距离数组。known_shortest_distance[i][j]代表从Vi到Vj的最短距离*/
    int path_matrix[MAXSIZE][MAXSIZE];
    /*path_matrix[i][j]=k,代表:在从Vi到Vj的最短路径上,要经过顶点k;
    用k代替i,直到path_matrix[k][j]=j*/
} FloydTool;

void init_floyd_tool(FloydTool &ft, Gragh g) {
    for(int i = 0; i < g.num_of_vertexes; i++) {
        for(int j = 0; j < g.num_of_vertexes; j++) {
            ft.known_shortest_distance[i][j] = g.adjacent_matrix[i][j];
            ft.path_matrix[i][j] = j;
        }
    }
}

void floyd_shortest_distance(Gragh g,FloydTool &ft) {
    init_floyd_tool(ft,g);
    for(int i = 0;i < g.num_of_vertexes; i++){
        for(int j = 0;j < g.num_of_vertexes;j++){
            for(int k = 0;k < g.num_of_vertexes;k++){
                if(ft.known_shortest_distance[j][k] >
                   ft.known_shortest_distance[j][i] + ft.known_shortest_distance[i][k]){
                    ft.known_shortest_distance[j][k] =
                    ft.known_shortest_distance[j][i] + ft.known_shortest_distance[i][k];
                    ft.path_matrix[j][k] = ft.path_matrix[j][i];
                   }
            }
        }
    }
}

 

 

改进版:

void floyd_shortest_distance(Gragh g,FloydTool &ft) {
    init_floyd_tool(ft,g);
    for(int i = 0;i < g.num_of_vertexes; i++){
        for(int j = 0;j < g.num_of_vertexes;j++){
            if(ft.known_shortest_distance[j][i] == INFINITY)continue;
            for(int k = j + 1;k < g.num_of_vertexes;k++){
                if(ft.known_shortest_distance[k][i] == INFINITY)continue;
                if(ft.known_shortest_distance[j][k] >
                   ft.known_shortest_distance[j][i] + ft.known_shortest_distance[i][k]){
                    ft.known_shortest_distance[j][k] =
                    ft.known_shortest_distance[j][i] + ft.known_shortest_distance[i][k];
                    ft.path_matrix[j][k] = ft.path_matrix[j][i];
                   }
            }
        }
    }
}

 

数据结构——图——最短路径D&F算法