Graphs Outline

20
01/24/03 Algorithm Theory Hiroaki Kobayashi 1 Graphs ORD DFW SFO LAX 8 0 2 1743 1843 1233 337 01/24/03 Algorithm Theory Hiroaki Kobayashi 2 Outline Graphs

Transcript of Graphs Outline

Page 1: Graphs Outline

Graphs 03/1/22 22時13分

1

01/24/03Algorithm TheoryHiroaki Kobayashi 1

Graphs

ORD

DFW

SFO

LAX

802

1743

1843

1233337

01/24/03Algorithm TheoryHiroaki Kobayashi 2

OutlineGraphs

DefinitionApplicationsTerminologyPropertiesADT

Data structures for graphsEdge list structureAdjacency list structureAdjacency matrix structure

Page 2: Graphs Outline

Graphs 03/1/22 22時13分

2

01/24/03Algorithm TheoryHiroaki Kobayashi 3

GraphA graph is a pair (V, E), where

V is a set of nodes, called verticesE is a collection of pairs of vertices, called edgesVertices and edges are positions and store elements

Example:A vertex represents an airport and stores the three-letter airport codeAn edge represents a flight route between two airports and stores the mileage of the route

ORD PVD

MIADFW

SFO

LAX

LGA

HNL

849

802

13871743

1843

10991120

1233

337

2555

142

01/24/03Algorithm TheoryHiroaki Kobayashi 4

Edge TypesDirected edge

ordered pair of vertices (u,v)first vertex u is the originsecond vertex v is the destinatione.g., a flight

Undirected edgeunordered pair of vertices (u,v)e.g., a flight route

Directed graphall the edges are directede.g., route network

Undirected graphall the edges are undirectede.g., flight network

ORD PVDflight

AA 1206

ORD PVD849miles

Page 3: Graphs Outline

Graphs 03/1/22 22時13分

3

01/24/03Algorithm TheoryHiroaki Kobayashi 5

John

DavidPaul

brown.edu

cox.net

cs.brown.edu

att.netqwest.net

math.brown.edu

cslab1bcslab1a

ApplicationsElectronic circuits

Printed circuit boardIntegrated circuit

Transportation networksHighway networkFlight network

Computer networksLocal area networkInternetWeb

DatabasesEntity-relationship diagram

01/24/03Algorithm TheoryHiroaki Kobayashi 6

TerminologyEnd vertices (or endpoints) of an edge

U and V are the endpoints of aEdges incident on a vertex

a, d, and b are incident on VAdjacent vertices

U and V are adjacentDegree of a vertex

X has degree 5 Parallel edges

h and i are parallel edgesSelf-loop

j is a self-loop

XU

V

W

Z

Y

a

c

b

e

d

f

g

h

i

j

Page 4: Graphs Outline

Graphs 03/1/22 22時13分

4

01/24/03Algorithm TheoryHiroaki Kobayashi 7

P1

Terminology (cont.)Path

sequence of alternating vertices and edges begins with a vertexends with a vertexeach edge is preceded and followed by its endpoints

Simple pathpath such that all its vertices and edges are distinct

