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,

Slides:



Advertisements
Similar presentations
BackTracking Algorithms
Advertisements

Depth-First Search1 Part-H2 Depth-First Search DB A C E.
Graphs CSC 220 Data Structure. Introduction One of the Most versatile data structures like trees. Terminology –Nodes in trees are vertices in graphs.
1 Graphs Traversals In many graph problems, we need to traverse the vertices of the graph in some order Analogy: Binary tree traversals –Pre-order Traversal.
3.3 Spanning Trees Tucker, Applied Combinatorics, Section 3.3, by Patti Bodkin and Tamsen Hunter.
Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u
Algorithms and Data Structures
TECH Computer Science Graphs and Graph Traversals  // From Tree to Graph  // Many programs can be cast as problems on graph Definitions and Representations.
Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u
Breadth-First and Depth-First Search
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Chapter 8, Part I Graph Algorithms.
Graph Search Methods Spring 2007 CSE, POSTECH. Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u. A search method.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both.
Discrete Structures Lecture 13: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.
Graph Algorithms: Minimum Spanning Tree We are given a weighted, undirected graph G = (V, E), with weight function w:
Spanning Trees.
Spanning Trees. 2 Spanning trees Suppose you have a connected undirected graph Connected: every node is reachable from every other node Undirected: edges.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
Depth-first search COMP171 Fall Graph / Slide 2 Depth-First Search (DFS) * DFS is another popular graph search strategy n Idea is similar to pre-order.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
Module #1 - Logic 1 Based on Rosen, Discrete Mathematics & Its Applications. Prepared by (c) , Michael P. Frank and Modified By Mingwu Chen Trees.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
Spanning Trees. Spanning trees Suppose you have a connected undirected graph –Connected: every node is reachable from every other node –Undirected: edges.
Backtracking.
Depth-First Search Lecture 24 COMP171 Fall Graph / Slide 2 Depth-First Search (DFS) * DFS is another popular graph search strategy n Idea is similar.
1 Minimum Spanning Trees Longin Jan Latecki Temple University based on slides by David Matuszek, UPenn, Rose Hoberman, CMU, Bing Liu, U. of Illinois, Boting.
Minimum Spanning Trees. Subgraph A graph G is a subgraph of graph H if –The vertices of G are a subset of the vertices of H, and –The edges of G are a.
1 Minimum Spanning Trees Longin Jan Latecki Temple University based on slides by David Matuszek, UPenn, Rose Hoberman, CMU, Bing Liu, U. of Illinois, Boting.
Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,
COSC 2007 Data Structures II Chapter 14 Graphs III.
Graph Traversal BFS & DFS. Review of tree traversal methods Pre-order traversal In-order traversal Post-order traversal Level traversal a bc d e f g hi.
Graphs. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
1 Subgraphs A subgraph S of a graph G is a graph such that The vertices of S are a subset of the vertices of G The edges of S are a subset of the edges.
Graph Theory and Applications
Graphs. Graphs Similar to the graphs you’ve known since the 5 th grade: line graphs, bar graphs, etc., but more general. Those mathematical graphs are.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
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,
1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency.
COSC 2007 Data Structures II
Graphs Slide credits:  K. Wayne, Princeton U.  C. E. Leiserson and E. Demaine, MIT  K. Birman, Cornell U.
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Breadth-first and depth-first traversal CS1114
Graph Traversal Text Weiss, § 9.6 Depth-First Search Think Stack Breadth-First Search Think Queue.
Graph Searching CSIT 402 Data Structures II. 2 Graph Searching Methodology Depth-First Search (DFS) Depth-First Search (DFS) ›Searches down one path as.
Backtracking. 2 Suppose you have to make a series of decisions, among various choices, where You don’t have enough information to know what to choose.
Chapter 11. Chapter Summary  Introduction to trees (11.1)  Application of trees (11.2)  Tree traversal (11.3)  Spanning trees (11.4)
Chapter 11. Chapter Summary Introduction to Trees Applications of Trees (not currently included in overheads) Tree Traversal Spanning Trees Minimum Spanning.
CSC317 1 At the same time: Breadth-first search tree: If node v is discovered after u then edge uv is added to the tree. We say that u is a predecessor.
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
Code: BCA302 Data Structures with C Prof. (Dr.) Monalisa Banerjee By.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
Review Graph Directed Graph Undirected Graph Sub-Graph Spanning Sub-Graph Degree of a Vertex Weighted Graph Elementary and Simple Path Link List Representation.
Backtracking.
Discrete Mathematicsq
Trees Chapter 11.
Spanning Trees Longin Jan Latecki Temple University based on slides by
Graphs Chapter 13.
Spanning Trees.
Spanning Trees Longin Jan Latecki Temple University based on slides by
Minimum Spanning Trees
Backtracking.
Backtracking.
Spanning Trees Longin Jan Latecki Temple University based on slides by
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Backtracking.
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 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 A connected, undirected graph Four of the spanning trees of the graph

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 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 graphOne possible result of a BFS starting from top One possible result of a DFS starting from top

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

8 DFS example Start from v 3 v1v1 v4v4 v3v3 v5v5 v2v2 G v3v3 1 v2v2 2 v1v1 3 v4v4 4 v5v5 5 x x x x x

9 Backtracking (animation) start? ? dead end ? ? ? success! dead end

10 Coloring a map You wish to color a map with not more than four colors red, yellow, green, blue Adjacent countries must be in different 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

11 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

12 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

13 Solution for this map Solution found = true map[0] is red map[1] is yellow map[2] is green map[3] is blue map[4] is yellow map[5] is green map[6] is blue map[7] is red

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

15 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

16 BFS example Start from v 5 v5v5 1 v3v3 2 v4v4 3 v2v2 4 v1v1 5 v1v1 v4v4 v3v3 v5v5 v2v2 G x VisitQueue (front to back) v5v5 v5v5 empty v3v3 v3v3 v4v4 v 3, v 4 v4v4 v2v2 v 4, v 2 v2v2 empty v1v1 v1v1 x x x x

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