ALG0183 Algorithms & Data Structures Lecture 20 u unweighted breadth-first search 8/25/20091 ALG0183 Algorithms & Data Structures by Dr Andy Brooks Chapter.

Slides:



Advertisements
Similar presentations
ALG0183 Algorithms & Data Structures Lecture 23 a acyclic with neg. weights (topological sort algorithm) 8/25/20091 ALG0183 Algorithms & Data Structures.
Advertisements

ALG0183 Algorithms & Data Structures
CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
Analysis of Algorithms Depth First Search. Graph A representation of set of objects Pairs of objects are connected Interconnected objects are called “vertices.
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.
§2 Topological Sort 〖 Example 〗 Courses needed for a computer science degree at a hypothetical university How shall we convert this list into a graph?
CSE 101- Winter ‘15 Discussion Section January 26th 2015.
Tree Searching. Tree searches A tree search starts at the root and explores nodes from there, looking for a goal node (a node that satisfies certain conditions,
CSE 373: Data Structures and Algorithms Lecture 19: Graphs III 1.
Breadth-First and Depth-First Search
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
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.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 CHAPTER 4 - PART 2 GRAPHS 1.
CSCI 3160 Design and Analysis of Algorithms Tutorial 2 Chengyu Lin.
1 Shortest Path Algorithms Given a graph G = (V, E) and a distinguished vertex s, find the shortest weighted path from s to every other vertex in G. weighted.
Graph & BFS.
Directed Graph Algorithms CSE 373 Data Structures Lecture 14.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
CS2420: Lecture 37 Vladimir Kulyukin Computer Science Department Utah State University.
17-Jun-15 Searching in BeeperHunt (Mostly minor variations on old slides)
Graph COMP171 Fall Graph / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D E A C F B Vertex Edge.
Graphs. Graph definitions There are two kinds of graphs: directed graphs (sometimes called digraphs) and undirected graphs Birmingham Rugby London Cambridge.
Graph & BFS Lecture 22 COMP171 Fall Graph & BFS / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D.
Graphs. Graphs Many interesting situations can be modeled by a graph. Many interesting situations can be modeled by a graph. Ex. Mass transportation system,
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1.
CSE 780 Algorithms Advanced Algorithms Graph Algorithms Representations BFS.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
Shortest Path Problem For weighted graphs it is often useful to find the shortest path between two vertices Here, the “shortest path” is the path that.
More Graph Algorithms Weiss ch Exercise: MST idea from yesterday Alternative minimum spanning tree algorithm idea Idea: Look at smallest edge not.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
Tirgul 7 Review of graphs Graph algorithms: – BFS (next tirgul) – DFS – Properties of DFS – Topological sort.
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.
CS261 Data Structures DFS and BFS – Edge List Representation.
1 WEEK 9-10 Graphs II Unweighted Shortest Paths Dijkstra’s Algorithm Graphs with negative costs Acyclic Graphs Izmir University of Economics.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path problem.
Tree Searching Breadth First Search Dept First Search.
Chapter 2 Graph Algorithms.
ALG0183 Algorithms & Data Structures Lecture 21 d Dijkstra´s algorithm 8/25/20091 ALG0183 Algorithms & Data Structures by Dr Andy Brooks Chapter 14 Weiss.
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,
Shortest Path Problem Weight of the graph –Nonnegative real number assigned to the edges connecting to vertices Weighted graphs –When a graph.
A RTIFICIAL I NTELLIGENCE UNIT : 2 Search Techniques.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
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.
CSE 2331 / 5331 Topic 12: Shortest Path Basics Dijkstra Algorithm Relaxation Bellman-Ford Alg.
10 Copyright © William C. Cheng Data Structures - CSCI 102 Graph Terminology A graph consists of a set of Vertices and a set of Edges C A B D a c b d e.
Chapter 15 Linked Data Structures Slides prepared by Rose Williams, Binghamton University Kenrick Mock University of Alaska Anchorage Copyright © 2008.
Introduction to Graphs And Breadth First Search. Graphs: what are they? Representations of pairwise relationships Collections of objects under some specified.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
Trees, Binary Search Trees, Balanced Trees, Graphs Graph Fundamentals Telerik Algo Academy
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
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
– Graphs 1 Graph Categories Strong Components Example of Digraph
Graphs Upon completion you will be able to:
Graphs & Paths Presentation : Part II. Graph representation Given graph G = (V, E). May be either directed or undirected. Two common ways to represent.
SLIDES ADAPTED FROM ALEX MARIAKAKIS, WITH MATERIAL FROM KRYSTA YOUSOUFIAN, MIKE ERNST, KELLEN DONOHUE Section 5: HW6 and Interfaces Justin Bare and Deric.
Graphs and Paths Data Structures & Problem Solving Using JAVA Second Edition Mark Allen Weiss Chapter 14 © 2002 Addison Wesley.
Chapter 05 Introduction to Graph And Search Algorithms.
Source: David Lee Matuszek
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.
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.
abstract data types built on other ADTs
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
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.
Graphs Chapter 11 Objectives Upon completion you will be able to:
Presentation transcript:

ALG0183 Algorithms & Data Structures Lecture 20 u unweighted breadth-first search 8/25/20091 ALG0183 Algorithms & Data Structures by Dr Andy Brooks Chapter 14 Weiss

8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 2 breadth-first search Definition: Any search algorithm that considers neighbors of a vertex, that is, outgoing edges of the vertex's predecessor in the search, before any outgoing edges of the vertex. Extremes are searched last. This is typically implemented with a queue. vertexedgesqueue Note: In a tree, called a level-order traversal.level-order traversal vertex vertex´s predecessor in a queue and searched next queue/biðröð outgoing edges (level 2 nodes)

Queue(data structure) 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 3 delete from head (dequeue) add to tail (enqueue) (level 1 nodes) (level 2 nodes) (If this was a queue for innoculation against swine flu´ should young adults not be prioritized over middle-aged and old adults?)

Pseudocode implementation download 31/10/09 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 4 1. Enqueue the root node. 2. Dequeue a node and examine it. If the element sought is found in this node, quit the search and return a result. Otherwise enqueue any successors (the direct child nodes) that have not yet been discovered. 3. If the queue is empty, every node on the graph has been examined – quit the search and return "not found". 4.Repeat from Step 2. Note: Using a stack instead of a queue would turn this algorithm into a depth-first search.

Breadth-first searching A breadth-first search (BFS) explores nodes nearest the root before exploring nodes further away For example, after searching A, then B, then C, the search proceeds with D, E, F, G Node are explored in the order A B C D E F G H I J K L M N O P Q J will be found before N LM N OP G Q H J IK FED BC A slide material by Tony Chan 8/25/20095 ALG0183 Algorithms & Data Structures by Dr Andy Brooks

Depth-first searching A depth-first search (DFS) explores a path all the way to a leaf before backtracking and exploring another path For example, after searching A, then B, then D, the search backtracks and tries another path from B Node are explored in the order A B D E H L M N I O P C F G J K Q N will be found before J LM N OP G Q H J IK FED BC A slide material by Tony Chan 8/25/20096 ALG0183 Algorithms & Data Structures by Dr Andy Brooks

unweighted single-source shortest-path problem problem: “Find the shortest path (measured by number of edges) from a designated vertex S to every vertex.” Weiss “The shortest-path algorithms are all single-source algorithms, which begin at some starting point and compute the shortest path from it to all vertices.” Weiss “The unweighted shortest-path problem is a special case of the weighted shortest-path problem (in which all weights are 1).” Weiss “All variations of the shortest-path problem have similar solutions.” Weiss 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 7

Directed graph example from Weiss figures © Addison-Wesley 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 8 The starting vertex in this example is specified as V 2 and the u algorithm records that V 2 can be reached from V 2 in zero edges.

Directed graph example from Weiss figures © Addison-Wesley 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 9 The graph after all the vertices whose path length from the starting vertex is 1 have been found. (The “eyeball” or “wavefront” has passed over V 0 and V 5.) The graph after all the vertices whose shortest path from the starting vertex is 2 have been found. The final shortest paths. V 0 and V 5 are one edge away. The shortest path to V 1 and V 3 is 2. The shortest path to V 4 and V 6 is 3. (V 2 V 0 V 1 V 4 or V 2 V 0 V 3 V 4 ?)

8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 10 V2 V0 4 V2 V5 5 V0 V1 2 V0 V3 1 V3 V2 2 V3 V5 8 V3 V6 4 V6 V5 1 V1 V3 3 V1 V4 10 V3 V4 2 V4 V6 6 WeissGraphCh14.txt File read... 7 vertices Enter start node:V2 Enter destination node:V4 Enter algorithm (u, d, n, a ): u (Cost is: 3.0) V2 to V0 to V1 to V4 Enter start node: Sample I/O for WeissGraphCh14.txt In the adjacency list for V0, V1 comes before V3. So V1´s edges will be added to the queue before V3’s. The shortest path to V4 will be found through V1 first. What happens if we reverse the order of the V0 entries in bold?

8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 11 V2 V0 4 V2 V5 5 V0 V3 1 V0 V1 2 V3 V2 2 V3 V5 8 V3 V6 4 V6 V5 1 V1 V3 3 V1 V4 10 V3 V4 2 V4 V6 6 WeissGraphCh14.txt File read... 7 vertices Enter start node:V2 Enter destination node:V4 Enter algorithm (u, d, n, a ): u (Cost is: 3.0) V2 to V0 to V3 to V4 Enter start node: Sample I/O for WeissGraphCh14.txt In the adjacency list for V0, V3 now comes before V1. So V3´s edges will be added to the queue before V1’s. The shortest path to V4 will now be found through V3 first. The u algorithm does not report all the shortest-paths to a vertext, only the first shortest-path it finds. What happens if we reverse the order of the V0 entries in bold?

Features of algorithm u by Weiss. “If a path to a vertex has cost D v and w is adjacent to v, then there exists a path to w of cost D w = D v +1.” “All the shortest-path algorithms work by starting with D w = ∞ and reducing its value when an appropriate v is scanned.” “When a given v is scanned, we update the vertices w adjacent to v by scanning through v´s adjacency list.” – Some authors talk about “eyeballing” from vertex v or sending out a “wavefront” from vertex v to describe the action of scanning v´s adjacency list. In breadth-first search, “we are guaranteed that the first time D w is lowered from ∞, it is lowered to the value of the length of the shortest path to w.” 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 12

Features of algorithm u by Weiss. “These actions also tell us that the next-to-last vertex on the path to w is v, so one extra line of code allows us to store the actual path.” After processing v´s adjacent vertices, we move to another vertex u such that D u = D v. – If that is impossible, we move to a u that satisfies D u = D v +1. “If that is not possible, we are done.” (The queue is empty.) “We start with an empty queue and then we enqueue the starting vertex S.” 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 13

8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 14 // Represents a vertex in the graph. class Vertex { public String name; // Vertex name the vertex name never changes public List adj; // Adjacent vertices the list of edges never changes public double dist; // Cost from the starting vertex to this vertex public Vertex prev; // Previous vertex on shortest path public int scratch;// Extra variable used in algorithm public Vertex( String nm ) the constructor { name = nm; adj = new LinkedList ( ); reset( ); } public void reset( ) initialization { dist = Graph.INFINITY; prev = null; scratch = 0; } //{ dist = Graph.INFINITY; prev = null; pos = null; scratch = 0; } // public PairingHeap.Position pos; // Used for dijkstra2 (Chapter 23) } (reminder from earlier lecture)

8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 15 // Represents an edge in the graph. class Edge { public Vertex dest; // Second vertex in Edge the destination public double cost; // Edge cost weight public Edge( Vertex d, double c ) the constructor { dest = d; cost = c; } Edges are stored in an adjacency list. destination cost (reminder from earlier lecture)

8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 16 public void unweighted( String startName ) { clearAll( ); call to the reset method for each vertex Vertex start = vertexMap.get( startName ); get start vertex from the map if( start == null ) throw new NoSuchElementException( "Start vertex not found" ); Queue q = new LinkedList ( ); make a queue q.add( start ); enqueue the start vertex start.dist = 0; start to itself has distance zero while( !q.isEmpty( ) ) { stop when the queue is empty Vertex v = q.remove( ); get the vertex at head of queue for( Edge e : v.adj ) { Vertex w = e.dest; if( w.dist == INFINITY ) { are we meeting the vertex for the first time? w.dist = v.dist + 1; change its distance (no longer infinity) w.prev = v; make a note of the previous vertex (v) on shortest path q.add( w ); add the vertex to the tail of the queue } Eventually vertices will be added to the queue which have no outgoing edges and the while loop will terminate.

Queue q = new LinkedList ( ); Queue is an interface in java.util. This statement defines an interface variable (q) to reference a collection object. LinkedList implements the Queue interface. q is a Queue variable, not a LinkedList variable. By using interface variables in this way, it is easier to change the underlying implementation. If MyList, which also implements the Queue interface, is more space and/or time efficient, then we just need to write: Queue q = new MyList ( ); means only objects of type Vertex are allowed. – improved “type checking” for Java a benefit of the generics mechanism in Java 1.5 and later 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 17

Some Big-Oh of algorithm u by Weiss (breadth-first search) A loop within a loop means surely that the algorithm is quadratic? No. The inner loop is to do with scanning a number of edges. The outer loop is to do with removing a vertex from a queue of vertices. – A vertex is enqueued and dequeued at most once. So Big-Oh is O(|V|+|E|) -> O(|E|). – The number of edges dominates. 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 18

The state of the queue as algorithm u executes. (After each iteration of the while loop.) 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 19 A B 0 B C 0 B D 0 claw.txt A B CD A at the beginning B after 1 st iteration CD D empty

The state of the queue as algorithm u executes. (After each iteration of the while loop.) 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 20 B A 1 A C 1 B D 1 D C 1 C E 1 C F 1 R2.txt AB CD EF B at the beginning A D after 1 st iteration D C C (C not added twice) E F F empty Important note: for assessment purposes, students should be able to describe the state of the queue as shown above for the operation of Weiss´s algorithm u on any small graph specified as a list of edges (as in R2.txt above).