0% found this document useful (0 votes)
197 views

Graph

The document defines and describes graphs and their basic terminology and properties. A graph is a pair (V,E) where V is a set of vertices and E is a set of edges connecting the vertices. Graphs can model real-world relationships like transportation networks or computer networks. Common graph problems involve finding paths or cycles between vertices. The document outlines different graph structures, directed vs undirected edges, and defines terms like adjacent vertices, degree, paths, and cycles.

Uploaded by

api-3705606
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
197 views

Graph

The document defines and describes graphs and their basic terminology and properties. A graph is a pair (V,E) where V is a set of vertices and E is a set of edges connecting the vertices. Graphs can model real-world relationships like transportation networks or computer networks. Common graph problems involve finding paths or cycles between vertices. The document outlines different graph structures, directed vs undirected edges, and defines terms like adjacent vertices, degree, paths, and cycles.

Uploaded by

api-3705606
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Graphs 2/21/2003 9:27 AM

Graphs
1843 ORD
SFO

802
43
17
337

1233
LAX DFW

Graphs version 1.3 1

Outline and Reading


Graphs (§6.1)
„ Definitions
„ Applications
„ Terminology
„ Properties
„ ADT
Data structures for graphs (§6.2)
„ Edge list structure
„ Adjacency list structure
„ Adjacency matrix structure

Graphs version 1.3 2

Graph
A graph is a pair (V, E), where
„ V is a set of nodes, called vertices
„ E is a collection of edges (pairs of vertices)
„ Vertices and edges are positions and store elements
Graphs useful for representing real-world relationships:
„ vertex = airport
„ edge = flight route, storing mileage
„ Abstract real-world problems into problems on graphs
849 PVD
1843 ORD 2
SFO 14
802

43 LGA
17
337

7
HNL 2555 138 10
99
1233
LAX DFW 1120
MIA
Graphs version 1.3 3

1
Graphs 2/21/2003 9:27 AM

Applications
cslab1a cslab1b

Electronic circuits math.brown.edu


„ Printed circuit board
„ Integrated circuit
cs.brown.edu
Transportation networks
„ Highway network brown.edu
„ Flight network qwest.net

Computer networks att.net

„ Local area network


„ Internet cox.net
„ Web John

Databases Paul
David

„ Entity-relationship diagram
Graphs version 1.3 4

Sample problems
What is cheapest way to fly from X to Y?
If airport X closes from bad weather, can I
still fly between every other pair of cities?
Many classes have prereqs; in what order can
I take the classes for my major?
How much traffic can flow between
intersection X and intersection Y
How can I minimize the amount of wiring
needed to connect some outlets together?

Graphs version 1.3 5

Edge Types
Directed edge
ordered pair of vertices (u,v)
„
flight
„ first vertex u is the origin ORD AA 1206 PVD
„ second vertex v is the destination
„ e.g., a flight
Undirected edge
unordered pair of vertices (u,v) 849
ORD PVD
„

„ e.g., a flight route miles


Directed graph
„ all the edges are directed
„ e.g., route network
Undirected graph
„ all the edges are undirected
„ e.g., flight network

Graphs version 1.3 6

2
Graphs 2/21/2003 9:27 AM

Terminology
End vertices (or endpoints) of an
edge
„ U and V are the endpoints of a V
Edges incident on a vertex a b
h j
„ a, d, and b are incident on V
Adjacent vertices U d X Z
U and V are adjacent
„
c e i
Degree of a vertex
„ X has degree 5 W g
Parallel edges (typically not used)
„ h and i are parallel edges f
Self-loop (typically not used) Y
„ j is a self-loop

Graphs version 1.3 7

Terminology (cont.)
Path
„ sequence of alternating vertices
and edges
V
„ begins and ends with some vertex a b
„ each edge is preceded and P1
followed by its endpoints d
Simple path
U X Z
P2 h
„ path such that all its vertices and c e
edges are distinct
Reachable W g
„ path exists
Examples
f
„ P1=(V,b,X,h,Z) is a simple path
Y
„ P2=(U,c,W,e,X,g,Y,f,W,d,V) is a
path that is not simple
„ Z is reachable from U
Graphs version 1.3 8

