63
1 Graph Algorithms Lecture 09 Asst. Prof. Dr. Bunyarit Uyyanonvara Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology Sirindhorn International Institute of Technology Thammasat University http://www.siit.tu.ac.th/bunyarit [email protected] 02 5013505 X 2005 ITS033 – Programming & Algorithms

1 Graph Algorithms Lecture 09 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

Embed Size (px)

Citation preview

Page 1: 1 Graph Algorithms Lecture 09 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

1

Graph AlgorithmsLecture 09

Asst. Prof. Dr. Bunyarit UyyanonvaraAsst. Prof. Dr. Bunyarit UyyanonvaraIT Program, Image and Vision Computing Lab.

School of Information and Computer Technology

Sirindhorn International Institute of Technology

Thammasat Universityhttp://www.siit.tu.ac.th/bunyarit

[email protected] 5013505 X 2005

ITS033 – Programming & Algorithms

Page 2: 1 Graph Algorithms Lecture 09 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

2

ITS033Topic 01Topic 01 -- Problems & Algorithmic Problem SolvingProblems & Algorithmic Problem SolvingTopic 02Topic 02 – Algorithm Representation & Efficiency Analysis – Algorithm Representation & Efficiency AnalysisTopic 03Topic 03 - State Space of a problem - State Space of a problemTopic 04Topic 04 - Brute Force Algorithm - Brute Force AlgorithmTopic 05Topic 05 - Divide and Conquer - Divide and ConquerTopic 06Topic 06 -- Decrease and ConquerDecrease and ConquerTopic 07Topic 07 - Dynamics Programming - Dynamics ProgrammingTopic 08 - Transform and ConquerTopic 09 - Graph AlgorithmsTopic 09 - Graph AlgorithmsTopic 10Topic 10 - Minimum Spanning Tree - Minimum Spanning TreeTopic 11Topic 11 - Shortest Path Problem - Shortest Path ProblemTopic 12Topic 12 - Coping with the Limitations of Algorithms Power - Coping with the Limitations of Algorithms Power

http://www.siit.tu.ac.th/bunyarit/its033.phphttp://www.siit.tu.ac.th/bunyarit/its033.phpand and http://www.vcharkarn.com/vlesson/showlesson.php?lessonid=7http://www.vcharkarn.com/vlesson/showlesson.php?lessonid=7

Midterm

Page 3: 1 Graph Algorithms Lecture 09 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

3

Outline and Reading

Graphs () Definition Applications Terminology Properties ADT

Graphs representation Edge list structure Adjacency list structure Adjacency matrix structure

Page 4: 1 Graph Algorithms Lecture 09 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

4

Graph Algorithms: Introduction

Lecture 09.1

Asst. Prof. Dr. Bunyarit UyyanonvaraAsst. Prof. Dr. Bunyarit UyyanonvaraIT Program, Image and Vision Computing Lab.

School of Information and Computer Technology

Sirindhorn International Institute of Technology

Thammasat Universityhttp://www.siit.tu.ac.th/bunyarit

[email protected] 5013505 X 2005

ITS033 – Programming & Algorithms

Page 5: 1 Graph Algorithms Lecture 09 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

5

Graphs are made of dots connected by lines.

Graphs are very powerful tools for creating mathematical models of a wide variety of situations.

Graph Theory is the branch of mathematics that involves the study of graphs.

Graph theory has been used to find the best way to route and schedule airplanes and invent a secret code that no one can crack.

Basic TerminologyGraph

Page 6: 1 Graph Algorithms Lecture 09 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

6

Basic TerminologyGraph

• A graph is a pair of sets (V, E)

• where • V is the vertex-set and • E the edge-set is a family

of pairs (possibly directed) of V.

Page 7: 1 Graph Algorithms Lecture 09 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

7

Graph

A graph is a pair A graph is a pair ((V, EV, E)), where, where VV is a set of nodes, called vertices is a set of nodes, called vertices EE is a collection of pairs of vertices, called edges is a collection of pairs of vertices, called edges Vertices and edges are positions and store elementsVertices and edges are positions and store elements

Example:Example: A vertex represents an airport and stores the three-letter airport codeA vertex represents an airport and stores the three-letter airport code An edge represents a flight route between two airports and stores the An edge represents a flight route between two airports and stores the

mileage of the routemileage of the route

ORDPVD

MIA

DFW

SFO

LAX

LGA

HNL

849

802

13871743

1843

10991120

1233

337

2555

142

Page 8: 1 Graph Algorithms Lecture 09 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

8

Basic TerminologyVertex

• " Vertex" is a synonym for a node of a graph, i.e.,

• one of the points on which the graph is defined and which may be connected by graph edges

Page 9: 1 Graph Algorithms Lecture 09 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

9

Edge Types Directed edgeDirected edge

ordered pair of verticesordered pair of vertices ( (uu,,vv)) first vertex first vertex uu is the origin is the origin second vertex second vertex vv is the destination is the destination e.g., a flighte.g., a flight

Undirected edgeUndirected edge unordered pair of verticesunordered pair of vertices ( (uu,,vv)) e.g., a flight routee.g., a flight route

Directed graphDirected graph all the edges are directedall the edges are directed e.g., route networke.g., route network

Undirected graphUndirected graph all the edges are undirectedall the edges are undirected e.g., flight networke.g., flight network

ORD PVDflightAA 1206

ORD PVD849miles

Page 10: 1 Graph Algorithms Lecture 09 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

10

Basic Terminology: Degree

• The degree of a vertex of G is the number of edges incident to it.

• What is a degree of each vertex for a complete graph with 7 vertices ?

Page 11: 1 Graph Algorithms Lecture 09 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

11

Basic Terminology

Indegree The number of inward directed graph edges from a given graph vertex in a directed graph.

Outdegree The number of outward directed graph edges from a given graph vertex in a directed graph.

Page 12: 1 Graph Algorithms Lecture 09 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

12

Path A path in a graph

is a connecting set of (V,E) from an original vertex to a destination vertex.

Page 13: 1 Graph Algorithms Lecture 09 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

13

P1

Terminology (cont.)

PathPath sequence of alternating sequence of alternating

vertices and edges vertices and edges begins with a vertexbegins with a vertex ends with a vertexends with a vertex each edge is preceded and each edge is preceded and

followed by its endpointsfollowed by its endpoints

Simple pathSimple path path such that all its vertices path such that all its vertices

and edges are distinctand edges are distinct

ExamplesExamples PP11=(V,b,X,h,Z)=(V,b,X,h,Z) is a simple path is a simple path PP22=(U,c,W,e,X,g,Y,f,W,d,V)=(U,c,W,e,X,g,Y,f,W,d,V) is a is a

path that is not simplepath that is not simple

XU

V

W

Z

Y

a

c

b

e

d

f

g

hP2

Page 14: 1 Graph Algorithms Lecture 09 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

14

Terminology (cont.) CycleCycle

circular sequence of alternating circular sequence of alternating vertices and edges vertices and edges

each edge is preceded and each edge is preceded and followed by its endpointsfollowed by its endpoints

Simple cycleSimple cycle cycle such that all its vertices cycle such that all its vertices

and edges are distinctand edges are distinct

ExamplesExamples CC11=(V,b,X,g,Y,f,W,c,U,a,=(V,b,X,g,Y,f,W,c,U,a,)) is a is a

simple cyclesimple cycle CC22=(U,c,W,e,X,g,Y,f,W,d,V,a,=(U,c,W,e,X,g,Y,f,W,d,V,a,))

is a cycle that is not simpleis a cycle that is not simple

C1

XU

V

W

Z

Y

a

c

b

e

d

f

g

hC2

Page 15: 1 Graph Algorithms Lecture 09 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

15

Basic Terminology

• A graph is called complete when every pair of vertices is an edge.

• The complete graph has

• undirected edges, (binomial coefficient)

Page 16: 1 Graph Algorithms Lecture 09 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

16

Connected Graph A connected graph

has a path from every vertex to every other

Page 17: 1 Graph Algorithms Lecture 09 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

17

Connectivity

A graph is connected if there is a path between every pair of vertices

A connected component of a graph G is a maximal connected subgraph of G

Connected graph

Non connected graph with two connected components

Page 18: 1 Graph Algorithms Lecture 09 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

18

Graph Variations A A weighted graphweighted graph

associates associates weights with weights with either the edges either the edges or the verticesor the vertices E.g., a road E.g., a road

map: edges map: edges might be might be weighted w/ weighted w/ distandistan

Page 19: 1 Graph Algorithms Lecture 09 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

19

Subgraphs

A subgraph S of a graph G is a graph such that The vertices of S are a

subset of the vertices of G The edges of S are a

subset of the edges of G

A spanning subgraph of G is a subgraph that contains all the vertices of G

Subgraph

Spanning subgraph

Page 20: 1 Graph Algorithms Lecture 09 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

20

Trees and Forests

A (free) tree is an undirected graph T such that T is connected T has no cyclesThis definition of tree is

different from the one of a rooted tree

A forest is an undirected graph without cycles

The connected components of a forest are trees

Tree

Forest

Page 21: 1 Graph Algorithms Lecture 09 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

21

Spanning Trees and Forests A spanning tree of a

connected graph is a spanning subgraph that is a tree

A spanning tree is not unique unless the graph is a tree

Spanning trees have applications to the design of communication networks

A spanning forest of a graph is a spanning subgraph that is a forest

Graph

Spanning tree

Page 22: 1 Graph Algorithms Lecture 09 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

22

John

DavidPaul

brown.edu

cox.net

cs.brown.edu

att.netqwest.net

math.brown.edu

cslab1bcslab1a

Applications

Electronic circuits Printed circuit board Integrated circuit

Computer networks Local area network Internet Web

Databases Entity-relationship diagram

Page 23: 1 Graph Algorithms Lecture 09 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

23

Applications

Transportation networks Highway network Flight network

Page 24: 1 Graph Algorithms Lecture 09 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

24

Terminology

GraphGraph VertexVertex Edge, Directed Edge, undirected edgeEdge, Directed Edge, undirected edge Path, Simple PathPath, Simple Path Cycle, Simple CycleCycle, Simple Cycle Degree, Indegree, OutdegreeDegree, Indegree, Outdegree

Complete GraphComplete Graph Connected GraphConnected Graph Weighted GraphWeighted Graph Directed GraphDirected Graph Undierected graphUndierected graph

SubgraphSubgraph Spanning treeSpanning tree

Page 25: 1 Graph Algorithms Lecture 09 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

25

Graph Algorithms: Representation

Lecture 09.2

Asst. Prof. Dr. Bunyarit UyyanonvaraAsst. Prof. Dr. Bunyarit UyyanonvaraIT Program, Image and Vision Computing Lab.

School of Information and Computer Technology

Sirindhorn International Institute of Technology

Thammasat Universityhttp://www.siit.tu.ac.th/bunyarit

[email protected] 5013505 X 2005

ITS033 – Programming & Algorithms

Page 26: 1 Graph Algorithms Lecture 09 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

26

Representing Graphs with Adjacency Matrix Assume V = {1, 2, Assume V = {1, 2, ……, , nn}}

An An adjacency matrixadjacency matrix represents the graph as a represents the graph as a n n x x nn matrix matrix A:A: A[A[ii, , jj] ] = 1 if edge (= 1 if edge (ii, , jj) ) E (or weight of edge) E (or weight of edge)

= 0 if edge (= 0 if edge (ii, , jj) ) E E

Page 27: 1 Graph Algorithms Lecture 09 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

27

Graphs: Adjacency Matrix Example:

1

2 4

3

a

d

b c

1 2 3 4

1

2

3 ??4

fromfrom

toto

Page 28: 1 Graph Algorithms Lecture 09 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

28

Graphs: Adjacency Matrix Example:

1

2 4

3

a

d

b c

1 2 3 4

1 0 1 1 0

2 0 0 1 0

3 0 0 0 0

4 0 0 1 0

fromfrom

toto

Page 29: 1 Graph Algorithms Lecture 09 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

29

An entry in row i and column j corresponds to the edge e = (v,, vj). Its value is the weight of the edge, or -1 if the edge does not exist.

Adjacency Matrix

(a)

D

A

C

E

B fromfrom

toto

Page 30: 1 Graph Algorithms Lecture 09 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

30

An entry in row i and column j corresponds to the edge e = (v,, vj). Its value is the weight of the edge, or -1 if the edge does not exist.

Adjacency Matrix - weighted

(b)D

B

A

E

C

4

2

7

3

6

4

1

2

fromfrom

toto

Page 31: 1 Graph Algorithms Lecture 09 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

31

Adjacency List Adjacency list: for each vertex v V, store a list of vertices

adjacent to v Example:

Adj[1] = {2,3} Adj[2] = {3} Adj[3] = {} Adj[4] = {3}

Variation: can also keep a list of edges coming into vertex

1

2 4

3

Page 32: 1 Graph Algorithms Lecture 09 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

32

Adjacency Lists

( a )

D

A

C

E

B

A

E

D

C

B

Vert ices Set o f N eigh b o rs

B 1 C 1

C 1

B 1

D 1B 1

A

E

D

C

B

Vert ices Set o f N eigh b o rs( b )

D

B

A

E

C

4

2 7

3

6

4

1 2

C 1

C 7B 4

A 2

B 3

E 4

E 2

D 6

Page 33: 1 Graph Algorithms Lecture 09 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

33

Graph Searching Given: a graph G = (V, E), directed or undirected

Goal: methodically explore every vertex and every edge

Ultimately: build a tree on the graph Pick a vertex as the root Choose certain edges to produce a tree Note: might also build a forest if graph is not connected

Page 34: 1 Graph Algorithms Lecture 09 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

34

Graph Algorithms: Depth First Search

Lecture 09.3

Asst. Prof. Dr. Bunyarit UyyanonvaraAsst. Prof. Dr. Bunyarit UyyanonvaraIT Program, Image and Vision Computing Lab.

School of Information and Computer Technology

Sirindhorn International Institute of Technology

Thammasat Universityhttp://www.siit.tu.ac.th/bunyarit

[email protected] 5013505 X 2005

ITS033 – Programming & Algorithms

DB

A

C

E

Page 35: 1 Graph Algorithms Lecture 09 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

35

Outline and Reading

Depth-first search Algorithm Example Properties Analysis

Applications of DFS Path finding Cycle finding

Page 36: 1 Graph Algorithms Lecture 09 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

36

Depth-First Search

Depth-first search (DFS) is an algorithm for traversing or searching a tree, tree structure, or graph.

Intuitively, one starts at the root (selecting some node as the root in the graph case) and explores as far as possible along each branch before backtracking.

Page 37: 1 Graph Algorithms Lecture 09 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

37

Depth-First Search

Formally, DFS is an uninformed search that progresses by expanding the first child node of the search tree that appears and thus going deeper and deeper until a goal node is found, or until it hits a node that has no children.

Then the search backtracks, returning to the most recent node it hadn't finished exploring.

Page 38: 1 Graph Algorithms Lecture 09 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

38

Depth-First Search

Depth-first search (DFS) is a general technique for traversing a graph

A DFS traversal of a graph G Visits all the vertices

and edges of G Determines whether

G is connected Computes the

connected components of G

Computes a spanning forest of G

Page 39: 1 Graph Algorithms Lecture 09 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

39

Depth-First Search

DFS on a graph with n vertices and m edges takes O(nm ) time

DFS can be further extended to solve other graph problems Find and report a path between two given

vertices Find a cycle in the graph

Depth-first search is to graphs what Euler tour is to binary trees

Page 40: 1 Graph Algorithms Lecture 09 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

40

DFS Algorithm The algorithm uses a mechanism

for setting and getting “labels” of vertices and edges

Algorithm DFS(G, v)Input graph G and a start vertex v of G Output labeling of the edges of G in the connected component of v as discovery

edges and back edges

setLabel(v, VISITED)for all e G.incidentEdges(v)

if getLabel(e) UNEXPLOREDw opposite(v,e)if getLabel(w) UNEXPLORED

setLabel(e, DISCOVERY)DFS(G, w)

elsesetLabel(e, BACK)

Page 41: 1 Graph Algorithms Lecture 09 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

41

Example DB

A

C

E

DB

A

C

E

DB

A

C

E

discovery edge

back edge

A visited vertex

A unexplored vertex

unexplored edge

Page 42: 1 Graph Algorithms Lecture 09 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

42

Example (cont.)

DB

A

C

E

DB

A

C

E

DB

A

C

E

DB

A

C

E

Page 43: 1 Graph Algorithms Lecture 09 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

43

DFS and Maze Traversal

The DFS algorithm is similar to a classic strategy for exploring a maze We mark each

intersection, corner and dead end (vertex) visited

We mark each corridor (edge ) traversed

We keep track of the path back to the entrance (start vertex) by means of a rope (recursion stack)

Page 44: 1 Graph Algorithms Lecture 09 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

44

Properties of DFS

Property 1DFS(G, v) visits all the vertices and edges in the connected component of v

Property 2The discovery edges labeled by DFS(G, v) form a spanning tree of the connected component of v

DB

A

C

E

Page 45: 1 Graph Algorithms Lecture 09 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

45

Analysis of DFS

Setting/getting a vertex/edge label takes O(1) time Each vertex is labeled twice

once as UNEXPLORED once as VISITED

Each edge is labeled twice once as UNEXPLORED once as DISCOVERY or BACK

Method incidentEdges is called once for each vertex

DFS runs in O(n m) time provided the graph is represented by the adjacency list structure Recall that v deg(v) 2m

Page 46: 1 Graph Algorithms Lecture 09 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

46

Path Finding

We can specialize the DFS algorithm to find a path between two given vertices u and z using the template method pattern

We call DFS(G, u) with u as the start vertex

We use a stack S to keep track of the path between the start vertex and the current vertex

As soon as destination vertex z is encountered, we return the path as the contents of the stack

Algorithm pathDFS(G, v, z)setLabel(v, VISITED)S.push(v)if v z

return S.elements()for all e G.incidentEdges(v)

if getLabel(e) UNEXPLORED

w opposite(v,e)if getLabel(w)

UNEXPLORED setLabel(e, DISCOVERY)S.push(e)pathDFS(G, w, z)S.pop(e)

else setLabel(e, BACK)

S.pop(v)

Page 47: 1 Graph Algorithms Lecture 09 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

47

Cycle Finding

We can specialize the DFS algorithm to find a simple cycle using the template method pattern

We use a stack S to keep track of the path between the start vertex and the current vertex

As soon as a back edge (v, w) is encountered, we return the cycle as the portion of the stack from the top to vertex w

Algorithm cycleDFS(G, v, z)setLabel(v, VISITED)S.push(v)for all e G.incidentEdges(v)

if getLabel(e) UNEXPLOREDw opposite(v,e)S.push(e)if getLabel(w) UNEXPLORED

setLabel(e, DISCOVERY)pathDFS(G, w, z)S.pop(e)

elseT new empty stackrepeat

o S.pop()T.push(o)

until o wreturn T.elements()

S.pop(v)

Page 48: 1 Graph Algorithms Lecture 09 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

48

Graph Algorithms: Breadth First Search

Lecture 09.4

Asst. Prof. Dr. Bunyarit UyyanonvaraAsst. Prof. Dr. Bunyarit UyyanonvaraIT Program, Image and Vision Computing Lab.

School of Information and Computer Technology

Sirindhorn International Institute of Technology

Thammasat Universityhttp://www.siit.tu.ac.th/bunyarit

[email protected] 5013505 X 2005

ITS033 – Programming & Algorithms

CB

A

E

D

L0

L1

FL2

Page 49: 1 Graph Algorithms Lecture 09 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

49

Outline and Reading

Breadth-first search Algorithm Example Properties Analysis Applications

DFS vs. BFS Comparison of applications Comparison of edge labels

Page 50: 1 Graph Algorithms Lecture 09 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

50

Breadth-First Search

Breadth-first search (BFS) is a general technique for traversing a graph

A BFS traversal of a graph G Visits all the vertices and edges of G Determines whether G is connected Computes the connected components of G Computes a spanning forest of G

Page 51: 1 Graph Algorithms Lecture 09 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

51

Breadth-First Search

BFS on a graph with n vertices and m edges takes O(nm ) time

BFS can be further extended to solve other graph problems Find and report a path with the minimum number

of edges between two given vertices Find a simple cycle, if there is one

Page 52: 1 Graph Algorithms Lecture 09 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

52

BFS Algorithm

The algorithm uses a mechanism for setting and getting “labels” of vertices and edges

Algorithm BFS(G, s)L0 new empty sequenceL0.insertLast(s)setLabel(s, VISITED)i 0 while Li.isEmpty()

Li 1 new empty sequence for all v Li.elements()

for all e G.incidentEdges(v) if getLabel(e) UNEXPLORED

w opposite(v,e)if getLabel(w)

UNEXPLOREDsetLabel(e, DISCOVERY)setLabel(w, VISITED)Li 1.insertLast(w)

elsesetLabel(e, CROSS)

i i 1

Algorithm BFS(G)Input graph GOutput labeling of the edges

and partition of the vertices of G

for all u G.vertices()setLabel(u, UNEXPLORED)

for all e G.edges()setLabel(e, UNEXPLORED)

for all v G.vertices()if getLabel(v)

UNEXPLORED

BFS(G, v)

Page 53: 1 Graph Algorithms Lecture 09 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

53

ExampleCB

A

E

D

discovery edge

cross edge

A visited vertex

A unexplored vertex

unexplored edge

L0

L1

F

CB

A

E

D

