Chapter 7: Greedy Algorithms - PowerPoint PPT Presentation

1 / 27
About This Presentation
Title:

Chapter 7: Greedy Algorithms

Description:

List of edges in a graph. n the number of vertices. Output: ... EF. G. I. J. Count. 2. i. 3. n. 10. kruskal(e, n) { while (count n-1) ... – PowerPoint PPT presentation

Number of Views:19
Avg rating:3.0/5.0
Slides: 28
Provided by: csSi
Category:

less

Transcript and Presenter's Notes

Title: Chapter 7: Greedy Algorithms


1
Chapter 7 Greedy Algorithms
  • Kruskals, Prims, and Dijkstras Algorithms

2
Kruskals Algorithm
  • Solves the Minimum Spanning Tree Problem
  • Input
  • List of edges in a graph
  • n the number of vertices
  • Output
  • Prints the list of edges in the Minimum Spanning
    Tree

3
4
5
A
B
C
3
6
4
5
6
7
3
4
G
D
E
F
7
6
4
5
3
5
5
J
H
I
4
Kruskals
4
5
A
B
C
3
6
4
5
6
  • kruskal(e, n)
  • sort(e)

7
3
4
D
E
F
G
7
6
4
5
3
5
5
H
J
I
5
Kruskals
  • kruskal(e, n)
  • sort(e)

6
A
B
C
D
E
F
G
H
I
J
  • kruskal(e, n)
  • sort(e)
  • for (i A to J)
  • makeset(i)

7
A
B
C
D
E
F
G
H
I
J
  • kruskal(e, n)
  • ...
  • count 0
  • i 1

Count 0
i 1
8
  • kruskal(e, n)
  • while (count lt n-1)
  • if (findset(ei.v) ! findset(ei.w))
  • print(ei.v ei.w)
  • count
  • union(ei.v, ei.w)
  • i

A
B
C
D
E
F
G
H
I
J
count 0
n 10
i 1
9
  • kruskal(e, n)
  • while (count lt n-1)
  • if (findset(ei.v) ! findset(ei.w))
  • print(ei.v ei.w)
  • count
  • union(ei.v, ei.w)
  • i

A
B
C
DH
E
F
G
I
J
Count 1
n 10
i 2
10
  • kruskal(e, n)
  • while (count lt n-1)
  • if (findset(ei.v) ! findset(ei.w))
  • print(ei.v ei.w)
  • count
  • union(ei.v, ei.w)
  • i

A
B
C
DH
EF
G
I
J
Count 2
n 10
i 3
11
  • kruskal(e, n)
  • while (count lt n-1)
  • if (findset(ei.v) ! findset(ei.w))
  • print(ei.v ei.w)
  • count
  • union(ei.v, ei.w)
  • i

ADH
B
C
EF
G
I
J
Count 3
n 10
i 4
12
  • kruskal(e, n)
  • while (count lt n-1)
  • if (findset(ei.v) ! findset(ei.w))
  • print(ei.v ei.w)
  • count
  • union(ei.v, ei.w)
  • i

ADH
B
C
EFG
I
J
Count 4
n 10
i 5
13
  • kruskal(e, n)
  • while (count lt n-1)
  • if (findset(ei.v) ! findset(ei.w))
  • print(ei.v ei.w)
  • count
  • union(ei.v, ei.w)
  • i

ADHB
C
EFG
I
J
Count 5
n 10
i 6
14
  • kruskal(e, n)
  • while (count lt n-1)
  • if (findset(ei.v) ! findset(ei.w))
  • print(ei.v ei.w)
  • count
  • union(ei.v, ei.w)
  • i

ADHB
CEFG
I
J
Count 6
n 10
i 7
15
  • kruskal(e, n)
  • while (count lt n-1)
  • if (findset(ei.v) ! findset(ei.w))
  • print(ei.v ei.w)
  • count
  • union(ei.v, ei.w)
  • i

ADHB
CEFGJ
I
Count 7
n 10
i 8
16
  • kruskal(e, n)
  • while (count lt n-1)
  • if (findset(ei.v) ! findset(ei.w))
  • print(ei.v ei.w)
  • count
  • union(ei.v, ei.w)
  • i

ADHBCEFGJ
I
Count 8
n 10
i 9
17
  • kruskal(e, n)
  • while (count lt n-1)
  • if (findset(ei.v) ! findset(ei.w))
  • print(ei.v ei.w)
  • count
  • union(ei.v, ei.w)
  • i

ADHBCEFGJ
I
Count 8
n 10
i 10
18
  • kruskal(e, n)
  • while (count lt n-1)
  • if (findset(ei.v) ! findset(ei.w))
  • print(ei.v ei.w)
  • count
  • union(ei.v, ei.w)
  • i

ADHBCEFGJI
Count 9
n 10
i 11
19
4
5
A
B
C

3
6
4
5
6
7
3
4
D
E
F
G
7
6
4
5
3
5
5
H
J
I
20
4
5
A
B
C

3
6
4
5
6
7
3
4
D
E
F
G
7
6
4
5
3
5
5
H
J
I
21
4
5
A
B
C

A
3
6
4
5
6
7
3
4
D
E
F
G
B
D
E
7
6
4
5
3
5
5
H
J
I
H
F
C
G
J
I
22
Theorem 7.2.5 pp. 280
  • Let G be a connected, weighted graph, and let G
    be a sub-graph of a minimal spanning tree of G.
    Let C be a component of G, and let S be the set
    of all Edges with one vertex in C and the other
    not in C. If we add a minimum weight edge in S to
    G, the resulting graph is also contained in a
    minimal spanning tree of G

23
Theorem 7.2.5 pp. 280
4
5
A
B
C

G
Minimal Spanning Tree of G
3
6
4
5
6
A
7
3
4
D
E
F
G
7
6
4
5
B
D
E
3
5
5
H
J
I
H
F
  • Let G be a connected, weighted graph, and let G
    be a sub-graph of a minimal spanning tree of G.
    Let C be a component of G, and let S be the set
    of all Edges with one vertex in C and the other
    not in C. If we add a minimum weight edge in S to
    G, the resulting graph is also contained in a
    minimal spanning tree of G

C
G
J
I
24
Theorem 7.2.5 pp. 280
4
5
G Subset of Minimal Spanning Tree of G
A
B
C

G
3
6
4
5
6
7
3
4
D
E
F
G
A
C
7
6
4
5
3
5
5
H
J
I
D
E
  • G be a sub-graph of a minimal spanning tree of
    G. Let C be a component of G, and let S be the
    set of all Edges with one vertex in C and the
    other not in C.

S
25
Theorem 7.2.5 pp. 280
4
5
G Subset of Minimal Spanning Tree of G
A
B
C

G
3
6
4
5
6
7
3
4
D
E
F
G
A
C
7
6
4
5
3
5
5
H
J
I
D
E
  • If we add a minimum weight edge in S to G, the
    resulting graph is also contained in a minimal
    spanning tree of G

S
26
Theorem 7.2.6 Kruskals Algorithm finds minimum
spanning tree
  • Proof by induction
  • G is a sub-graph constructed by Kruskals
    Algorithm
  • G is initially empty but each step of the
    Algorithm increases the size of G
  • Inductive Assumption G is contained in the MST.

27
Theorem 7.2.6 Kruskals Algorithm finds minimum
spanning tree
  • Proof by induction
  • Let (v,w) be the next edge selected by Kruskals
    Algorithm
  • Kruskals algorithm finds the minimum weight edge
    (v,w) such that v and w are not already in G
  • C can be any subset of the MST, so you can always
    construct a C such that v is in C and w is not.
  • Therefore, by Theorem 7.2.5, when (v,w) is added
    to G, the resulting graph is also contained in
    the MST.
Write a Comment
User Comments (0)
About PowerShow.com