









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An explanation of prim's and kruskal's algorithms for finding the minimum spanning tree (mst) of a graph. The pseudocode for both algorithms, as well as explanations of how they work. Prim's algorithm grows a single tree by repeatedly adding the least cost edge that connects a vertex in the existing tree to a vertex not in the existing tree. Kruskal's algorithm grows a tree by repeatedly adding the least cost edge that does not introduce a cycle among the edges included so far. Both algorithms are important in graph theory and have applications in various fields, including computer networks and transportation networks.
Typology: Study notes
1 / 15
This page cannot be seen from the preview
Don't miss anything!










1
http://www.cse.unl.edu/~goddard/Courses/CSCE310J
2
Giving credit where credit is due:
3
» This is what you did before you learned to not to think ahead and plan
» Each individual choice is best according to some limited “short- term” criterion, that is not too expensive to evaluate » Once a choice is made, it cannot be undone! Even if it becomes evident later that it was a poor choice Sometimes life is like that
» Incurs the minimum short-term cost, » With the hope that a lot of small short-term costs add up to small overall cost. 4
» The greedy choice property : an optimal solution can be obtained by making the greedy choice at each step. » Optimal substructures : optimal solutions contain optimal sub- solutions. » Many optimal solutions, but we only need one such solution.
» Hence, greedy algorithms are often more efficient than dynamic programming.
» Actions with a small short-term cost may lead to a situation, where further large costs are unavoidable.
5
6
7
maximize such that
where 0 ≤ fi ≤ 1
n
i
1
n
i
i i ≤ = 1
8
That is, p 1 =v 1 /w 1 ≥ p 2 =v 2 /w 2 ≥ ... ≥ pn =vn/wn
9
fractionalKnapsack(V, W, capacity, n, KnapSack) { sortByDescendingProfit(V,W,n) KnapSack = 0; capacityLeft = C; for (i = 1; (i <= n) && (capacityLeft > 0); ++i) { if (W[i] < capacityLeft) KnapSack[i] = 1; capacityLeft -= W[i]; else KnapSack[i] = capacityLeft/W[i]; capacityLeft = 0; } } } What is the complexity of this algorithm? 10
pi 3 1.33 1 0.5 8 2 1.2.
wi 4 3 5 6 1 4 10 4
vi 12 4 5 3 8 8 12 1
i 1 2 3 4 5 6 7 8
pi 8 3 2 1.33 1.2 1 0.5.
wi 1 4 4 3 10 5 6 4
vi 8 12 8 4 12 5 3 1
i 6 1 6 2 7 3 4 8
11
pi 8 3 2 1.33 1.2 1 0.5.
wi 1 4 4 3 3 /10 5 6 4
vi 8 12 8 4 12 5 3 1
i 6 1 6 2 7 3 4 8
12
19
20
21
» Grow a single tree by repeatedly adding the least cost edge that connects a vertex in the existing tree to a vertex not in the existing tree Intermediary solution is a subtree
» Grow a tree by repeatedly adding the least cost edge that does not introduce a cycle among the edges included so far Intermediary solution is a spanning forest
22
MST-Prim(G, w, r) Q = V[G]; for each u ∈∈∈∈ Q key[u] = ∞∞∞∞; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v ∈∈∈∈ Adj[ u ] if (v ∈∈∈∈ Q and w( u,v ) < key[ v ]) p[v] = u; key[v] = w(u,v);
23
MST-Prim(G, w, r) Q = V[G]; for each u ∈∈∈∈ Q key[u] = ∞∞∞∞; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v ∈∈∈∈ Adj[ u ] if (v ∈∈∈∈ Q and w( u,v ) < key[ v ]) p[v] = u; key[v] = w(u,v);
14 10
3
6 4 5
2
9
15
8 Run on example graph
24
MST-Prim(G, w, r) Q = V[G]; for each u ∈∈∈∈ Q key[u] = ∞∞∞∞; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v ∈∈∈∈ Adj[ u ] if (v ∈∈∈∈ Q and w( u,v ) < key[ v ]) p[v] = u; key[v] = w(u,v);
14 10
3
6 4 5
2
9
15
8 Run on example graph
25
MST-Prim(G, w, r) Q = V[G]; for each u ∈∈∈∈ Q key[u] = ∞∞∞∞; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v ∈∈∈∈ Adj[ u ] if (v ∈∈∈∈ Q and w( u,v ) < key[ v ]) p[v] = u; key[v] = w(u,v);
(^14 )
3
6 4 5
2
9
15
8 Pick a start vertex r
r
26
MST-Prim(G, w, r) Q = V[G]; for each u ∈∈∈∈ Q key[u] = ∞∞∞∞; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v ∈∈∈∈ Adj[ u ] if (v ∈∈∈∈ Q and w( u,v ) < key[ v ]) p[v] = u; key[v] = w(u,v);
(^14 )
3
6 4 5
2
9
15
8 Red vertices have been removed from Q
u
27
MST-Prim(G, w, r) Q = V[G]; for each u ∈∈∈∈ Q key[u] = ∞∞∞∞; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v ∈∈∈∈ Adj[ u ] if (v ∈∈∈∈ Q and w( u,v ) < key[ v ]) p[v] = u; key[v] = w(u,v);
(^14 )
3
6 4 5
2
9
15
8 Red arrows indicate parent pointers
u
28
MST-Prim(G, w, r) Q = V[G]; for each u ∈∈∈∈ Q key[u] = ∞∞∞∞; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v ∈∈∈∈ Adj[ u ] if (v ∈∈∈∈ Q and w( u,v ) < key[ v ]) p[v] = u; key[v] = w(u,v);
(^14 )
3
6 4 5
2
9
15
8
u
29
MST-Prim(G, w, r) Q = V[G]; for each u ∈∈∈∈ Q key[u] = ∞∞∞∞; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v ∈∈∈∈ Adj[ u ] if (v ∈∈∈∈ Q and w( u,v ) < key[ v ]) p[v] = u; key[v] = w(u,v);
14 10
3
6 4 5
2
9
15
8 u
30
MST-Prim(G, w, r) Q = V[G]; for each u ∈∈∈∈ Q key[u] = ∞∞∞∞; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v ∈∈∈∈ Adj[ u ] if (v ∈∈∈∈ Q and w( u,v ) < key[ v ]) p[v] = u; key[v] = w(u,v);
14 10
3
6 4 5
2
9
15
8 u
37
MST-Prim(G, w, r) Q = V[G]; for each u ∈∈∈∈ Q key[u] = ∞∞∞∞; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v ∈∈∈∈ Adj[ u ] if (v ∈∈∈∈ Q and w( u,v ) < key[ v ]) p[v] = u; key[v] = w(u,v);
(^14 )
3
6 4 5
2
9
15
8
u
38
MST-Prim(G, w, r) Q = V[G]; for each u ∈∈∈∈ Q key[u] = ∞∞∞∞; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v ∈∈∈∈ Adj[ u ] if (v ∈∈∈∈ Q and w( u,v ) < key[ v ]) p[v] = u; key[v] = w(u,v);
(^14 )
3
6 4 5
2
9
15
8
u
39
MST-Prim(G, w, r) Q = V[G]; for each u ∈∈∈∈ Q key[u] = ∞∞∞∞; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v ∈∈∈∈ Adj[ u ] if (v ∈∈∈∈ Q and w( u,v ) < key[ v ]) p[v] = u; key[v] = w(u,v);
(^14 )
3
6 4 5
2
9
15
8
u
40
MST-Prim(G, w, r) Q = V[G]; for each u ∈∈∈∈ Q key[u] = ∞∞∞∞; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v ∈∈∈∈ Adj[ u ] if (v ∈∈∈∈ Q and w( u,v ) < key[ v ]) p[v] = u; key[v] = w(u,v);
(^14 )
3
6 4 5
2
9
15
8
u
41
MST-Prim(G, w, r) Q = V[G]; for each u ∈∈∈∈ Q key[u] = ∞∞∞∞; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v ∈∈∈∈ Adj[ u ] if (v ∈∈∈∈ Q and w( u,v ) < key[ v ]) p[v] = u; key[v] = w(u,v);
14 10
3
6 4 5
2
9
15
8
u
42
MST-Prim(G, w, r) Q = V[G]; for each u ∈∈∈∈ Q key[u] = ∞∞∞∞; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v ∈∈∈∈ Adj[ u ] if (v ∈∈∈∈ Q and w( u,v ) < key[ v ]) p[v] = u; key[v] = w(u,v);
14 10
3
6 4 5
2
9
15
8
u
43
MST-Prim(G, w, r) Q = V[G]; for each u ∈∈∈∈ Q key[u] = ∞∞∞∞; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v ∈∈∈∈ Adj[ u ] if (v ∈∈∈∈ Q and w( u,v ) < key[ v ]) p[v] = u; key[v] = w(u,v);
44
MST-Prim(G, w, r) Q = V[G]; for each u ∈∈∈∈ Q key[u] = ∞∞∞∞; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v ∈∈∈∈ Adj[ u ] if (v ∈∈∈∈ Q and w( u,v ) < key[ v ]) p[v] = u; DecreaseKey(v, w(u,v));
45
MST-Prim(G, w, r) Q = V[G]; for each u ∈∈∈∈ Q key[u] = ∞∞∞∞; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v ∈∈∈∈ Adj[ u ] if (v ∈∈∈∈ Q and w( u,v ) < key[ v ]) p[v] = u; DecreaseKey(v, w(u,v));
46
MST-Prim(G, w, r) Q = V[G]; for each u ∈∈∈∈ Q key[u] = ∞∞∞∞; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v ∈∈∈∈ Adj[ u ] if (v ∈∈∈∈ Q and w( u,v ) < key[ v ]) p[v] = u; key[v] = w(u,v);
47
Kruskal() { T = ∅∅∅∅; for each v ∈ V MakeSet(v); sort E by increasing edge weight w for each (u,v) ∈∈∈∈ E (in sorted order) if FindSet(u) ≠≠≠≠ FindSet(v) T = T {{u,v}}; Union(FindSet(u), FindSet(v)); }
48
Kruskal() { T = ∅∅∅∅; for each v ∈ V MakeSet(v); sort E by increasing edge weight w for each (u,v) ∈∈∈∈ E (in sorted order) if FindSet(u) ≠≠≠≠ FindSet(v) T = T {{u,v}}; Union(FindSet(u), FindSet(v)); }
55
Kruskal() { T = ∅∅∅∅; for each v ∈ V MakeSet(v); sort E by increasing edge weight w for each (u,v) ∈∈∈∈ E (in sorted order) if FindSet(u) ≠≠≠≠ FindSet(v) T = T {{u,v}}; Union(FindSet(u), FindSet(v)); }
56
Kruskal() { T = ∅∅∅∅; for each v ∈ V MakeSet(v); sort E by increasing edge weight w for each (u,v) ∈∈∈∈ E (in sorted order) if FindSet(u) ≠≠≠≠ FindSet(v) T = T {{u,v}}; Union(FindSet(u), FindSet(v)); }
57
Kruskal() { T = ∅∅∅∅; for each v ∈ V MakeSet(v); sort E by increasing edge weight w for each (u,v) ∈∈∈∈ E (in sorted order) if FindSet(u) ≠≠≠≠ FindSet(v) T = T