單源最短路徑(不計邊長)

2021-10-02 12:02:40 字數 1761 閱讀 7835

**只需在bfs基礎上進行修改即可

bfs**可參照

廣度優先搜尋**實現

最終列印最短路徑長度和路徑陣列

(輸入**權重手動設定為1)

#include

"單源最短路徑.h"

#include

"stdlib.h"

#include

"stdio.h"

#include

"queue.h"

int dist[maxvertexnum]

;int path[maxvertexnum]

;void

initdistandpath

(int

* dist,

int* path)

for( i =

0; i < maxvertexnum; i++)}

//建立乙個沒有邊的圖

mgraph creatgraph

(int vertexnum)

graph-

>nv = vertexnum;

graph-

>ne =0;

for(v =

0; v < graph-

>nv; v++)}

return graph;

}//構建乙個圖

mgraph buildgraph()

for(i =

0; i < graph-

>ne; i++)}

for(v =

0; v < graph-

>nv; v++

)return graph;

}void bfs (mgraph graph, vertex s)}}

for( w =

0; w < graph-

>nv; w++

)printf

("路徑陣列 :\n");

for(w =

0; w < graph-

>nv; w++)}

//插入邊

void

insertedge

(mgraph graph, edge e)

#ifndef bfs

#define maxvertexnum 100

//圖的最大節點

#define infinity 0xffff

typedef

int vertex;

//頂點下標

typedef

int weigthtype;

//邊的權值

typedef

int datatype;

//頂點的資料型別

//邊的定義

typedef

struct enode

*ptrtoenode;

typedef ptrtoenode edge;

//圖的定義

typedef

struct gnode

*ptrtognode;

typedef ptrtognode mgraph;

void

insertedge

(mgraph graph, edge e)

;mgraph buildgraph()

;void

bfs(mgraph graph, vertex s)

;#endif

// !bfs

#pragma once

單源最短路徑

include define max 999 define maxverts 10 typedef struct graph void chushi graph g void dij graph int key,int int int main for i 1 i g.numverts i dij ...

單源最短路徑

最優子結構 最短路徑的子路徑也是最短路徑,動態規劃和貪心演算法的乙個重要指標。環路 一條最短路徑不可能包含環路 1 環路權重為負,如果有一條環路權重為負,則不存在最短路徑 2 環路權重為零,如果包含該環路,則將該環路去掉即可 3 環路權重為正,去掉改環路可以得到更短的路徑,因此不可能是最短路徑 最短...

單源最短路徑

單源最短路徑問題,即在圖中求出給定頂點到其他任一頂點的最短路徑。1.最短路徑的最優子結構性質 該性質描述為 如果p i,j 是從頂點i到j的最短路徑,k和s是這條路徑上的乙個中間頂點,那麼p k,s 必定是從k到s的最短路徑。證明 假設p i,j 是從頂點i到j的最短路徑,則有p i,j p i,k...