publicclass route
public route(stackstack)
}
publicclass mygraphwhere tweight : icomparable
//////
找最小權重
/// ///
起點 ///
終點 ///
路線 ///
public tweight findminweight(tnode start, tnode end, out routeroute)
if (_nodes.containskey(end) == false
)
_searchednodes = new hashset();
var minweightnode =findlowestweightandnotsearchednode();
while (equalitycomparer.default.equals(minweightnode, default) == false
)
//更新權重表和父節點表.
_weights[item.key] =newweight;
_parents[item.key] =minweightnode;
}//當前節點搜尋完畢後,新增到已搜尋集合.
_searchednodes.add(minweightnode);
minweightnode =findlowestweightandnotsearchednode();
}route =getroute(end);
return
_weights[end];
}//////
找出權重最小,且還未搜尋過的節點.
/// ///
public
tnode findlowestweightandnotsearchednode()
}return
default
; }
//////
獲取路線
/// ///
///private routegetroute(tnode end)
return
new route(stack);
}}
test:
dictionary> nodedic = new dictionary>();nodedic.add(
"start
", new dictionary , });
nodedic.add("a
", new dictionary , });
nodedic.add("b
", new dictionary , });
nodedic.add("c
", new dictionary , , });
nodedic.add("d
", new dictionary });
nodedic.add(
"end
", new dictionary());
dictionary
costdic = new dictionary,,,
,};
dictionary
parentsdic = new dictionary,,,
,,
};func
@delegate = (x, y) => x +y;
mygraph
g = new mygraph(nodedic, costdic, parentsdic, @delegate);
var result = g.findminweight("
start
", "
end", out
varroute);
console.writeline(result);
console.writeline(route.fullroute);
狄克斯特拉演算法
廣度優先演算法,它找出的是段數最少的路徑 無向圖 如果我們要找出最快的路徑 加權圖 可以使用狄克斯特拉演算法。狄克斯特拉演算法包含四個步驟 1.找出 最便宜 的節點,即可在最短時間內到達的節點 2.更新該節點的鄰居的開銷 3.重複這個過程,直到對圖中的每個節點都這樣做了 4.計算最終路徑 以下圖為例...
狄克斯特拉演算法
是由荷蘭計算機科學家狄克斯特拉於1959 年提出的。是從乙個頂點到其餘各頂點的最短路徑演算法,解決的是有向無環圖中最短路徑問題,且不能有負權邊。狄克斯特拉演算法主要特點是以起始點為中心向外層層擴充套件,直到擴充套件到終點為止。示例 找出從起點到終點的最短路徑 當前起點到各點的花費,選擇最小且沒有被檢...
(原創)狄克斯特拉演算法
1.廣度優先搜尋用於計算非加權圖中的最短路徑 2.狄克斯特拉演算法用於計算加權圖中的最短路徑 不能帶有負權邊 備註 當圖的每條邊都帶有乙個數字時,這些數字成為權重。帶權重的圖稱為加權圖,反之稱為非加權圖。1.從起點開始。2.找到該點最便宜的鄰居節點。3.若該節點的開銷優於之前記錄的開銷,則更新該節點...