+ All Categories
Home > Documents > Analysis of Algorithms - VNUvietanh/courses/cs3329/6_Graphs_part1.pdf · Graphs 3 Graph A graph is...

Analysis of Algorithms - VNUvietanh/courses/cs3329/6_Graphs_part1.pdf · Graphs 3 Graph A graph is...

Date post: 09-May-2020
Category:
Upload: others
View: 6 times
Download: 0 times
Share this document with a friend
57
Graphs 1 Graphs ORD DFW SFO LAX
Transcript
Page 1: Analysis of Algorithms - VNUvietanh/courses/cs3329/6_Graphs_part1.pdf · Graphs 3 Graph A graph is a pair (V, E), where V is a set of nodes, called vertices E is a collection of pairs

Graphs 1

Graphs

ORD

DFW

SFO

LAX

Page 2: Analysis of Algorithms - VNUvietanh/courses/cs3329/6_Graphs_part1.pdf · Graphs 3 Graph A graph is a pair (V, E), where V is a set of nodes, called vertices E is a collection of pairs

Graphs 2

Outline and Reading

Graphs (§12.1) Definition

Applications

Terminology

Properties

ADT

Data structures for graphs (§12.2) Edge list structure

Adjacency list structure

Adjacency matrix structure

Page 3: Analysis of Algorithms - VNUvietanh/courses/cs3329/6_Graphs_part1.pdf · Graphs 3 Graph A graph is a pair (V, E), where V is a set of nodes, called vertices E is a collection of pairs

Graphs 3

GraphA graph is a pair (V, E), where V is a set of nodes, called vertices

E is a collection of pairs of vertices, called edges

Vertices and edges are positions and store elements

Example: A vertex represents an airport and stores the three-letter airport code

An edge represents a flight route between two airports and stores the mileage of the route

ORDPVD

MIADFW

SFO

LAX

LGA

HNL

Page 4: Analysis of Algorithms - VNUvietanh/courses/cs3329/6_Graphs_part1.pdf · Graphs 3 Graph A graph is a pair (V, E), where V is a set of nodes, called vertices E is a collection of pairs

Graphs 4

Edge TypesDirected edge ordered pair of vertices (u,v)

first vertex u is the origin second vertex v is the destination

e.g., a flight

Undirected edge unordered pair of vertices (u,v)

e.g., a flight route

Weighted edgeDirected graph (Digraph) all the edges are directed e.g., route network

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

Weighted graph all the edges are weighted

ORD DFWflight

AA 1206

ORD DFW

u v

(u,v)

flightroute

802 miles

802 miles

Page 5: Analysis of Algorithms - VNUvietanh/courses/cs3329/6_Graphs_part1.pdf · Graphs 3 Graph A graph is a pair (V, E), where V is a set of nodes, called vertices E is a collection of pairs

Graphs 5

John

DavidPaul

brown.edu

cox.net

cs.brown.edu

att.net

qwest.net

math.brown.edu

cslab1bcslab1a

ApplicationsElectronic circuits

Printed circuit board

Integrated circuit

Transportation networks

Highway network

Flight network

Computer networks

Local area network

Internet

Web

Databases

Entity-relationship diagram

Page 6: Analysis of Algorithms - VNUvietanh/courses/cs3329/6_Graphs_part1.pdf · Graphs 3 Graph A graph is a pair (V, E), where V is a set of nodes, called vertices E is a collection of pairs

Graphs 6

TerminologyEnd vertices (or endpoints) of an edge U and V are the endpoints of a

Edges incident on a vertex a, d, and b are incident on V

Adjacent vertices U and V are adjacent

Degree of a vertex X has degree 5

Parallel edges h and i are parallel edges

Self-loop j is a self-loop

XU

V

W

Z

Y

a

c

b

e

d

f

g

h

i

j

Page 7: Analysis of Algorithms - VNUvietanh/courses/cs3329/6_Graphs_part1.pdf · Graphs 3 Graph A graph is a pair (V, E), where V is a set of nodes, called vertices E is a collection of pairs

Graphs 7

Terminology (cont.)

Outgoing edges of a vertex h and b are the outgoing

edges of X

Incoming edges of a vertex e, g, and i are incoming

edges of X

In-degree of a vertex X has in-degree 3

Out-degree of a vertex X has out-degree 2

X

V

W

Z

Y

b

e

d

f

g

h

i

j

Page 8: Analysis of Algorithms - VNUvietanh/courses/cs3329/6_Graphs_part1.pdf · Graphs 3 Graph A graph is a pair (V, E), where V is a set of nodes, called vertices E is a collection of pairs

Graphs 8

P1

Terminology (cont.)

Path sequence of alternating

vertices and edges

begins with a vertex

ends with a vertex

each edge is preceded and followed by its endpoints

Simple path path such that all its vertices

and edges are distinct

Examples P1=(V,b,X,h,Z) is a simple path

P2=(U,c,W,e,X,g,Y,f,W,d,V) is a path that is not simple

XU

V

W

Z

Y

a

c

b

e

d

f

g

hP2

Page 9: Analysis of Algorithms - VNUvietanh/courses/cs3329/6_Graphs_part1.pdf · Graphs 3 Graph A graph is a pair (V, E), where V is a set of nodes, called vertices E is a collection of pairs

Graphs 9

Terminology (cont.)Cycle

circular sequence of alternating vertices and edges

each edge is preceded and followed by its endpoints

Simple cycle

cycle such that all its vertices and edges are distinct

Examples

C1=(V,b,X,g,Y,f,W,c,U,a,) is a simple cycle

C2=(U,c,W,e,X,g,Y,f,W,d,V,a,)is a cycle that is not simple

C1

XU

V

W

Z

Y

a

c

b

e

d

f

g

hC2

Page 10: Analysis of Algorithms - VNUvietanh/courses/cs3329/6_Graphs_part1.pdf · Graphs 3 Graph A graph is a pair (V, E), where V is a set of nodes, called vertices E is a collection of pairs

Graphs 10

Exercise on Terminology# of vertices?# of edges?What type of the graph is it? ANS: undirected weighted graphShow the end vertices of the edge with largest weightShow the vertices of smallest degree and largest degreeShow the edges incident to the vertices in the above questionEnumerate pairs of adjacent verticesIdentify the shortest simple path from HNL to PVDIdentify the simple cycle with the most edges

ORDPVD

MIADFW

SFO

LAX

LGA

HNL

Page 11: Analysis of Algorithms - VNUvietanh/courses/cs3329/6_Graphs_part1.pdf · Graphs 3 Graph A graph is a pair (V, E), where V is a set of nodes, called vertices E is a collection of pairs

Graphs 11

Properties of Undirected Graphs

Notationn number of vertices

m number of edges

deg(v) degree of vertex v

Property 1 – Total degree

Sv deg(v) = ?

Property 2 – Total number of edges

In an undirected graph with no self-loops and no multiple edges

m Upper bound?

Lower bound? m

Example

n = ?

m = ?

deg(v) = ?

A graph with given number of vertices (4) and maximum

number of edges

Page 12: Analysis of Algorithms - VNUvietanh/courses/cs3329/6_Graphs_part1.pdf · Graphs 3 Graph A graph is a pair (V, E), where V is a set of nodes, called vertices E is a collection of pairs

Graphs 12

Properties of Undirected Graphs

Notationn number of vertices

m number of edges

deg(v) degree of vertex v

Property 1 – Total degree

Sv deg(v) = 2m

Proof: each edge is counted twice

Property 2 – Total number of edges

In an undirected graph with no self-loops and no multiple edges

m n (n - 1)/2

Proof: each vertex has degree at most (n - 1)

Example

n = 4

m = 6

deg(v) = 3

A graph with given number of vertices (4) and maximum

number of edges

Page 13: Analysis of Algorithms - VNUvietanh/courses/cs3329/6_Graphs_part1.pdf · Graphs 3 Graph A graph is a pair (V, E), where V is a set of nodes, called vertices E is a collection of pairs

Graphs 13

Properties of Directed GraphsNotation

n number of vertices

m number of edges

deg(v) degree of vertex v

Property 1 – Total in-degree and out-degree

Sv in-deg(v) = ?

Sv out-deg(v) = ?

Property 2 – Total number of edges

In an directed graph with no self-loops and no multiple edges

m Upper bound?

Lower bound? m

Example

n = ?

m = ?

deg(v) = ?

A graph with given number of vertices (4) and maximum

number of edges

Page 14: Analysis of Algorithms - VNUvietanh/courses/cs3329/6_Graphs_part1.pdf · Graphs 3 Graph A graph is a pair (V, E), where V is a set of nodes, called vertices E is a collection of pairs

Graphs 14

Properties of Directed GraphsNotation

n number of vertices

m number of edges

deg(v) degree of vertex v

Property 1 – Total in-degree and out-degree

Sv in-deg(v) = m

Sv out-deg(v) = m

Property 2 – Total number of edges

In an directed graph with no self-loops and no multiple edges

m n (n - 1)

Example

n = 4

m = 12

deg(v) = 6

A graph with given number of vertices (4) and maximum

number of edges

Page 15: Analysis of Algorithms - VNUvietanh/courses/cs3329/6_Graphs_part1.pdf · Graphs 3 Graph A graph is a pair (V, E), where V is a set of nodes, called vertices E is a collection of pairs

Graphs 15

Main Methods of the Graph ADT

Vertices and edges are positions store elements

Accessor methods aVertex() incidentEdges(v) adjacentVertices(v) degree(v) endVertices(e) opposite(v, e) areAdjacent(v, w)

isDirected(e) origin(e) destination(e)

Update methods insertVertex(o) insertEdge(v, w, o) insertDirectedEdge(v, w, o) removeVertex(v) removeEdge(e)

Generic methods numVertices() numEdges() vertices() edges()

Specific to directed edges

Page 16: Analysis of Algorithms - VNUvietanh/courses/cs3329/6_Graphs_part1.pdf · Graphs 3 Graph A graph is a pair (V, E), where V is a set of nodes, called vertices E is a collection of pairs

Graphs 16

Exercise on ADTaVertex()incidentEdges(ORD)adjacentVertices(ORD)degree(ORD)endVertices((LGA,MIA))opposite(DFW, (DFW,LGA))areAdjacent(DFW, SFO)

isDirected((DFW,LGA))origin ((DFW,LGA))destination((DFW,LGA)))

ORDPVD

MIADFW

SFO

LAX

LGA

HNL

insertVertex(IAH)insertEdge(MIA, PVD, 1200)removeVertex(ORD)removeEdge((DFW,ORD))

Page 17: Analysis of Algorithms - VNUvietanh/courses/cs3329/6_Graphs_part1.pdf · Graphs 3 Graph A graph is a pair (V, E), where V is a set of nodes, called vertices E is a collection of pairs

Graphs 17

Edge List Structure

An edge list can be stored in a sequence, a vector, a list or a dictionary such as a hash table

ORDPVD

MIADFW

LGA

(ORD, PVD)

(ORD, DFW)

(LGA, PVD)

(LGA, MIA)

(DFW, LGA)

849

802

142

1099

1387

(DFW, MIA) 1120

Edge List

ORD

LGA

PVD

DFW

MIA

Vertex Sequence

Page 18: Analysis of Algorithms - VNUvietanh/courses/cs3329/6_Graphs_part1.pdf · Graphs 3 Graph A graph is a pair (V, E), where V is a set of nodes, called vertices E is a collection of pairs

Graphs 18

Asymptotic PerformanceVertices and edges are positions

store elements

Accessor methods Accessing vertex sequence

aVertex() O(1)

degree(v) O(1)

Accessing edge list endVertices(e) O(1)

opposite(v, e) O(1)

isDirected(e) O(1)

origin(e) O(1)

destination(e) O(1)

Generic methods numVertices() O(1)

numEdges() O(1)

vertices() O(n)

edges() O(m)

(ORD, PVD)

(ORD, DFW)

(LGA, PVD)

(LGA, MIA)

(DFW, LGA)

849

802

142

1099

1387

(DFW, MIA) 1120

Edge List

ORD

LGA

PVD

DFW

MIA

Vertex Sequence

2

3

2

3

2

Degree

False

False

False

False

False

False

Weight Directed

Specific to directed edges

Page 19: Analysis of Algorithms - VNUvietanh/courses/cs3329/6_Graphs_part1.pdf · Graphs 3 Graph A graph is a pair (V, E), where V is a set of nodes, called vertices E is a collection of pairs

Graphs 19

Asymptotic Performance of Edge List Structure

n vertices, m edges

no parallel edges

no self-loops

Bounds are “big-Oh”

EdgeList

Space n + m

incidentEdges(v)

adjacentVertices(v)m

areAdjacent (v, w) m

insertVertex(o) 1

insertEdge(v, w, o) 1

removeVertex(v) m

removeEdge(e) 1

(ORD, PVD)

(ORD, DFW)

(LGA, PVD)

(LGA, MIA)

(DFW, LGA)

849

802

142

1099

1387

(DFW, MIA) 1120

Edge List

ORD

LGA

PVD

DFW

MIA

Vertex Sequence

2

3

2

3

2

False

False

False

False

False

False

Weight Directed Degree

Page 20: Analysis of Algorithms - VNUvietanh/courses/cs3329/6_Graphs_part1.pdf · Graphs 3 Graph A graph is a pair (V, E), where V is a set of nodes, called vertices E is a collection of pairs

Graphs 20

Edge List Structure• Vertex object

• element

• reference to position in vertex sequence

• Edge object• element

• origin vertex object

• destination vertex object

• reference to position in edge sequence

• Vertex sequence• sequence of vertex

objects

• Edge sequence• sequence of edge objects

v

u

w

a c

b

a

zd

u v w z

b c d

Page 21: Analysis of Algorithms - VNUvietanh/courses/cs3329/6_Graphs_part1.pdf · Graphs 3 Graph A graph is a pair (V, E), where V is a set of nodes, called vertices E is a collection of pairs

Graphs 21

Adjacency List Structure

ORD PVD

MIADFW

LGA

(ORD, PVD)

(ORD, DFW)

(LGA, PVD)

(LGA, MIA)

(DFW, LGA)

849

802

142

1099

1387

(DFW, MIA)1120

Edge List

ORD

LGA

PVD

DFW

MIA

(ORD, PVD)

Adjacency List

(ORD, DFW)

(LGA, PVD) (LGA, MIA)

(PVD, ORD) (PVD, LGA)

(LGA, DFW)

(DFW, ORD) (DFW, LGA) (DFW, MIA)

(MIA, LGA) (MIA, DFW)

Page 22: Analysis of Algorithms - VNUvietanh/courses/cs3329/6_Graphs_part1.pdf · Graphs 3 Graph A graph is a pair (V, E), where V is a set of nodes, called vertices E is a collection of pairs

Graphs 22

Asymptotic Performance of Adjacency List Structure

• n vertices, m edges

• no parallel edges

• no self-loops

• Bounds are “big-Oh”

AdjacencyList

Space n + m

incidentEdges(v)

adjacentVertices(v)deg(v)

areAdjacent (v, w) min(deg(v), deg(w))

insertVertex(o) 1

insertEdge(v, w, o) 1

removeVertex(v) deg(v)*

removeEdge(e) 1*

(ORD, PVD)

(ORD, DFW)

(LGA, PVD)

(LGA, MIA)

(DFW, LGA)

849

802

142

1099

1387

(DFW, MIA)1120

Edge List

ORD

LGA

PVD

DFW

MIA

(ORD, PVD)

Adjacency List

(ORD, DFW)

(LGA, PVD) (LGA, MIA)

(PVD, ORD) (PVD, LGA)

(LGA, DFW)

(DFW, ORD) (DFW, LGA) (DFW, MIA)

(MIA, LGA) (MIA, DFW)

Page 23: Analysis of Algorithms - VNUvietanh/courses/cs3329/6_Graphs_part1.pdf · Graphs 3 Graph A graph is a pair (V, E), where V is a set of nodes, called vertices E is a collection of pairs

Graphs 23

Adjacency List Structure• Edge list structure

• Incidence sequence for each vertex

• sequence of references to edge objects of incident edges

• Augmented edge objects

• references to associated positions in incidence sequences of end vertices

u

v

w

a b

a

u v w

b

Page 24: Analysis of Algorithms - VNUvietanh/courses/cs3329/6_Graphs_part1.pdf · Graphs 3 Graph A graph is a pair (V, E), where V is a set of nodes, called vertices E is a collection of pairs

Graphs 24

Adjacency MatrixStructure

0 1 2 3 4

0 0 0 1 1 0

1 0 0 1 1 1

2 1 1 0 0 0

3 1 1 0 0 1

4 0 1 0 1 0

0:ORD 2:PVD

4:MIA3:DFW

1:LGA

(ORD, PVD)

(ORD, DFW)

(LGA, PVD)

(LGA, MIA)

(DFW, LGA)

849

802

142

1099

1387

(DFW, MIA)1120

Edge List

ORD

LGA

PVD

DFW

MIA

Vertex Sequence

0

1

2

3

4

Page 25: Analysis of Algorithms - VNUvietanh/courses/cs3329/6_Graphs_part1.pdf · Graphs 3 Graph A graph is a pair (V, E), where V is a set of nodes, called vertices E is a collection of pairs

Graphs 25

Asymptotic Performance of Adjacency Matrix Structure• n vertices, m edges

• no parallel edges

• no self-loops

• Bounds are “big-Oh”

Adjacency Matrix

Space n2

incidentEdges(v)

adjacentVertices(v)n

areAdjacent (v, w) 1

insertVertex(o) n2

insertEdge(v, w, o) 1

removeVertex(v) n2

removeEdge(e) 1

0 1 2 3 4

0 0 0 1 1 0

1 0 0 1 1 1

2 1 1 0 0 0

3 1 1 0 0 1

4 0 1 0 1 0

ORD

LGA

PVD

DFW

MIA

Vertex Sequence

0

1

2

3

4

(ORD, PVD)

(ORD, DFW)

(LGA, PVD)

(LGA, MIA)

(DFW, LGA)

849

802

142

1099

1387

(DFW, MIA)1120

Edge List

Adjacency Matrix

Page 26: Analysis of Algorithms - VNUvietanh/courses/cs3329/6_Graphs_part1.pdf · Graphs 3 Graph A graph is a pair (V, E), where V is a set of nodes, called vertices E is a collection of pairs

Graphs 26

Adjacency Matrix Structure• Edge list structure

• Augmented vertex objects

• Integer key (index) associated with vertex

• 2D-array adjacency array

• Reference to edge object for adjacent vertices

• Null for non nonadjacent vertices

• The “old fashioned” version just has 0 for no edge and 1 for edge

u

v

w

a b

0 1 2

0

1

2 a

u v w0 1 2

b

Page 27: Analysis of Algorithms - VNUvietanh/courses/cs3329/6_Graphs_part1.pdf · Graphs 3 Graph A graph is a pair (V, E), where V is a set of nodes, called vertices E is a collection of pairs

Graphs 27

Asymptotic Performance• n vertices, m edges

• no parallel edges

• no self-loops

• Bounds are “big-Oh”

EdgeList

AdjacencyList

Adjacency Matrix

Space n + m n + m n2

incidentEdges(v)

adjacentVertices(v)m deg(v) n

areAdjacent (v, w) m min(deg(v), deg(w)) 1

insertVertex(o) 1 1 n2

insertEdge(v, w, o) 1 1 1

removeVertex(v) m deg(v) n2

removeEdge(e) 1 1 1

Page 28: Analysis of Algorithms - VNUvietanh/courses/cs3329/6_Graphs_part1.pdf · Graphs 3 Graph A graph is a pair (V, E), where V is a set of nodes, called vertices E is a collection of pairs

Graphs 28

Depth-First Search

DB

A

C

E

Page 29: Analysis of Algorithms - VNUvietanh/courses/cs3329/6_Graphs_part1.pdf · Graphs 3 Graph A graph is a pair (V, E), where V is a set of nodes, called vertices E is a collection of pairs

Graphs 29

Outline and Reading

Definitions (§12.1) Subgraph

Connectivity

Spanning trees and forests

Depth-first search (§12.3.1) Algorithm

Example

Properties

Analysis

Applications of DFS Path finding

Cycle finding

Page 30: Analysis of Algorithms - VNUvietanh/courses/cs3329/6_Graphs_part1.pdf · Graphs 3 Graph A graph is a pair (V, E), where V is a set of nodes, called vertices E is a collection of pairs

Graphs 30

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 31: Analysis of Algorithms - VNUvietanh/courses/cs3329/6_Graphs_part1.pdf · Graphs 3 Graph A graph is a pair (V, E), where V is a set of nodes, called vertices E is a collection of pairs

Graphs 31

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 32: Analysis of Algorithms - VNUvietanh/courses/cs3329/6_Graphs_part1.pdf · Graphs 3 Graph A graph is a pair (V, E), where V is a set of nodes, called vertices E is a collection of pairs

Graphs 32

Trees and Forests

A (free) tree is an undirected graph T such that T is connected

T has no cycles

This 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 33: Analysis of Algorithms - VNUvietanh/courses/cs3329/6_Graphs_part1.pdf · Graphs 3 Graph A graph is a pair (V, E), where V is a set of nodes, called vertices E is a collection of pairs

Graphs 33

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 34: Analysis of Algorithms - VNUvietanh/courses/cs3329/6_Graphs_part1.pdf · Graphs 3 Graph A graph is a pair (V, E), where V is a set of nodes, called vertices E is a collection of pairs

Graphs 34

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

DFS on a graph with nvertices and m edges takes O(n + m ) 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 35: Analysis of Algorithms - VNUvietanh/courses/cs3329/6_Graphs_part1.pdf · Graphs 3 Graph A graph is a pair (V, E), where V is a set of nodes, called vertices E is a collection of pairs

Graphs 35

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

A(A) = {B, C, D, E}

A(B) = {A, C, F}A(B) = {A, C, F}

A(C) = {A, B, D, E}

F

F

FG

G

G

A(C) = {A, B, D, E}A(C) = {A, B, D, E}

Page 36: Analysis of Algorithms - VNUvietanh/courses/cs3329/6_Graphs_part1.pdf · Graphs 3 Graph A graph is a pair (V, E), where V is a set of nodes, called vertices E is a collection of pairs

Graphs 36

Example (cont.)

DB

A

C

E

DB

A

C

E

DB

A

C

E

DB

A

C

E

A(D) = {A, C} A(E) = {A, C}

F F

F F

A(C) = {A, B, D, E}A(C) = {A, B, D, E}

A(D) = {A, C}A(D) = {A, C}

A(E) = {A, C}A(E) = {A, C}

G

G

G

G

Page 37: Analysis of Algorithms - VNUvietanh/courses/cs3329/6_Graphs_part1.pdf · Graphs 3 Graph A graph is a pair (V, E), where V is a set of nodes, called vertices E is a collection of pairs

Graphs 37

Example (cont.)

DB

A

C

E

F

A(C) = {A, B, D, E}A(B) = {A, C, F}

G

DB

A

C

E

F G

A(G) = Φ

DB

A

C

E

F

A(F) = {B}

A(B) = {A, C, F}A(A) = {A, B, C, D}

G

Page 38: Analysis of Algorithms - VNUvietanh/courses/cs3329/6_Graphs_part1.pdf · Graphs 3 Graph A graph is a pair (V, E), where V is a set of nodes, called vertices E is a collection of pairs

Graphs 38

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 39: Analysis of Algorithms - VNUvietanh/courses/cs3329/6_Graphs_part1.pdf · Graphs 3 Graph A graph is a pair (V, E), where V is a set of nodes, called vertices E is a collection of pairs

Graphs 39

DFS AlgorithmThe 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 Gin the connected component of vas discovery edges and back edges

setLabel(v, VISITED)

for all e G.incidentEdges(v)

if getLabel(e) = UNEXPLORED

w opposite(v,e)

if getLabel(w) = UNEXPLORED

setLabel(e, DISCOVERY)

DFS(G, w)

else

setLabel(e, BACK)

Algorithm DFS(G)

Input graph G

Output labeling of the edges of Gas discovery edges andback edges

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

DFS(G, v)

Page 40: Analysis of Algorithms - VNUvietanh/courses/cs3329/6_Graphs_part1.pdf · Graphs 3 Graph A graph is a pair (V, E), where V is a set of nodes, called vertices E is a collection of pairs

Graphs 40

Exercise on DFS Algorithm

CB

A

E

D

F

Page 41: Analysis of Algorithms - VNUvietanh/courses/cs3329/6_Graphs_part1.pdf · Graphs 3 Graph A graph is a pair (V, E), where V is a set of nodes, called vertices E is a collection of pairs

Graphs 41

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

F G

v1

v2

Page 42: Analysis of Algorithms - VNUvietanh/courses/cs3329/6_Graphs_part1.pdf · Graphs 3 Graph A graph is a pair (V, E), where V is a set of nodes, called vertices E is a collection of pairs

Graphs 42

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

• Function DFS(G, v) and the method incidentEdges are called once for each vertex

DB

A

C

E

F G

Page 43: Analysis of Algorithms - VNUvietanh/courses/cs3329/6_Graphs_part1.pdf · Graphs 3 Graph A graph is a pair (V, E), where V is a set of nodes, called vertices E is a collection of pairs

Graphs 43

Analysis of DFSDFS runs in O(n + m) time provided the graph is represented by the adjacency list structure Recall that ∑v deg(v) = 2m

Algorithm DFS(G, v)

Input graph G and a start vertex v of G

Output labeling of the edges of Gin the connected component of vas discovery edges and back edges

setLabel(v, VISITED)

for all e G.incidentEdges(v)

if getLabel(e) = UNEXPLORED

w opposite(v,e)

if getLabel(w) = UNEXPLORED

setLabel(e, DISCOVERY)

DFS(G, w)

else

setLabel(e, BACK)

Algorithm DFS(G)

Input graph G

Output labeling of the edges of Gas discovery edges andback edges

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

DFS(G, v)

O(n)

O(m)

O(n +m)

O(deg(v))

Page 44: Analysis of Algorithms - VNUvietanh/courses/cs3329/6_Graphs_part1.pdf · Graphs 3 Graph A graph is a pair (V, E), where V is a set of nodes, called vertices E is a collection of pairs

Graphs 44

Path FindingWe 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 uas 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 45: Analysis of Algorithms - VNUvietanh/courses/cs3329/6_Graphs_part1.pdf · Graphs 3 Graph A graph is a pair (V, E), where V is a set of nodes, called vertices E is a collection of pairs

Graphs 45

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) = UNEXPLORED

w opposite(v,e)

S.push(e)

if getLabel(w) = UNEXPLORED

setLabel(e, DISCOVERY)

pathDFS(G, w, z)

S.pop(e)

else

T new empty stack

repeat

o S.pop()

T.push(o)

until o = w

return T.elements()

S.pop(v)

Page 46: Analysis of Algorithms - VNUvietanh/courses/cs3329/6_Graphs_part1.pdf · Graphs 3 Graph A graph is a pair (V, E), where V is a set of nodes, called vertices E is a collection of pairs

Graphs 46

Breadth-First Search

CB

A

E

D

L0

L1

FL2

