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

39 graph traversals and algorithms
SMART_READER_LITE
LIVE PREVIEW

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)


slide-1
SLIDE 1

ECE 2574 Introduction to Data Structures and Algorithms 39: Graph Traversals and Algorithms

Chris Wyatt Electrical and Computer Engineering Virginia Tech

slide-2
SLIDE 2

Traversals and Searching

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

Weighted Graphs

slide-3
SLIDE 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?

slide-4
SLIDE 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)

slide-5
SLIDE 5

Depth-First Traversal: example

DFS(b)

a b c d e f g h i j k l

slide-6
SLIDE 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)

slide-7
SLIDE 7

Breadth-First Traversal: example

BFS(b)

a b c d e f g h i j k l

slide-8
SLIDE 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

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

slide-10
SLIDE 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)

slide-11
SLIDE 11

Best-First Search: example

Root at b, goal is a

a b c d e f g h i j k l 4 3 1 3 2 4 5 2 1 2 3 1 6

slide-12
SLIDE 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

slide-13
SLIDE 13

Some other important graph algorithms and problems

Topological Sorting Minimal Spanning Tree Shortest Path (Dijkstra's Algorithm), a simplification

  • f A*

A very famous graph problem is the Traveling Salesperson Problem.

slide-14
SLIDE 14

Example

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

a b c d e f

slide-15
SLIDE 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!