Breadth-first and depth-first traversal CS1114

Slides:



Advertisements
Similar presentations
CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
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.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
GRAPHS Trees Without Rules. Graph  A data structure that consists of a set of nodes (called vertices) and a set of edges that relate the nodes to each.
Breadth-First and Depth-First Search
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
Chapter 5 Decrease and Conquer. Homework 7 hw7 (due 3/17) hw7 (due 3/17) –page 127 question 5 –page 132 questions 5 and 6 –page 137 questions 5 and 6.
1 Graphs: Traversal Searching/Traversing a graph = visiting the vertices of a graph by following the edges in a systematic way Example: Given a highway.
GRAPHS AND GRAPH TRAVERSALS 9/26/2000 COMP 6/4030 ALGORITHMS.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
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.
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.
1 Breadth First Search AB F I EH DC G FIFO Queue - front.
CSE332: Data Abstractions Lecture 16: Topological Sort / Graph Traversals Tyler Robison Summer
Graphs CS-240/341. Graphs Used for representing many-to-many relationships –can take two forms directed (digraph) - a finite set of elements called vertices.
Clustering and greedy algorithms — Part 2 Prof. Noah Snavely CS1114
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
Spanning Trees. 2 Spanning trees Suppose you have a connected undirected graph Connected: every node is reachable from every other node Undirected: edges.
Fall 2007CS 2251 Iterators and Tree Traversals. Fall 2007CS 2252 Binary Trees In a binary tree, each node has at most two subtrees A set of nodes T is.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
CS 1114: Graphs and Blobs Prof. Graeme Bailey (notes modified from Noah Snavely, Spring 2009)
Blobs and Graphs Prof. Noah Snavely CS1114
Graphs & Graph Algorithms Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
CISC220 Fall 2009 James Atlas Nov 13: Graphs, Line Intersections.
Spanning Trees. Spanning trees Suppose you have a connected undirected graph –Connected: every node is reachable from every other node –Undirected: edges.
Connected components and graph traversal Prof. Noah Snavely CS1114
CS261 Data Structures DFS and BFS – Edge List Representation.
Search Related Algorithms. Graph Code Adjacency List Representation:
Tree Searching Breadth First Search Dept First Search.
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,
Data Structures and Algorithms Ver. 1.0 Session 17 Objectives In this session, you will learn to: Implement a graph Apply graphs to solve programming problems.
Connected Components Fun with graphs, searching, and queues.
Lecture 13 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
Graph Introduction, Searching Graph Theory Basics - Anil Kishore.
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.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
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
Depth-First Search Lecture 21: Graph Traversals
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Breadth-first and depth-first traversal Prof. Noah Snavely CS1114
Graphs & Paths Presentation : Part II. Graph representation Given graph G = (V, E). May be either directed or undirected. Two common ways to represent.
Chapter 05 Introduction to Graph And Search Algorithms.
Graph Searching CSIT 402 Data Structures II. 2 Graph Searching Methodology Depth-First Search (DFS) Depth-First Search (DFS) ›Searches down one path as.
CSE 421 Algorithms Richard Anderson Autumn 2015 Lecture 5.
Best-first search is a search algorithm which explores a graph by expanding the most promising node chosen according to a specified rule.
Data Structures and Algorithms Lists, Stacks, Queues, and Graphs Sorting and searching algorithms.
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.
Blobs and Graphs Prof. Noah Snavely CS1114
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
Review Graph Directed Graph Undirected Graph Sub-Graph Spanning Sub-Graph Degree of a Vertex Weighted Graph Elementary and Simple Path Link List Representation.
Set Collection A Bag is a general collection class that implements the Collection interface. A Set is a collection that resembles a Bag with the provision.
Chapter 15 Lists Objectives
CS 1114: Implementing Search
Spanning Trees Longin Jan Latecki Temple University based on slides by
Data Structures – Stacks and Queus
Richard Anderson Autumn 2016 Lecture 5
Breadth First Search - A B C D E F G H I front FIFO Queue.
Richard Anderson Winter 2019 Lecture 5
Prof. Ramin Zabih Graph Traversal Prof. Ramin Zabih
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 .
Presentation transcript:

Breadth-first and depth-first traversal CS1114

2 Blobs are components! A B C D EFG H A C B D F E H G

3 Finding blobs

Blobs are connected components!

5 Finding components 1.Pick a 1 to start with, where you don’t know which component it is in –When there aren’t any, you’re done 2.Give it a new component color 3.Assign the same component color to each pixel that is part of the same component –Basic strategy: color any neighboring 1’s, have them color their neighbors, and so on

6  For each vertex we visit, we color its neighbors and remember that we need to visit them at some point –Need to keep track of the vertices we still need to visit in a todo list –After we visit a vertex, we’ll pick one of the vertices in the todo list to visit next  This is also called graph traversal Strategy for finding components

Stacks and queues  Two ways of representing a “todo list”  Stack: Last In First Out (LIFO) –(Think cafeteria trays) –The newest task is the one you’ll do next  Queue: First In First Out (FIFO) –(Think a line of people at the cafeteria) –The oldest task is the one you’ll do next 7

Stacks  Two operations:  Push: add something to the top of the stack  Pop: remove the thing on top of the stack 8

Queue  Two operations:  Enqueue: add something to the end of the queue  Dequeue: remove something from the front of the queue 9

Graph traversal  Suppose you’re in a maze  What strategy can you use to find the exit? 10

Graph traversal 11 Paris Berlin London Rome Frankfurt Vienna Prague Naples Warsaw Hamburg Oslo Stockholm Pick a node to start with

12 Paris Berlin 1 London Rome Frankfurt Vienna Prague Naples Warsaw Hamburg Current node: London Todo list: [ ] Graph traversal (stack) Oslo Stockholm

13 Paris Berlin 1 London Rome Frankfurt Vienna Prague Naples Warsaw Hamburg Graph traversal (stack) Current node: London Todo list: [ Paris ] Oslo Stockholm

14 2 Paris Berlin 1 London Rome Frankfurt Vienna Prague Naples Warsaw Hamburg Graph traversal (stack) Current node: Paris Todo list: [ ] Oslo Stockholm

15 2 Paris Berlin 1 London Rome Frankfurt Vienna Prague Naples Warsaw Hamburg Graph traversal (stack) Current node: Paris Todo list: [ Frankfurt, Berlin, Rome ] Oslo Stockholm

16 2 Paris Berlin 1 London 3 Rome Frankfurt Vienna Prague Naples Warsaw Hamburg Graph traversal (stack) Current node: Rome Todo list: [ Frankfurt, Berlin ] Oslo Stockholm

17 2 Paris Berlin 1 London 3 Rome Frankfurt Vienna Prague Naples Warsaw Hamburg Graph traversal (stack) Current node: Rome Todo list: [ Frankfurt, Berlin, Naples ] Oslo Stockholm

18 2 Paris Berlin 1 London 3 Rome Frankfurt Vienna Prague 4 Naples Warsaw Hamburg Graph traversal (stack) Current node: Naples Todo list: [ Frankfurt, Berlin ] Oslo Stockholm

19 2 Paris 5 Berlin 1 London 3 Rome Frankfurt Vienna Prague 4 Naples Warsaw Hamburg Graph traversal (stack) Current node: Berlin Todo list: [ Frankfurt ] Oslo Stockholm

20 2 Paris 5 Berlin 1 London 3 Rome Frankfurt Vienna Prague 4 Naples Warsaw Hamburg Graph traversal (stack) Current node: Berlin Todo list: [ Frankfurt, Hamburg, Vienna ] Oslo Stockholm

21 2 Paris 5 Berlin 1 London 3 Rome Frankfurt 6 Vienna Prague 4 Naples Warsaw Hamburg Graph traversal (stack) Current node: Vienna Todo list: [ Frankfurt, Hamburg ] Oslo Stockholm