Terminology (cont.)
Cycle
„ circular sequence of alternating
vertices and edges
„ each edge is preceded and V
followed by its endpoints a b C3
„ edges traversed only in one
direction d
U X Z
Simple cycle C2 h
„ cycle such that all its vertices
c e
and edges are distinct
W g
Examples
„ C1=(V,b,X,g,Y,f,W,c,U,a,↵) is a C1
simple cycle f
„ C2=(U,c,W,e,X,g,Y,f,W,d,V,a,↵)
Y
is a cycle that is not simple
„ C3=(X,h,Z,h,X) is not a cycle

Graphs version 1.3 9

3
Graphs 2/21/2003 9:27 AM

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 Subgraph
subset of the edges of G
A spanning subgraph of G
is a subgraph that
contains all the vertices
of G

Spanning subgraph

Graphs version 1.3 10

Connectivity
A graph is
connected if there is
a path between
every pair of Connected graph
vertices
A connected
component of a
graph G is a
maximal connected
subgraph of G Non connected graph with two
connected components

Graphs version 1.3 11

Trees and Forests


A (free) tree is an
undirected graph T such
that
„ T is connected
„ T has no cycles Tree
Different definition than a
rooted tree
A forest is an undirected
graph without cycles
The connected
components of a forest
are trees
Forest

Graphs version 1.3 12

4
Graphs 2/21/2003 9:27 AM

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 Graph
Spanning trees have
applications to the design
of communication
networks
A spanning forest of a
graph is a spanning
subgraph that is a forest
Spanning tree
Graphs version 1.3 13

Properties
Property 1 Notation
Σv deg(v) = 2m n number of vertices
Proof: each edge is m number of edges
counted twice deg(v) degree of vertex v
Property 2
In an undirected graph Example
(with no self-loops or
parallel edges) „ n = 4

m ≤ n (n − 1)/2 „ m = 6
Proof: at most one edge „ deg(v) = 3
for every unique
combination of 2 for all
vertices vertices
What is the bound on m
for a directed graph?Graphs version 1.3 14

Main Methods of the Graph ADT


Vertices and Edges Accessor methods
accessor method: „ Iterator vertices()
„ Object element() „ Iterator edges()
Update methods „ Iterator incidentEdges(v)
„ Vertex insertVertex(o) „ Vertex[2] endVertices(e)
„ Edge insertEdge(v, w, o) „ Vertex opposite(v, e)
„ void removeVertex(v) „ boolean areAdjacent(v, w)
„ void removeEdge(e) Methods for directed edges
„ Vertex origin(e)
Accessor methods „ Vertex destination(e)
„ int numVertices() „ boolean isDirected(e)
„ int numEdges() „ Edge insertDirectedEdge(v, w, o)

„ Vertex aVertex()

Graphs version 1.3 15

5
Graphs 2/21/2003 9:27 AM

Edge List Structure


Vertex object u
„ element a c
Edge object b d
v w z
„ element
„ origin vertex object
„ destination vertex object
„ directed boolean flag
Vertex sequence u v w z
„ sequence of vertex
objects
Edge sequence a b c d
„ sequence of edge objects

Graphs version 1.3 16

Graph ADT with Positions


Recall
„ Position = place where item is stored in a sequence
In Goodrich’s book:
„ A Vertex is a Position
„ An Edge is a Position
Features of Positions
„ Enables faster removal
„ Implementation slightly more complex
„ Unnecessary when removeVertex and removeEdge are not
used

Graphs version 1.3 17

Edge List Structure (w/ Positions)


Vertex object u
„ element a c
„ reference to position in b d
vertex sequence v w z
Edge object
„ element
„ origin vertex object
„ destination vertex object
„ reference to position in u v w z
edge sequence
Vertex sequence
„ sequence of vertex a b c d
objects
Edge sequence
„ sequence of edge objects

Graphs version 1.3 18

6
Graphs 2/21/2003 9:27 AM

Adjacency List Structure


a v b
Edge list
u w
structure
Each Vertex now
stores incidence
sequence u v w
„ sequence of
references to
edge objects of
incident edges

a b

Graphs version 1.3 19

Adjacency List Structure (w/


Positions)
Edge list structure a v b
(w/ Positions) u w
Incidence sequence
for each vertex
„ sequence of
references to edge
objects of incident u v w
edges
Augmented edge
objects
„ references to
associated
positions in
incidence a b
sequences of end
vertices

Graphs version 1.3 20

Adjacency Matrix Structure


