Download presentation

Presentation is loading. Please wait.

Published byGertrude Dalton Modified over 2 years ago

1
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

2
8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 2 breadth-first search http://www.itl.nist.gov/div897/sqg/dads/HTML/breadthfirst.html 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)

3
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?)

4
Pseudocode implementation http://en.wikipedia.org/wiki/Breadth-first_search download 31/10/09 http://en.wikipedia.org/wiki/Breadth-first_search 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.

5
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 http://staff.unak.is/not/tony/teaching/ai/

6
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 http://staff.unak.is/not/tony/teaching/ai/

7
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

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

9
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 ?)

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

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

12
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

13
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

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

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

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

17
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

18
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

19
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

20
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).

Similar presentations

OK

© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.

© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on business plan preparation By appt only movie john Free ppt on brain machine interface application Download ppt on nationalism in indochina Ppt on different types of forests in india Ppt on cube and cuboid volumes Ppt on spiritual leadership book Ppt on history of indian mathematicians and mathematics Ppt on solar system for class 6 download Ppt on national parks in india