): Brian Williams, Spring 0415 Outline Problem Formulation –Problem solving as state space **search** Mathematical Model –Graphs and **search** trees Reasoning Algorithms –**Depth** and breadth-**first** **search** Brian Williams, Spring 0416 Problem Formulation: A Graph Operator Link (edge) State Node (vertex) Directed Graph (one-way streets) d e Undirected Graph (two-way streets) neighbors (**adjacent**) Incident to edge Start Vertex of Edge End Vertex of Edge Brian/

definition and do not guide the **search** with any additional information about the problem. –Breadth-**first** **search** –Uniform-cost **search** –**Depth**-**first** **search** –**Depth**-limited **search** –Iterative deepening **search** 24 Breadth-**first** **search** (BFS) Expand shallowest unexpanded node Expands **search** nodes level by level, all nodes/(n) gives the shortest solution If the rules are relaxed so that a tile can move to any **adjacent** square, then h 2 (n) gives the shortest solution 69 Inventing Heuristics Many good heuristics can be /

-array **adjacency** array Reference to edge object for **adjacent** vertices Null for non nonadjacent vertices The “old fashioned” version just has 0 for no edge and 1 for edge u v w ab 012 0 1 2 a uv w 01 2 b **DEPTH**-**FIRST** **SEARCH** 14 /the design of communication networks A spanning forest of a graph is a spanning subgraph that is a forest Graph Spanning tree **Depth**-**First** **Search** **Depth**-**first** **search** (DFS) is a general technique for traversing a graph A DFS traversal of a graph G Visits all the /

46 5 7 12 Un-rooted tree Tree rooted with root 1 15 Tree-Related Concepts The nodes **adjacent** to x and below x are called the children of x, and x is called their parents / 29 Deletion from a BST (pseudocode) Delete (datatype b, Tree T) 1.**Search** for b in tree T. If not found, return. 2.Call x the **first** node found to contain b 3.If x is a leaf, remove x and / 1 2 1 23 45 6 1 23 4 1 23 45 46 **Depth**/Height of Full Trees and Almost Complete Trees The height (or **depth** ) h of such trees is O(log n) Proof: In the case/

： v 2 becomes the new head in Q. 2> ： after enqueue a new vertex v into the Q. Let v r+1 be v. Note that u’s **adjacency** list is being scanned and u is just removed. neighbors Thus, And The rest, for i=1,…,r-1, remain unaffected. p12. Thm 22.5: (Correctness of BFS)/ 3. Forward edges( u, v ): v is a descendent of u, not a tree edge 4. Cross edges( u, v ): Other edges Example In a **depth**-**first** **search** of an undirected graph, every edge is either a tree edge or a back edge p26. 3/62/91/10 4/5 7/8 12/13 (a) y zs/

previously expanded state. This is called cycle checking, or multiple path checking. Why can’t we utilize this technique with **depth**-**first** **search**? If we modify **depth**-**first** **search** to do cycle checking what happens to space complexity? 76 Fahiem Bacchus, University of Toronto Cycle Checking Example 2 34 3 45/in the easier problem. 8-Puzzle moves. Can move a tile from square A to B if A is **adjacent** (left, right, above, below) to B and B is blank Can relax some of these conditions 1. can move from A to/

visited: mark the vertex as visited process the vertex insert **adjacent** unvisited vertices into collection Fundamentals of Python: From **First** Programs Through Data Structures97 Breadth-**First** and **Depth**-**First** Traversals (continued) To traverse all vertices (recursive version): traverseAll/ –If dfs has been used, we build a **depth**-**first** **search** tree –It is also possible to build a breadth-**first** **search** tree Fundamentals of Python: From **First** Programs Through Data Structures100 Spanning Trees and Forests A spanning/

Chapter 22: Elementary Graph Algorithms Overview: Definition of a graph Representation of graphs **adjacency** list matrix Elementary **search** algorithms breadth-**first** **search** (BFS) **depth**-**first** **search**(DFS) topological sort strongly connected components Notation Graph G(V,E) is a data structure defined by a set of vertices V a set of eges E |V| = # of vertices |/

