CSE 326: Data Structures Lecture #19 More Fun with Graphs Henry Kautz Winter Quarter 2002.

Slides:



Advertisements
Similar presentations
Review: Search problem formulation
Advertisements

IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
Graph Searching (Graph Traversal) Algorithm Design and Analysis Week 8 Bibliography: [CLRS] – chap 22.2 –
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
Algorithm Strategies Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Spanning Trees Lecture 20 CS2110 – Spring
Review: Search problem formulation
CSE 326: Data Structures Lecture #21 Graphs II.5 Alon Halevy Spring Quarter 2001.
Chapter 9: Greedy Algorithms The Design and Analysis of Algorithms.
1 CSE 326: Data Structures: Graphs Lecture 19: Monday, Feb 24, 2003.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
Graphs. Graphs Many interesting situations can be modeled by a graph. Many interesting situations can be modeled by a graph. Ex. Mass transportation system,
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
Minimal Spanning Trees. Spanning Tree Assume you have an undirected graph G = (V,E) Spanning tree of graph G is tree T = (V,E T E, R) –Tree has same set.
Blind Search-Part 2 Ref: Chapter 2. Search Trees The search for a solution can be described by a tree - each node represents one state. The path from.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Review of Graphs A graph is composed of edges E and vertices V that link the nodes together. A graph G is often denoted G=(V,E) where V is the set of vertices.
CSE 326: Data Structures Part 8 Graphs
Shortest Path Algorithms. Kruskal’s Algorithm We construct a set of edges A satisfying the following invariant:  A is a subset of some MST We start with.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
SPANNING TREES Lecture 21 CS2110 – Spring
Minimum Spanning Trees
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.
Minimum Spanning Trees CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Algorithm Course Dr. Aref Rashad February Algorithms Course..... Dr. Aref Rashad Part: 5 Graph Algorithms.
Prims’ spanning tree algorithm Given: connected graph (V, E) (sets of vertices and edges) V1= {an arbitrary node of V}; E1= {}; //inv: (V1, E1) is a tree,
State-Space Searches. 2 State spaces A state space consists of A (possibly infinite) set of states The start state represents the initial problem Each.
Spanning Trees CSIT 402 Data Structures II 1. 2 Two Algorithms Prim: (build tree incrementally) – Pick lower cost edge connected to known (incomplete)
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.
Lecture 3: Uninformed Search
Search CPSC 386 Artificial Intelligence Ellen Walker Hiram College.
CSE373: Data Structures & Algorithms Lecture 17: Dijkstra’s Algorithm Lauren Milne Summer 2015.
SPANNING TREES Lecture 20 CS2110 – Fall Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)
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++
Graph Algorithms GAM 376 Robin Burke Fall Outline Graphs Theory Data structures Graph search Algorithms DFS BFS Project #1 Soccer Break Lab.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
SPANNING TREES Lecture 21 CS2110 – Fall Nate Foster is out of town. NO 3-4pm office hours today!
Minimum Spanning Trees CSE 373 Data Structures Lecture 21.
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
Graphs + Shortest Paths David Kauchak cs302 Spring 2013.
Graph Searching CSIT 402 Data Structures II. 2 Graph Searching Methodology Depth-First Search (DFS) Depth-First Search (DFS) ›Searches down one path as.
CSE373: Data Structures & Algorithms Lecture 19: Dijkstra’s algorithm and Spanning Trees Catie Baker Spring 2015.
For Monday Read chapter 4 exercise 1 No homework.
CSE 326: Data Structures Lecture #17 Heuristic Graph Search Henry Kautz Winter Quarter 2002.
1 GRAPHS – Definitions A graph G = (V, E) consists of –a set of vertices, V, and –a set of edges, E, where each edge is a pair (v,w) s.t. v,w  V Vertices.
Graphs. What is a graph? In simple words, A graph is a set of vertices and edges which connect them. A node (or vertex) is a discrete position in the.
CSE 326: Data Structures Lecture #23 Dijkstra and Kruskal (sittin’ in a graph) Steve Wolfman Winter Quarter 2000.
Instructor: Lilian de Greef Quarter: Summer 2017
May 12th – Minimum Spanning Trees
CSE373: Data Structures & Algorithms Lecture 19: Spanning Trees
Graphs Representation, BFS, DFS
Outline This topic covers Prim’s algorithm:
Hannah Tang and Brian Tjaden Summer Quarter 2002
CSE373: Data Structures & Algorithms Lecture 19: Spanning Trees
CSE332: Data Abstractions Lecture 18: Minimum Spanning Trees
Spanning Trees Lecture 20 CS2110 – Spring 2015.
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Lecture 14 Minimum Spanning Tree (cont’d)
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
More Graphs Lecture 19 CS2110 – Fall 2009.
Presentation transcript:

CSE 326: Data Structures Lecture #19 More Fun with Graphs Henry Kautz Winter Quarter 2002

Today How to Make Depth-First Search Find Optimal Paths –Why bother? Finding Connected Components –Application to machine vision Finding Minimum Spanning Trees –Yet another use for union/find

Is BFS the Hands Down Winner? Consider finding a path from vertex S to G in an unweighted graph where you do not have a heuristic function h(n). Depth-first search –Simple to implement (implicit or explict stack) –Does not always find shortest paths –Must be careful to “mark” visited vertices, or you could go into an infinite loop if there is a cycle Breadth-first search –Simple to implement (queue) –Always finds shortest paths –Marking visited nodes can improve efficiency, but even without doing so search is guaranteed to terminate

Space Requirements Consider space required by the stack or queue… Suppose –G is known to be at distance d from S –Each vertex n has k out-edges –There are no (undirected or directed) cycles BFS queue will grow to size k d –Will simultaneously contain all nodes that are at distance d (once last vertex at distance d-1 is expanded) –For k=10, d=15, size is 1,000,000,000,000,000

DFS Space Requirements Consider DFS, where we limit the depth of the search to d –Force a backtrack at d+1 –When visiting a node n at depth d, stack will contain (at most) k-1 siblings of n parent of n siblings of parent of n grandparent of n siblings of grandparent of n … DFS queue grows at most to size dk –For k=10, d=15, size is 150 –Compare with BFS 1,000,000,000,000,000

Conclusion For very large graphs – ones that are generated “on the fly” rather than stored entirely in memory – DFS is hugely more memory efficient, if we know the distance to the goal vertex! But suppose we don’t know d. What is the (obvious) strategy?

Iterative Deepening DFS IterativeDeepeningDFS(vertex s, g){ for (i=1;true;i++) if DFS(i, s, g) return; } // Also need to keep track of path found bool DFS(int limit, vertex s, g){ if (s==g) return true; if (limit-- <= 0) return false; for (n in children(s)) if (DFS(limit, n, g)) return true; return false; }

Analysis of Iterative Deepening Even without “marking” nodes as visited, iterative-deepening DFS never goes into an infinite loop –For very large graphs, memory cost of keeping track of visited vertices may make marking prohibitive Work performed with limit < actual distance to G is wasted – but the wasted work is usually small compared to amount of work done during the last iteration

Asymptotic Analysis There are “pathological” graphs for which iterative deepening is bad: S G n=d

A Better Case Suppose each vertex n has k out-edges –We don’t worry about cycles – just search the vertices over again Exhaustive DFS to level i reaches k i vertices – requires time ck i for some constant c Iterative Deepening DFS(d) = ignore low order terms!

(More) Conclusions To find a shortest path between two nodes in a unweighted graph where no heuristic function is known, use either BFS or Iterated DFS If the graph is large, Iterated DFS typically uses much less memory If a good heuristic function is known, use A* –But what about memory requirements for A* for very large graphs??!!

(Final?) Conclusions & Questions In the worst case A* can also require a (priority) queue of size exponential in d, the distance to the goal vertex Question: Can one create an iterated, depth-first version of A* that (typically) uses less memory? –Yes, but you’ll have to wait until you take CSE 473, Introduction to Artificial Intelligence to see it! Related Question: How can we adapt Iterated DFS for weighted graphs, in order to get an algorithm that is more memory efficient than Dijkstra’s?

Counting Connected Components Initialize the cost of each vertex to  Num_cc = 0 While there are vertices of cost  { Pick an arbitrary such vertex S, set its cost to 0 Find paths from S Num_cc ++ }

Using DFS Set each vertex to “unvisited” Num_cc = 0 While there are unvisited vertices { Pick an arbitrary such vertex S Perform DFS from S, marking vertices as visited Num_cc ++ } Complexity = O(|V|+|E|)

Using Union / Find Put each node in its own equivalence class Num_cc = 0 For each edge E = Union(x,y) Return number of equivalence classes Complexity =

Using Union / Find Put each node in its own equivalence class Num_cc = 0 For each edge E = Union(x,y) Return number of equivalence classes Complexity = O(|V|+|E| ack(|E|,|V|))

Machine Vision: Blob Finding

