Download presentation
Presentation is loading. Please wait.
Published byVirginia Glenn Modified over 9 years ago
1
Introduction to Graph Theory Lecture 16: Graph Searching Algorithms
2
Before getting started … The study materials are from “Algorithms in C” by Robert Sedgewick. The chapter is available online.
3
Analogy to Exploring a Maze We can view graph searching as exploring a maze Passages in the maze -> edges in the graph Intersections in the maze -> vertices in the graph The big question here is --- how to explore the maze systematically to ensure complete coverage.
4
Exploring a Maze When exploring a maze, we want to Traverse every part of the maze Avoid retracing our steps Let’s construct our maze first Every intersection has a light A passage has two doors (with windows), one at each end By opening the door at one end, we can decide if the intersection at the other end is lit
5
Maze Exploration Strategy The goal is to turn on all the lights, which are initially off, and open all the doors, which are initially off. What should we do to meet this goal? Here we assume that we unroll a ball of string behind us. So we can always find our way out
6
Tremaux Exploration At the current intersection: (i) if no closed door, jump to step (iii). Else open any closed door to any passage and leave it open (ii) if the intersection at the other end is lit, go to step (i), Else follow that passage and light the intersection at the other end (iii) if all doors are open, check is at the starting point. If not, go back down the passge that brought you here (rolling the string back up), and go to step (i)
7
Example So, with the same example, how many different ways are there to traverse the maze?
8
Depth-First Search Tremaus exploration leads us right to DFS Visit a vertex, and mark it as having been visited Recursive visit all the unvisited vertices adjacent to it We can represent the lights in the intersections using a vertex-indexed array If a vertex is visited, the corresponding entry is marked.
9
Pseudo-Code (1) With an adjacency matrix
10
Pseudo-Code (1) With an adjacency-list What is the trace using this adjacency list?
11
To Handle Disconnected Graph We need a wrapper function which finds an unmarked vertex (starting vertex), and triggers the recursive search starting from that vertex
12
Cost of Running DFS What is the time complexity if using adjacency matrix? What is the time complexity if using adjacency list? Which data structure is a better choice?
13
DFS Trees The result of DFS search is a set of trees The trees can be represented in a number of ways: Order of visiting Parent link Two links For every edge
14
Types of Tree Links If traversing an edge from v->w vertices, we can classify the graph edge by looking at the pre and st data structure. Tree edges Tree link if w is unmarked A parent link if st[w] is v Back edges Back link if pre[w]<pre[v] Down link if pre[w]>pre[v] tree link parent link back link down link
15
(cont) Each graph edge is represented either as One tree link and one parent link, OR One down link and one back link Meaning of the links: Tree: trigger recursive search (1 st visit) Parent: do nothing (2 nd visit) Down: recursive search completed for w (2 nd visit) Back: recursive search in progress for w (1 st visit)
16
Some Remarks This representation will provide us a basis for understanding numerous important graph- processing algorithm. DFS corresponds to pre-order tree traversal. It is also possible to do post-ordering, if we assign the number after the recursive call.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.