found. At a leaf, backtrack to the lowest right child and repeat. 1 2 34 5 67 **Depth** **First** **Search** Example 2: DFS on directed graph. Start at some source vertex S. Find (or explore) the **first** vertex that is **adjacent** to S. Repeat with this vertex and explore the **first** vertex that is **adjacent** to it. When a vertex is found that has no unexplored vertices/

a goal state Expand node with smallest h(n) Like **depth** **first** Prefers to follow single path Backtracking Not optimal, but sometimes good Best **First** **Search** – Greedy **Search** (not bad) Best **First** **Search** Best **First** **Search** orders nodes on the OPEN list by increasing value of an/tour is a (Hamiltonian) circuit Variant 1: It is a connected second degree graph (each node has exactly two **adjacent** edges) Removing the connectivity constraint leads to h1: find the cheapest second degree graph from the given graph (with o/

the input graph is represented as an **adjacency**-matrix? How can it be determined that an undirected graph is a full two sided graph? What is the algorithm to determine the shortest path between two given nodes in a graph? What is its run time? **Depth**-**First**-**Search** (DFS) The DFS algorithm executes a **depth** **search** over the graph. The **search** starts each time at a different/

Basic Structure: –Variables: Graph nodes, Classes, Boolean vars –Domains: Colors, Time slots, Truth values –Constraints: No two **adjacent** nodes with same color, No two classes in same time, consistent, satisfying ass’t Problem Characteristics Values: –Finite? Infinite? /!Yes! No! Z=BZ=RZ=B Z=R Z=BZ=R Z=BZ=R Backtracking Issues CSP as **Search** –**Depth**-**first** **search** Maximum **depth** = # of variables –Continue until (partial/complete) assignment Consistent: return result Violates constraint -> backtrack to previous/

**Depth**-**first** **search** COMP171 Fall 2005 Graph / Slide 2 **Depth**-**First** **Search** (DFS) * DFS is another popular graph **search** strategy n Idea is similar to pre-order traversal (visit children **first**) * DFS can provide certain information about the graph that BFS cannot n It can tell / does DFS find valid paths? Yes. 2 4 3 5 1 7 6 9 8 0 source Graph / Slide 26 Time Complexity of DFS (Using **adjacency** list) * We never visited a vertex more than once * We had to examine all edges of the vertices n We know Σ vertex v degree/

COMP171 Fall 2006 Graph / Slide 2 **Depth**-**First** **Search** (DFS) * DFS is another popular graph **search** strategy n Idea is similar to pre-order traversal (visit node, then visit children recursively) * DFS can provide certain information about the/v - whenever DFS returns from a vertex v, we climb up in the tree from v to its parent Graph / Slide 27 Time Complexity of DFS (Using **adjacency** list) * We never visited a vertex more than once * We had to examine all edges of the vertices n We know Σ vertex v degree(v) =/

efficiency for both **search** and insertion/deletion … however, there are some ways Finding the minimum We begin with, fast insertion/deletion, and fast **search** for minimum value, as a **first** step Problem: / use cell & pointers as list? Actually, this is a good way Representing the **adjacency** relation by the pointers, to up, right child, and left child However, actually,/the parent, and delete the both children Skew would Grow **Search**/update time is linear in the **depth** of the target leaf They are fast when the tree/

side-paths that were not taken ›Uses a stack (instead of a queue in BFS) ›Permits an easy recursive implementation 3 **Depth** **First** **Search** Algorithm Recursive marking algorithm Initially every vertex is unmarked DFS(i: vertex) mark i; for each j **adjacent** to i do if j is unmarked then DFS(j) end{DFS} Marks all vertices reachable from i i j k DFS/

://www.csie.ntu.edu.tw/~hil/ 2010/1/8Depth-**first** search2 Outline **Depth**-**first** **search** on graph –Connected components –Topological sort –Strongly connected components 2010/1/8 **Depth**-**first** **search** 3 Graph The **adjacency** among a set of nodes 2010/1/8Depth-**first** search4 Examples 1 2 3 5 4 6 2010/1/8Depth-**first** search5 Notation 2010/1/8Depth-**first** search6 Examples 1 2 3 5 4 6 2010/1/

, …) This lecture: algorithms on graphs, in particular Representation of graphs Breadth-**First** **Search** (BFS) **Depth**-**First** **Search** (DFS) Testing bipartiteness Testing for (directed) cycles Ordering nodes in acyclic directed graphs Graph Algorithms How to represent graphs? Given an undirected graph G = (V,E) , how to represent it? **Adjacency** matrix: ith node is represented as ith row and ith column, edge between ith and jth nodes is/

