Presentation is loading. Please wait.

Presentation is loading. Please wait.

Prim’s Algorithm and an MST Speed-Up

Similar presentations


Presentation on theme: "Prim’s Algorithm and an MST Speed-Up"— Presentation transcript:

1 Prim’s Algorithm and an MST Speed-Up
Berj Chilingirian

2 Today’s Plan (1) Prim’s Algorithm (2) Implementation
(3) Runtime Analysis (4) Speed-Up

3 Improved MST Algorithm
Key Idea Improved MST Algorithm 𝑂(𝑚 log log 𝑛 ) Boruvka’s Algorithm 𝑂(𝑚 log 𝑛 ) Prim’s Algorithm 𝑂(𝑚+𝑛 log 𝑛 )

4 Minimum Spanning Tree Given: undirected, connected, weighted graph
Return: tree with all vertices and of minimum weight n vertices m edges

5 Prim’s Algorithm: The Idea

6 Algorithm 𝐼𝑁𝐼𝑇𝐼𝐴𝐿𝐼𝑍𝐸 𝐴=∅
𝐼𝑁𝐼𝑇𝐼𝐴𝐿𝐼𝑍𝐸 𝐴=∅ 𝐼𝑁𝐼𝑇𝐼𝐴𝐿𝐼𝑍𝐸 𝑆= 𝑠 // 𝑠 𝑖𝑠 𝑠𝑜𝑚𝑒 𝑎𝑟𝑏𝑖𝑡𝑟𝑎𝑟𝑦 𝑣𝑒𝑟𝑡𝑒𝑥 𝑖𝑛 𝐺 𝑊𝐻𝐼𝐿𝐸 S≠𝑉 𝑓=𝑚𝑖𝑛𝑖𝑚𝑢𝑚 𝑤𝑒𝑖𝑔ℎ𝑡 𝑒𝑑𝑔𝑒 𝑢,𝑣 𝑤𝑖𝑡ℎ 𝑒𝑥𝑎𝑐𝑡𝑙𝑦 𝑜𝑛𝑒 𝑒𝑛𝑑𝑝𝑜𝑖𝑛𝑡, 𝑢, 𝑖𝑛 𝑆 𝑎𝑑𝑑 𝑜𝑡ℎ𝑒𝑟 𝑒𝑛𝑑𝑝𝑜𝑖𝑛𝑡 𝑣 𝑡𝑜 𝑆 𝑎𝑑𝑑 𝑒𝑑𝑔𝑒 𝑓 𝑡𝑜 𝐴

7 Correctness Cut Lemma: At every step, all edges in 𝐴 are in the edges of the MST.

8 Simple Implementation
𝑓𝑜𝑟 𝑒𝑎𝑐ℎ 𝑣𝑒𝑟𝑡𝑒𝑥 𝑣 𝑖𝑛 𝑉 𝑘𝑒𝑦 𝑣 ←∞ 𝑝𝑟𝑒𝑑 𝑣 ←𝑛𝑢𝑙𝑙 𝑘𝑒𝑦 𝑠 ←0 𝑆←{𝑠} 𝑤ℎ𝑖𝑙𝑒 |𝑆|≠|𝑉| 𝑣←𝑣𝑒𝑟𝑡𝑒𝑥 𝑤𝑖𝑡ℎ 𝑚𝑖𝑛𝑖𝑚𝑢𝑚 𝑘𝑒𝑦 𝑣 𝑎𝑑𝑑 𝑣 𝑡𝑜 𝑆 𝑓𝑜𝑟 𝑒𝑎𝑐ℎ 𝑒𝑑𝑔𝑒 𝑣,𝑢 𝑤ℎ𝑒𝑟𝑒 𝑢∉𝑆 𝑖𝑓 𝑘𝑒𝑦 𝑢 >𝑐 𝑣,𝑢 𝑘𝑒𝑦 𝑢 ←𝑐(𝑣,𝑢) 𝑝𝑟𝑒𝑑[𝑢]←𝑣

9 Better Implementation
𝑄←𝑃𝑟𝑖𝑜𝑟𝑖𝑡𝑦𝑄𝑢𝑒𝑢𝑒 𝑓𝑜𝑟 𝑒𝑎𝑐ℎ 𝑣𝑒𝑟𝑡𝑒𝑥 𝑣 𝑖𝑛 𝑉 𝑘𝑒𝑦 𝑣 ←∞ 𝑝𝑟𝑒𝑑 𝑣 ←𝑛𝑢𝑙𝑙 𝑸.𝒊𝒏𝒔𝒆𝒓𝒕(𝒗, 𝒌𝒆𝒚 𝒗 ) 𝑸.𝒅𝒆𝒄𝒓𝑲𝒆𝒚(𝒔,𝟎) 𝑤ℎ𝑖𝑙𝑒 ! 𝑸.𝒊𝒔𝑬𝒎𝒑𝒕𝒚 𝑣←𝑸.𝒆𝒙𝒕𝒓𝒂𝒄𝒕𝑴𝒊𝒏 𝑓𝑜𝑟 𝑒𝑎𝑐ℎ 𝑒𝑑𝑔𝑒 𝑣,𝑢 𝑤ℎ𝑒𝑟𝑒 𝑢∈𝑄 𝑖𝑓 𝑘𝑒𝑦 𝑢 >𝑐 𝑣,𝑢 𝑘𝑒𝑦 𝑢 ←𝑐(𝑣,𝑢) 𝑸.𝒅𝒆𝒄𝒓𝑲𝒆𝒚 𝒖,𝒌𝒆𝒚[𝒖] 𝑝𝑟𝑒𝑑[𝑢]←𝑣

10 Runtime Analysis Procedure Number of Calls Time per Operation Array
Binary Heap Fibonacci Heap Insert 𝑛 𝑂(𝑛) 𝑂 log 𝑛 𝑂(1) Extract Min Decrease Key 𝑚 Prim’s - 𝑂( 𝑛 2 ) 𝑂(𝑚log⁡𝑛) 𝑂(𝑚+𝑛 log 𝑛 )

11 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 𝐺 ∗

12 After 𝒌 iterations of Boruvka’s Algorithm
Boruvka’s + Prim’s After 𝒌 iterations of Boruvka’s Algorithm # of Edges ≤𝑚 # of Vertices ≤ 𝑛 2 𝑘

13 # of vertices is ≤ 𝑛 2 log log 𝑛 = 𝒏 𝐥𝐨𝐠 𝒏
Boruvka’s + Prim’s Suppose 𝑘= log log 𝑛 , then # of vertices is ≤ 𝑛 2 log log 𝑛 = 𝒏 𝐥𝐨𝐠 𝒏

14 For 𝑘= log log 𝑛 iterations
Boruvka’s + Prim’s Boruvka’s Algorithm For 𝑘= log log 𝑛 iterations 𝑂(𝑚 log log 𝑛) Prim’s Algorithm 𝑛 ∗ ≤ 𝑛 log 𝑛 𝐺 ∗ 𝑂 𝑚+ 𝑛 ∗ log 𝑛 =𝑂 𝑚+ 𝑛 log 𝑛 log 𝑛 =𝑂(𝑚+𝑛)

15 Boruvka’s + Prim’s 𝑂(𝑚 log log 𝑛 ) + 𝑂 𝑚+ 𝑛 log 𝑛 log 𝑛 =𝑂(𝑚 log log 𝑛 ) Boruvka’s Algorithm Prim’s Algorithm

16 Questions?

17 Similarity to Dijkstra’s
Dijkstra’s Algorithm Prim’s Algorithm 𝑄←𝑃𝑟𝑖𝑜𝑟𝑖𝑡𝑦𝑄𝑢𝑒𝑢𝑒 𝑓𝑜𝑟 𝑒𝑎𝑐ℎ 𝑣𝑒𝑟𝑡𝑒𝑥 𝑣 𝑖𝑛 𝑉 𝑘𝑒𝑦 𝑣 ←∞ 𝑝𝑟𝑒𝑑 𝑣 ←𝑛𝑢𝑙𝑙 𝑄.𝑖𝑛𝑠𝑒𝑟𝑡(𝑣, 𝑘𝑒𝑦 𝑣 ) 𝑄.𝑑𝑒𝑐𝑟𝐾𝑒𝑦(𝑠,0) 𝑤ℎ𝑖𝑙𝑒 ! 𝑄.𝑖𝑠𝐸𝑚𝑝𝑡𝑦 𝑣←𝑄.𝑒𝑥𝑡𝑟𝑎𝑐𝑡𝑀𝑖𝑛 𝑓𝑜𝑟 𝑒𝑎𝑐ℎ 𝑒𝑑𝑔𝑒 𝑣,𝑢 𝑤ℎ𝑒𝑟𝑒 𝑢∈𝑄 𝒊𝒇 𝒌𝒆𝒚 𝒖 >𝒄 𝒗,𝒖 +𝒌𝒆𝒚 𝒗 𝒌𝒆𝒚 𝒖 ←𝒄 𝒗,𝒖 +𝒌𝒆𝒚[𝒗] 𝑸.𝒅𝒆𝒄𝒓𝑲𝒆𝒚 𝒖,𝒌𝒆𝒚[𝒖] 𝑝𝑟𝑒𝑑 𝑢 ←𝑣 𝑄←𝑃𝑟𝑖𝑜𝑟𝑖𝑡𝑦𝑄𝑢𝑒𝑢𝑒 𝑓𝑜𝑟 𝑒𝑎𝑐ℎ 𝑣𝑒𝑟𝑡𝑒𝑥 𝑣 𝑖𝑛 𝑉 𝑘𝑒𝑦 𝑣 ←∞ 𝑝𝑟𝑒𝑑 𝑣 ←𝑛𝑢𝑙𝑙 𝑄.𝑖𝑛𝑠𝑒𝑟𝑡(𝑣, 𝑘𝑒𝑦 𝑣 ) 𝑄.𝑑𝑒𝑐𝑟𝐾𝑒𝑦(𝑠,0) 𝑤ℎ𝑖𝑙𝑒 ! 𝑄.𝑖𝑠𝐸𝑚𝑝𝑡𝑦 𝑣←𝑄.𝑒𝑥𝑡𝑟𝑎𝑐𝑡𝑀𝑖𝑛 𝑓𝑜𝑟 𝑒𝑎𝑐ℎ 𝑒𝑑𝑔𝑒 𝑣,𝑢 𝑤ℎ𝑒𝑟𝑒 𝑢∈𝑄 𝒊𝒇 𝒌𝒆𝒚 𝒖 >𝒄 𝒗,𝒖 𝒌𝒆𝒚 𝒖 ←𝒄(𝒗,𝒖) 𝑸.𝒅𝒆𝒄𝒓𝑲𝒆𝒚 𝒖,𝒌𝒆𝒚[𝒖] 𝑝𝑟𝑒𝑑[𝑢]←𝑣


Download ppt "Prim’s Algorithm and an MST Speed-Up"

Similar presentations


Ads by Google