Chapter 18: Graph Applications

Review:

V= nodes, E= edges

Graph G=(V,E)

Subgraph G'=(V',E'): V' V, E' E (note: graphs can be disconnected)

If G' contains no cycles then G'=T is a tree.

T= (V',E') is a spanning tree of G if V'= V

The right two are spanning trees of the graph on the left:

Minimum Cost Spanning Trees

The cost of a spanning tree is the sum of the costs (weights) of the edges in the spanning tree.

A minimum-cost spanning tree is a spanning tree of least cost.

Cost on edges C: E -> R+

(i.e., is a function from the edges to the real numbers)

Spanning tree of minimum cost

Application: circuit equations, broadcasting

n nodes spanning tree must contain n-1 edges

Greedy Strategy

Methods we will be considering both use the greedy design strategy

At each stage include edge of minimum cost which does not introduce a cycle.

Edges included so far form a forest (Kruskal) (page 726)

Edges included so far form a tree (Prim) (page 731)

Kruskal

Kruskal's method first sorts edges by cost

(1,2)-- 10

(3,6)-- 15

(4,6)-- 20

(2,6)-- 25

(1,4)-- 30

(3,5)-- 35 ...

Before
After
Step 1
Step 2
Step 3
Step 4

See also Figure 18.11

Use Union and Find to convert a forest of n isolated nodes to a spanning tree

note: link between nodes in Union-Find data structure not necessarily same as edges in spanning tree. We want no cycles in this construction.

Time: (page 730)

e= |E|, n= |V|

Sorting: eloge
Text notes that "not essential to sort...so long as the next edge can be determined easily" - suggest a heap (O(log e) for next to consider and O(e) to build the heap.)

The rest of the algorithm then takes at most 2e Finds and (n-1) Unions. ( time)
so this part of the algorithm is sightly more than 0(n + e)

Adding up all parts get O(n + eloge). If each node has even one edge then e > n. Hence bounded mostly by the sort -- O(eloge)

Theorem: Procedure Kruskal produces a minimum cost spanning tree.

Proof: (page 729 also) Uses transformation and contradiction

Prim

Edges included so far form a tree

Before
After

See also Figure 18.14

Also in text at Figure 18.13 and discussed on page 731

Time

O(n2)

Outer loop n-1 (basically n to get all vertices) times - inside loop ("over all edges") O(n) time)

but ways to implement to get 0(n2)

Prim 0(n2)

Again, note that if # of e is close to n2 -- almost complete graph

Kruskal O(e log e)

e<< n2 or e ~= n - a sparse graph

note: if a lot of edges Kruscal's looks like n2 log n2

This, or even, n2 log n is worse than just n2

If # edges << n2 (i.e. sparse)

use Kruskal 0(e log e)

otherwise use Prim's 0(n2)

Use Figure 18.17 to try things.

And check out java demos for Prim and Kruscal
(A (6,10) graph means 6 vertices and 10 edges)