Prims Algorithm: Difference between revisions
No edit summary |
No edit summary |
||
| Line 1: | Line 1: | ||
{{Infobox Algorithm|class=[[Minimum_Spanning_Tree]] <br> [[Graph Algorithm]] <br> [[Greedy Algorithm]]|runtime=Heap: O(E log V) <br> Array: O(V^2)}} | {{Infobox Algorithm|class=[[Minimum_Spanning_Tree]] <br> [[Graph Algorithm]] <br> [[Greedy Algorithm]]|runtime=V*Extract_min + V + E + E update <br> Heap: O(E log V) <br> Array: O(V^2)}}<aside class="portable-infobox noexcerpt pi-background pi-theme-default pi-layout-stacked"> | ||
== Prims Algorithm == | |||
</aside><span></span> | |||
= Approach: Greedy = | = Approach: Greedy = | ||
Revision as of 23:52, 12 March 2024
<aside class="portable-infobox noexcerpt pi-background pi-theme-default pi-layout-stacked">
Prims Algorithm
</aside>
Approach: Greedy
Implementation
for each u in V:
key[u] = infinity // cost array
pi[u] = infinity // from array
Q = new PriorityQueue(V)
key[root] = 0
while Q is not empty:
u = extractMin(Q)
# Reduce nodes
for v in adj[u]:
if v in Q and w[u,v] < key[v]:
key[v] = w[u,v]
Analysis
Priority queue is slower than array when the graph is dense, so sometimes it's better to use Dijsktra's algorithm.
Proof: Greedy
Greeedy strategy: Let the greedy choice be the edge that is smallest that crosses the cut between Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle A} and .
Name greedy choice: let Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle r \rightarrow x} be the smallest edge that crosses the cut from Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle A = r} and Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle V - r} .
Given an optimal solution with (r,y), prove that Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle A = OPT - (r,y) + (r, x)} is still optimal.
Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle A} is still a tree since there is no cycles created.
Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle w(r,y) \geq w(r,x)} due to its properties as the greedy choice.
Therefore, Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle A} must be optimal.
