Spanning Trees

12
1 Spanning Trees Longin Jan Latecki Temple University based on slides by David Matuszek, UPenn, Rose Hoberman, CMU, Bing Liu, U. of Illinois, Boting Yang, U. of Regina

description

Spanning Trees. Longin Jan Latecki Temple University based on slides by David Matuszek, UPenn, Rose Hoberman, CMU, Bing Liu, U. of Illinois, Boting Yang, U. of Regina. A connected, undirected graph. Four of the spanning trees of the graph. Spanning trees. - PowerPoint PPT Presentation

Transcript of Spanning Trees

Page 1: Spanning Trees

1

Spanning Trees

Longin Jan Latecki

Temple University

based on slides by

David Matuszek, UPenn,

Rose Hoberman, CMU,

Bing Liu, U. of Illinois,

Boting Yang, U. of Regina

Page 2: Spanning Trees

2

Spanning trees

Suppose you have a connected undirected graph Connected: every node is reachable from every other node Undirected: edges do not have an associated direction

...then a spanning tree of the graph is a connected subgraph in which there are no cycles

A connected,undirected graph

Four of the spanning trees of the graph

Page 3: Spanning Trees

3

Theorem

A simple graph is connected iff it has a spanning tree.

Page 4: Spanning Trees

4

Finding a spanning tree To find a spanning tree of a graph,

pick an initial node and call it part of the spanning tree do a search from the initial node:

each time you find a node that is not in the spanning tree, add to the spanning tree both the new node and the edge you followed to get to it

An undirected graph One possible result of a BFSstarting from top

One possible result of a DFSstarting from top

Page 5: Spanning Trees

5

Graph Traversal Algorithm

To traverse a tree, we use tree traversal algorithms like pre-order, in-order, and post-order to visit all the nodes in a tree

Similarly, graph traversal algorithm tries to visit all the nodes it can reach.

If a graph is disconnected, a graph traversal that begins at a node v will visit only a subset of nodes, that is, the connected component containing v.

Page 6: Spanning Trees

6

Two basic traversal algorithms

Two basic graph traversal algorithms: Depth-first-search (DFS)

After visit node v, DFS strategy proceeds along a path from v as deeply into the graph as possible before backing up

Breadth-first-search (BFS) After visit node v, BFS strategy visits every node adjacent to v before

visiting any other nodes

Page 7: Spanning Trees

7

Depth-first search (DFS)

DFS strategy looks similar to pre-order. From a given node v, it first visits itself. Then, recursively visit its unvisited neighbors one by one.

DFS can be defined recursively as follows.

procedure DSF(G: connected graph with vertices v1, v2, …, vn)T:=tree consisting only of the vertex v1visit(v1)mark v1 as visited;procedure visit(v: vertex of G)for each node w adjacent to v not yet in Tbegin

add vertex w and edge {v, w} to Tvisit(w);mark w as visited;

end

Page 8: Spanning Trees

8

DFS example

Start from v3

v1

v4

v3

v5

v2

G

v31

v22

v1

3v4

4

v5

5

xxxx x

Page 9: Spanning Trees

9

Breadth-first search (BFS) BFS strategy looks similar to level-order. From a given node v, it

first visits itself. Then, it visits every node adjacent to v before visiting any other nodes.

1. Visit v 2. Visit all v’s neighbors 3. Visit all v’s neighbors’ neighbors …

Similar to level-order, BFS is based on a queue.

Page 10: Spanning Trees

10

Algorithm for BFS

procedure BFS(G: connected graph with vertices v1, v2, …, vn)T:= tree consisting only of vertex v1L:= empty listput v1 in the list L of unprocessed vertices;while L is not empty

beginremove the first vertex v from Lfor each neighbor w of v

if w is not in L and not in T thenbegin

add w to the end of the list Ladd w and edge {v, w} to T

endend

Page 11: Spanning Trees

11

BFS example Start from v5

v5

1

v32

v4

3

v2

4

v1

5

v1

v4

v3

v5

v2

G

x

Visit Queue (front to back)

v5 v5

empty

v3 v3

v4 v3, v4

v4

v2 v4, v2

v2

empty

v1 v1

empty

x

xx x

Page 12: Spanning Trees

12

Backtracking Applications

Use backtracking to find a subset of{31, 27, 15, 11, 7, 5}with sum equal to 39.