having odd degree. One must start at one of the odd-degree vertices. 5/15 §6 Applications of **Depth**-**First** **Search** 2 6 9 8 3 7 1 12 4 10 5 11 Note: The path should be maintained as a linked list. For each **adjacency** list, maintain a pointer to the last edge scanned. T = O( |E| + |V| ) Note: The path should be/

were not taken ›Uses a stack (instead of a queue in BFS) ›Allows an easy recursive implementation 3/7/03Graph **Searching** - Lecture 206 **Depth** **First** **Search** Algorithm Recursive marking algorithm Initially every vertex is unmarked DFS(i: vertex) mark i; for each j **adjacent** to i do if j is unmarked then DFS(j) end{DFS} Marks all vertices reachable from i 3/7/03Graph/

there are no more **adjacent** to s white vertices in G, algorithm marks vertex s as black and performs some procedure under the vertex (e.g. prints data) Step 7: continue from step 2 until G has no more white vertices Graphs: **depth**-**first** **search**, sample Graphs: **depth**-**first** **search**, sample Graphs: **depth**-**first** **search**, sample on usage Graphs: **depth**-**first** **search**, sample on usage Graphs: **depth**-**first** **search**, sample on usage Graphs: **depth**-**first** **search** For given graph/

invoke scan(s) Procedure scan(s) mark and visit s for each neighbor w of s if the neighbor is not reached invoke scan(w) **Depth**-**First** **Search** with Stack Initialization: mark all vertices as unvisited, visit(s) while the stack is not empty: pop (v,w) if w is not visited/add edge (v,w) to tree T DepthFirst(w) Visit(v) mark v as visited Example **Depth** **first** traversal: 1, 2, 6, 3, 5, 4 the particular order is dependent on the order of nodes in the **adjacency** lists 1 4 **Adjacency** lists 1: 2, 3, 4 2: 6, 3, 1 3: 1, 2, 6/

-10 Visitation order for a) a **depth**-**first** **search**; b) a breadth-**first** **search** © 2006 Pearson Addison-Wesley. All rights reserved14 A-15 **Depth**-**First** **Search** **Depth**-**first** **search** (DFS) traversal –Proceeds along a path from v as deeply into the graph as possible before backing up –Does not completely specify the order in which it should visit the vertices **adjacent** to v –A last visited, **first** explored strategy © 2006 Pearson Addison-Wesley/

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/ 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/

we consider two cases Case 1: the **adjacent** siblings of v are 2-nodes Fusion operation: we merge v with an **adjacent** sibling w and move an entry from u/) is red } z recolor(z) Goodrich, Tamassia **Search** Trees46 Deletion To perform operation remove (k), we **first** execute the deletion algorithm for binary **search** trees Let v be the internal node removed, w the / $2, zig-zag = $2. Thus, cost for playing a node at **depth** d = $d. Imagine that we store rank(v) cyber-dollars at each node v of the splay tree/

As such, a tree leaf corresponds to a complete compound label (including all the problem variables). A **depth** **first** **search** in the tree, resorting to backtracking when a node corresponds to a dead end (unsatisfiability), corresponds to an /should be sparse and possessing no node with high degree. In the latter case, the distance to the farthest apart **adjacent** node dominates. The principle exploited by the heuristics, avoid irrelevant backtracking, is obtained, hopefully more efficiently, by constraint/

