Podcast Ch24c Title: Breadth First Search

Slides:



Advertisements
Similar presentations
Graphs CSC 220 Data Structure. Introduction One of the Most versatile data structures like trees. Terminology –Nodes in trees are vertices in graphs.
Advertisements

Topological Sort Topological sort is the list of vertices in the reverse order of their finishing times (post-order) of the depth-first search. Topological.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Algorithms and Data Structures
Breadth-First and Depth-First Search
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 24 Graphs.
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
Algoritma Traversal Graph. Graph Traversal Algorithms In general, graphs do not have a vertex, like a root, that initiates unique paths to each of the.
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 10 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 03 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 25 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 09 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
CISC220 Fall 2009 James Atlas Nov 13: Graphs, Line Intersections.
CS261 Data Structures DFS and BFS – Edge List Representation.
Computer Science 112 Fundamentals of Programming II Graph Algorithms.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 25 Graph.
Search Related Algorithms. Graph Code Adjacency List Representation:
Graph. Terminologi Graph A graph consists of a set of vertices V, along with a set of edges E that connect pairs of vertices. – An edge e = (vi,vj) connects.
Data Structures & Algorithms Graphs. Graph Terminology A graph consists of a set of vertices V, along with a set of edges E that connect pairs of vertices.
Where’s the title? You gotta search for it!. PotW Solution String s = new Scanner(System.in).next(); int[] prev = new int[s.length() * 2 + 2]; Arrays.fill(prev,
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
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
Representing Graphs Depth First Search Breadth First Search Graph Searching Algorithms.
Review Graph Directed Graph Undirected Graph Sub-Graph Spanning Sub-Graph Degree of a Vertex Weighted Graph Elementary and Simple Path Link List Representation.
CSE 373 Data Structures and Algorithms
Podcast Ch17b Title: Iterative Tree Traversal
Podcast Ch24d Title: Depth First Search and Acyclic Graphs
Podcast Ch26a Title: Representing Graphs
CSE 2331/5331 Topic 9: Basic Graph Alg.
Csc 2720 Instructor: Zhuojun Duan
Podcast Ch17d Title: Drawing a Binary Tree
Graph Search Lecture 17 CS 2110 Fall 2017.
Lecture 12 Graph Algorithms
Podcast Ch17a Title: Expression Trees
CS120 Graphs.
Data Structures and Algorithms for Information Processing
Data Structures for Java William H. Ford William R. Topp
Graphs Graph transversals.
Graphs Chapter 15 explain graph-based algorithms Graph definitions
Can you get there from here?
Podcast Ch25d Title: Minimum Path Algorithm
Graphs Part 2 Adjacency Matrix
Podcast Ch24b Title: Graphs and Digraphs
Richard Anderson Autumn 2016 Lecture 5
Data Structures & Algorithms
COMP171 Depth-First Search.
Podcast Ch25c Title: Shortest Path Algorithm
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
Depth-First Search CSE 2011 Winter April 2019.
Algorithms Searching in a Graph.
Podcast Ch22b Title: Inserting into a Heap
Graph Implementation.
Podcast Ch18a Title: Overview of Binary Search Trees
GRAPHS G=<V,E> Adjacent vertices Undirected graph
Podcast Ch18d Title: Binary Search Tree Iterator
Podcast Ch21d Title: Hash Class Iterators
Podcast Ch26b Title: Digraph Class Implementation
Podcast Ch27a Title: Overview of AVL Trees
Podcast Ch21a Title: Hash Functions
Podcast Ch21f Title: HashSet Class
Podcast Ch23d Title: Huffman Compression
Podcast Ch27b Title: AVLTree implementation
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Podcast Ch24b Title: Strongly Connected Components
Podcast Ch21b Title: Collision Resolution
Lecture 11 Graph Algorithms
Presentation transcript:

Podcast Ch24c Title: Breadth First Search Description: Breadth first search algorithm; bfs() method; an example Participants: Barry Kurtz (instructor); John Helfert and Tobie Williams (students) Textbook: Data Structures for Java; William H. Ford and William R. Topp

Graph Traversal Algorithms In general, graphs do not have a vertex, like a root, that initiates unique paths to each of the vertices. From any starting vertex in a graph, it might not be possible to search all of the vertices. In addition, a graph could have a cycle that results in multiple visits to a vertex.

Graph Traversal Algorithms The breadth-first search visits vertices in the order of their path length from a starting vertex. It may not visit every vertex of the graph The depth-first search traverses all the vertices of a graph by making a series of recursive calls that follow paths through the graph. Graph algorithms discern the state of a vertex during the algorithm by using the colors WHITE, GRAY, and BLACK.

Breadth-First Search Algorithm

Breadth-First Search Algorithm Color all vertices of the sample graph WHITE and push the starting vertex (A) onto the queue visitQueue. Pop A from the queue, color it BLACK, and insert it into visitList, which is the list of visited vertices. Push all WHITE neighbors onto the queue.

Breadth-First Search Algorithm Pop B from the queue and place it in visitList with color BLACK. The only adjacent vertex for B is D, which is still colored WHITE. Color D GRAY and add it to the queue

Breadth-First Search Algorithm Pop C and place it in visitList. The adjacent vertex G is GRAY. No new vertices enter the queue.

Breadth-First Search Algorithm Pop vertex G from the queue and place it in visitList. G has no adjacent vertices, so pop D from the queue. The neighbors, E and F, enter the queue.

Breadth-First Search Algorithm Continue in this fashion until the queue is empty.

Student Question Provide two different linked lists that could be created in a breadth-first search of vertices starting at vertex "A". One list assumes that "F" is selected as the first adjacent vertex and the second list assumes that "C" is selected as the first adjacent vertex. List 1: ________________________________________________________ List 2: ________________________________________________________

Student Question Identify a list produced by a breadth first search of the graph starting at vertex "E". List: ________________________________________________________ Are there starting vertices from which all vertices in the graph are reachable? If so, identify them. ______________________________

Implementing Breadth-First Search The color attribute of a vertex. public enum VertexColor { WHITE, GRAY, BLACK }

bfs() // perform the breadth-first traversal // from sVertex and return the list // of visited vertices public static <T> LinkedList<T> bfs( DiGraph<T> g, T sVertex) { // queue stores adjacent vertices; list // stores visited vertices LinkedQueue<T> visitQueue = new LinkedQueue<T>(); LinkedList<T> visitList = new LinkedList<T>(); // set and iterator retrieve and scan // neighbors of a vertex Set<T> edgeSet; Iterator<T> edgeIter; T currVertex = null, neighborVertex = null;

bfs() (continued) // check that starting vertex is valid if (!g.containsVertex(sVertex)) throw new IllegalArgumentException( "bfs(): starting vertex not in the graph"); // color all vertices WHITE g.colorWhite(); // initialize queue with starting vertex visitQueue.push(sVertex); while (!visitQueue.isEmpty()) { // remove a vertex from the queue, color // it black, and add to the list of // visited vertices currVertex = visitQueue.pop(); g.setColor(currVertex,VertexColor.BLACK); visitList.add(currVertex);

bfs() (continued) // obtain set of neighbors for current vertex edgeSet = g.getNeighbors(currVertex); // sequence through the neighbors and look // for vertices that have not been visited edgeIter = edgeSet.iterator(); while (edgeIter.hasNext()){ neighborVertex = edgeIter.next(); if (g.getColor(neighborVertex) == VertexColor.WHITE) { // color unvisited vertex GRAY and // push it onto queue g.setColor(neighborVertex,VertexColor.GRAY); visitQueue.push(neighborVertex); } return visitList;

Running Time of Breadth-First Search The running time for the breadth-first search is O(V + E), where V is the number of vertices and E is the number of edges.

Breadth-First Search Example // create g; declare startVertex and visitList DiGraph<String> g = DiGraph.readGraph("bfsgraph.dat"); String startVertex; List<String> visitList; ... // call bfs() with arguments g and startVertx visitList = DiGraphs.bfs(g, startVertex); // output the visitList System.out.println("BFS visitList from " + startVertex + ": " + visitList);

Breadth-First Search Example (concluded) Output: Run 1: (startVertex = "A") BFS visitList from A: [A, G, B, C, D, E, F] Run 2: (startVertex = "D") BFS visitList from D: [D, E, F, G] Run 3: (startVertex = "E") BFS visitList from E: [E]

Student Question DiGraph<String> g = null; Set<String> vSet, neighborSet; g = DiGraph.readGraph("graphG.dat"); System.out.println(g); vSet = g.vertexSet(); for (String v : vSet) { neighborSet = g.getNeighbors(v); System.out.println("Vertex " + v); System.out.println(" Neighbors: " + neighborSet); for (String s : neighborSet) System.out.println("Weight edge(" + v + ", " + s + ") = " + g.getWeight(v,s)); }

Student Question Draw this graph Run: Vertex D Neighbors: [] Vertex E Neighbors: [D, A] Weight edge(E, D) = 1 Weight edge(E, A) = 1 Vertex A Neighbors: [D, B] Weight edge(A, D) = 1 Weight edge(A, B) = 1 Vertex B Neighbors: [C] Weight edge(B, C) = 1 Vertex C Neighbors: [D] Weight edge(C, D) = 1