ExamplesP1=(V,b,X,h,Z) is a simple pathP2=(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

01/24/03Algorithm TheoryHiroaki Kobayashi 8

Terminology (cont.)Cycle

circular sequence of alternating vertices and edges each edge is preceded and followed by its endpoints

Simple cyclecycle such that all its vertices and edges are distinct

ExamplesC1=(V,b,X,g,Y,f,W,c,U,a,↵) is a simple cycleC2=(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 5: Graphs Outline

Graphs 03/1/22 22時13分

5

01/24/03Algorithm TheoryHiroaki Kobayashi 9

PropertiesNotation

n number of verticesm number of edges

deg(v) degree of vertex v

Property 1

Σv deg(v) = 2mProof: each edge is

counted twice

Property 2In an undirected graph

with no self-loops and no multiple edgesm ≤ n (n − 1)/2

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

Examplen = 4m = 6deg(v) = 3

What is the bound for a directed graph?

A simple graph with n vertices has O(n2) edges

01/24/03Algorithm TheoryHiroaki Kobayashi 10

Main Methods of the Graph ADTVertices and edges

are positionsstore elements

Accessor methodsaVertex()incidentEdges(v)endVertices(e)isDirected(e)origin(e)destination(e)opposite(v, e)areAdjacent(v, w)

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

Generic methodsnumVertices()numEdges()vertices()edges()

Page 6: Graphs Outline

Graphs 03/1/22 22時13分

6

01/24/03Algorithm TheoryHiroaki Kobayashi 11

Edge List StructureVertex object

elementreference to position in vertex sequence

Edge objectelementorigin vertex objectdestination vertex objectreference to position in edge sequence

Vertex sequencesequence of vertex objects

Edge sequencesequence of edge objects

v

u

w

a c

b

a

zd

u v w z

b c d

01/24/03Algorithm TheoryHiroaki Kobayashi 12

Adjacency List StructureEdge list structureIncidence 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

wa b

a

u v w

b

Page 7: Graphs Outline

Graphs 03/1/22 22時13分

7

01/24/03Algorithm TheoryHiroaki Kobayashi 13

Adjacency Matrix StructureEdge list structureAugmented vertex objects

Integer key (index) associated with vertex

2D-array adjacency array

Reference to edge object for adjacent verticesNull for non nonadjacent vertices

u

v

wa b

2

1

0

210

∅∅

∅∅

a

u v w0 1 2

b

01/24/03Algorithm TheoryHiroaki Kobayashi 14

Asymptotic Performance

n2n + mn + mSpace

n2deg(v)mremoveVertex(v)111insertEdge(v, w, o)n211insertVertex(o)

111removeEdge(e)

1min(deg(v), deg(w))mareAdjacent (v, w)ndeg(v)mincidentEdges(v)

Adjacency Matrix

AdjacencyList

EdgeList

n vertices, m edgesno parallel edgesno self-loopsBounds are “big-Oh”

Page 8: Graphs Outline

Graphs 03/1/22 22時13分

8

01/24/03Algorithm TheoryHiroaki Kobayashi 15

Depth-First Search

DB

A

C

E

01/24/03Algorithm TheoryHiroaki Kobayashi 16

OutlineDefinitions

SubgraphConnectivitySpanning trees and forests

Depth-first searchAlgorithmExamplePropertiesAnalysis

Applications of DFSPath findingCycle finding

Page 9: Graphs Outline

Graphs 03/1/22 22時13分

9

01/24/03Algorithm TheoryHiroaki Kobayashi 17

SubgraphsA subgraph S of a graph G is a graph such that

The vertices of S are a subset of the vertices of GThe 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

01/24/03Algorithm TheoryHiroaki Kobayashi 18

Connectivity

A graph is connected if there is a path between every pair of verticesA connected component of a graph G is a maximal connected subgraph of G

Connected graph

Non connected graph with two connected components

Page 10: Graphs Outline

Graphs 03/1/22 22時13分

10

01/24/03Algorithm TheoryHiroaki Kobayashi 19

Trees and ForestsA (free) tree is an undirected graph T such that

T is connectedT has no cycles

This definition of tree is different from the one of a rooted tree

A forest is an undirected graph without cyclesThe connected components of a forest are trees

Tree

Forest

01/24/03Algorithm TheoryHiroaki Kobayashi 20

Spanning Trees and ForestsA spanning tree of a connected graph is a spanning subgraph that is a treeA spanning tree is not unique unless the graph is a treeSpanning trees have applications to the design of communication networksA spanning forest of a graph is a spanning subgraph that is a forest

Graph

Spanning tree

Page 11: Graphs Outline

Graphs 03/1/22 22時13分

11

01/24/03Algorithm TheoryHiroaki Kobayashi 21

Depth-First SearchDepth-first search (DFS) is a general technique for traversing a graphA DFS traversal of a graph G

Visits all the vertices and edges of GDetermines whether G is connectedComputes the connected components of GComputes a spanning forest of G

DFS on a graph with nvertices and m edges takes O(n + m ) timeDFS can be further extended to solve other graph problems

Find and report a path between two given verticesFind a cycle in the graph

01/24/03Algorithm TheoryHiroaki Kobayashi 22

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 GOutput labeling of the edges of G

in the connected component of vas 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)

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

as 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 12: Graphs Outline

Graphs 03/1/22 22時13分

12

01/24/03Algorithm TheoryHiroaki Kobayashi 23

Example

DB

A

C

E

DB

A

C

E

DB

A

C

E

discovery edgeback edge

A visited vertexA unexplored vertex

unexplored edge

01/24/03Algorithm TheoryHiroaki Kobayashi 24

Example (cont.)

DB

A

C

E

DB

A

C

E

DB

A

C

E

DB

A

C

E

Page 13: Graphs Outline

Graphs 03/1/22 22時13分

13

01/24/03Algorithm TheoryHiroaki Kobayashi 25

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) visitedWe mark each corridor (edge ) traversedWe keep track of the path back to the entrance (start vertex) by means of a rope (recursion stack)

