1 / 18

Spanning Trees

A spanning tree is a connected subgraph of a graph with no cycles. This article explores different algorithms for finding spanning trees and graph traversal using depth-first search (DFS) and breadth-first search (BFS).

paulac
Download Presentation

Spanning Trees

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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

  2. A connected,undirected graph Four of the spanning trees of the graph 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 with the same nodes in which there are no cycles

  3. Theorem • A simple graph is connected iff it has a spanning tree. Proof. We assume that a graph G is connected. If G has a simple circuit, we remove one edge from it. We repeat this step, until there is no more circuits in G. Let T be the obtained subgraph. T has the same nodes as G, since we did not remove any node. T has no circuits, since we removed all circuits. T is connected, since in every circuit there was an alternative path. The proof in the opposite direction is very simple. Since the presented algorithm to construct a spanning tree is very inefficient, we need a better one.

  4. One possible result of a BFSstarting from top An undirected graph One possible result of a DFSstarting from top 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

  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.

  6. Two basic traversal algorithms • Two basic graph traversal algorithms: • Depth-first-search (DFS), also called backtracking • After visiting node v, DFS 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 visits every node adjacent to v before visiting any other nodes

  7. Backtracking (animation) dead end ? dead end dead end ? start ? ? dead end dead end ? success!

  8. The backtracking algorithm • Backtracking is really quite simple--we “explore” each node, as follows: • To “explore” node N: • 1. If N is a goal node, return “success” • 2. If N is a leaf node, return “failure” • 3.For each child C of N, • 3.1. Explore C • 3.1.1. If C was successful, return “success” • 4.Return “failure”

  9. 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 v1 visit(v1) mark v1 as visited; procedure visit(v: vertex of G) for each node w adjacent to v not yet in T begin mark w as visited; add vertex w and edge {v, w} to T visit(w); end

  10. DFS example • Start from v3 1 v3 2 v2 v2 v3 v1 x x x 4 3 v1 x v4 x v5 v4 5 v5 G

  11. Coloring a map • You wish to color a map withnot more than four colors • red, yellow, green, blue • Adjacent countries must be indifferent colors • You don’t have enough information to choose colors • Each choice leads to another set of choices • One or more sequences of choices may (or may not) lead to a solution • Many coloring problems can be solved with backtracking

  12. Full example: Map coloring • The Four Color Theorem states that any map on a plane can be colored with no more than four colors, so that no two countries with a common border are the same color • For most maps, finding a legal coloring is easy • For some maps, it can be fairly difficult to find a legal coloring

  13. Backtacking • We go through all the countries recursively, starting with country zero • At each country we decide a color • It must be different from all adjacent countries • If we cannot find a legal color, we report failure • If we find a color, we use it and recurred with the next country • If we ran out of countries (colored them all), we reported success • When we returned from the topmost call, we were done

  14. Solution for this map • Solution found = truemap[0] is redmap[1] is yellowmap[2] is greenmap[3] is bluemap[4] is yellowmap[5] is greenmap[6] is bluemap[7] is red

  15. 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.

  16. Algorithm for BFS procedure BFS(G: connected graph with vertices v1, v2, …, vn) T:= tree consisting only of vertex v1 L:= empty list put v1 in the list L of unprocessed vertices; while L is not empty begin remove the first vertex v from L for each neighbor w of v if w is not in L and not in T then begin add w to the end of the list L add w and edge {v, w} to T end end

  17. v2 v3 v1 x x 2 3 v3 x v4 x 4 5 v4 v2 v1 v5 G BFS example • Start from v5 1 v5 x

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

More Related