Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
Algorithmen und Datenstrukturen
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Skripte
Algorithmen und Datenstrukturen
Commits
de56055b
Commit
de56055b
authored
7 months ago
by
Wolfgang Mulzer
Browse files
Options
Downloads
Patches
Plain Diff
Kruskal's algorithm.
parent
a508dbda
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
58-mst.tex
+78
-1
78 additions, 1 deletion
58-mst.tex
skript.pdf
+0
-0
0 additions, 0 deletions
skript.pdf
with
78 additions
and
1 deletion
58-mst.tex
+
78
−
1
View file @
de56055b
...
...
@@ -195,4 +195,81 @@ Using the best available results for this, we achieve a running time
if
$
O
(
|V|
\log
|V|
+
|E|
)
$
.
\paragraph
{
Kruskal's algorithm.
}
Kruskal's algorithm uses a more global strategy
for selecting the next safe edge.
for selecting the next safe edge. The idea is to take in each step the
\emph
{
lightest
}
edge in
$
E
$
that crosses a cut that respects the current set
$
A
$
. How does such an
edge look like? In general, the subgraph
$
(
V, A
)
$
is a
\emph
{
forest
}
: a graph on
$
V
$
that does not contain any cycles, but that consists of several connected components
(some of them may be a single vertex). For an edge
$
e
\in
E
\setminus
A
$
, there
is a cut
$
(
S, V
\setminus
S
)
$
that respects
$
A
$
and that is crossed by
$
e
$
if and only
if the endpoints of
$
e
$
lie in different components of the forest
$
(
V, A
)
$
(this cut
must separate the components that contain the endpoints of
$
e
$
, and the other components
can be assigned arbitrarily).
Thus, Kruskal's algorithm works as follows: sort the edges in
$
E
$
by weight, from the lightest
edge to the heaviest edge. Set
$
A
=
\emptyset
$
. For each edge
$
e
$
in this order, check if
$
e
$
connects two different components of the current forest
$
(
V, A
)
$
. If so, add
$
e
$
to
$
A
$
.
Otherwise, do nothing. Continue, until all edges have been considered.
In other words: in each step, Kruskal's algorithm adds the lightest edge to
$
A
$
that adds
a new connection.
The pseudocode is as follows:
\begin{verbatim}
A <-
{}
Sort e by weight
for each edge e in sorted order do
if e connects two different components of (V, A) then
A <- A +
{
e
}
return A
\end{verbatim}
We still need to explain how to check whether an edge
$
e
$
connects
two different components of
$
(
V, A
)
$
. For this, we need a way
to track the connected components of
$
(
V, A
)
$
, as edges
are added to
$
A
$
. More precisely, we need to support the
following process: initially, we have
$
A
=
\emptyset
$
, and
each component of
$
(
V, A
)
$
consists of a single vertex.
Then, in each round, we are given an edge
$
\{
v, w
\}
$
in
$
E
$
,
and we need to
\emph
{
find
}
the components of
$
(
V, A
)
$
that contain
the endpoints
$
v
$
and
$
w
$
. If these two components are distinct,
we need to construct the
\emph
{
union
}
of the two connected components
for
$
v
$
and
$
w
$
.
This is called the
\emph
{
union-find-problem
}
. The abstract problem is as follows:
we are given a set
$
\mathcal
{
U
}
=
\{
1
,
\dots
, n
\}
$
with
$
n
$
elements, and our task is to
maintain a
\emph
{
partition
}
$
\mathcal
{
P
}
=
\{
U
_
1
, U
_
2
,
\dots
, U
_
k
\}
$
of
$
U
$
under the following
operations
\footnote
{
Recall that a partition of
$
U
$
is a set
$
\mathcal
{
P
}
=
\{
U
_
1
, U
_
2
,
\dots
, U
_
k
\}
$
of nonempty, pairwise disjoint subsets of
$
U
$
such that every element of
$
U
$
occurs
in exactly one set.
Formally, we have (i)
$
U
_
i
\subseteq
U
$
and
$
U
_
i
\neq
\emptyset
$
,
for
$
i
=
1
,
\dots
, k
$
; (ii)
$
U
_
i
\neq
U
_
j
$
, for
$
i, j
=
1
,
\dots
, k
$
,
$
i
\neq
j
$
;
and (iii)
$
\bigcup
_{
i
=
1
}^
k U
_
i
=
U
$
.
}
:
(i)
\texttt
{
initialize
}$
(
U
)
$
: create a new partition
$
\mathcal
{
P
}
=
\{\{
1
\}
,
\{
2
\}
,
\dots
,
\{
n
\}\}
$
in which every set consists of a single element; (ii)
\texttt
{
find
}$
(
a
)
$
, for
$
u
\in
\mathcal
{
U
}$
: return a
\emph
{
representative element
}
$
v
\in
U
_
i
$
, where
$
U
_
i
$
is the set in the current partition
$
\mathcal
{
P
}$
that contains
$
u
$
. Crucially, we require
that for all
$
u
\in
U
_
i
$
, the function
\texttt
{
find
}$
(
a
)
$
returns
\emph
{
the same
}
representative
$
v
\in
U
_
i
$
(note that it also follows that
for
$
u
_
1
, u
_
2
\in
U
$
that lie in
\emph
{
different
}
sets of the current partition,
\texttt
{
find
}$
(
u
_
1
)
$
and
\texttt
{
find
}$
(
u
_
2
)
$
return
\emph
{
different
}
representatives);
and (iii)
\texttt
{
union
}$
(
v
_
1
, v
_
2
)
$
, where
$
v
_
1
\in
U
$
is the representative element for a
set
$
U
_
i
$
in the current partition and
$
v
_
2
$
is the representative element for a
different set
$
U
_
j
$
in the current partition: replace the sets
$
U
_
i, U
_
j
$
in the
current partition
$
\mathcal
{
P
}$
by their union
$
U
_
i
\cup
U
_
j
$
. That is,
the new partition after
\texttt
{
union
}$
(
v
_
1
, v
_
2
)
$
is
$
\left
(
\mathcal
{
P
}
\setminus
\{
U
_
i, U
_
j
\}\right
)
\cup
\{
U
_
i
\cup
U
_
j
\}
$
.
Given a data structure that supports the operations of the union-find problem,
we can now provide a more detailed pseudo-code for Kruskal's algorithm:
\begin{verbatim}
A <-
{}
initialize(V)
Sort e by weight
for each edge e =
{
v, w
}
in sorted order do
a = find(v)
b = find(w)
if a != b then
A <- A +
{
e
}
union(a, b)
return A
\end{verbatim}
This diff is collapsed.
Click to expand it.
skript.pdf
+
0
−
0
View file @
de56055b
No preview for this file type
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment