graph theory, dfs & bfs kelly choi 08-07-2006. 08-07-20062 what is a graph? a set of vertices...

24
Graph Theory, Graph Theory, DFS & BFS DFS & BFS Kelly Choi Kelly Choi 08-07-2006 08-07-2006

Upload: grace-mckenzie

Post on 16-Dec-2015

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Graph Theory, DFS & BFS Kelly Choi 08-07-2006. 08-07-20062 What is a graph? A set of vertices and edges –Directed/Undirected –Weighted/Unweighted –Cyclic/Acyclic

Graph Theory, Graph Theory, DFS & BFS DFS & BFS

Kelly ChoiKelly Choi

08-07-200608-07-2006

Page 2: Graph Theory, DFS & BFS Kelly Choi 08-07-2006. 08-07-20062 What is a graph? A set of vertices and edges –Directed/Undirected –Weighted/Unweighted –Cyclic/Acyclic

08-07-2006 2

What is a graph?What is a graph?• A set of vertices and edges

– Directed/Undirected– Weighted/Unweighted– Cyclic/Acyclic

vertex

edge

Page 3: Graph Theory, DFS & BFS Kelly Choi 08-07-2006. 08-07-20062 What is a graph? A set of vertices and edges –Directed/Undirected –Weighted/Unweighted –Cyclic/Acyclic

08-07-2006 3

Representation of GraphsRepresentation of Graphs• Adjacency Matrix

– A V x V array, with matrix[i][j] storing whether there is an edge between the ith vertex and the jth vertex

• Adjacency Linked List– One linked list per vertex, each storing

directly reachable vertices

• Edge List

Page 4: Graph Theory, DFS & BFS Kelly Choi 08-07-2006. 08-07-20062 What is a graph? A set of vertices and edges –Directed/Undirected –Weighted/Unweighted –Cyclic/Acyclic

08-07-2006 4

Representation of GraphsRepresentation of GraphsAdjacency Matrix

Adjacency Linked List

Edge List

Memory Storage

O(V2) O(V+E) O(V+E)

Check whether (u,v) is an edge

O(1) O(deg(u)) O(deg(u))

Find all adjacent vertices of a vertex u

O(V) O(deg(u)) O(deg(u))

deg(u): the number of edges connecting vertex u

Page 5: Graph Theory, DFS & BFS Kelly Choi 08-07-2006. 08-07-20062 What is a graph? A set of vertices and edges –Directed/Undirected –Weighted/Unweighted –Cyclic/Acyclic

08-07-2006 5

Graph SearchingGraph Searching• Why do we do graph searching?

What do we search for?• What information can we find from

graph searching?• How do we search the graph? Do we

need to visit all vertices? In what order?

Page 6: Graph Theory, DFS & BFS Kelly Choi 08-07-2006. 08-07-20062 What is a graph? A set of vertices and edges –Directed/Undirected –Weighted/Unweighted –Cyclic/Acyclic

08-07-2006 6

Depth-First Search (DFS)Depth-First Search (DFS)• Strategy: Go as far as you can (if you

have not visit there), otherwise, go back and try another way

Page 7: Graph Theory, DFS & BFS Kelly Choi 08-07-2006. 08-07-20062 What is a graph? A set of vertices and edges –Directed/Undirected –Weighted/Unweighted –Cyclic/Acyclic

08-07-2006 7

ImplementationImplementationDFS (vertex u) {

mark u as visitedfor each vertex v directly reachable from u

if v is unvisitedDFS (v)

}

• Initially all vertices are marked as unvisited

Page 8: Graph Theory, DFS & BFS Kelly Choi 08-07-2006. 08-07-20062 What is a graph? A set of vertices and edges –Directed/Undirected –Weighted/Unweighted –Cyclic/Acyclic

08-07-2006 8

Topological SortTopological Sort• Topological order:

A numbering of the vertices of a directed acyclic graph such that every edge from a vertex numbered i to a vertex numbered j satisfies i<j

• Topological Sort: Finding the topological order of a directed acyclic graph

Page 9: Graph Theory, DFS & BFS Kelly Choi 08-07-2006. 08-07-20062 What is a graph? A set of vertices and edges –Directed/Undirected –Weighted/Unweighted –Cyclic/Acyclic

08-07-2006 9

Example: Teacher’s ProblemExample: Teacher’s Problem• Emily wants to distribute candies to N students one by one, with a rule that if student A is teased by B, A can receive candy before B.

• Given lists of students teased by each students, find a possible sequence to give the candies

Page 10: Graph Theory, DFS & BFS Kelly Choi 08-07-2006. 08-07-20062 What is a graph? A set of vertices and edges –Directed/Undirected –Weighted/Unweighted –Cyclic/Acyclic

08-07-2006 10

Shortest Paths?Shortest Paths?• Given vertices s and t, if we try to

find a path from s to t by DFS, the first path found may not be the shortest one.

• How can I find a shortest path?

• Let’s first review what we have done

Page 11: Graph Theory, DFS & BFS Kelly Choi 08-07-2006. 08-07-20062 What is a graph? A set of vertices and edges –Directed/Undirected –Weighted/Unweighted –Cyclic/Acyclic

08-07-2006 11

An Idea of What We DidAn Idea of What We Did1. Start by visiting a certain vertex2. Vertices fall into 3 categories:

– Unvisited– Visited– Visited & Dead (All reachable vertices

from these vertices are visited)

Page 12: Graph Theory, DFS & BFS Kelly Choi 08-07-2006. 08-07-20062 What is a graph? A set of vertices and edges –Directed/Undirected –Weighted/Unweighted –Cyclic/Acyclic