a variable. As such, a tree leaf corresponds to a complete compound label (including all the problem variables). A **depth** **first** **search** in the tree, resorting to backtracking when a node corresponds to a dead end (unsatisfiability), corresponds thus to an/should be sparse and possessing no node with high degree. In the latter case, the distance to the farthest apart **adjacent** node dominates. 2.The principle exploited by the heuristics, avoid irrelevant backtracking, is obtained, hopefully more efficiently, by /

done otherwise check if there is a path from any of the **adjacent** vertices repeat looking further and further from v Two different approaches to order of **searching**: breadth-**first** **search** (BFS), **depth**-**first** **search** (DFS) BFS VS DFS PATH FINDING Is there a path / determine isPath(a,k) Assume neighbours are chosen in alphabetical order D EPTH F IRST S EARCH Basic approach to **depth**-**first** **search**: visit and mark current vertex for each neighbour, traverse it recursively Notes: need a mechanism for "marking" vertices /

CSC401 -- Analysis of Algorithms 3-25 Deletion To perform operation remove (k), we **first** execute the deletion algorithm for binary **search** trees Let v be the internal node removed, w the external node removed, and r/an underflow at node v with parent u, we consider two cases Case 2: an **adjacent** sibling w of v is a 3-node or a 4-node –Transfer operation: 1/ $1, zig-zig = $2, zig-zag = $2. Thus, cost for playing a node at **depth** d = $d. Imagine that we store rank(v) cyber-dollars at each node v of the splay tree/

it runs out of time Idea: Try something like **depth** **first** **search**. Iterative Deepening A* (IDA*) **Search** **depth**-**first**, but to a fixed **depth**, as we did for Iterative Deepening if you dont find a solution, increase the **depth** tolerance and try again **depth** is measured in f(n) Analysis of Iterative / on “features” E.g., x1(n) = number of misplaced tiles E.g., x2(n) = number of goal-**adjacent**-pairs that are currently **adjacent** h(n) = w 1 x1(n) + w 2 x2(n) Weights could be learned again via repeated puzzle-solving/

5 7 12 Unrooted tree Tree rooted with root 1 CS 10315 Tree-Related Concepts The nodes **adjacent** to x and below x are called the children of x, and x is called their/ The **depth** of a node is the number of edges from the root to that node. The **depth** (or height) of a rooted tree is the **depth** of the lowest leaf **Depth** of node 10: 3 **Depth** of this/ from a BST (pseudocode) Delete(datatype b, Tree T) 1.**Search** for b in tree T. If not found, return. 2.Call x the **first** node found to contain b 3.If x is a leaf, remove/

= $1, zig-zig = $2, zig-zag = $2. Thus, cost for splaying a node at **depth** d = $d. Imagine that we store rank(v) & cyber-dollars at each node v of the splay/ underflow at node v with parent u, we consider two cases Case 2: an **adjacent** sibling w of v is a 3-node or a 4-node Transfer operation: 1/z) is red z = recolor(z); } 65 Deletion To perform operation remove (k), we **first** execute the deletion algorithm for binary **search** trees Let v be the internal node removed, w the external node removed, and r the sibling/

out of time How can we solve the memory problem for A* **search**? Idea: Try something like **depth** **first** **search**, but let’s not forget everything about the branches we have partially explored. Recursive Best-**First** **Search** (RBFS) Similar to DFS, but keeps track of the f-value of/ based on “features” E.g., x1(n) = number of misplaced tiles E.g., x2(n) = number of goal-**adjacent**-pairs that are currently **adjacent** h(n) = w1 x1(n) + w2 x2(n) Weights could be learned again via repeated puzzle-solving Try to identify/

Many traversal algorithms are known **Depth**-**First** **Search** (DFS) Visit nodes successors **first** Usually implemented by recursion Breadth-**First** **Search** (BFS) Nearest nodes visited **first** Implemented by a queue 18 **Depth**-**First** **Search** **first** visits all descendants of given node recursively/multiple successors 68 Node (vertex) Element of graph Can have name or value Keeps a list of **adjacent** nodes Edge Connection between two nodes Can be directed / undirected Can be weighted / unweighted /

