Prim’s Algorithm and an MST Speed-Up
Download
Report
Transcript Prim’s Algorithm and an MST Speed-Up
Prim’s Algorithm
and an MST Speed-Up
BERJ CHILINGIRIAN
Today’s Plan
(1) Prim’s Algorithm
(2) Implementation
(3) Runtime Analysis
(4) Speed-Up
Key Idea
Boruvka’s Algorithm
𝑂(𝑚 log 𝑛)
Prim’s Algorithm
𝑂(𝑚 + 𝑛 log 𝑛)
Improved MST
Algorithm
𝑂(𝑚 log log 𝑛)
Minimum Spanning Tree
Given: undirected, connected, weighted graph
n vertices
m edges
Return: tree with all vertices and of minimum weight
Prim’s Algorithm: The Idea
Algorithm
𝐼𝑁𝐼𝑇𝐼𝐴𝐿𝐼𝑍𝐸
𝐼𝑁𝐼𝑇𝐼𝐴𝐿𝐼𝑍𝐸
𝑊𝐻𝐼𝐿𝐸
𝐴=∅
𝑆= 𝑠
// 𝑠 𝑖𝑠 𝑠𝑜𝑚𝑒 𝑎𝑟𝑏𝑖𝑡𝑟𝑎𝑟𝑦 𝑣𝑒𝑟𝑡𝑒𝑥 𝑖𝑛 𝐺
S≠𝑉
𝑓 = 𝑚𝑖𝑛𝑖𝑚𝑢𝑚 𝑤𝑒𝑖𝑔ℎ𝑡 𝑒𝑑𝑔𝑒 𝑢, 𝑣 𝑤𝑖𝑡ℎ 𝑒𝑥𝑎𝑐𝑡𝑙𝑦 𝑜𝑛𝑒 𝑒𝑛𝑑𝑝𝑜𝑖𝑛𝑡, 𝑢, 𝑖𝑛 𝑆
𝑎𝑑𝑑 𝑜𝑡ℎ𝑒𝑟 𝑒𝑛𝑑𝑝𝑜𝑖𝑛𝑡 𝑣 𝑡𝑜 𝑆
𝑎𝑑𝑑 𝑒𝑑𝑔𝑒 𝑓 𝑡𝑜 𝐴
Correctness
Cut Lemma: At every step, all edges in 𝐴 are in the
edges of the MST.
Simple Implementation
𝑓𝑜𝑟 𝑒𝑎𝑐ℎ 𝑣𝑒𝑟𝑡𝑒𝑥 𝑣 𝑖𝑛 𝑉
𝑘𝑒𝑦 𝑣 ← ∞
𝑝𝑟𝑒𝑑 𝑣 ← 𝑛𝑢𝑙𝑙
𝑘𝑒𝑦 𝑠 ← 0
𝑆 ← {𝑠}
𝑤ℎ𝑖𝑙𝑒 |𝑆| ≠ |𝑉|
𝑣 ← 𝑣𝑒𝑟𝑡𝑒𝑥 𝑤𝑖𝑡ℎ 𝑚𝑖𝑛𝑖𝑚𝑢𝑚 𝑘𝑒𝑦 𝑣
𝑎𝑑𝑑 𝑣 𝑡𝑜 𝑆
𝑓𝑜𝑟 𝑒𝑎𝑐ℎ 𝑒𝑑𝑔𝑒 𝑣, 𝑢 𝑤ℎ𝑒𝑟𝑒 𝑢 ∉ 𝑆
𝑖𝑓 𝑘𝑒𝑦 𝑢 > 𝑐 𝑣, 𝑢
𝑘𝑒𝑦 𝑢 ← 𝑐(𝑣, 𝑢)
𝑝𝑟𝑒𝑑[𝑢] ← 𝑣
Better Implementation
𝑄 ← 𝑃𝑟𝑖𝑜𝑟𝑖𝑡𝑦𝑄𝑢𝑒𝑢𝑒
𝑓𝑜𝑟 𝑒𝑎𝑐ℎ 𝑣𝑒𝑟𝑡𝑒𝑥 𝑣 𝑖𝑛 𝑉
𝑘𝑒𝑦 𝑣 ← ∞
𝑝𝑟𝑒𝑑 𝑣 ← 𝑛𝑢𝑙𝑙
𝑸. 𝒊𝒏𝒔𝒆𝒓𝒕(𝒗, 𝒌𝒆𝒚 𝒗 )
𝑸. 𝒅𝒆𝒄𝒓𝑲𝒆𝒚(𝒔, 𝟎)
𝑤ℎ𝑖𝑙𝑒 ! 𝑸. 𝒊𝒔𝑬𝒎𝒑𝒕𝒚
𝑣 ← 𝑸. 𝒆𝒙𝒕𝒓𝒂𝒄𝒕𝑴𝒊𝒏
𝑓𝑜𝑟 𝑒𝑎𝑐ℎ 𝑒𝑑𝑔𝑒 𝑣, 𝑢 𝑤ℎ𝑒𝑟𝑒 𝑢 ∈ 𝑄
𝑖𝑓 𝑘𝑒𝑦 𝑢 > 𝑐 𝑣, 𝑢
𝑘𝑒𝑦 𝑢 ← 𝑐(𝑣, 𝑢)
𝑸. 𝒅𝒆𝒄𝒓𝑲𝒆𝒚 𝒖, 𝒌𝒆𝒚[𝒖]
𝑝𝑟𝑒𝑑[𝑢] ← 𝑣
Runtime Analysis
Procedure
Number Time per Operation Time per Operation Time per Operation
of Calls
Array
Binary Heap
Fibonacci Heap
Insert
𝑛
𝑂(𝑛)
𝑂 log 𝑛
𝑂(1)
Extract Min
𝑛
𝑂(𝑛)
𝑂 log 𝑛
𝑂 log 𝑛
Decrease Key
𝑚
𝑂(1)
𝑂 log 𝑛
𝑂(1)
-
𝑂(𝑛2 )
𝑂(𝑚log 𝑛)
𝑂(𝑚 + 𝑛 log 𝑛)
Prim’s
Achieving 𝑂(𝑚 log log 𝑛) Time
Yao, 1975:
use linear median finding algorithm to approx. sorting of edges
Boruvka’s + Prim’s:
run Boruvka’s for 𝑘 iterations to produce 𝐺 ∗
run Prim’s on 𝐺 ∗
Boruvka’s + Prim’s
After 𝒌 iterations of Boruvka’s Algorithm
# of Edges
≤𝑚
𝑛
# of Vertices
≤ 𝑘
2
Boruvka’s + Prim’s
Suppose 𝑘 = log log 𝑛, then
# of vertices is ≤
𝑛
2log log 𝑛
=
𝒏
𝐥𝐨𝐠 𝒏
Boruvka’s + Prim’s
Boruvka’s Algorithm
For 𝑘 = log log 𝑛 iterations
𝐺∗
𝑂(𝑚 log log 𝑛)
Prim’s Algorithm
𝑛
∗
𝑛 ≤
log 𝑛
𝑂 𝑚 + 𝑛∗ log 𝑛
𝑛
=𝑂 𝑚+
log 𝑛
log 𝑛
= 𝑂(𝑚 + 𝑛)
Boruvka’s + Prim’s
𝑛
𝑂(𝑚 log log 𝑛) + 𝑂 𝑚 +
log 𝑛 = 𝑂(𝑚 log log 𝑛)
log 𝑛
Boruvka’s Algorithm
Prim’s Algorithm
Questions?
Similarity to Dijkstra’s
DIJKSTRA’S ALGORITHM
PRIM’S ALGORITHM
𝑄 ← 𝑃𝑟𝑖𝑜𝑟𝑖𝑡𝑦𝑄𝑢𝑒𝑢𝑒
𝑓𝑜𝑟 𝑒𝑎𝑐ℎ 𝑣𝑒𝑟𝑡𝑒𝑥 𝑣 𝑖𝑛 𝑉
𝑘𝑒𝑦 𝑣 ← ∞
𝑝𝑟𝑒𝑑 𝑣 ← 𝑛𝑢𝑙𝑙
𝑄. 𝑖𝑛𝑠𝑒𝑟𝑡(𝑣, 𝑘𝑒𝑦 𝑣 )
𝑄 ← 𝑃𝑟𝑖𝑜𝑟𝑖𝑡𝑦𝑄𝑢𝑒𝑢𝑒
𝑓𝑜𝑟 𝑒𝑎𝑐ℎ 𝑣𝑒𝑟𝑡𝑒𝑥 𝑣 𝑖𝑛 𝑉
𝑘𝑒𝑦 𝑣 ← ∞
𝑝𝑟𝑒𝑑 𝑣 ← 𝑛𝑢𝑙𝑙
𝑄. 𝑖𝑛𝑠𝑒𝑟𝑡(𝑣, 𝑘𝑒𝑦 𝑣 )
𝑄. 𝑑𝑒𝑐𝑟𝐾𝑒𝑦(𝑠, 0)
𝑄. 𝑑𝑒𝑐𝑟𝐾𝑒𝑦(𝑠, 0)
𝑤ℎ𝑖𝑙𝑒 ! 𝑄. 𝑖𝑠𝐸𝑚𝑝𝑡𝑦
𝑣 ← 𝑄. 𝑒𝑥𝑡𝑟𝑎𝑐𝑡𝑀𝑖𝑛
𝑓𝑜𝑟 𝑒𝑎𝑐ℎ 𝑒𝑑𝑔𝑒 𝑣, 𝑢 𝑤ℎ𝑒𝑟𝑒 𝑢 ∈ 𝑄
𝒊𝒇 𝒌𝒆𝒚 𝒖 > 𝒄 𝒗, 𝒖 + 𝒌𝒆𝒚 𝒗
𝒌𝒆𝒚 𝒖 ← 𝒄 𝒗, 𝒖 + 𝒌𝒆𝒚[𝒗]
𝑸. 𝒅𝒆𝒄𝒓𝑲𝒆𝒚 𝒖, 𝒌𝒆𝒚[𝒖]
𝑝𝑟𝑒𝑑 𝑢 ← 𝑣
𝑤ℎ𝑖𝑙𝑒 ! 𝑄. 𝑖𝑠𝐸𝑚𝑝𝑡𝑦
𝑣 ← 𝑄. 𝑒𝑥𝑡𝑟𝑎𝑐𝑡𝑀𝑖𝑛
𝑓𝑜𝑟 𝑒𝑎𝑐ℎ 𝑒𝑑𝑔𝑒 𝑣, 𝑢 𝑤ℎ𝑒𝑟𝑒 𝑢 ∈ 𝑄
𝒊𝒇 𝒌𝒆𝒚 𝒖 > 𝒄 𝒗, 𝒖
𝒌𝒆𝒚 𝒖 ← 𝒄(𝒗, 𝒖)
𝑸. 𝒅𝒆𝒄𝒓𝑲𝒆𝒚 𝒖, 𝒌𝒆𝒚[𝒖]
𝑝𝑟𝑒𝑑[𝑢] ← 𝑣