Edge list structure a v b
Augmented vertex u w
objects
„ Integer key (index)
associated with vertex
2D-array adjacency
array 0 u 1 v 2 w
„ Reference to edge
object for adjacent
0 1 2
vertices
„ Null for non 0 ∅ ∅
nonadjacent vertices 1 ∅
The “old fashioned” a ∅ ∅ b
2
version just has 0 for
no edge and 1 for edge

Graphs version 1.3 21

7
Graphs 2/21/2003 9:27 AM

Asymptotic Performance
n vertices, m edges Edge Adjacency Adjacency
List List Matrix
Space O(n + m) O(n + m) O(n2 )
Iterating through
O(m) O( deg(v) ) O(n)
incidentEdges(v)
O( min(deg(v),
areAdjacent (v, w) O(m) O(1)
deg(w)) )
insertVertex(o) O(1) O(1) O(n2 )
insertEdge(v, w, o) O(1) O(1) O(1)
removeVertex(v) O(m) O( deg(v) ) O(n2 )
removeEdge(e) O(1) O(1) O(1)
Notes: Assuming no parallel edges or self-loops
Using Positions (for removeVertex and removeEdge)
Graphs version 1.3 22

Depth-First Search
A

B D E

Graphs version 1.3 23

Outline and Reading


Depth-first search (§6.3.1)
„ Algorithm
„ Example
„ Properties
„ Analysis
Applications of DFS (§6.5)
„ Path finding
„ Cycle finding

Graphs version 1.3 24

8
Graphs 2/21/2003 9:27 AM

Depth-First Search
Depth-first search (DFS) is
„ general graph traversal technique
„ visits all the vertices and edges of G
„ with n vertices and m edges takes O(n + m ) time
„ a recursive traversal like Euler tour for binary trees
A DFS traversal of a graph G can be used to
„ Determines whether G is connected
„ Computes the connected components of G
„ Computes a spanning forest of G
„ Find and report a path between two given vertices
„ Find a cycle in the graph
Graphs version 1.3 25

Example
A
A unexplored vertex
A visited vertex
B D E
unexplored edge
discovery edge C
back edge

A A

B D E B D E

C C

Graphs version 1.3 26

Example (cont.)
A A

B D E B D E

C C

A A

B D E B D E

C C

Graphs version 1.3 27

9
Graphs 2/21/2003 9:27 AM

DFS Algorithm
The algorithm uses a mechanism
for setting and getting “labels” of Algorithm DFS(G, v)
vertices and edges Input graph G and a start vertex v of G
Algorithm DFS_Sweep(G) Output labeling of the edges of G
Input graph G in the connected component of v
Output labeling of the edges of G as discovery edges and back edges
as discovery edges and setLabel(v, VISITED)
back edges for all e ∈ G.incidentEdges(v)
for all u ∈ G.vertices() if getLabel(e) = UNEXPLORED
setLabel(u, UNEXPLORED) w ← G.opposite(v,e)
for all e ∈ G.edges() if getLabel(w) = UNEXPLORED
setLabel(e, UNEXPLORED) setLabel(e, DISCOVERY)
for all v ∈ G.vertices() DFS(G, w)
if getLabel(v) = UNEXPLORED else
DFS(G, v) setLabel(e, BACK)

Graphs version 1.3 28

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)
Graphs version 1.3 29

Another Example of Depth


First Search
2
1 6

3
5
7
4

Graphs version 1.3 30

10
Graphs 2/21/2003 9:27 AM

DFS Algorithm
The algorithm uses a mechanism
for setting and getting “labels” of Algorithm DFS(G, v)
vertices and edges Input graph G and a start vertex v of G
Algorithm DFS_Sweep(G) Output labeling of the edges of G
Input graph G in the connected component of v
Output labeling of the edges of G as discovery edges and back edges
as discovery edges and setLabel(v, VISITED)
back edges for all e ∈ G.incidentEdges(v)
for all u ∈ G.vertices() if getLabel(e) = UNEXPLORED
setLabel(u, UNEXPLORED) w ← G.opposite(v,e)
for all e ∈ G.edges() if getLabel(w) = UNEXPLORED
setLabel(e, UNEXPLORED) setLabel(e, DISCOVERY)
for all v ∈ G.vertices() DFS(G, w)
if getLabel(v) = UNEXPLORED else
DFS(G, v) setLabel(e, BACK)

