160 likes | 192 Views
Introduction to Graph Theory. Lecture 16: Graph Searching Algorithms. Before getting started …. The study materials are from “Algorithms in C” by Robert Sedgewick. The chapter is available online. Analogy to Exploring a Maze. We can view graph searching as exploring a maze
E N D
Introduction to Graph Theory Lecture 16: Graph Searching Algorithms
Before getting started … • The study materials are from “Algorithms in C” by Robert Sedgewick. • The chapter is available online.
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.
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
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
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)
Example • So, with the same example, how many different ways are there to traverse the maze?
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.
Pseudo-Code (1) • With an adjacency matrix
Pseudo-Code (1) • With an adjacency-list What is the trace using this adjacency list?
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
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?
DFS Trees • The result of DFS search is a set of trees • The trees can be represented in a number of ways: Parent link Order of visiting Two links For every edge
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 down link back link parent link
(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 (1st visit) • Parent: do nothing (2nd visit) • Down: recursive search completed for w (2nd visit) • Back: recursive search in progress for w (1st visit)
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.