Skip to content

Commit 43293a9

Browse files
committed
Kruskal polishing mostly
1 parent f70afb6 commit 43293a9

File tree

5 files changed

+32
-24
lines changed

5 files changed

+32
-24
lines changed

src/algorithms/controllers/kruskal.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// Prim's MST algorithm; code copied+modified from Dijkstra's shortest
2-
// path algorithm animation (might some junk code left over)
1+
// Kruskal's MST algorithm; code copied+modified from Prim's?
2+
// (might some junk code left over)
33
import GraphTracer from '../../components/DataStructures/Graph/GraphTracer';
44
import Array2DTracer from '../../components/DataStructures/Array/Array2DTracer';
55
import {colors} from './graphSearchColours';
@@ -134,8 +134,8 @@ export default {
134134
}
135135
edges.sort((a, b) => a.weight - b.weight);
136136

137-
nodes.push('i'); // initialize the display
138-
findD.push('find(i)');
137+
nodes.push('n'); // initialize the display
138+
findD.push('find(n)');
139139
selectedD.push('Selected');
140140
costD.push('Cost');
141141
edgesD.push('Edges');

src/algorithms/explanations/KRUSKALExp.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ it is ignored, otherwise it is selected to combine the two trees into one.
1818

1919
The forest representation is designed so the checking and combining is
2020
an instance of the "union-find" problem, for which there are efficient
21-
algorithms (we don't give details in this animation but present them
22-
elsewhere). As well as the set of selected edges (which is returned
23-
at the end), there is a set of sets of nodes that are connected by
24-
selected edges (the inner sets are the sets of nodes in each tree of
25-
the forest). Selecting a new edge combines two trees and this is a
26-
union operation. Checking if two nodes are in the same tree can be done
27-
by using the find operation for each node, returning a representative
28-
node for each tree, and seeing if they are the same.
21+
algorithms (we don't give details in this animation but present them as a
22+
separate animation). Each tree is represented as a set of nodes (thus,
23+
the forest is represented by a set of sets). Initially, the inner sets
24+
are singletons, one for each node in the graph. Checking if two nodes
25+
are in the same tree is done by calling the *find* operation for each
26+
of the two nodes, returning a representative node for each tree, and
27+
seeing if they are the same. Adding a new edge, connecting two trees
28+
in the forest, is done with a *union* operation on the two sets.
2929

3030
Here we number all nodes for simplicity so we can use arrays for the graph
3131
representation, sets of integers for union-find, etc. For consistency

src/algorithms/extra-info/KRUSKALInfo.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,7 @@ use the existing code to find the second one by re-numbering the graph nodes?
3737
For Graph1 with the other weight options, can you convince yourself that
3838
there is a single minimum spanning tree?
3939

40-
40+
The animation shows a sorted list of edges, used to select edges in
41+
order of increasing weight/cost. What is the disadvantage of using a
42+
sorted list? What are some alternatives that could be used and how do
43+
they compare?

src/algorithms/pseudocode/HashingInsertion.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,9 @@ const main = `
152152
InsertAll
153153
For each key k1 in OldT \\B 31
154154
\\In{
155-
HashInsert(T, k1) // recursive! \\B 32
156-
\\Expl{ There will never be multiple levels of
155+
HashInsert(T, k1) \\B 32
156+
\\Expl{ Note: this call is recursive but there will never
157+
be multiple levels of
157158
recursion because the new table will be large enough
158159
to easily accomodate all the keys that were in T.
159160
We don't animate the details of these insertions.

src/algorithms/pseudocode/kruskal.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ Kruskal(G) // Compute minimum spanning tree for graph G \\B Kruskal(G)
3030
\\Expl}
3131
\\Note{highlight edge + trees for n1, n2?
3232
\\Note}
33-
if n1 and n2 are in different trees \\Ref DifferentTrees
33+
if n1 and n2 are in different trees/sets in NodeSets \\Ref DifferentTrees
3434
\\In{
3535
Add e to Selected \\B addSelected
3636
\\Expl{ The counter used to keep track of size(Selected)
3737
can be incremented here.
3838
\\Expl}
39-
union(NodeSets, n1, n2) // update NodeSets, combining n1&n2 \\B union
39+
union(n1, n2) // update NodeSets, combining n1&n2 \\B union
4040
\\Expl{ This is a union-find operation that takes the union
4141
of the sets containing n1 and n2, respectively, since
4242
they are now connected by a selected edge.
@@ -68,22 +68,26 @@ Init
6868
from the while loop significantly earlier in some cases.
6969
\\Expl}
7070
NodeSets <- set of singleton sets with each node in G \\B initNodeSets
71-
\\Expl{ NodeSets is a set of sets of nodes that are connected by
72-
selected edges (like a forest but without information about
73-
which edges are used to connect the nodes of each tree).
74-
Initially there are no selected edges so we have singleton sets.
71+
\\Expl{ NodeSets represents the structure of the forest: which nodes
72+
are connected to each other (but without information about which
73+
edges are used). Each tree is represented as a just set of nodes;
74+
the forest is a set of sets. A "union find" data structure is
75+
used for efficiency. All nodes in the same set/tree return the
76+
same result for the find() function. Initially there are no
77+
selected edges so each set/tree contains a single node and
78+
find() thus returns a different value for each node.
7579
\\Expl}
7680
\\Code}
7781
7882
\\Code{
7983
DifferentTrees
8084
\\Note{highlight find(n1), find(n2)?
8185
\\Note}
82-
if find(NodeSets, n1) != find(NodeSets, n2) \\B DifferentTrees
86+
if find(n1) != find(n2) \\B DifferentTrees
8387
\\Expl{ Find is a union-find operation that returns a representative
84-
element of a set containing a given element. If the elements
88+
element of a set in NodeSets containing a given element. If the elements
8589
returned for n1 and n2 are not equal, it means they are not
86-
in the same set, so they are not connected by selected edges.
90+
in the same set/tree, so they are not connected by selected edges.
8791
\\Expl}
8892
\\Code}
8993

0 commit comments

Comments
 (0)