Lecture 9: Prim’s Algorithm Implementation
CSC 226: Algorithms and Data Structures II Quinton ’s Algorithm Pseudocode
𝑷𝒓𝒊𝒎𝑴𝑺𝑻(𝑮):
Input: A weighted connected graph 𝑮 with 𝒏 vertices and 𝒎 edges Output: An MST 𝑻 for 𝑮
Copyright By PowCoder代写 加微信 powcoder
Data structures: Array 𝑫 maintains minimum distance to vertices, Indexed Priority Queue 𝑸 of vertices, and Tree 𝑻
Pick an arbitrary vertex 𝒗 in 𝑮 𝑫𝒗←𝟎
for each vertex 𝒖 ≠ 𝒗 do
for each vertex 𝒖 do // including v
Add 𝒖,𝐧𝐮𝐥𝐥 𝐞𝐝𝐠𝐞 ,𝑫 𝒖 to 𝑸 // 𝐷 𝑢 is the priority key while 𝑸 is not empty do
𝒖, 𝒆 ← 𝑸. 𝒓𝒆𝒎𝒐𝒗𝒆𝑴𝒊𝒏()
Add vertex 𝒖 and edge 𝒆 to 𝑻
for each vertex 𝒛 adjacent to 𝒖 such that 𝒛 is in 𝑸 do
if𝒘𝒖,𝒛 <𝑫𝒛then 𝑫𝒛 ←𝒘 𝒖,𝒛
Change 𝒛 entry in 𝑸 to 𝒛,(𝒖,𝒛) ,𝑫 𝒛
Prim’s Algorithm Pseudocode
𝑷𝒓𝒊𝒎𝑴𝑺𝑻(𝑮):
Input: A weighted connected graph 𝑮 with 𝒏 vertices and 𝒎 edges Output: An MST 𝑻 for 𝑮
Data structures: Array 𝑫 maintains minimum distance to vertices, Indexed Priority Queue 𝑸 of vertices, and Tree 𝑻
Pick an arbitrary vertex 𝒗 in 𝑮 𝑫𝒗←𝟎
for each vertex 𝒖 ≠ 𝒗 do
for each vertex 𝒖 do // including v
Add 𝒖,𝐧𝐮𝐥𝐥𝐞𝐝𝐠𝐞 ,𝑫 𝒖 while 𝑸 is not empty do
to𝑸//𝐷 𝑢 istheprioritykey
𝒖, 𝒆 ← 𝑸. 𝒓𝒆𝒎𝒐𝒗𝒆𝑴𝒊𝒏()
Add vertex 𝒖 and edge 𝒆 to 𝑻
for each vertex 𝒛 adjacent to 𝒖 such that 𝒛 is in 𝑸 do
if𝒘𝒖,𝒛 <𝑫𝒛then 𝑫𝒛 ←𝒘 𝒖,𝒛
Change 𝒛 entry in 𝑸 to
𝒛,(𝒖,𝒛) ,𝑫 𝒛
𝒂𝒃𝒄𝒅𝒆𝒇𝒈𝒉𝒊𝒋
Prim’s Algorithm Pseudocode
𝑷𝒓𝒊𝒎𝑴𝑺𝑻(𝑮):
Input: A weighted connected graph 𝑮 with 𝒏 vertices and 𝒎 edges Output: An MST 𝑻 for 𝑮
Data structures: Array 𝑫 maintains minimum distance to vertices, Indexed Priority Queue 𝑸 of vertices, and Tree 𝑻
Pick an arbitrary vertex 𝒗 in 𝑮 𝑫𝒗←𝟎
for each vertex 𝒖 ≠ 𝒗 do
for each vertex 𝒖 do // including v
Add 𝒖,𝐧𝐮𝐥𝐥𝐞𝐝𝐠𝐞 ,𝑫 𝒖 while 𝑸 is not empty do
to𝑸//𝐷 𝑢 istheprioritykey
𝒖, 𝒆 ← 𝑸. 𝒓𝒆𝒎𝒐𝒗𝒆𝑴𝒊𝒏()
Add vertex 𝒖 and edge 𝒆 to 𝑻
for each vertex 𝒛 adjacent to 𝒖 such that 𝒛 is in 𝑸 do
if𝒘𝒖,𝒛 <𝑫𝒛then 𝑫𝒛 ←𝒘 𝒖,𝒛
Change 𝒛 entry in 𝑸 to
𝒛,(𝒖,𝒛) ,𝑫 𝒛
𝒂𝒃𝒄𝒅𝒆𝒇𝒈𝒉𝒊𝒋
Prim’s Algorithm Pseudocode
𝑷𝒓𝒊𝒎𝑴𝑺𝑻(𝑮):
Input: A weighted connected graph 𝑮 with 𝒏 vertices and 𝒎 edges Output: An MST 𝑻 for 𝑮
Data structures: Array 𝑫 maintains minimum distance to vertices, Indexed Priority Queue 𝑸 of vertices, and Tree 𝑻
Pick an arbitrary vertex 𝒗 in 𝑮 𝑫𝒗←𝟎
for each vertex 𝒖 ≠ 𝒗 do
for each vertex 𝒖 do // including v
Add 𝒖,𝐧𝐮𝐥𝐥𝐞𝐝𝐠𝐞 ,𝑫 𝒖 while 𝑸 is not empty do
to𝑸//𝐷 𝑢 istheprioritykey
𝒖, 𝒆 ← 𝑸. 𝒓𝒆𝒎𝒐𝒗𝒆𝑴𝒊𝒏()
Add vertex 𝒖 and edge 𝒆 to 𝑻
for each vertex 𝒛 adjacent to 𝒖 such that 𝒛 is in 𝑸 do
if𝒘𝒖,𝒛 <𝑫𝒛then 𝑫𝒛 ←𝒘 𝒖,𝒛
Change 𝒛 entry in 𝑸 to
𝒛,(𝒖,𝒛) ,𝑫 𝒛
𝒂𝒃𝒄𝒅𝒆𝒇𝒈𝒉𝒊𝒋
Prim’s Algorithm Pseudocode
𝑷𝒓𝒊𝒎𝑴𝑺𝑻(𝑮):
Input: A weighted connected graph 𝑮 with 𝒏 vertices and 𝒎 edges Output: An MST 𝑻 for 𝑮
Data structures: Array 𝑫 maintains minimum distance to vertices, Indexed Priority Queue 𝑸 of vertices, and Tree 𝑻
Pick an arbitrary vertex 𝒗 in 𝑮 𝑫𝒗←𝟎
for each vertex 𝒖 ≠ 𝒗 do
for each vertex 𝒖 do // including v
Add 𝒖,𝐧𝐮𝐥𝐥𝐞𝐝𝐠𝐞 ,𝑫 𝒖 while 𝑸 is not empty do
to𝑸//𝐷 𝑢 istheprioritykey
𝒖, 𝒆 ← 𝑸. 𝒓𝒆𝒎𝒐𝒗𝒆𝑴𝒊𝒏()
Add vertex 𝒖 and edge 𝒆 to 𝑻
for each vertex 𝒛 adjacent to 𝒖 such that 𝒛 is in 𝑸 do
if𝒘𝒖,𝒛 <𝑫𝒛then 𝑫𝒛 ←𝒘 𝒖,𝒛
Change 𝒛 entry in 𝑸 to
𝒛,(𝒖,𝒛) ,𝑫 𝒛
𝒂𝒃𝒄𝒅𝒆𝒇𝒈𝒉𝒊𝒋
Prim’s Algorithm Pseudocode
𝑷𝒓𝒊𝒎𝑴𝑺𝑻(𝑮):
Input: A weighted connected graph 𝑮 with 𝒏 vertices and 𝒎 edges Output: An MST 𝑻 for 𝑮
Data structures: Array 𝑫 maintains minimum distance to vertices, Indexed Priority Queue 𝑸 of vertices, and Tree 𝑻
Pick an arbitrary vertex 𝒗 in 𝑮 𝑫𝒗←𝟎
for each vertex 𝒖 ≠ 𝒗 do
for each vertex 𝒖 do // including v
Add 𝒖,𝐧𝐮𝐥𝐥𝐞𝐝𝐠𝐞 ,𝑫 𝒖 while 𝑸 is not empty do
to𝑸//𝐷 𝑢 istheprioritykey
𝒖, 𝒆 ← 𝑸. 𝒓𝒆𝒎𝒐𝒗𝒆𝑴𝒊𝒏()
Add vertex 𝒖 and edge 𝒆 to 𝑻
for each vertex 𝒛 adjacent to 𝒖 such that 𝒛 is in 𝑸 do
if𝒘𝒖,𝒛 <𝑫𝒛then 𝑫𝒛 ←𝒘 𝒖,𝒛
Change 𝒛 entry in 𝑸 to
𝒛,(𝒖,𝒛) ,𝑫 𝒛
𝒂𝒃𝒄𝒅𝒆𝒇𝒈𝒉𝒊𝒋
Prim’s Algorithm Pseudocode
𝑷𝒓𝒊𝒎𝑴𝑺𝑻(𝑮):
Input: A weighted connected graph 𝑮 with 𝒏 vertices and 𝒎 edges Output: An MST 𝑻 for 𝑮
Data structures: Array 𝑫 maintains minimum distance to vertices, Indexed Priority Queue 𝑸 of vertices, and Tree 𝑻
Pick an arbitrary vertex 𝒗 in 𝑮 𝑫𝒗←𝟎
for each vertex 𝒖 ≠ 𝒗 do
for each vertex 𝒖 do // including v
Add 𝒖,𝐧𝐮𝐥𝐥𝐞𝐝𝐠𝐞 ,𝑫 𝒖 while 𝑸 is not empty do
to𝑸//𝐷 𝑢 istheprioritykey
𝒖, 𝒆 ← 𝑸. 𝒓𝒆𝒎𝒐𝒗𝒆𝑴𝒊𝒏()
Add vertex 𝒖 and edge 𝒆 to 𝑻
for each vertex 𝒛 adjacent to 𝒖 such that 𝒛 is in 𝑸 do
if𝒘𝒖,𝒛 <𝑫𝒛then 𝑫𝒛 ←𝒘 𝒖,𝒛
Change 𝒛 entry in 𝑸 to
𝒛,(𝒖,𝒛) ,𝑫 𝒛
𝒂𝒃𝒄𝒅𝒆𝒇𝒈𝒉𝒊𝒋
Prim’s Algorithm Pseudocode
𝑷𝒓𝒊𝒎𝑴𝑺𝑻(𝑮):
Input: A weighted connected graph 𝑮 with 𝒏 vertices and 𝒎 edges Output: An MST 𝑻 for 𝑮
Data structures: Array 𝑫 maintains minimum distance to vertices, Indexed Priority Queue 𝑸 of vertices, and Tree 𝑻
Pick an arbitrary vertex 𝒗 in 𝑮 𝑫𝒗←𝟎
for each vertex 𝒖 ≠ 𝒗 do
for each vertex 𝒖 do // including v
Add 𝒖,𝐧𝐮𝐥𝐥𝐞𝐝𝐠𝐞 ,𝑫 𝒖 while 𝑸 is not empty do
to𝑸//𝐷 𝑢 istheprioritykey
𝒖, 𝒆 ← 𝑸. 𝒓𝒆𝒎𝒐𝒗𝒆𝑴𝒊𝒏()
Add vertex 𝒖 and edge 𝒆 to 𝑻
for each vertex 𝒛 adjacent to 𝒖 such that 𝒛 is in 𝑸 do
if𝒘𝒖,𝒛 <𝑫𝒛then 𝑫𝒛 ←𝒘 𝒖,𝒛
Change 𝒛 entry in 𝑸 to
𝒛,(𝒖,𝒛) ,𝑫 𝒛
𝒂𝒃𝒄𝒅𝒆𝒇𝒈𝒉𝒊𝒋
Prim’s Algorithm Pseudocode
𝑷𝒓𝒊𝒎𝑴𝑺𝑻(𝑮):
Input: A weighted connected graph 𝑮 with 𝒏 vertices and 𝒎 edges Output: An MST 𝑻 for 𝑮
Data structures: Array 𝑫 maintains minimum distance to vertices, Indexed Priority Queue 𝑸 of vertices, and Tree 𝑻
Pick an arbitrary vertex 𝒗 in 𝑮 𝑫𝒗←𝟎
for each vertex 𝒖 ≠ 𝒗 do
for each vertex 𝒖 do // including v
Add 𝒖,𝐧𝐮𝐥𝐥𝐞𝐝𝐠𝐞 ,𝑫 𝒖 while 𝑸 is not empty do
to𝑸//𝐷 𝑢 istheprioritykey
𝒖, 𝒆 ← 𝑸. 𝒓𝒆𝒎𝒐𝒗𝒆𝑴𝒊𝒏()
Add vertex 𝒖 and edge 𝒆 to 𝑻
for each vertex 𝒛 adjacent to 𝒖 such that 𝒛 is in 𝑸 do
if𝒘𝒖,𝒛 <𝑫𝒛then 𝑫𝒛 ←𝒘 𝒖,𝒛
Change 𝒛 entry in 𝑸 to
𝒛,(𝒖,𝒛) ,𝑫 𝒛
𝒂𝒃𝒄𝒅𝒆𝒇𝒈𝒉𝒊𝒋
Prim’s Algorithm Pseudocode
𝑷𝒓𝒊𝒎𝑴𝑺𝑻(𝑮):
Input: A weighted connected graph 𝑮 with 𝒏 vertices and 𝒎 edges Output: An MST 𝑻 for 𝑮
Data structures: Array 𝑫 maintains minimum distance to vertices, Indexed Priority Queue 𝑸 of vertices, and Tree 𝑻
Pick an arbitrary vertex 𝒗 in 𝑮 𝑫𝒗←𝟎
for each vertex 𝒖 ≠ 𝒗 do
for each vertex 𝒖 do // including v
Add 𝒖,𝐧𝐮𝐥𝐥𝐞𝐝𝐠𝐞 ,𝑫 𝒖 while 𝑸 is not empty do
to𝑸//𝐷 𝑢 istheprioritykey
𝒖, 𝒆 ← 𝑸. 𝒓𝒆𝒎𝒐𝒗𝒆𝑴𝒊𝒏()
Add vertex 𝒖 and edge 𝒆 to 𝑻
for each vertex 𝒛 adjacent to 𝒖 such that 𝒛 is in 𝑸 do
if𝒘𝒖,𝒛 <𝑫𝒛then 𝑫𝒛 ←𝒘 𝒖,𝒛
Change 𝒛 entry in 𝑸 to
𝒛,(𝒖,𝒛) ,𝑫 𝒛
𝒂𝒃𝒄𝒅𝒆𝒇𝒈𝒉𝒊𝒋
Prim’s Algorithm Pseudocode
𝑷𝒓𝒊𝒎𝑴𝑺𝑻(𝑮):
Input: A weighted connected graph 𝑮 with 𝒏 vertices and 𝒎 edges Output: An MST 𝑻 for 𝑮
Data structures: Array 𝑫 maintains minimum distance to vertices, Indexed Priority Queue 𝑸 of vertices, and Tree 𝑻
Pick an arbitrary vertex 𝒗 in 𝑮 𝑫𝒗←𝟎
for each vertex 𝒖 ≠ 𝒗 do
for each vertex 𝒖 do // including v
Add 𝒖,𝐧𝐮𝐥𝐥𝐞𝐝𝐠𝐞 ,𝑫 𝒖 while 𝑸 is not empty do
to𝑸//𝐷 𝑢 istheprioritykey
𝒖, 𝒆 ← 𝑸. 𝒓𝒆𝒎𝒐𝒗𝒆𝑴𝒊𝒏()
Add vertex 𝒖 and edge 𝒆 to 𝑻
for each vertex 𝒛 adjacent to 𝒖 such that 𝒛 is in 𝑸 do
if𝒘𝒖,𝒛 <𝑫𝒛then 𝑫𝒛 ←𝒘 𝒖,𝒛
Change 𝒛 entry in 𝑸 to
𝒛,(𝒖,𝒛) ,𝑫 𝒛
𝒂𝒃𝒄𝒅𝒆𝒇𝒈𝒉𝒊𝒋
Prim’s Algorithm Pseudocode
𝑷𝒓𝒊𝒎𝑴𝑺𝑻(𝑮):
Input: A weighted connected graph 𝑮 with 𝒏 vertices and 𝒎 edges Output: An MST 𝑻 for 𝑮
Data structures: Array 𝑫 maintains minimum distance to vertices, Indexed Priority Queue 𝑸 of vertices, and Tree 𝑻
Pick an arbitrary vertex 𝒗 in 𝑮 𝑫𝒗←𝟎
for each vertex 𝒖 ≠ 𝒗 do
for each vertex 𝒖 do // including v
Add 𝒖,𝐧𝐮𝐥𝐥𝐞𝐝𝐠𝐞 ,𝑫 𝒖 while 𝑸 is not empty do
to𝑸//𝐷 𝑢 istheprioritykey
𝒖, 𝒆 ← 𝑸. 𝒓𝒆𝒎𝒐𝒗𝒆𝑴𝒊𝒏()
Add vertex 𝒖 and edge 𝒆 to 𝑻
for each vertex 𝒛 adjacent to 𝒖 such that 𝒛 is in 𝑸 do
if𝒘𝒖,𝒛 <𝑫𝒛then 𝑫𝒛 ←𝒘 𝒖,𝒛
Change 𝒛 entry in 𝑸 to
𝒛,(𝒖,𝒛) ,𝑫 𝒛
𝒂𝒃𝒄𝒅𝒆𝒇𝒈𝒉𝒊𝒋
Prim’s Algorithm Pseudocode
𝑷𝒓𝒊𝒎𝑴𝑺𝑻(𝑮):
Input: A weighted connected graph 𝑮 with 𝒏 vertices and 𝒎 edges Output: An MST 𝑻 for 𝑮
Data structures: Array 𝑫 maintains minimum distance to vertices, Indexed Priority Queue 𝑸 of vertices, and Tree 𝑻
Pick an arbitrary vertex 𝒗 in 𝑮 𝑫𝒗←𝟎
for each vertex 𝒖 ≠ 𝒗 do
for each vertex 𝒖 do // including v
Add 𝒖,𝐧𝐮𝐥𝐥𝐞𝐝𝐠𝐞 ,𝑫 𝒖 while 𝑸 is not empty do
to𝑸//𝐷 𝑢 istheprioritykey
𝒖, 𝒆 ← 𝑸. 𝒓𝒆𝒎𝒐𝒗𝒆𝑴𝒊𝒏()
Add vertex 𝒖 and edge 𝒆 to 𝑻
for each vertex 𝒛 adjacent to 𝒖 such that 𝒛 is in 𝑸 do
if𝒘𝒖,𝒛 <𝑫𝒛then 𝑫𝒛 ←𝒘 𝒖,𝒛
Change 𝒛 entry in 𝑸 to
𝒛,(𝒖,𝒛) ,𝑫 𝒛
𝒂𝒃𝒄𝒅𝒆𝒇𝒈𝒉𝒊𝒋
Prim’s Algorithm Pseudocode
𝑷𝒓𝒊𝒎𝑴𝑺𝑻(𝑮):
Input: A weighted connected graph 𝑮 with 𝒏 vertices and 𝒎 edges Output: An MST 𝑻 for 𝑮
Data structures: Array 𝑫 maintains minimum distance to vertices, Indexed Priority Queue 𝑸 of vertices, and Tree 𝑻
Pick an arbitrary vertex 𝒗 in 𝑮 𝑫𝒗←𝟎
for each vertex 𝒖 ≠ 𝒗 do
for each vertex 𝒖 do // including v
Add 𝒖,𝐧𝐮𝐥𝐥𝐞𝐝𝐠𝐞 ,𝑫 𝒖 while 𝑸 is not empty do
to𝑸//𝐷 𝑢 istheprioritykey
𝒖, 𝒆 ← 𝑸. 𝒓𝒆𝒎𝒐𝒗𝒆𝑴𝒊𝒏()
Add vertex 𝒖 and edge 𝒆 to 𝑻
for each vertex 𝒛 adjacent to 𝒖 such that 𝒛 is in 𝑸 do
if𝒘𝒖,𝒛 <𝑫𝒛then 𝑫𝒛 ←𝒘 𝒖,𝒛
Change 𝒛 entry in 𝑸 to
𝒛,(𝒖,𝒛) ,𝑫 𝒛
𝒂𝒃𝒄𝒅𝒆𝒇𝒈𝒉𝒊𝒋
Prim’s Algorithm Pseudocode
𝑷𝒓𝒊𝒎𝑴𝑺𝑻(𝑮):
Input: A weighted connected graph 𝑮 with 𝒏 vertices and 𝒎 edges Output: An MST 𝑻 for 𝑮
Data structures: Array 𝑫 maintains minimum distance to vertices, Indexed Priority Queue 𝑸 of vertices, and Tree 𝑻
Pick an arbitrary vertex 𝒗 in 𝑮 𝑫𝒗←𝟎
for each vertex 𝒖 ≠ 𝒗 do
for each vertex 𝒖 do // including v
Add 𝒖,𝐧𝐮𝐥𝐥𝐞𝐝𝐠𝐞 ,𝑫 𝒖 while 𝑸 is not empty do
to𝑸//𝐷 𝑢 istheprioritykey
𝒖, 𝒆 ← 𝑸. 𝒓𝒆𝒎𝒐𝒗𝒆𝑴𝒊𝒏()
Add vertex 𝒖 and edge 𝒆 to 𝑻
for each vertex 𝒛 adjacent to 𝒖 such that 𝒛 is in 𝑸 do
if𝒘𝒖,𝒛 <𝑫𝒛then 𝑫𝒛 ←𝒘 𝒖,𝒛
Change 𝒛 entry in 𝑸 to
𝒛,(𝒖,𝒛) ,𝑫 𝒛
𝒂𝒃𝒄𝒅𝒆𝒇𝒈𝒉𝒊𝒋
Prim’s Algorithm Pseudocode
𝑷𝒓𝒊𝒎𝑴𝑺𝑻(𝑮):
Input: A weighted connected graph 𝑮 with 𝒏 vertices and 𝒎 edges Output: An MST 𝑻 for 𝑮
Data structures: Array 𝑫 maintains minimum distance to vertices, Indexed Priority Queue 𝑸 of vertices, and Tree 𝑻
Pick an arbitrary vertex 𝒗 in 𝑮 𝑫𝒗←𝟎
for each vertex 𝒖 ≠ 𝒗 do
for each vertex 𝒖 do // including v
Add 𝒖,𝐧𝐮𝐥𝐥𝐞𝐝𝐠𝐞 ,𝑫 𝒖 while 𝑸 is not empty do
to𝑸//𝐷 𝑢 istheprioritykey
𝒖, 𝒆 ← 𝑸. 𝒓𝒆𝒎𝒐𝒗𝒆𝑴𝒊𝒏()
Add vertex 𝒖 and edge 𝒆 to 𝑻
for each vertex 𝒛 adjacent to 𝒖 such that 𝒛 is in 𝑸 do
if𝒘𝒖,𝒛 <𝑫𝒛then 𝑫𝒛 ←𝒘 𝒖,𝒛
Change 𝒛 entry in 𝑸 to
𝒛,(𝒖,𝒛) ,𝑫 𝒛
𝒂𝒃𝒄𝒅𝒆𝒇𝒈𝒉𝒊𝒋
Prim’s Algorithm Pseudocode
𝑷𝒓𝒊𝒎𝑴𝑺𝑻(𝑮):
Input: A weighted connected graph 𝑮 with 𝒏 vertices and 𝒎 edges Output: An MST 𝑻 for 𝑮
Data structures: Array 𝑫 maintains minimum distance to vertices, Indexed Priority Queue 𝑸 of vertices, and Tree 𝑻
Pick an arbitrary vertex 𝒗 in 𝑮 𝑫𝒗←𝟎
for each vertex 𝒖 ≠ 𝒗 do
for each vertex 𝒖 do // including v
Add 𝒖,𝐧𝐮𝐥𝐥𝐞𝐝𝐠𝐞 ,𝑫 𝒖 while 𝑸 is not empty do
to𝑸//𝐷 𝑢 istheprioritykey
𝒖, 𝒆 ← 𝑸. 𝒓𝒆𝒎𝒐𝒗𝒆𝑴𝒊𝒏()
Add vertex 𝒖 and edge 𝒆 to 𝑻
for each vertex 𝒛 adjacent to 𝒖 such that 𝒛 is in 𝑸 do
if𝒘𝒖,𝒛 <𝑫𝒛then 𝑫𝒛 ←𝒘 𝒖,𝒛
Change 𝒛 entry in 𝑸 to
𝒛,(𝒖,𝒛) ,𝑫 𝒛
𝒂𝒃𝒄𝒅𝒆𝒇𝒈𝒉𝒊𝒋
Prim’s Algorithm Pseudocode
𝑷𝒓𝒊𝒎𝑴𝑺𝑻(𝑮):
Input: A weighted connected graph 𝑮 with 𝒏 vertices and 𝒎 edges Output: An MST 𝑻 for 𝑮
Data structures: Array 𝑫 maintains minimum distance to vertices, Indexed Priority Queue 𝑸 of vertices, and Tree 𝑻
Pick an arbitrary vertex 𝒗 in 𝑮 𝑫𝒗←𝟎
for each vertex 𝒖 ≠ 𝒗 do
for each vertex 𝒖 do // including v
Add 𝒖,𝐧𝐮𝐥𝐥𝐞𝐝𝐠𝐞 ,𝑫 𝒖 while 𝑸 is not empty do
to𝑸//𝐷 𝑢 istheprioritykey
𝒖, 𝒆 ← 𝑸. 𝒓𝒆𝒎𝒐𝒗𝒆𝑴𝒊𝒏()
Add vertex 𝒖 and edge 𝒆 to 𝑻
for each vertex 𝒛 adjacent to 𝒖 such that 𝒛 is in 𝑸 do
if𝒘𝒖,𝒛 <𝑫𝒛then 𝑫𝒛 ←𝒘 𝒖,𝒛
Change 𝒛 entry in 𝑸 to
𝒛,(𝒖,𝒛) ,𝑫 𝒛
𝒂𝒃𝒄𝒅𝒆𝒇𝒈𝒉𝒊𝒋
Prim’s Algorithm Pseudocode
𝑷𝒓𝒊𝒎𝑴𝑺𝑻(𝑮):
Input: A weighted connected graph 𝑮 with 𝒏 vertices and 𝒎 edges Output: An MST 𝑻 for 𝑮
Data structures: Array 𝑫 maintains minimum distance to vertices, Indexed Priority Queue 𝑸 of vertices, and Tree 𝑻
Pick an arbitrary vertex 𝒗 in 𝑮 𝑫𝒗←𝟎
for each vertex 𝒖 ≠ 𝒗 do
for each vertex 𝒖 do // including v
Add 𝒖,𝐧𝐮𝐥𝐥𝐞𝐝𝐠𝐞 ,𝑫 𝒖 while 𝑸 is not empty do
to𝑸//𝐷 𝑢 istheprioritykey
𝒖, 𝒆 ← 𝑸. 𝒓𝒆𝒎𝒐𝒗𝒆𝑴𝒊𝒏()
Add vertex 𝒖 and edge 𝒆 to 𝑻
for each vertex 𝒛 adjacent to 𝒖 such that 𝒛 is in 𝑸 do
if𝒘𝒖,𝒛 <𝑫𝒛then 𝑫𝒛 ←𝒘 𝒖,𝒛
Change 𝒛 entry in 𝑸 to
𝒛,(𝒖,𝒛) ,𝑫 𝒛
𝒂𝒃𝒄𝒅𝒆𝒇𝒈𝒉𝒊𝒋
Prim’s Algorithm Pseudocode
𝑷𝒓𝒊𝒎𝑴𝑺𝑻(𝑮):
Input: A weighted connected graph 𝑮 with 𝒏 vertices and 𝒎 edges Output: An MST 𝑻 for 𝑮
Data structures: Array 𝑫 maintains minimum distance to vertices, Indexed Priority Queue 𝑸 of vertices, and Tree 𝑻
Pick an arbitrary vertex 𝒗 in 𝑮 𝑫𝒗←𝟎
for each vertex 𝒖 ≠ 𝒗 do
for each vertex 𝒖 do // including v
Add 𝒖,𝐧𝐮𝐥𝐥𝐞𝐝𝐠𝐞 ,𝑫 𝒖 while 𝑸 is not empty do
to𝑸//𝐷 𝑢 istheprioritykey
𝒖, 𝒆 ← 𝑸. 𝒓𝒆𝒎𝒐𝒗𝒆𝑴𝒊𝒏()
Add vertex 𝒖 and edge 𝒆 to 𝑻
for each vertex 𝒛 adjacent to 𝒖 such that 𝒛 is in 𝑸 do
if𝒘𝒖,𝒛 <𝑫𝒛then 𝑫𝒛 ←𝒘 𝒖,𝒛
Change 𝒛 entry in 𝑸 to
𝒛,(𝒖,𝒛) ,𝑫 𝒛
𝒂𝒃𝒄𝒅𝒆𝒇𝒈𝒉𝒊𝒋
Prim’s Algorithm Pseudocode
𝑷𝒓𝒊𝒎𝑴𝑺𝑻(𝑮):
Input: A weighted connected graph 𝑮 with 𝒏 vertices and 𝒎 edges Output: An MST 𝑻 for 𝑮
Data structures: Array 𝑫 maintains minimum distance to vertices, Indexed Priority Queue 𝑸 of vertices, and Tree 𝑻
Pick an arbitrary vertex 𝒗 in 𝑮 𝑫𝒗←𝟎
for each vertex 𝒖 ≠ 𝒗 do
for each vertex 𝒖 do // including v
Add 𝒖,𝐧𝐮𝐥𝐥𝐞𝐝𝐠𝐞 ,𝑫 𝒖 while 𝑸 is not empty do
to𝑸//𝐷 𝑢 istheprioritykey
𝒖, 𝒆 ← 𝑸. 𝒓𝒆𝒎𝒐𝒗𝒆𝑴𝒊𝒏()
Add vertex 𝒖 and edge 𝒆 to 𝑻
for each vertex 𝒛 adjacent to 𝒖 such that 𝒛 is in 𝑸 do
if𝒘𝒖,𝒛 <𝑫𝒛then 𝑫𝒛 ←𝒘 𝒖,𝒛
Change 𝒛 entry in 𝑸 to
𝒛,(𝒖,𝒛) ,𝑫 𝒛
𝒂𝒃𝒄𝒅𝒆𝒇𝒈𝒉𝒊𝒋
Prim’s Algorithm Pseudocode
𝑷𝒓𝒊𝒎𝑴𝑺𝑻(𝑮):
Input: A weighted connected graph 𝑮 with 𝒏 vertices and 𝒎 edges Output: An MST 𝑻 for 𝑮
Data structures: Array 𝑫 maintains minimum distance to vertices, Indexed Priority Queue 𝑸 of vertices, and Tree 𝑻
Pick an arbitrary vertex 𝒗 in 𝑮 𝑫𝒗←𝟎
for each vertex 𝒖 ≠ 𝒗 do
for each vertex 𝒖 do // including v
Add 𝒖,𝐧𝐮𝐥𝐥𝐞𝐝𝐠𝐞 ,𝑫 𝒖 while 𝑸 is not empty do
to𝑸//𝐷 𝑢 istheprioritykey
𝒖, 𝒆 ← 𝑸. 𝒓𝒆𝒎𝒐𝒗𝒆𝑴𝒊𝒏()
Add vertex 𝒖 and edge 𝒆 to 𝑻
for each vertex 𝒛 adjacent to 𝒖 such that 𝒛 is in 𝑸 do
if𝒘𝒖,𝒛 <𝑫𝒛then 𝑫𝒛 ←𝒘 𝒖,𝒛
Change 𝒛 entry in 𝑸 to
𝒛,(𝒖,𝒛) ,𝑫 𝒛
𝒂𝒃𝒄𝒅𝒆𝒇𝒈𝒉𝒊𝒋
Prim’s Algorithm Pseudocode
𝑷𝒓𝒊𝒎𝑴𝑺𝑻(𝑮):
Input: A weighted connected graph 𝑮 with 𝒏 vertices and 𝒎 edges Output: An MST 𝑻 for 𝑮
Data structures: Array 𝑫 maintains minimum distance to vertices, Indexed Priority Queue 𝑸 of vertices, and Tree 𝑻
Pick an arbitrary vertex 𝒗 in 𝑮 𝑫𝒗←𝟎
for each vertex 𝒖 ≠ 𝒗 do
for each vertex 𝒖 do // including v
Add 𝒖,𝐧𝐮𝐥𝐥𝐞𝐝𝐠𝐞 ,𝑫 𝒖 while 𝑸 is not empty do
to𝑸//𝐷 𝑢 istheprioritykey
𝒖, 𝒆 ← 𝑸. 𝒓𝒆𝒎𝒐𝒗𝒆𝑴𝒊𝒏()
Add vertex 𝒖 and edge 𝒆 to 𝑻
for each vertex 𝒛 adjacent to 𝒖 such that 𝒛 is in 𝑸 do
if𝒘𝒖,𝒛 <𝑫𝒛then 𝑫𝒛 ←𝒘 𝒖,𝒛
Change 𝒛 entry in 𝑸 to
𝒛,(𝒖,𝒛) ,𝑫 𝒛
𝒂𝒃𝒄𝒅𝒆𝒇𝒈𝒉𝒊𝒋
Prim’s Algorithm Pseudocode
𝑷𝒓𝒊𝒎𝑴𝑺𝑻(𝑮):
Input: A weighted connected graph 𝑮 with 𝒏 vertices and 𝒎 edges Output: An MST 𝑻 for 𝑮
Data structures: Array 𝑫 maintains minimum distance to vertices, Indexed Priority Queue 𝑸 of vertices, and Tree 𝑻
Pick an arbitrary vertex 𝒗 in 𝑮 𝑫𝒗←𝟎
for each vertex 𝒖 ≠ 𝒗 do
for each vertex 𝒖 do // including v
Add 𝒖,𝐧𝐮𝐥𝐥𝐞𝐝𝐠𝐞 ,𝑫 𝒖 while 𝑸 is not empty do
to𝑸//𝐷 𝑢 istheprioritykey
𝒖, 𝒆 ← 𝑸. 𝒓𝒆𝒎𝒐𝒗𝒆𝑴𝒊𝒏()
Add vertex 𝒖 and edge 𝒆 to 𝑻
for each vertex 𝒛 adjacent to 𝒖 such that 𝒛 is in 𝑸 do
if𝒘𝒖,𝒛 <𝑫𝒛then 𝑫𝒛 ←𝒘 𝒖,𝒛
Change 𝒛 entry in 𝑸 to
𝒛,(𝒖,𝒛) ,𝑫 𝒛
𝒂𝒃𝒄𝒅𝒆𝒇𝒈𝒉𝒊𝒋
Prim’s Algorithm Pseudocode
𝑷𝒓𝒊𝒎𝑴𝑺𝑻(𝑮):
Input: A weighted connected graph 𝑮 with 𝒏 vertices and 𝒎 edges Output: An MST 𝑻 for 𝑮
Data structures: Array 𝑫 maintains minimum distance to vertices, Indexed Priority Queue 𝑸 of vertices, and Tree 𝑻
Pick an arbitrary vertex 𝒗 in 𝑮 𝑫𝒗←𝟎
for each vertex 𝒖 ≠ 𝒗 do
for each vertex 𝒖 do // including v
Add 𝒖,𝐧𝐮𝐥𝐥𝐞𝐝𝐠𝐞 ,𝑫 𝒖 while 𝑸 is not empty do
to𝑸//𝐷 𝑢 istheprioritykey
𝒖, 𝒆 ← 𝑸. 𝒓𝒆𝒎𝒐𝒗𝒆𝑴𝒊𝒏()
Add vertex 𝒖 and edge 𝒆 to 𝑻
for each vertex 𝒛 adjacent to 𝒖 such that 𝒛 is in 𝑸 do
if𝒘𝒖,𝒛 <𝑫𝒛then 𝑫𝒛 ←𝒘 𝒖,𝒛
Change 𝒛 entry in 𝑸 to
𝒛,(𝒖,𝒛) ,𝑫 𝒛
𝒂𝒃𝒄𝒅𝒆𝒇𝒈𝒉𝒊𝒋
Prim’s Algorithm Pseudocode
𝑷𝒓𝒊𝒎𝑴𝑺𝑻(𝑮):
Input: A weighted connected graph 𝑮 with 𝒏 vertices and 𝒎 edges Output: An MST 𝑻 for 𝑮
Data structures: Array 𝑫 maintains minimum distance to vertices, Indexed Priority Queue 𝑸 of vertices, and Tree 𝑻
Pick an arbitrary vertex 𝒗 in 𝑮 𝑫𝒗←𝟎
for each vertex 𝒖 ≠ 𝒗 do
for each vertex 𝒖 do // including v
Add 𝒖,𝐧𝐮𝐥𝐥𝐞𝐝𝐠𝐞 ,𝑫 𝒖 while 𝑸 is not empty do
to𝑸//𝐷 𝑢 istheprioritykey
𝒖, 𝒆 ← 𝑸. 𝒓𝒆𝒎𝒐𝒗𝒆𝑴𝒊𝒏()
Add vertex 𝒖 and edge 𝒆 to 𝑻
for each vertex 𝒛 adjacent to 𝒖 such that 𝒛 is in 𝑸 do
if𝒘𝒖,𝒛 <𝑫𝒛then 𝑫𝒛 ←𝒘 𝒖,𝒛
Change 𝒛 entry in 𝑸 to
𝒛,(𝒖,𝒛) ,𝑫 𝒛
𝒂𝒃𝒄𝒅𝒆𝒇𝒈𝒉𝒊𝒋
Prim’s Algorithm Pseudocode
𝑷𝒓𝒊𝒎𝑴𝑺𝑻(𝑮):
Input: A weighted connected graph 𝑮 with 𝒏 vertices and 𝒎 edges Output: An MST 𝑻 for 𝑮
Data structures: Array 𝑫 maintains minimum distance to vertices, Indexed Priority Queue 𝑸 of vertices, and Tree 𝑻
Pick an arbitrary vertex 𝒗 in 𝑮 𝑫𝒗←𝟎
for each vertex 𝒖 ≠ 𝒗 do
for each vertex 𝒖 do // including v
Add 𝒖,𝐧𝐮𝐥𝐥𝐞𝐝𝐠𝐞 ,𝑫 𝒖 while 𝑸 is not empty do
to𝑸//𝐷 𝑢 istheprioritykey
𝒖, 𝒆 ← 𝑸. 𝒓𝒆𝒎𝒐𝒗𝒆𝑴𝒊𝒏()
Add vertex 𝒖 and edge 𝒆 to 𝑻
for each vertex 𝒛 adjacent to 𝒖 such that 𝒛 is in 𝑸 do
if𝒘𝒖,𝒛 <𝑫𝒛then 𝑫𝒛 ←𝒘 𝒖,𝒛
Change 𝒛 entry in 𝑸 to
𝒛,(𝒖,𝒛) ,𝑫 𝒛
𝒂𝒃𝒄𝒅𝒆𝒇𝒈𝒉𝒊𝒋
Prim’s Algorithm Pseudocode
𝑷𝒓𝒊𝒎𝑴𝑺𝑻(𝑮):
Input: A weighted connected graph 𝑮 with 𝒏 vertices and 𝒎 edges Output: An MST 𝑻 for 𝑮
Data structures: Array 𝑫 maintains minimum distance to vertices, Indexed Priority Queue 𝑸 of vertices, and Tree 𝑻
Pick an arbitrary vertex 𝒗 in 𝑮 𝑫𝒗←𝟎
for each vertex 𝒖 ≠ 𝒗 do
for each vertex 𝒖 do // including v
Add 𝒖,𝐧𝐮𝐥𝐥𝐞𝐝𝐠𝐞 ,𝑫 𝒖 while 𝑸 is not empty do
to𝑸//𝐷 𝑢 istheprioritykey
𝒖, 𝒆 ← 𝑸. 𝒓𝒆𝒎𝒐𝒗𝒆𝑴𝒊𝒏()
Add vertex 𝒖 and edge 𝒆 to 𝑻
for each vertex 𝒛 adjacent to 𝒖 such that 𝒛 is in 𝑸 do
if𝒘𝒖,𝒛 <𝑫𝒛then 𝑫𝒛 ←𝒘 𝒖,𝒛
Change 𝒛 entry in 𝑸 to
𝒛,(𝒖,𝒛) ,𝑫 𝒛
𝒂𝒃𝒄𝒅𝒆𝒇𝒈𝒉𝒊𝒋
Prim’s Algorithm Running Time
𝑷𝒓𝒊𝒎𝑴𝑺𝑻(𝑮): Implemented as a heap.
Input: A weighted connected graph 𝑮 with 𝒏 vertices and 𝒎 edges Output: An MST 𝑻 f
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com