Minimum Spanning Tree: Difference between revisions

From Rice Wiki
No edit summary
 
(3 intermediate revisions by the same user not shown)
Line 4: Line 4:
* spanning, meaning it connects all nodes
* spanning, meaning it connects all nodes


= MST Problem =
The '''MST problem''' takes a connected graph <math>G</math> and outputs an MST for that graph.


The '''MST problem''' takes a connected graph <math>G</math> and outputs
Algorithm that solves this problem includes [[Kruskal's Algorithm]] and [[Prims Algorithm]].
an MST for that graph.


== Approach: Greedy ==
= Suboptimality =


The approach is to try to add the smallest edges as long as they do not
The MST problem exhibits the optimal substructure property.
create a cycle; add an edge to the tree that is minimum across the cut
of <math>T</math> vs. <math>V - T</math>


Given the MST of <math>V_{n - 1} = v_1, v_2, \ldots, v_{n-1} </math>,
Given MST tree for the graph G
the MST of <math>V</math> should be that of <math>V_{n-1}</math> plus
the edge that connects to <math>v_n</math> that is the shortest.


<math>
<math>
OPT(n) = OPT(n-1) + min( (v_i, v_n) \in E )
OPT = \text{set of edges}
</math>
</math>


where the number of edges is <math>n - 1</math>, the weight of edges is
minimum, and the tree is spanning.
Consider <math>(x,y)</math>, where X is a leaf node.
Prove that <math>A = OPT - (x, y)</math> is a MST for the subproblem
<math>G - X</math>
By contradiction, assume that <math>A</math> is not optimal. There must
be <math>B</math> such that
<math>
w(B) < w(A)
</math>
Adding the edge (x, y) back to both graphs
<math>B + (x, y)</math> is a viable tree.
<math>
w(B + w(x,y)) < w(A + w(x,y)) = w(OPT)
</math>
Therefore, OPT is not the optimal solution.
By contradiction, <math>A</math> must be optimal.


== Implementation ==


<pre>
MST(G):
    mst_nodes = start_node
    mst = adjacency list with no edges
    # candidate_edges is a min heap
    candidate_edges = (start_node, G[start_node])
    while mst_nodes is not spanning:
        new_edge = candidate_edges.pop()
</pre>


[[Category:Algorithms]]
[[Category:Algorithms]]

Latest revision as of 17:40, 20 March 2024

A minimum spanning tree is

  • a tree, meaning it has no cycle
  • minimum, meaning it has minimum weight
  • spanning, meaning it connects all nodes

The MST problem takes a connected graph and outputs an MST for that graph.

Algorithm that solves this problem includes Kruskal's Algorithm and Prims Algorithm.

Suboptimality

The MST problem exhibits the optimal substructure property.

Given MST tree for the graph G

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 OPT = \text{set of edges} }

where the number of edges is 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 n - 1} , the weight of edges is minimum, and the tree is spanning.

Consider 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 (x,y)} , where X is a leaf node.

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 - (x, y)} is a MST for the subproblem 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 G - X}

By contradiction, assume 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} is not optimal. There must be 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 B} such 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 w(B) < w(A) }

Adding the edge (x, y) back to both graphs

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 B + (x, y)} is a viable tree.

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(B + w(x,y)) < w(A + w(x,y)) = w(OPT) }

Therefore, OPT is not the optimal solution.

By contradiction, 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.