Blob Finding Matrix can be considered an efficient representation of a graph with a very regular structure Cell = vertex Adjacent cells of same color = edge between vertices Blob finding = finding connected components

Tradeoffs Both DFS and Union/Find approaches are (essentially) O(|E|+|V|) = O(|E|) for binary images For each component, DFS (“recursive labeling”) can move all over the image – entire image must be in main memory Better in practice: row-by-row processing –localizes accesses to memory –typically 1-2 orders of magnitude faster!

High-Level Blob-Labeling Scan through image left/right and top/bottom If a cell is same color as (connected to) cell to right or below, then union them Give the same blob number to cells in each equivalence class

Blob-Labeling Algorithm Put each cell in it’s own equivalence class For each cell if color[x,y] == color[x+1,y] then Union(, ) if color[x,y] == color[x,y+1] then Union(, ) label = 0 For each root blobnum[x,y] = ++ label; For each cell blobnum[x,y] = blobnum( Find( ) )

Spanning tree: a subset of the edges from a connected graph that… …touches all vertices in the graph (spans the graph) …forms a tree (is connected and contains no cycles) Minimum spanning tree: the spanning tree with the least total edge cost. Spanning Tree

Applications of Minimal Spanning Trees Communication networks VLSI design Transportation systems Good approximation to some NP-hard problems (more later)

Kruskal’s Algorithm for Minimum Spanning Trees A greedy algorithm: Initialize all vertices to unconnected While there are still unmarked edges Pick a lowest cost edge e = (u, v) and mark it If u and v are not already connected, add e to the minimum spanning tree and connect u and v Sound familiar? (Think maze generation.)

Kruskal’s Algorithm in Action (1/5) A C B D FH G E

Kruskal’s Algorithm in Action (2/5) A C B D FH G E

Kruskal’s Algorithm in Action (3/5) A C B D FH G E

Kruskal’s Algorithm in Action (4/5) A C B D FH G E

Kruskal’s Algorithm Completed (5/5) A C B D FH G E

Why Greediness Works Proof by contradiction that Kruskal’s finds a minimum spanning tree: Assume another spanning tree has lower cost than Kruskal’s. Pick an edge e 1 = (u, v) in that tree that’s not in Kruskal’s. Consider the point in Kruskal’s algorithm where u’s set and v’s set were about to be connected. Kruskal selected some edge to connect them: call it e 2. But, e 2 must have at most the same cost as e 1 (otherwise Kruskal would have selected it instead). So, swap e 2 for e 1 (at worst keeping the cost the same) Repeat until the tree is identical to Kruskal’s, where the cost is the same or lower than the original cost: contradiction!

Data Structures for Kruskal’s Algorithm Pick the lowest cost edge… findMin/deleteMin If u and v are not already connected… …connect u and v. union |E| times: runtime: Once: Initialize heap of edges… buildHeap |E| + |E| log |E| + |E| ack(|E|,|V|)

Data Structures for Kruskal’s Algorithm Pick the lowest cost edge… findMin/deleteMin If u and v are not already connected… …connect u and v. union |E| times: runtime: Once: Initialize heap of edges… buildHeap |E| + |E| log |E| + |E| ack(|E|,|V|) = O(|E|log|E|)

Prim’s Algorithm Can also find Minimum Spanning Trees using a variation of Dijkstra’s algorithm: Pick a initial node Until graph is connected: Choose edge (u,v) which is of minimum cost among edges where u is in tree but v is not Add (u,v) to the tree Same “greedy” proof, same asymptotic complexity

Does Greedy Always Work? Consider the following problem: Given a graph G = (V,E) and a designated subset of vertices S, find a minimum cost tree that includes all of S Exactly the same as a minimum spanning tree, except that it does not have to include ALL the vertices – only the specified subset of vertices. –Does Kruskal’s or Prim’s work?

Nope! Greedy can fail to be optimal –because different solutions may contain different “non- designated” vertices, proof that you can covert one to the other doesn’t go through This Minimum Steiner Tree problem has no known solution of O(n k ) for any fixed k –NP-complete problems strike again! –Finding a spanning tree and then pruning it a pretty good approximation

Some other NP-Complete Problems If you see one: approximate or search (maybe A*), and be prepared to wait… Traveling Salesman – given a complete weighted graph, find a minimum cost simple cycle of all the nodes. Graph Coloring – can each node in a graph be given a color from a set of k colors, such that no adjacent nodes receive the same color?

A Great Book You Should Own! Computers and Intractability: A Guide to the Theory of NP-Completeness, by Michael S. Garey and David S. Johnson