資料結構——圖——最短路徑D&F演算法

來源:互聯網
上載者:User

標籤:blog   max   log   path   結構   最短路   變數   type   short   

一、Dijkstra演算法(貪心地求最短距離的演算法)

  在此演算法中,我按照自己的理解去命名,理解起來會輕鬆一些。

  

#define MAXSIZE 100#define UNVISITED 0#define VISITED 1#define INFINITY 66666typedef 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演算法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.