39 graph traversals and algorithms
play

39: Graph Traversals and Algorithms Chris Wyatt Electrical and - PowerPoint PPT Presentation

ECE 2574 Introduction to Data Structures and Algorithms 39: Graph Traversals and Algorithms Chris Wyatt Electrical and Computer Engineering Virginia Tech Traversals and Searching Traversals and Searching Depth-First Search (DFS)


  1. ECE 2574 Introduction to Data Structures and Algorithms 39: Graph Traversals and Algorithms Chris Wyatt Electrical and Computer Engineering Virginia Tech

  2. Traversals and Searching Traversals and Searching Depth-First Search (DFS) Breadth-First Search (BFS) Best-First Search Weighted Graphs A* Search Introduction to Graph Algorithms

  3. Depth-First Traversal Given an initial vertex V DFS(V) mark V as visited for each unvisited vertex U adjacent to V DFS(U) How can we implement this using recursion? How can we store the unvisited vertices? How fast can we mark and test visited? What order should the adjacent vertices be visited?

  4. Stack-based DFS Given an initial vertex V DFS(V) mark V as visited for each unvisited vertex U adjacent to V push(U) while(stack not empty) pop -> W mark W as visited for each unvisited vertex U adjacent to W push(U)

  5. Depth-First Traversal: example DFS(b) c d i b h l a j e g f k

  6. Breadth-First Traversal Given an initial vertex V BFS(V) mark V as visited for each unvisited vertex U adjacent to V enqueue(U) while(queue not empty) dequeue -> W mark W as visited for each unvisited vertex U adjacent to W enqueue(U)

  7. Breadth-First Traversal: example BFS(b) c d i b h l a j e g f k

  8. Graph Search Problems Given a graph rooted at some vertex R with a goal G, searching the graph for G is a common task. In some cases the path is important example: N-puzzle problem In others it is not example: constraint satisfaction problems

  9. Weighted Graphs In many cases the edges have a cost or weight associated with them (distance for example). The performance of Graph Search can then be analyzed along the following lines Is the solution optimal ? Is the solution complete (if the goal exists it is found)?

  10. Best-First Search DFS and BFS are called uninformed because they simply expand nodes (into the stack or queue) in the same or arbitrary order. Informed search algorithms expand nodes according to a criteria. Example: Best-first (greedy) search expands the nodes based on the cost of the edge. Similar to DFS with the stack replaced by a priority queue (heap)

  11. Best-First Search: example Root at b, goal is a 2 c 4 d i b 3 1 3 3 h l 4 a 2 j e 2 5 1 6 g f 1 k

  12. A* Search A classic algorithm that can ensure optimality and completeness is called A-star (A*). A* uses a heuristic to help select the next vertex to expand: h(V) is the heuristic for vertex V. To implement use Best-First Search with the priority f(V) = g(V) + h(V), where g is the path cost from the root Example: N-puzzle problem

  13. Some other important graph algorithms and problems Topological Sorting Minimal Spanning Tree Shortest Path (Dijkstra's Algorithm), a simplification of A* A very famous graph problem is the Traveling Salesperson Problem.

  14. Example Write a simple program to represent the graph below using an adjacency list. a c e f d b After constructing the graph, print out all vertices connected to vertex a (or print none exist) using depth first search.

  15. Next Actions and Reminders Read CH pp. 671-681 on STL Containers Program 5 is due 12/11. Please fill out the SPOT survey!

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend