2 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014
Graph
• Definition A graph consists of a finite set of vertices, , and a finite
set of edges, .
• Properties and are sets: each vertex
and each edge are unique. Edge: where Vertices and edges may store
elements.
3 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014
Graphs
• 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.
ORD
PVD
MIA
DFW
SFO
LAX
LGA
HNL
849
802
13871743
1843
1099
1120
1233
337
2555
142
4 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014
Applications
• Electronic circuits Printed circuit board Integrated circuit
• Transportation networks Highway network Flight network
• Computer networks Local area network Internet Web
• Databases Entity relationship diagram‐
John
DavidPaul
brown.edu
cox.net
cs.brown.edu
att.netqwest.net
math.brown.edu
cslab1bcslab1a
5 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014
Applications
ImageNet
Gene network
6 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014
Basic Definitions in Graph
• Undirected graph Graph with undirected edges Undirected edges:
• Directed graph (= digraph) Graph with directed edges Directed edges
• Ordered pair of vertices, • The first vertex is the origin and the second vertex is the destination.
• Sparse graph Graph with few edges , where denotes the number of elements in a set
• Dense graph Graph with many edges:
ORD PVDflight
AA 1206
ORD PVD849
miles
7 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014
Undirected Graph
𝒙𝒖
𝒗
𝒘 𝒚
𝒙𝒖
𝒗
𝒘 𝒚
𝑉={𝑢 ,𝑣 ,𝑤 , 𝑥 , 𝑦 }𝐸={ (𝑢 ,𝑣 ) , (𝑣 ,𝑥 ) , (𝑢 ,𝑤 ) , (𝑤 ,𝑥 ) , (𝑤 , 𝑦 ) , (𝑥 , 𝑦 ) }
𝑉={𝑢 ,𝑣 ,𝑤 , 𝑥 , 𝑦 }𝐸={ (𝑢 ,𝑣 ) , (𝑤 ,𝑥 ) , (𝑤 , 𝑦 ) , (𝑥 , 𝑦 ) }
8 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014
Directed Graph
𝒙𝒖
𝒗
𝒘 𝒚
𝑉={𝑢 ,𝑣 ,𝑤 , 𝑥 , 𝑦 }𝐸={ (𝑣 ,𝑢 ) , (𝑤 ,𝑢) , (𝑣 , 𝑥 ) , (𝑥 ,𝑤 ) , (𝑦 ,𝑤 ) , (𝑥 , 𝑦 ) , (𝑦 ,𝑥 ) }
9 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014
Terminology
• End vertices (or endpoints) of an edge and are the endpoints of
• Edges incident on a vertex , and are incident on .
• Adjacent vertices
Vertices and are adjacent.
• Degree of a vertex Number of incident edges
• Self loop‐ Edge between the same vertex is a self loop.‐
𝒙𝒖
𝒗
𝒘
𝒛
𝒚
𝒆𝒂
𝒆𝒄
𝒆𝒃
𝒆𝒆
𝒆𝒅
𝒆 𝒇
𝒆𝒈
𝒆𝒉
𝒆𝒊
𝒆 𝒋
10 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014
Terminology (cont’d)
• Path Sequence of vertices
such that Length of a path
• Number of edges on the path• The length of the path from a
vertex to itself is 0.
• Simple path Path such that all its vertices and
edges are distinct Example: Non simple path: ‐
𝒑𝟏
𝒙𝒖
𝒗
𝒘
𝒛
𝒚
𝒑𝟐
11 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014
Terminology (cont’d)
• Cycle Sequence of vertices
such that and No backtracking
• Simple cycle Cycle such that all its vertices and
edges are distinct. Example: Non simple cycle example: visiting the ‐
same node multiple times
𝒙𝒖
𝒗
𝒘
𝒛
𝒚
𝒄𝟏
𝒄𝟐
12 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014
Terminology in Directed Graph
• Adjacency For directed graphs, vertex is adjacent
to if and only if .
• Degree Indegree
• Number of incoming edges
Outdegree• Number of outgoing edges
• Path and cycle in directed graph Should consider the direction of edges
• Directed Acyclic Graph (DAG): directed graph with no cycles
𝒙𝒖
𝒗
𝒘
𝒛
𝒚
13 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014
Properties of Graphs
• For undirected graph , where and
• Each edge is counted twice. with no self loops ‐
and no multiple edges Each vertex has degree at most
• What is the bound for a directed graph?
𝑛=4 ,𝑚=6 , deg(𝑣)=3
14 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014
Connectivity
• Connected graph There is a path from every vertex to
every other vertex.
• Connected component Maximal connected subgraph
• Complete graph There is an edge between every pair
of vertices.
Connected graph
Non connected graph with two connected components
Complete graph
15 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014
Connectivity in Directed Graph
• Strongly connected graph There is a path from every vertex to
every other vertex.
• Weakly connected graph There is a path from every vertex to
every other vertex, disregarding the direction of the edges.
Strongly connected graph
Weakly connected graph
16 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014
Connectivity
• Subgraph A graph is a subgraph of if and
only if and
• A spanning subgraph A subgraph that contains all the
vertices of the original graph
Subgraph
Spanning subgraph
17 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014
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
• Forest A forest is an undirected graph
without cycles The connected components of
a forest are trees
Tree
Forest
18 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014
Spanning Trees and Forests
• Spanning tree A spanning subgraph that is a tree Not unique unless the graph is a tree
• Spanning forest A spanning subgraph that is a forest
Graph Spanning tree
19