Graphs version 1.3 31

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
DFS(G, v) called once for each vertex v
Inner loop in DFS(G, v) runs in O(deg(v)) time
„ Not counting time inside recursive calls
„ Assuming adjacency list implementation
DFS runs in O(n + m) time
„ Recall that Σv deg(v)Graphs
= 2m
version 1.3 32

Properties of DFS
Property 1
DFS(G, v) visits all the
vertices and edges in the
connected component of v A
Property 2
The discovery edges labeled
by DFS(G, v) form a B D E
spanning tree of the
connected component of v
called DFS tree, rooted at v
C
Property 3
DFS_Sweep(G) visits all
vertices and edges of G

Graphs version 1.3 33

11
Graphs 2/21/2003 9:27 AM

Connected Components &


DFS Spanning Forest
Algorithm ccDFS_Sweep(G)
Input graph G
Use DFS(G, v) to label all
Output labeling of the vertices
and edges of G based on
edges and vertices in one
component number. connected component
for all u ∈ G.vertices() (property 1)
setLabel(u, UNEXPLORED) DFS_Sweep can label all
for all e ∈ G.edges() connected components
setLabel(e, UNEXPLORED) (property 3)
comp_num ← 1 In a DFS_Sweep call, consider
for all v ∈ G.vertices() subgraph of
if getLabel(v) = UNEXPLORED
„ all vertices
perform DFS(G, v) search,
„ all DISCOVERY Edges
labeling vertices and edges
found in the search with By DFS property 2 and 3, this
comp_num. subgraph is a Spanning
comp_num ← comp_num +1 Forest of G.
Graphs version 1.3 34

More Properties of DFS


Classifying Edges by DFS
Edge (v,w) type:
tree = in the DFS tree
back = w is ancestor of v
in DFS tree A
forward = w is descendent
of v in DFS tree
cross = w is neither ancestor B D E
nor descendant of v
in DFS tree
(Assuming edge first explored from
v to w). C
Property 4: edges labeled BACK
are in fact back edges
Property 5: back edges form a
cycle
Graphs version 1.3 35

Path Finding
Specialize DFS to find a path
between two given vertices v Algorithm pathDFS(G, v, z)
and z setLabel(v, VISITED)
S.push(v)
Call DFS(G, v, z) where
if v = z
„ G is the graph return S.elements()
„ v is the start vertex for all e ∈ G.incidentEdges(v)
„ z is the destination vertex if getLabel(e) = UNEXPLORED
Use a stack S to keep track w ← opposite(v,e)
of the path between the if getLabel(w) = UNEXPLORED
start vertex and the current setLabel(e, DISCOVERY)
vertex S.push(e)
As soon as destination pathDFS(G, w, z)
vertex z is encountered, we S.pop(e)
return the path as the else
contents of the stack setLabel(e, BACK)
S.pop(v)
Graphs version 1.3 36

12
Graphs 2/21/2003 9:27 AM

Cycle Finding
Algorithm cycleDFS(G, v)
Specialize DFS to find a setLabel(v, VISITED)
simple cycle S.push(v)
We use a stack S to for all e ∈ G.incidentEdges(v)
if getLabel(e) = UNEXPLORED
keep track of the path w ← opposite(v,e)
between the start vertex S.push(e)
and the current vertex if getLabel(w) = UNEXPLORED
setLabel(e, DISCOVERY)
As soon as a back edge cycleDFS(G, w)
(v, w) is encountered, S.pop(e)
we return the cycle as else
T ← new empty stack
the portion of the stack repeat
from the top to vertex w o ← S.pop()
T.push(o)
until o = w
return T.elements()
S.pop(v)
Graphs version 1.3 37

abstract DFS Template


Algorithm DFS(G, v)
Use Template Method setLabel(v, VISITED)
Design Pattern to startVisit(v)
implement DFS for all e ∈ G.incidentEdges(v)
Extend template to if getLabel(e) = UNEXPLORED
implement any algorithm w ← opposite(v,e)
if getLabel(w) = UNEXPLORED
that uses DFS.
setLabel(e, DISCOVERY)
Extensions need to traverseDiscovery(e)
define the following: if (not isDone())
„ startVisit() DFS(G, w)
„ traverseDiscovery() else
setLabel(e, BACK)
„ traverseBack()
traverseBack(e)
„ isDone() finishVisit(v)
„ finishVisit()
„ a method that returns
results.

Graphs version 1.3 38

13

You might also like