16
Introduction to Graph Theory Lecture 16: Graph Searching Algorithms

Introduction to Graph Theory Lecture 16: Graph Searching Algorithms

Embed Size (px)

Citation preview

Page 1: Introduction to Graph Theory Lecture 16: Graph Searching Algorithms

Introduction to Graph Theory

Lecture 16: Graph Searching Algorithms

Page 2: 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.

Page 3: Introduction to Graph Theory Lecture 16: Graph Searching Algorithms

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.

Page 4: Introduction to Graph Theory Lecture 16: Graph Searching Algorithms

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

Page 5: Introduction to Graph Theory Lecture 16: Graph Searching Algorithms

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

Page 6: Introduction to Graph Theory Lecture 16: Graph Searching Algorithms

Tremaux Exploration

At the current intersection: (i) if no closed door, jump to step (iii). Else open a

ny closed door to any passage and leave it open (ii) if the intersection at the other end is lit, go to st

ep (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)

Page 7: Introduction to Graph Theory Lecture 16: Graph Searching Algorithms

Example

So, with the same example, how many different ways are there to traverse the maze?

Page 8: Introduction to Graph Theory Lecture 16: Graph Searching Algorithms

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 t

o it We can represent the lights in the intersectio

ns using a vertex-indexed array If a vertex is visited, the corresponding entry is ma

rked.

Page 9: Introduction to Graph Theory Lecture 16: Graph Searching Algorithms

Pseudo-Code (1)

With an adjacency matrix

Page 10: Introduction to Graph Theory Lecture 16: Graph Searching Algorithms

Pseudo-Code (1) With an adjacency-list

What is the trace using this adjacency list?

Page 11: Introduction to Graph Theory Lecture 16: Graph Searching Algorithms

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

Page 12: Introduction to Graph Theory Lecture 16: Graph Searching Algorithms

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?

Page 13: Introduction to Graph Theory Lecture 16: Graph Searching Algorithms

DFS Trees The result of DFS search is a set of trees The trees can be represented in a number of

ways:

Order of visitingParent link

Two links For every edge

Page 14: Introduction to Graph Theory Lecture 16: Graph Searching Algorithms

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

Page 15: Introduction to Graph Theory Lecture 16: Graph Searching Algorithms

(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)

Page 16: Introduction to Graph Theory Lecture 16: Graph Searching Algorithms

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.