L0

L1

F

CB

A

E

D

L0L1

F

Page 54: 1 Graph Algorithms Lecture 09 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

54

Example (cont.)

CB

A

E

D

L0

L1

F

CB

A

E

D

L0

L1

FL2

CB

A

E

D

L0

L1

FL2

CB

A

E

D

L0

L1

FL2

Page 55: 1 Graph Algorithms Lecture 09 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

55

Example (cont.)

CB

A

E

D

L0

L1

FL2

CB

A

E

D

L0

L1

FL2

CB

A

E

D

L0

L1

FL2

Page 56: 1 Graph Algorithms Lecture 09 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

56

Properties

NotationGs: connected component of s

Property 1BFS(G, s) visits all the vertices and edges of Gs

Property 2The discovery edges labeled by BFS(G, s) form a spanning tree Ts of Gs

Property 3For each vertex v in Li

The path of Ts from s to v has i edges Every path from s to v in Gs has at

least i edges

CB

A

E

D

L0

L1

FL2

CB

A

E

D

F

Page 57: 1 Graph Algorithms Lecture 09 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

57

Analysis

Setting/getting a vertex/edge label takes O(1) time Each vertex is labeled twice

once as UNEXPLORED once as VISITED

Each edge is labeled twice once as UNEXPLORED once as DISCOVERY or CROSS

Each vertex is inserted once into a sequence Li Method incidentEdges is called once for each vertex BFS runs in O(n m) time provided the graph is

represented by the adjacency list structure Recall that v deg(v) 2m

Page 58: 1 Graph Algorithms Lecture 09 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

58

Applications

Using the template method pattern, we can specialize the BFS traversal of a graph G to solve the following problems in O(n m) time Compute the connected components of G Compute a spanning forest of G Find a simple cycle in G, or report that G is a

forest Given two vertices of G, find a path in G

between them with the minimum number of edges, or report that no such path exists

Page 59: 1 Graph Algorithms Lecture 09 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

59

DFS vs. BFS

CB

A

E

D

L0

L1

FL2

CB

A

E

D

F

DFS BFS

Applications DFS BFSSpanning forest, connected components, paths, cycles

Shortest paths

Biconnected components

Page 60: 1 Graph Algorithms Lecture 09 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

60

DFS vs. BFS (cont.)

Back edge (v,w) w is an ancestor of v

in the tree of discovery edges

Cross edge (v,w) w is in the same level

as v or in the next level in the tree of discovery edges

CB

A

E

D

L0

L1

FL2

CB

A

E

D

F

DFS BFS

Page 61: 1 Graph Algorithms Lecture 09 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

61

Applications

Space complexity of DFS is much lower than BFS (breadth-first search). It also lends itself much better to heuristic methods of choosing a likely-looking branch.

Time complexity of both algorithms are proportional to the number of vertices plus the number of edges in the graphs they traverse (O(|V| + |E|)).

Page 62: 1 Graph Algorithms Lecture 09 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

62

ITS033Topic 01Topic 01 -- Problems & Algorithmic Problem SolvingProblems & Algorithmic Problem SolvingTopic 02Topic 02 – Algorithm Representation & Efficiency Analysis – Algorithm Representation & Efficiency AnalysisTopic 03Topic 03 - State Space of a problem - State Space of a problemTopic 04Topic 04 - Brute Force Algorithm - Brute Force AlgorithmTopic 05Topic 05 - Divide and Conquer - Divide and ConquerTopic 06Topic 06 -- Decrease and ConquerDecrease and ConquerTopic 07Topic 07 - Dynamics Programming - Dynamics ProgrammingTopic 08 - Transform and ConquerTopic 09 - Graph AlgorithmsTopic 09 - Graph AlgorithmsTopic 10Topic 10 - Minimum Spanning Tree - Minimum Spanning TreeTopic 11Topic 11 - Shortest Path Problem - Shortest Path ProblemTopic 12Topic 12 - Coping with the Limitations of Algorithms Power - Coping with the Limitations of Algorithms Power

http://www.siit.tu.ac.th/bunyarit/its033.phphttp://www.siit.tu.ac.th/bunyarit/its033.phpand and http://www.vcharkarn.com/vlesson/showlesson.php?lessonid=7http://www.vcharkarn.com/vlesson/showlesson.php?lessonid=7

Midterm

Page 63: 1 Graph Algorithms Lecture 09 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology

63

End of Chapter 9

Thank you!