Data Structures in Java
Lecture 18: Spanning Trees
11/23/2015 Daniel Bauer
1
Data Structures in Java Lecture 18: Spanning Trees 11/23/2015 - - PowerPoint PPT Presentation
Data Structures in Java Lecture 18: Spanning Trees 11/23/2015 Daniel Bauer 1 A General View of Graph Search Goals: Explore the graph systematically starting at s to Find a vertex t / Find a path from s to t . Find the shortest
Lecture 18: Spanning Trees
11/23/2015 Daniel Bauer
1
v1 v2 v3 v4 v5 v6 v7
Goals:
2
v1 v2 v3 v4 v5 v6 v7
In every step of the search we maintain
(adjacent to the explored graph). Agenda: (v2,v5), (v4,v5), (v4,v7)
3
v1 v2 v3 v4 v5 v6 v7
The graph search algorithms discussed so far differ almost only in the type of agenda they use:
Agenda: (v2,v5), (v4,v5), (v4,v7)
4
minimum path costs (we don’t miss any shorter solutions by choosing the shortest edge greedily).
|S|=1. Trivial. Length shortest path is 0.
s
5
for the subset S, |S| = k.
x s u y v
S
from s to v that does not contain (u,v).
another edge (x,y) leaving S.
than (u,v) because we didn’t choose it before (u,v)
6
BR 1 basement living room kitchen dining room garage
BR2 BR3 Attic
7
BR 1 basement living room kitchen dining room garage
BR2 BR3 Attic 8 2 2 3 1 3 10 10 10 8 5 4 4 4 4 4 4 4 4
8
BR 1 basement living room kitchen dining room garage
BR2 BR3 Attic 8 10 10 10 8 4 4 4 4
Total cost: 62
9
BR 1 basement living room kitchen dining room garage
BR2 BR3 Attic 8 2 1 3 10 8 4 4 4
Total cost: 44
10
BR 1 basement living room kitchen dining room garage
BR2 BR3 Attic 8 2 2 3 1 3 5 4 4
Total cost: 32
11
v1 v2 v3 v4 v5 v6 v7
in the graph. T=(V, ET ⊆ E)
12
v1 v2 v3 v4 v5 v6 v7
T is acyclic. There is a single path between any pair of vertices.
in the graph. T=(V, ET ⊆ E)
13
v1 v2 v3 v4 v5 v6 v7 v1 v2 v3 v4 v5 v6 v7
T is acyclic. There is a single path between any pair of vertices.
in the graph. T=(V, ET ⊆ E)
14
v1 v2 v3 v4 v5 v6 v7 v1 v3 v4 v2 v5 v6 v7
Any node can be the root of the spanning tree.
T is acyclic. There is a single path between any pair of vertices.
in the graph. T=(V, ET ⊆ E)
15
v1 v2 v3 v4 v5 v6 v7 v1 v3 v4 v2 v5 v6 v7
Number of edges in a spanning tree: |V|-1
in the graph. T=(V, ET ⊆ E)
16
smallest amount of wire).
(directed graphs. This is harder).
17
the minimum sum of edge weights.
v1 v2 v3 v4 v5 v6 v7
4 2 10 2 1 3 7 5 8 1 4 6
18
v1 v2 v3 v4 v5 v6 v7
4 2 10 2 1 3 7 5 8 1 4 6
(often there are multiple minimum spanning trees)
the minimum sum of edge weights. Total cost = 16
19
algorithm.
weight of an edge connecting v to other vertices already visited.
from another vertices that have not been seen yet.
expand the vertex with the lowest cost annotation first.
20
v2 v3 v4 v5 v6 v7
2 4 2 1 3 10 7 4 6 1 5 8
Use a Priority Queue q
set v.cost = ∞, set v.visited = false
set s.cost = 0, s.visited = true;
∞ ∞ ∞ ∞ ∞ ∞ v1
21
v2 v3 v4 v5 v6 v7
2 4 2 1 3 10 7 4 6 1 5 8
2 1 4 ∞ ∞ ∞ v1
Use a Priority Queue q
set v.cost = ∞, set v.visited = false
set s.cost = 0, s.visited = true;
22
v2 v3
v4
v5 v6 v7
2 4 2 1 3 10 7 4 6 1 5 8
2 1 2 8 4 7 v1
Use a Priority Queue q
set v.cost = ∞, set v.visited = false
set s.cost = 0, s.visited = true;
23
v2
v3
v4
v5 v6 v7
2 4 2 1 3 10 7 4 6 1 5 8
2 1 2 8 4 7 v1
Use a Priority Queue q
set v.cost = ∞, set v.visited = false
set s.cost = 0, s.visited = true;
24
v2 v3 v4
v5 v6 v7
2 4 2 1 3 10 7 4 6 1 5 8
2 1 2 5 4 7 v1
Use a Priority Queue q
set v.cost = ∞, set v.visited = false
set s.cost = 0, s.visited = true;
25
v2 v3 v4
v5 v6
v7
2 4 2 1 3 10 7 4 6 1 5 8
2 1 2 1 4 6 v1
Use a Priority Queue q
set v.cost = ∞, set v.visited = false
set s.cost = 0, s.visited = true;
26
v2 v3 v4
v5
v6 v7
2 4 2 1 3 10 7 4 6 1 5 8
2 1 2 1 4 6 v1
Use a Priority Queue q
set v.cost = ∞, set v.visited = false
set s.cost = 0, s.visited = true;
27
v2 v3 v4 v5 v6 v7
2 4 2 1 3 10 7 4 6 1 5 8
2 1 2 1 4 6 v1
Use a Priority Queue q
set v.cost = ∞, set v.visited = false
set s.cost = 0, s.visited = true;
28
v2 v3 v4 v5 v6 v7
2 2 1 4 6 1
2 1 2 1 4 6 v1
Use a Priority Queue q
set v.cost = ∞, set v.visited = false
set s.cost = 0, s.visited = true;
Running time: Same as Dijkstra’s Algorithm O(|E| log |V|)
29
v1 v2 v3 v4 v5 v6 v7
4 2 10 2 1 3 7 5 8 1 4 6
same tree it would produce a cycle. Reject it.
30
v1 v2 v3 v4 v5 v6 v7
4 2 10 2 1 3 7 5 8 1 4 6
(v1,v2) 2 (v1,v3) 4 (v1,v4) 1 (v2,v4) 3 (v2,v5) 10 (v3,v4) 2 (v3,v6) 4 (v4,v5) 7 (v4,v6) 8 (v4,v7) 4 Sort edges (or keep them on a heap) (v5,v7) 6 (v6,v7) 1
31
v1 v2 v3 v4 v5 v6 v7
4 2 10 2 1 3 7 5 8 1 4 6
(v1,v2) 2 (v1,v3) 4 (v1,v4) 1 (v2,v4) 3 (v2,v5) 10 (v3,v4) 2 (v3,v6) 4 (v4,v5) 7 (v4,v6) 8 (v4,v7) 4 (v5,v7) 6 (v6,v7) 1
32
(v1,v2) 2 (v1,v3) 4 (v1,v4) 1 (v2,v4) 3 (v2,v5) 10 (v3,v4) 2 (v3,v6) 4 (v4,v5) 7 (v4,v6) 8 (v4,v7) 4 (v5,v7) 6 (v6,v7) 1 OK
v1 v2 v3 v5 v6 v7
4 2 10 2 1 3 7 5 8 1 4 6
v4
33
v1 v2 v4 v5 v6 v7
4 2 10 2 1 3 7 5 8 1 4 6
(v1,v2) 2 (v1,v3) 4 (v1,v4) 1 (v2,v4) 3 (v2,v5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,v2) 2 (v1,v3) 4 (v1,v4) 1 (v2,v4) 3 (v2,v5) 10 (v3,v4) 2 (v3,v6) 4 (v4,v5) 7 (v4,v6) 8 (v4,v7) 4 (v5,v7) 6 (v6,v7) 1 OK OK
v3
OK OK reject reject reject OK
40
v1 v2 v4 v5 v6 v7
4 2 10 2 1 3 7 5 8 1 4 6
(v1,v2) 2 (v1,v3) 4 (v1,v4) 1 (v2,v4) 3 (v2,v5) 10 (v3,v4) 2 (v3,v6) 4 (v4,v5) 7 (v4,v6) 8 (v4,v7) 4 (v5,v7) 6 (v6,v7) 1 OK OK
v3
OK OK reject reject reject OK OK
41
a heap in O(|E|). Each deleteMin takes O(log |E|)
the same set. If not, take the union of the two sets.
structure (Weiss Chapter 8).
Total turns out to be: O(|E| log |V|)
42
(defined over some feature set).
43
bear chicke nn tortoise flea hair 1 feathers 1 eggs 1 1 1 milk 1 airborne 1 aquatic predator 1 toothed 1 backbone 1 1 1 breathes 1 1 1 1 venomou s fins legs 4 2 4 6 tail 1 1 domestic 1
…
https://archive.ics.uci.edu/ml/datasets/Zoo
101 animals represent each data item as a vector
(15 attributes).
44
45
to produce k clusters.
46
to produce k clusters.
47
to produce k clusters.
48
to produce k clusters.
49