01/24/03Algorithm TheoryHiroaki Kobayashi 26

Properties of DFSProperty 1

DFS(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 14: Graphs Outline

Graphs 03/1/22 22時13分

14

01/24/03Algorithm TheoryHiroaki Kobayashi 27

Analysis of DFSSetting/getting a vertex/edge label takes O(1) timeEach vertex is labeled twice

once as UNEXPLOREDonce as VISITED

Each edge is labeled twiceonce as UNEXPLOREDonce as DISCOVERY or BACK

Method incidentEdges is called once for each vertexDFS runs in O(n + m) time provided the graph is represented by the adjacency list structure

Recall that Σv deg(v) = 2m

01/24/03Algorithm TheoryHiroaki Kobayashi 28

Path FindingWe can specialize the DFS algorithm to find a path between two given vertices u and z using the template method patternWe call DFS(G, u) with uas the start vertexWe use a stack S to keep track of the path between the start vertex and the current vertexAs 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) = UNEXPLOREDw ← opposite(v,e)if getLabel(w) = UNEXPLORED

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

elsesetLabel(e, BACK)

S.pop(v)

Page 15: Graphs Outline

Graphs 03/1/22 22時13分

15

01/24/03Algorithm TheoryHiroaki Kobayashi 29

Cycle FindingWe can specialize the DFS algorithm to find a simple cycle using the template method patternWe use a stack S to keep track of the path between the start vertex and the current vertexAs 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)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)S.pop(e)

elseT ← new empty stackrepeat

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

until o = wreturn T.elements()

S.pop(v)

01/24/03Algorithm TheoryHiroaki Kobayashi 30

Breadth-First Search

CB

A

E

D

L0

L1

FL2

Page 16: Graphs Outline

Graphs 03/1/22 22時13分

16

01/24/03Algorithm TheoryHiroaki Kobayashi 31

OutlineBreadth-first search

AlgorithmExamplePropertiesAnalysisApplications

01/24/03Algorithm TheoryHiroaki Kobayashi 32

Breadth-First SearchBreadth-first search (BFS) is a general technique for traversing a graphA BFS traversal of a graph G

Visits all the vertices and edges of GDetermines whether G is connectedComputes the connected components of GComputes a spanning forest of G

BFS on a graph with nvertices and m edges takes O(n + m ) timeBFS 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 17: Graphs Outline

Graphs 03/1/22 22時13分

17

01/24/03Algorithm TheoryHiroaki Kobayashi 33

BFS AlgorithmThe 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 ← 0while ¬Li.isEmpty()

Li +1 ← new empty sequencefor 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)

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)

01/24/03Algorithm TheoryHiroaki Kobayashi 34

Example

CB

A

E

D

discovery edgecross edge

A visited vertexA unexplored vertex

unexplored edge

L0

L1

F

CB

A

E

D

L0

L1

F

CB

A

E

D

L0

L1

F

Page 18: Graphs Outline

Graphs 03/1/22 22時13分

18

01/24/03Algorithm TheoryHiroaki Kobayashi 35

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

01/24/03Algorithm TheoryHiroaki Kobayashi 36

Example (cont.)

CB

A

E

D

L0

L1

FL2

CB

A

E

D

L0

L1

FL2

CB

A

E

D

L0

L1

FL2

Page 19: Graphs Outline

Graphs 03/1/22 22時13分

19

01/24/03Algorithm TheoryHiroaki Kobayashi 37

PropertiesNotation

Gs: connected component of sProperty 1

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

Property 2The discovery edges labeled by BFS(G, s) form a spanning tree Tsof 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

01/24/03Algorithm TheoryHiroaki Kobayashi 38

AnalysisSetting/getting a vertex/edge label takes O(1) timeEach vertex is labeled twice

once as UNEXPLOREDonce as VISITED

Each edge is labeled twiceonce as UNEXPLOREDonce as DISCOVERY or CROSS

Each vertex is inserted once into a sequence LiMethod incidentEdges is called once for each vertexBFS runs in O(n + m) time provided the graph is represented by the adjacency list structure

Recall that Σv deg(v) = 2m

Page 20: Graphs Outline

Graphs 03/1/22 22時13分

20

01/24/03Algorithm TheoryHiroaki Kobayashi 39

ApplicationsUsing 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 GCompute a spanning forest of GFind a simple cycle in G, or report that G is a forestGiven two vertices of G, find a path in G between them with the minimum number of edges, or report that no such path exists