08-07-2006 12

An Idea of What We DidAn Idea of What We Did3. Find unvisited vertices by

expanding your path from visited (but not dead) vertices.

• In DFS, we choose the most recently visited vertex to expand.

• Are there any other strategies?

Page 13: Graph Theory, DFS & BFS Kelly Choi 08-07-2006. 08-07-20062 What is a graph? A set of vertices and edges –Directed/Undirected –Weighted/Unweighted –Cyclic/Acyclic

08-07-2006 13

Breadth-First Search (BFS)Breadth-First Search (BFS)• Instead of going as far as possible,

BFS tries to search all paths.• BFS makes use of a queue to store

visited (but not dead) vertices, expanding the path from the earliest visited vertices.

Page 14: Graph Theory, DFS & BFS Kelly Choi 08-07-2006. 08-07-20062 What is a graph? A set of vertices and edges –Directed/Undirected –Weighted/Unweighted –Cyclic/Acyclic

08-07-2006 14

1

4

3

25

6

Simulation of BFSSimulation of BFS• Queue: 1 4 3 5 2 6

Page 15: Graph Theory, DFS & BFS Kelly Choi 08-07-2006. 08-07-20062 What is a graph? A set of vertices and edges –Directed/Undirected –Weighted/Unweighted –Cyclic/Acyclic

08-07-2006 15

ImplementationImplementationwhile queue Q not empty

dequeue the first vertex u from Qfor each vertex v directly reachable from u

if v is unvisitedenqueue v to Qmark v as visited

• Initially all vertices except the start vertex are marked as unvisited and the queue contains the start vertex only

Page 16: Graph Theory, DFS & BFS Kelly Choi 08-07-2006. 08-07-20062 What is a graph? A set of vertices and edges –Directed/Undirected –Weighted/Unweighted –Cyclic/Acyclic

08-07-2006 16

There is more…There is more…• Other Graph Searching Algorithms:

– Bidirectional search (BDS)– Iterative deepening search (IDS)

Page 17: Graph Theory, DFS & BFS Kelly Choi 08-07-2006. 08-07-20062 What is a graph? A set of vertices and edges –Directed/Undirected –Weighted/Unweighted –Cyclic/Acyclic

08-07-2006 17

AdvantagesAdvantages• Guarantee shortest paths for

unweighted graphs• Use queue instead of recursive

functions – Avoiding stack overflow

Page 18: Graph Theory, DFS & BFS Kelly Choi 08-07-2006. 08-07-20062 What is a graph? A set of vertices and edges –Directed/Undirected –Weighted/Unweighted –Cyclic/Acyclic

08-07-2006 18

Flood FillFlood Fill• An algorithm that determines the

area connected to a given node in a multi-dimensional array

• Start BFS/DFS from the given node, counting the total number of nodes visited

• Example: Squareland (HKOI 2006)

Page 19: Graph Theory, DFS & BFS Kelly Choi 08-07-2006. 08-07-20062 What is a graph? A set of vertices and edges –Directed/Undirected –Weighted/Unweighted –Cyclic/Acyclic

08-07-2006 19

Graph ModelingGraph Modeling• Conversion of a problem into a graph

problem• Essential in solving most graph

problems

Page 20: Graph Theory, DFS & BFS Kelly Choi 08-07-2006. 08-07-20062 What is a graph? A set of vertices and edges –Directed/Undirected –Weighted/Unweighted –Cyclic/Acyclic

08-07-2006 20

Basics of graph modelingBasics of graph modeling• Identify the vertices and the edges• Identify the objective of the problem• State the objective in graph terms• Implementation:

– construct the graph from the input instance– run the suitable graph algorithms on the graph– convert the output to the required format

(cx, 2004)

Page 21: Graph Theory, DFS & BFS Kelly Choi 08-07-2006. 08-07-20062 What is a graph? A set of vertices and edges –Directed/Undirected –Weighted/Unweighted –Cyclic/Acyclic

08-07-2006 21

Other Topics in Graph Other Topics in Graph TheoryTheory

• Cut Vertices & Cut Edges• Euler Path/Circuit & Hamilton

Path/Circuit• Planarity

Page 22: Graph Theory, DFS & BFS Kelly Choi 08-07-2006. 08-07-20062 What is a graph? A set of vertices and edges –Directed/Undirected –Weighted/Unweighted –Cyclic/Acyclic

08-07-2006 22

Cut Vertices & Cut EdgesCut Vertices & Cut Edges• What is a cut vertex?

– A cut vertex is a type of graph vertex, the removal of which causes an increase in the number of connected components

• What is a cut edge?

Page 23: Graph Theory, DFS & BFS Kelly Choi 08-07-2006. 08-07-20062 What is a graph? A set of vertices and edges –Directed/Undirected –Weighted/Unweighted –Cyclic/Acyclic

08-07-2006 23

Euler Path & Hamilton PathEuler Path & Hamilton Path• An Euler path is a path in a graph

which visits each edge exactly once• A Hamilton path is a path in an

undirected graph which visits each vertex exactly once.

Page 24: Graph Theory, DFS & BFS Kelly Choi 08-07-2006. 08-07-20062 What is a graph? A set of vertices and edges –Directed/Undirected –Weighted/Unweighted –Cyclic/Acyclic

08-07-2006 24

PlanarityPlanarity• A planar graph is a graph that can be

drawn so that no edges intersect

• K5 and K3,3 are non-planar graphs