Data Structures

CSED233: Data Structures (2014F)
Lecture17: Graph I
Bohyung Han
CSE, POSTECH
[email protected]
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.
2
CSED233: Data Structures
by 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.
PVD
ORD
SFO
LGA
LAX
DFW
HNL
MIA
3
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2014
Applications
β€’ Electronic circuits
cslab1a
cslab1b
ο‚§ Printed circuit board
ο‚§ Integrated circuit
math.brown.edu
β€’ Transportation networks
cs.brown.edu
ο‚§ Highway network
ο‚§ Flight network
brown.edu
β€’ Computer networks
qwest.net
ο‚§ Local area network
ο‚§ Internet
ο‚§ Web
att.net
cox.net
β€’ Databases
John
ο‚§ Entity‐relationship diagram
Paul
4
David
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2014
Applications
Gene network
ImageNet
5
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2014
Basic Definitions in Graph
β€’ Undirected graph
ο‚§ Graph with undirected edges
ο‚§ Undirected edges: 𝑒, 𝑣 = 𝑣, 𝑒
ORD
849
miles
PVD
ORD
flight
AA 1206
PVD
β€’ 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: 𝐸 = 𝑂 𝑉
6
2
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2014
Undirected Graph
𝒗
𝒗
𝒖
𝒙
𝒖
𝒙
π’˜
π’š
π’˜
π’š
𝑉 = {𝑒, 𝑣, 𝑀, π‘₯, 𝑦}
𝑉 = {𝑒, 𝑣, 𝑀, π‘₯, 𝑦}
𝐸 = { 𝑒, 𝑣 , 𝑣, π‘₯ , 𝑒, 𝑀 , 𝑀, π‘₯ , 𝑀, 𝑦 , π‘₯, 𝑦 }
𝐸 = { 𝑒, 𝑣 , 𝑀, π‘₯ , 𝑀, 𝑦 , π‘₯, 𝑦 }
7
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2014
Directed Graph
𝒗
𝒖
𝒙
π’˜
π’š
𝑉 = {𝑒, 𝑣, 𝑀, π‘₯, 𝑦}
𝐸 = { 𝑣, 𝑒 , 𝑀, 𝑒 , 𝑣, π‘₯ , π‘₯, 𝑀 , 𝑦, 𝑀 , π‘₯, 𝑦 , 𝑦, π‘₯ }
8
CSED233: Data Structures
by 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
ο‚§ deg π‘₯ = 5
𝒆𝒋
𝒆𝒉
π’†π’ˆ
𝒆𝒇
β€’ Self‐loop
π’š
ο‚§ Edge between the same vertex
ο‚§ 𝑒𝑗 is a self‐loop.
9
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2014
Terminology (cont’d)
β€’ Path
ο‚§ Sequence of vertices 𝑣1 , 𝑣2 , … , 𝑣𝑛
such that 𝑣𝑖 , 𝑣𝑖+1 ∈ 𝐸
ο‚§ 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: 𝑝1 = 𝑣, π‘₯, 𝑧
ο‚§ Non‐simple path: 𝑝2 = 𝑒, 𝑀, π‘₯, 𝑦, 𝑀, 𝑣
10
π’˜
π’š
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2014
Terminology (cont’d)
β€’ Cycle
ο‚§ Sequence of vertices 𝑣1 , 𝑣2 , … , 𝑣𝑛
such that 𝑣𝑖 , 𝑣𝑖+1 ∈ 𝐸 and 𝑣1 = 𝑣𝑛
ο‚§ No backtracking
𝒗
β€’ Simple cycle
𝒖
ο‚§ Cycle such that all its vertices and
edges are distinct.
ο‚§ Example: 𝑐1 = 𝑣, π‘₯, 𝑦, 𝑀, 𝑒, 𝑣
ο‚§ Non‐simple cycle example: visiting the
same node multiple times
𝑐1 = 𝑒, 𝑀, π‘₯, 𝑦, 𝑀, 𝑣, 𝑒
11
π’„πŸ
𝒙
𝒛
π’„πŸ
π’˜
π’š
CSED233: Data Structures
by 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
β€’ deg 𝑖𝑛 π‘₯ = 2
𝒖
ο‚§ Outdegree
𝒙
𝒛
π’˜
β€’ Number of outgoing edges
β€’ deg π‘œπ‘’π‘‘ 𝑀 = 2
β€’ Path and cycle in directed graph
π’š
ο‚§ Should consider the direction of edges
β€’ Directed Acyclic Graph (DAG): directed graph with no cycles
12
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2014
Properties of Graphs
β€’ For undirected graph
ο‚§ 𝐺 = (𝑉, 𝐸), where 𝑉 = 𝑛 and 𝐸 = π‘š
ο‚§ 𝑣 deg(𝑣) = 2π‘š
β€’ Each edge is counted twice.
ο‚§ π‘š ≀ 𝑛(𝑛 βˆ’ 1)/2 with no self‐loops
and no multiple edges
ο‚§ Each vertex has degree at most 𝑛 βˆ’ 1
𝑛 = 4, π‘š = 6, deg(𝑣) = 3
β€’ What is the bound for a directed graph?
13
CSED233: Data Structures
by 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
Connected graph
ο‚§ There is an edge between every pair
of vertices.
Non connected graph with two
connected components
Complete graph
14
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2014
Connectivity in Directed Graph
β€’ Strongly connected graph
ο‚§ There is a path from every vertex to
every other vertex.
Strongly connected graph
β€’ Weakly connected graph
ο‚§ There is a path from every vertex to
every other vertex, disregarding the
direction of the edges.
Weakly connected graph
15
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2014
Connectivity
β€’ Subgraph
ο‚§ A graph 𝑆 is a subgraph of 𝐺 if and
only if 𝑉𝑆 βŠ† 𝑉𝐺 and 𝐸𝑆 βŠ† 𝐸𝐺
Subgraph
β€’ A spanning subgraph
ο‚§ A subgraph that contains all the
vertices of the original graph
Spanning subgraph
16
CSED233: Data Structures
by 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
Tree
β€’ Forest
ο‚§ A forest is an undirected graph
without cycles
ο‚§ The connected components of
a forest are trees
Forest
17
CSED233: Data Structures
by 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 tree
Graph
β€’ Spanning forest
ο‚§ A spanning subgraph that is a forest
18
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2014
19