some edges that are not 2D Paritioning Assume that the edge list for a given vertex is the column of the **adjacency** matrix Each block in the 2D partitioning contains partial edge lists Each processor has a frontier set of vertices, F, /true, it forms the neighbors and frontiers Severe load imbalance among the treads Scope for improvement Parallel **Depth** **First** **Search** Easy to parallelize Left subtree can be **searched** in parallel with the right subtree Statically assign a node to a processor – the whole subtree rooted/

Graphs John Edgar Understand graph terminology Implement graphs using **Adjacency** lists and **Adjacency** matrices Perform graph **searches** **Depth** **first** **search** Breadth **first** **search** Perform shortest-path algorithms Disjkstra’s algorithm A* algorithm 2 John/an estimate of the remaining cost to reach the target to direct its **search** 40 John Edgar The A* algorithm is similar to Dijkstra’s algorithm It performs a modified breadth **first** **search** and Uses a priority queue to select vertices The A* /

an underflow at node v with parent u, we consider two cases Case 2: an **adjacent** sibling w of v is a 3-node or a 4-node –Transfer operation: 1./zig = $1, zig-zig = $2, zig-zag = $2. Thus, cost for playing a node at **depth** d = $d. Imagine that we store rank(v) cyber-dollars at each node v of the splay tree (/ 31 64 3134 23 S0S0 S1S1 S2S2 S3S3 27 **Search** We **search** for a key x in a a skip list as follows: –We start at the **first** position of the top list –At the current position p, we compare /

= k + 1 } return -1 // not found } Algorithm 4.2.2 **Depth**-**First** **Search** This algorithm executes a **depth**-**first** **search** beginning at vertex start in a graph with vertices 1,..., n and outputs the vertices in the order in which they are visited. The graph is represented using **adjacency** lists; adj[i] is a reference to the **first** node in a linked list of nodes representing the vertices/

1 Undirected **Depth** **First** **Search** **Adjacency** Lists A: F C B G B: A C: A D: F E E: G F D F: A E D: G: E A: H: I: I: H: F A BCG DE HI 2 visit(A) (A, F) (A, C) (A, B) (A, G) Undirected **Depth** **First** **Search** F A BCG DE H Undiscovered Marked Finished Active I / Active I Stack visit(F) (F, A) (F, E) (F, D) E newly discovered E 5 visit(A) (A, F) (A, C) (A, B) (A, G) Undirected **Depth** **First** **Search** F A BCG DE H Undiscovered Marked Finished Active I Stack visit(F) (F, A) (F, E) (F, D) visit(E) (E, G) (E, F) (E, D) G newly/