22 2 Paris 5 Berlin 1 London 3 Rome Frankfurt 6 Vienna Prague 4 Naples Warsaw Hamburg Graph traversal (stack) Current node: Vienna Todo list: [ Frankfurt, Hamburg, Prague, Warsaw ] Oslo Stockholm

23 2 Paris 5 Berlin 1 London 3 Rome Frankfurt 6 Vienna Prague 4 Naples Warsaw Hamburg Graph traversal (stack) Current node: Vienna Todo list: [ Frankfurt, Hamburg, Prague, Warsaw ] Oslo Stockholm

24 2 Paris 5 Berlin 1 London 3 Rome Frankfurt 6 Vienna Prague 4 Naples 7 Warsaw Hamburg Graph traversal (stack) Current node: Warsaw Todo list: [ Frankfurt, Hamburg, Prague ] Oslo Stockholm

25 2 Paris 5 Berlin 1 London 3 Rome Frankfurt 6 Vienna 8 Prague 4 Naples 7 Warsaw Hamburg Graph traversal (stack) Current node: Prague Todo list: [ Frankfurt, Hamburg ] Oslo Stockholm

26 2 Paris 5 Berlin 1 London 3 Rome Frankfurt 6 Vienna 8 Prague 4 Naples 7 Warsaw 9 Hamburg Graph traversal (stack) Current node: Hamburg Todo list: [ Frankfurt ] Oslo Stockholm

27 2 Paris 5 Berlin 1 London 3 Rome 10 Frankfurt 6 Vienna 8 Prague 4 Naples 7 Warsaw 9 Hamburg Graph traversal (stack) Current node: Frankfurt Todo list: [ ] Oslo Stockholm

Depth-first search (DFS)  Call the starting node the root  We traverse paths all the way until we get to a dead-end, then backtrack (until we find an unexplored path)

Another strategy 29 1.Explore all the cities that are one hop away from the root 2.Explore all cities that are two hops away from the root 3.Explore all cities that are three hops away from the root …  This corresponds to using a queue

30 Paris Berlin 1 London Rome Frankfurt Vienna Prague Naples Warsaw Hamburg Current node: London Todo list: [ ] Graph traversal (queue) Oslo Stockholm

31 Paris Berlin 1 London Rome Frankfurt Vienna Prague Naples Warsaw Hamburg Graph traversal (queue) Current node: London Todo list: [ Paris ] Oslo Stockholm

32 2 Paris Berlin 1 London Rome Frankfurt Vienna Prague Naples Warsaw Hamburg Graph traversal (queue) Current node: Paris Todo list: [ ] Oslo Stockholm

33 2 Paris Berlin 1 London Rome Frankfurt Vienna Prague Naples Warsaw Hamburg Graph traversal (queue) Current node: Paris Todo list: [ Frankfurt, Berlin, Rome ] Oslo Stockholm

34 2 Paris Berlin 1 London Rome 3 Frankfurt Vienna Prague Naples Warsaw Hamburg Graph traversal (queue) Current node: Frankfurt Todo list: [ Berlin, Rome ] Oslo Stockholm

35 2 Paris Berlin 1 London Rome 3 Frankfurt Vienna Prague Naples Warsaw Hamburg Graph traversal (queue) Current node: Frankfurt Todo list: [ Berlin, Rome, Hamburg ] Oslo Stockholm

36 2 Paris 4 Berlin 1 London Rome 3 Frankfurt Vienna Prague Naples Warsaw Hamburg Graph traversal (queue) Current node: Berlin Todo list: [ Rome, Hamburg ] Oslo Stockholm

37 2 Paris 4 Berlin 1 London Rome 3 Frankfurt Vienna Prague Naples Warsaw Hamburg Graph traversal (queue) Current node: Berlin Todo list: [ Rome, Hamburg, Vienna ] Oslo Stockholm

