這是一個面試問題我被招聘者問,這個問題基本上是計算每一個節點的所有節點的最短路徑,而我的解決方案是以下什麼是使用map reduce通過bfs遍歷圖的有效方式?
啓動所有可能的邊緣(沒有扭轉 - B是一樣的BA)
每個節點將在以下(SRC,成本,current_list,DEST來表示),src和dest爲基本上所有可能的邊緣,我們開始較早
地圖:
for each edge you traverse, you duplicate your tuple and add the current
traversed node to the cost and current list.
if the node is the destination you annotate finish, if the the node is
in the current list, you annotate delete
減少:
Don't really need to do anything besides outputting finish and deleting
delete and let the other node go through the next round of map
And by outputting I mean for each src, dest pair only output the least cost
的招聘人員說,這是沒有效率的,我可以看到這一點,因爲你穿越combinatorialy如何效率不高,但我能想到的唯一的選擇是,如果您有n個節點,然後產卵n個服務器,並且爲招聘人員所說的每個節點做dijkstra也是錯誤的。有人可以幫我解決這個問題嗎?
編輯:
Ex。三角圖形
的邊緣AB,BC,CA與算法
- 首先,我們開始考慮到所有可能的源目的地對保持優勢的是反向的1
路徑成本不是唯一的 AB ,AC,BC(BA,CA,BC被省略)
對於每個源 - 目的地對我們有以下的元組
(src=A, cost=None, current_list=A, dest=B, annotate=continue)
(src=A, cost=None, current_list=A, dest=C, annotate=continue)
(src=B, cost=None, current_list=B, dest=C, annotate=continue)
現在我們開始在地圖減少算法
for each tuple in the tuple list we initiate: for each neighbor of the node at the end of current_list if the next neighbor is already in the current_list set annotate = delete elif the next neighbor is the dest set annotate = finish add path cost to cost else duplicate the current node add neighbor to current_list add path cost to cost delete the current tuple
減少
注意:我們減少src ,DEST對,並把它作爲我們的主要 在元組列表中的每個元組
if annotate == finish keep trace of min cost and delete tuple for each src dest pair that is not the current min then pass the current min as result elif annotate == delete delete the tuple else pass down to the next round of map
地圖
- Reduce
在我們的情況
(src=A, cost=None, current_list=A, dest=B, annotate=continue)
=>
(src=A, cost=1, current_list=AB, dest=B, annotate=finish)
(src=A, cost=1, current_list=AC, dest=B, annotate=continue)
(src=A, cost=None, current_list=A, dest=C, annotate=continue)
=>
(src=A, cost=1, current_list=AC, dest=C, annotate=finish)
(src=A, cost=1, current_list=AB, dest=C, annotate=continue)
(src=B, cost=None, current_list=B, dest=C, annotate=continue)
=>
(src=B, cost=1, current_list=BC, dest=C, annotate=finish)
(src=B, cost=1, current_list=BA, dest=C, annotate=continue)
因爲我們還是有一定的元組有註釋=繼續
(src=B, cost=1, current_list=BA, dest=C, annotate=continue)
=>
(src=B, cost=2, current_list=BAC, dest=C, annotate=finish)
(src=B, cost=2, current_list=BAB, dest=C, annotate=delete)
(src=A, cost=1, current_list=AC, dest=B, annotate=continue)
=>
(src=A, cost=2, current_list=ACB, dest=B, annotate=finish)
(src=A, cost=2, current_list=ACA, dest=B, annotate=delete)
(src=A, cost=1, current_list=AB, dest=C, annotate=continue)
=>
(src=A, cost=2, current_list=ABC, dest=C, annotate=finish)
(src=A, cost=2, current_list=ABA, dest=C, annotate=delete)
我們沒有繼續元組,現在我們只使用reduce來找到每個src dest對的最小值
你所描述的甚至沒有正確解決MapReduce的單源最短路徑問題,如果我是你的採訪者,我不會擔心效率,但是正確性。 – 2015-04-04 21:10:53
我不確定你的意思,減少部分只輸出一個代價最小的源節點目標節點對,並且我遍歷從A到B的所有可能的路徑,其中A和B是任意的 – Kevin 2015-04-04 21:13:54
所以你不想要所有對最短路徑,但兩個頂點之間的最小成本邊? – 2015-04-04 21:18:50