iterations, the total running time is O(n2). So, with **adjacency** matrix, BFS is O(n2) independent of the number of edges m. With **adjacent** lists, BFS is O(n+m); if m=O(n2) like in a dense graph, O(n+m)=O(n2). **Depth**-**First** **Search** (DFS) DFS is another popular graph **search** strategy Idea is similar to pre-order traversal (visit node, then/

**first** Potential quality measures: Final In-degree Final Pagerank Crawl heuristic: Breadth **First** **Search** (BFS) Partial Indegree Partial Pagerank Random walk Measure of page quality we’ll define later in the course. BFS & Spam (Worst case scenario) Start Page Start Page BFS **depth**/set of algorithms and a java implementation Fundamental goal – maintain node **adjacency** lists in memory For this, compressing the **adjacency** lists is the critical component **Adjacency** lists The set of neighbors of a node Assume each URL /

Mar 17, 2009Graphs & Graph Algorithms 3) **Adjacency** Matrix Repr. Performance: boolean isEdge(Node n, Node m) O ( 1 ) List inEdges(Node n) O ( |V| ) List outEdges(Node n) O ( |V| ) Space: O ( |V| 2 ) always! [ 18 ] Claus Brabrand, ITU, Denmark Mar 17, 2009Graphs & Graph Algorithms Outline Graphs Graph Representations Krak Data Representation Breadth **First** **Search** (BFS) **Depth** **First** **Search** (DFS) Topological Sorting Exercises Project (part 1/

out of time –How can we solve the memory problem for A* **search**? Idea: Try something like **depth** **first** **search**, but let’s not forget everything about the branches we have partially explored. 34 Recursive Best-**First** **Search** (RBFS) Similar to DFS, but keeps track of the f-value of/function based on “features” –E.g., x1(n) = number of misplaced tiles –E.g., x2(n) = number of goal-**adjacent**-pairs that are currently **adjacent** –h(n) = w 1 x1(n) + w 2 x2(n) Weights could be learned again via repeated puzzle-solving Try /

(B log B n) Proof: Recall that B-TREE-**SEARCH** function is called **first** and costs O(log n) by using binary **search**. Then, B-TREE-INSERT starts by visiting a node and proceeds upward. At most one node is visited per level/**depth** and only visited nodes can be splitted. A most one/The join procedure The key k 1 to be deleted is in a leaf x with exactly B elements. Let y be a node that is an “**adjacent** brother” of x. Suppose that y has exactly B elements. Procedure: Remove the key k 1. Let k 2 be the key that /

**search** strategies Uninformed **search** strategies use only the information available in the problem definition Breadth-**first** **search** Uniform-cost **search** **Depth**-**first** **search** **Depth**-limited **search** Iterative deepening **search** 21 CSC 8520 Spring 2010. Paula Matuszek Implementation: general tree **search** 22 CSC 8520 Spring 2010. Paula Matuszek Breadth-**first** **search**/the shortest solution If the rules are relaxed so that a tile can move to any **adjacent** square, then h 2 (n) gives the shortest solution 73 CSC 8520 Spring 2010./

Week 10 - Friday What did we talk about last time? Graph representations **Adjacency** matrix **Adjacency** lists **Depth** **first** **search** Base case Tells recursion when to stop Can have multiple base cases Have to have at least one /in a tree is called a traversal There are four traversals that we are interested in Preorder Postorder Inorder Level Order The **first** three are **depth** **first**, the last is breadth **first** 29 14 9.. 19. 26.. 34 31... 29 14 34 9 9 19 31 26 .. 9... 26 19 14.. 31. 34 29 29/

10 April 6, 2009 CSE 3101 7 Representations: Undirected Graphs **Adjacency** List **Adjacency** Matrix CSE 3101 8 Representations: Directed Graphs **Adjacency** List **Adjacency** Matrix CSE 3101 9 Breadth-**First** **Search** Goal: To recover the shortest paths from a source node s/of graph, e.g., –Topological sorts –Detection of cycles –Extraction of strongly connected components CSE 3101 47 **Depth**-**First** **Search** Explore every edge, starting from different vertices if necessary. As soon as vertex discovered, explore from it. Keep/

knowledge better? Example Graph S A B C F **Adjacency** List (setf (get s children) (a b)) (setf (get a children) (s b f)) (setf (get b children) (s a c)) (setf (get c children) (b f)) (setf (get f children) (a c)) Any Path **Search** Algorithms **Depth** **First** **Search** Hill Climbing Best **First** **Search** Breadth **First** **Search** Beam **Search** **Depth** **First** **Search** Add root to queue of partial paths Until queue is/

Fundamentals of AI13 Formal Definition for Example Initial state In(Arad) Successor function Maps state to **adjacent** cities {,, } Goal test state is In(Bucharest) Path cost Sum of distances along driving / **Search** Strategies Breadth-**first** **search** Uniform-cost **search** **Depth**-**first** **search** **Depth**-limited **search** Iterative deepening **search** Bidirectional **search** COSC 159 - Fundamentals of AI43 Breadth **First** **Search** Fringe is organized in FIFO queue 1 234 56 1 2 COSC 159 - Fundamentals of AI44 Breadth **First** **Search** /

state to generate new states: expand find all **adjacent** cities Choose one of the **adjacent** states, and expand the state from there Continue until you find solution **Search** strategy: Which states do you expand **first**? For 8-puzzle: each node is a configuration/Time complexity: Still need to explore all nodes: O(b d ) **Depth**-**first** can get lucky and find long path quickly **Depth**-**first** can get “lost” down a really long path **Depth**-**First** **Search** Complete No – if tree is infinite, could spend forever exploring one branch/

Ads by Google