Page 47: Analysis of Algorithms - VNUvietanh/courses/cs3329/6_Graphs_part1.pdf · Graphs 3 Graph A graph is a pair (V, E), where V is a set of nodes, called vertices E is a collection of pairs

Graphs 47

Outline and Reading

• Breadth-first search (Sect. 12.3.2)

• Algorithm

• Example

• Properties

• Analysis

• Applications

• DFS vs. BFS

• Comparison of applications

• Comparison of edge labels

Page 48: Analysis of Algorithms - VNUvietanh/courses/cs3329/6_Graphs_part1.pdf · Graphs 3 Graph A graph is a pair (V, E), where V is a set of nodes, called vertices E is a collection of pairs

Graphs 48

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

• BFS on a graph with nvertices and m edges takes O(n + m ) 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 49: Analysis of Algorithms - VNUvietanh/courses/cs3329/6_Graphs_part1.pdf · Graphs 3 Graph A graph is a pair (V, E), where V is a set of nodes, called vertices E is a collection of pairs

Graphs 49

Example

CB

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

L0

L1

F

Page 50: Analysis of Algorithms - VNUvietanh/courses/cs3329/6_Graphs_part1.pdf · Graphs 3 Graph A graph is a pair (V, E), where V is a set of nodes, called vertices E is a collection of pairs

Graphs 50

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

discovery edgecross edge

visited vertexAA unexplored vertex

unexplored edge

Page 51: Analysis of Algorithms - VNUvietanh/courses/cs3329/6_Graphs_part1.pdf · Graphs 3 Graph A graph is a pair (V, E), where V is a set of nodes, called vertices E is a collection of pairs

Graphs 51

Example (cont.)

CB

A

E

D

L0

L1

FL2

CB

A

E

D

L0

L1

FL2

CB

A

E

D

L0

L1

FL2

discovery edgecross edge

visited vertexAA unexplored vertex

unexplored edge

Page 52: Analysis of Algorithms - VNUvietanh/courses/cs3329/6_Graphs_part1.pdf · Graphs 3 Graph A graph is a pair (V, E), where V is a set of nodes, called vertices E is a collection of pairs

Graphs 52

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

Algorithm BFS(G, s)

L0 new empty sequence

L0.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) = UNEXPLORED

setLabel(e, DISCOVERY)

setLabel(w, VISITED)

Li +1.insertLast(w)

else

setLabel(e, CROSS)

i i +1

Algorithm BFS(G)

Input graph G

Output 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: Analysis of Algorithms - VNUvietanh/courses/cs3329/6_Graphs_part1.pdf · Graphs 3 Graph A graph is a pair (V, E), where V is a set of nodes, called vertices E is a collection of pairs

Graphs 53

PropertiesNotation

Gs: 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 iedges

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 54: Analysis of Algorithms - VNUvietanh/courses/cs3329/6_Graphs_part1.pdf · Graphs 3 Graph A graph is a pair (V, E), where V is a set of nodes, called vertices E is a collection of pairs

Graphs 54

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 Sv deg(v) = 2m

Page 55: Analysis of Algorithms - VNUvietanh/courses/cs3329/6_Graphs_part1.pdf · Graphs 3 Graph A graph is a pair (V, E), where V is a set of nodes, called vertices E is a collection of pairs

Graphs 55

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 56: Analysis of Algorithms - VNUvietanh/courses/cs3329/6_Graphs_part1.pdf · Graphs 3 Graph A graph is a pair (V, E), where V is a set of nodes, called vertices E is a collection of pairs

Graphs 56

DFS vs. BFS

CB

A

E

D

L0

L1

FL2

CB

A

E

D

F

DFS BFS

Applications DFS BFS

Spanning forest, connected components, paths, cycles

Shortest paths

Biconnected components

Page 57: Analysis of Algorithms - VNUvietanh/courses/cs3329/6_Graphs_part1.pdf · Graphs 3 Graph A graph is a pair (V, E), where V is a set of nodes, called vertices E is a collection of pairs

Graphs 57

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


Recommended