38 2 Paris 4 Berlin 1 London 5 Rome 3 Frankfurt Vienna Prague Naples Warsaw Hamburg Graph traversal (queue) Current node: Rome Todo list: [ Hamburg, Vienna ] Oslo Stockholm

39 2 Paris 4 Berlin 1 London 5 Rome 3 Frankfurt Vienna Prague Naples Warsaw Hamburg Graph traversal (queue) Current node: Rome Todo list: [ Hamburg, Vienna, Naples ] Oslo Stockholm

40 2 Paris 4 Berlin 1 London 5 Rome 3 Frankfurt Vienna Prague Naples Warsaw 6 Hamburg Graph traversal (queue) Current node: Hamburg Todo list: [ Vienna, Naples ] Oslo Stockholm

41 2 Paris 4 Berlin 1 London 5 Rome 3 Frankfurt 7 Vienna Prague Naples Warsaw 6 Hamburg Graph traversal (queue) Current node: Vienna Todo list: [ Naples ] Oslo Stockholm

42 2 Paris 4 Berlin 1 London 5 Rome 3 Frankfurt 7 Vienna Prague Naples Warsaw 6 Hamburg Graph traversal (queue) Current node: Vienna Todo list: [ Naples, Prague, Warsaw ] Oslo Stockholm

43 2 Paris 4 Berlin 1 London 5 Rome 3 Frankfurt 7 Vienna Prague 8 Naples Warsaw 6 Hamburg Graph traversal (queue) Current node: Naples Todo list: [ Prague, Warsaw ] Oslo Stockholm

44 2 Paris 4 Berlin 1 London 5 Rome 3 Frankfurt 7 Vienna 9 Prague 8 Naples Warsaw 6 Hamburg Graph traversal (queue) Current node: Prague Todo list: [ Warsaw ] Oslo Stockholm

45 2 Paris 4 Berlin 1 London 5 Rome 3 Frankfurt 7 Vienna 9 Prague 8 Naples 10 Warsaw 6 Hamburg Graph traversal (queue) Current node: Warsaw Todo list: [ ] Oslo Stockholm

Breadth-first search (BFS)  We visit all the vertices at the same level (same distance to the root) before moving on to the next level

BFS vs. DFS Breadth-first (queue) Depth-first (stack)

BFS vs. DFS 48 (tree = graph with no cycles)

Basic algorithms BREADTH-FIRST SEARCH (Graph G)  While there is an uncolored node r –Choose a new color –Create an empty queue Q –Let r be the root node, color it, and add it to Q –While Q is not empty Dequeue a node v from Q For each of v’s neighbors u If u is not colored, color it and add it to Q 49

Basic algorithms DEPTH-FIRST SEARCH (Graph G)  While there is an uncolored node r –Choose a new color –Create an empty stack S –Let r be the root node, color it, and push it on S –While S is not empty Pop a node v from S For each of v’s neighbors u If u is not colored, color it and push it onto S 50

Queues and Stacks  Examples of Abstract Data Types (ADTs)  ADTs fulfill a contract: –The contract tells you what the ADT can do, and what the behavior is –For instance, with a stack: We can push and pop If we push X onto S and then pop S, we get back X, and S is as before  Doesn’t tell you how it fulfills the contract 51

Implementing DFS  How can we implement a stack? –Needs to support several operations: –Push (add an element to the top) –Pop (remove the element from the top) –IsEmpty

Implementing a stack  IsEmpty function e = IsEmpty(S) e = (length(S) == 0);  Push (add an element to the top) function S = push(S, x) S = [ S x ]  Pop (remove an element from the top) function [S, x] = pop(S) n = length(S); x = S(n); S = S(1:n-1); % but what happens if n = 0? 53

Implementing BFS  How can we implement a queue? –Needs to support several operations: –Enqueue (add an element to back) –Dequeue (remove an element from front) –IsEmpty  Not quite as easy as a stack…