What is a graph?

What is a graph?
• A data structure that consists of a set of nodes
•
(vertices) and a set of edges that relate the nodes
to each other
The set of edges describes relationships among
the vertices
Visit for more Learning Resources
Formal definition of graphs
• A graph G is defined as follows:
G=(V,E)
V(G): a finite, nonempty set of vertices
E(G): a set of edges (pairs of vertices)
The Graph ADT (2/13)
 Definitions
 A graph G consists of two sets
 a finite, nonempty set of vertices V(G)
 a finite, possible empty set of edges E(G)
 G(V,E) represents a graph
 An undirected graph is one in which the pair of vertices in
an edge is unordered, (v0, v1) = (v1,v0)
 A directed graph is one in which each edge is a directed
pair of vertices, <v0, v1> != <v1, v0>
head
tail
The Graph ADT (3/13)
 Examples for Graph
 complete undirected graph: n(n-1)/2 edges
 complete directed graph: n(n-1) edges
1
0
0
0
2
1
3
G1
complete graph
V(G1)={0,1,2,3}
V(G2)={0,1,2,3,4,5,6}
V(G3)={0,1,2}
1
2
3
0
3
1
2
5
4
G2
6
2
G3
incomplete graph
E(G1)={(0,1),(0,2),(0,3),(1,2),(1,3),(2,3)}
E(G2)={(0,1),(0,2),(1,3),(1,4),(2,5),(2,6)}
E(G3)={<0,1>,<1,0>,<1,2>}
The Graph ADT (4/13)
 Restrictions on graphs
 A graph may not have an edge from a vertex, i, back to
itself. Such edges are known as self loops
 A graph may not have multiple occurrences of the same
edge. If we remove this restriction, we obtain a data
referred to as a multigraph
feedback
loops
multigraph
The Graph ADT (5/13)
 Adjacent and Incident
 If (v0, v1) is an edge in an undirected graph,
 v0 and v1 are adjacent
 The edge (v0, v1) is incident on vertices v0 and v1
V0
V1
 If <v0, v1> is an edge in a directed graph
 v0 is adjacent to v1, and v1 is adjacent from v0
 The edge <v0, v1> is incident on v0 and v1
V0
V1
The Graph ADT (6/13)
 A subgraph of G is a graph G’ such that V(G’)  V(G) and
E(G’)  E(G).
0
1
2
3
G1
0
1
2
G3
The Graph ADT (7/13)
 Path
 A path from vertex vp to vertex vq in a graph G, is a
sequence of vertices, vp, vi1, vi2, ..., vin, vq, such that (vp, vi1),
(vi1, vi2), ..., (vin, vq) are edges in an undirected graph.
 A path such as (0, 2), (2, 1), (1, 3) is also written as 0, 2, 1, 3
 The length of a path is the number of edges on it
0
1
0
2
3
1
0
2
3
1
2
3
The Graph ADT (8/13)
 Simple path and cycle
 simple path (simple directed path): a path in
which all vertices, except possibly the first and the
last, are distinct.
 A cycle is a simple path in which the first and the
last vertices are the same.
0
1
2
3
The Graph ADT (9/13)
 Connected graph
 In an undirected graph G, two vertices, v and v , are
connected if there is a path in G from v to v
 An undirected graph is connected if, for every pair of
distinct vertices v , v , there is a path from v to v
0
1
0
i
j
1
i
j
 Connected component
 A connected component of an undirected graph is a
maximal connected
subgraph.
 A tree is a graph
that is connected
and acyclic (i.e,
has no cycle).
The Graph ADT (10/13)
 Strongly Connected Component
 A directed graph is strongly connected if there is a directed
path from vi to vj and also from vj to vi
 A strongly connected component is a maximal subgraph
that is strongly connected
0
1
2
G3 not strongly connected
strongly connected component
(maximal strongly connected subgraph)
0
1
2
The Graph ADT (11/13)
 Degree
 The degree of a vertex is the number of edges incident to that
vertex.
 For directed graph
 in-degree (v) : the number of edges that have v as the head
 out-degree (v) : the number of edges that have v as the tail
 If di is the degree of a vertex i in a graph G with n vertices
and e edges, the number of edges is
n1
d )/ 2
e(
i
0
Weighted Graphs
Each edge has an associated weight or cost.
Clinton
20
Mukilteo
30
Kingston
35
Bainbridge
Edmonds
Seattle
60
Bremerton
There may be more
information in the graph as well.
Paths and Cycles
A path is a list of vertices {v1, v2, …, vn} such
that (vi, vi+1)  E for all 0  i < n.
A cycle is a path that begins and ends at the
same node.
Chicago
Seattle
Salt Lake City
San Francisco
Dallas
p = {Seattle, Salt Lake City, Chicago, Dallas, San Francisco, Seattle}
Path Length and Cost
Path length: the number of edges in the path
Path cost: the sum of the costs of each edge
Chicago
3.5
Seattle
2
2
2
Salt Lake City
2.5
2.5
2.5
3
San Francisco
Dallas
length(p) = 5
cost(p) = 11.5
Trees as Graphs
• Every tree is a graph
with some restrictions:
– the tree is directed
– there are no cycles
(directed or undirected)
– there is a directed path
from the root to every
node
A
B
C
D
E
F
G
BAD!
I
H
J
Directed Acyclic Graphs (DAGs)
DAGs are directed
graphs with no
cycles.
if program call
graph is a DAG,
then all
procedure calls
can be in-lined
main()
mult()
add()
access()
Trees  DAGs  Graphs
read()
Directed vs. undirected graphs
• When the edges in a graph have no
direction, the graph is called undirected
Directed vs. undirected graphs
(cont.)
• When the edges in a graph have a direction,
the graph is called directed (or digraph)
Warning: if the graph is directed, the
order of the vertices in each edge is
important !!
E(Graph2) = {(1,3) (3,1) (5,9) (9,11) (5,7)
Directed vs. Undirected Graphs
• In directed graphs, edges have a specific direction:
Han
Luke
Leia
• In undirected graphs, they don’t (edges are two-way):
Han
Luke
• Vertices u and v are adjacent ifLeia
(u, v)  E
Trees vs graphs
• Trees are special cases of graphs!!
Graph terminology
• Adjacent nodes: two nodes are adjacent if
they are connected by an edge
5 is adjacent to 7
7 is adjacent from 5
• Path: a sequence of vertices that connect two
•
nodes in a graph
Complete graph: a graph in which every vertex
is directly connected to every other vertex
Complete Graph
A complete graph is a graph that has the
maximum number of edges
– for undirected graph with n vertices, the maximum
number of edges is n(n-1)/2
– for directed graph with n vertices, the maximum
number of edges is n(n-1)
– example: G1 is a complete graph
CHAPTER 6
24
Examples for Graph
0
1
0
0
2
3
G1
1
3
5
4
G2
complete graph
V(G1)={0,1,2,3}
V(G2)={0,1,2,3,4,5,6}
V(G3)={0,1,2}
2
1
6
incomplete graph
2
G3
E(G1)={(0,1),(0,2),(0,3),(1,2),(1,3),(2,3)}
E(G2)={(0,1),(0,2),(1,3),(1,4),(2,5),(2,6)}
E(G3)={<0,1>,<1,0>,<1,2>}
complete undirected graph: n(n-1)/2 edges
complete directed graph: n(n-1) edges
CHAPTER 6
25
Adjacent nodes & Incident nnodes
If (v0, v1) is an edge in an undirected graph,
– v0 and v1 are adjacent nodes
– The edge (v0, v1) is incident on vertices v0 and v1
If <v0, v1> is an edge in a directed graph
– v0 is adjacent to v1, and v1 is adjacent from v0
– The edge <v0, v1> is incident on v0 and v1
CHAPTER 6
26
*Figure 6.3:Example of a graph with feedback loops and a
multigraph (p.260)
0
0
2
1
1
3
2
self edge
(b)
(a)
multigraph:
multiple occurrences
of the same edge
Figure 6.3
CHAPTER 6
27
Subgraph and Path
A subgraph of G is a graph G’ such that V(G’)
is a subset of V(G) and E(G’) is a subset of E(G)
A path from vertex vp to vertex vq in a graph G,
is a sequence of vertices, vp, vi1, vi2, ..., vin, vq,
such that (vp, vi1), (vi1, vi2), ..., (vin, vq) are edges
in an undirected graph
The length of a path is the number of edges on
it
CHAPTER 6
28
Figure 6.4: subgraphs of G1 and G3 (p.261)
0
1
0
2
0
1
1
2
2
3
0
1
3
G1
G3
2
3
(i)
(ii)
(iii)
(a) Some of the subgraph of G1
(iv)
0
0
0
0
0
1
1
1
2
2
1
2
(i)
(ii)
(iii)
(iv)
(b) Some of the subgraph of G3
CHAPTER 6
30
Connected Component
A connected component of an undirected graph
is a maximal connected subgraph.
A tree is a graph that is connected and acyclic.
A directed graph is strongly connected if there
is a directed path from vi to vj and also
from vj to vi.
A strongly connected component is a maximal
subgraph that is strongly connected.
CHAPTER 6
31
*Figure 6.5: A graph with two connected components (p.262)
connected component (maximal connected subgraph)
H1
H2
0
5
1
2
4
6
3
7
G4 (not connected)
CHAPTER 6
32
*Figure 6.6: Strongly connected components of G3 (p.262)
not strongly connected
strongly connected component
(maximal strongly connected subgraph)
0
0
2
1
1
2
G3
CHAPTER 6
33
Degree
The degree of a vertex is the number of edges
incident to that vertex
For directed graph,
– the in-degree of a vertex v is the number of edges
that have v as the head
– the out-degree of a vertex v is the number of
edges
that have v as the tail
– if di is nthe
degree of a vertex i in a graph G with n
1
vertices and e edges, the number of edges is
e(di )/ 2
0
CHAPTER 6
34
undirected graph
degree
3
3
0
0
2
1
1
2
2
3
3
3
3
G1
3
3
4
1
1
0
in:1, out: 1
1
in: 1, out: 2
2
in: 1, out: 0
G2
5
6
1
1
directed graph
in-degree
out-degree
G3CHAPTER 6
35
Graph Representations
Adjacency Matrix
Adjacency Lists
Adjacency Multilists
CHAPTER 6
36
Graph Representations (1/13)
 Adjacency Matrix
 Adjacency Lists
 Adjacency Multilists
Graph implementation
• Array-based implementation
– A 1D array is used to represent the vertices
– A 2D array (adjacency matrix) is used to
represent the edges
Graph implementation (cont.)
• Linked-list implementation
– A 1D array is used to represent the vertices
– A list is used for each vertex v which contains the
vertices which are adjacent from v (adjacency list)
Adjacency Matrix
Let G=(V,E) be a graph with n vertices.
The adjacency matrix of G is a two-dimensional
n by n array, say adj_mat
If the edge (vi, vj) is in E(G), adj_mat[i][j]=1
If there is no such edge in E(G), adj_mat[i][j]=0
The adjacency matrix for an undirected graph is
symmetric; the adjacency matrix for a digraph
need not be symmetric
40
Examples for Adjacency Matrix
1
0
1 1 1
0 1 1 
1 0 1

1 1 0
G1
5
6
3
1
7
2
0 1 0


1
0
1


0 0 0
G2
symmetric
undirected: n2/2
directed: n2
1
2
2
3
0
1

1

1
4
0
0
0
1

1

0
0

0
0

0
1 1
0 0
0 0
1 1
0 0
0 0
0 0
0 0
0 0 0 0 0
1 0 0 0 0 
1 0 0 0 0

0 0 0 0 0
0 0 1 0 0

0 1 0 1 0
0 0 1 0 1

0 0 0 1 0 
G4
41
0
1
4
0
1
2
2
6
3
3
0
1
2
3
1
0
0
0
7
2
2
1
1
1
0
2
G3
3
3
3
2
0
G1
0
1
2
5
1
2
0
1
2
3
4
5
6
7
1
0
0
1
5
4
5
6
G4
2
3
3
2
6
7
42
An undirected graph with n vertices and e edges ==> n head nodes and 2e list nodes
Some Graph Operations
Traversal
Given G=(V,E) and vertex v, find all wV,
such that w connects v.
– Depth First Search (DFS)
preorder tree traversal
– Breadth First Search (BFS)
level order tree traversal
Connected Components
Spanning Trees
43
Recall: Tree Traversals
a
b
f
c
h
g
e
d
k
i
j
l
abfgkcdhilje
Depth-First Search
• Pre/Post/In – order traversals are examples of
depth-first search
– Nodes are visited deeply on the left-most
branches before any nodes are visited on the
right-most branches
• Visiting the right branches deeply before the left would
still be depth-first! Crucial idea is “go deep first!”
• Difference in pre/post/in-order is how some
computation (e.g. printing) is done at current node
relative to the recursive calls
• In DFS the nodes “being worked on” are kept
on a stack
Iterative Version DFS
Pre-order Traversal
Push root on a Stack
Repeat until Stack is empty:
Pop a node
Process it
Push it’s children on the Stack
Level-Order Tree Traversal
• Consider task of traversing tree level by level from top to
bottom (alphabetic order)
a
• Is this also DFS?
b
f
c
h
g
k
e
d
i
j
l
Breadth-First Search
• No! Level-order traversal is an example of Breadth-First
Search
• BFS characteristics
– Nodes being worked on maintained in a FIFO Queue, not a stack
– Iterative style procedures often easier to design than recursive
procedures
Put root in a Queue
Repeat until Queue is empty:
Dequeue a node
Process it
Add it’s children to queue
QUEUE
a
bcde
cdefg
defg
efghij
fghij
ghij
hijk
ijk
jkl
kl
l
a
b
f
c
h
g
k
e
d
i
j
l
Graph Traversals
• Depth first search and breadth first search also
work for arbitrary (directed or undirected) graphs
– Must mark visited vertices so you do not go into an
infinite loop!
• Either can be used to determine connectivity:
– Is there a path between two given vertices?
– Is the graph (weakly) connected?
• Important difference: Breadth-first search always
finds a shortest path from the start vertex to any
other (for unweighted graphs)
– Depth first search may not!
A spanning tree is a minimal subgraph, G’, of G
such that V(G’)=V(G) and G’ is connected.
Any connected graph with n vertices must have
at least n-1 edges.
A biconnected graph is a connected graph that has
0
no articulation points.
1
2
biconnected graph
3
4
5
6
7
CHAPTER 6
51
Examples of Spanning Tree
0
1
2
3
G1
1
0
0
0
2
1
2
1
3
3
2
3
Possible spanning trees
CHAPTER 6
52
Spanning Trees
Either dfs or bfs can be used to create a
spanning tree
– When dfs is used, the resulting spanning tree is
known as a depth first spanning tree
– When bfs is used, the resulting spanning tree is
known as a breadth first spanning tree
While adding a nontree edge into any spanning
tree, this will create a cycle
CHAPTER 6
53
DFS VS BFS Spanning Tree
0
0
1
3
2
4
7
5
0
1
6 3
2
4
7
5
1
6 3
nontree edge
cycle
DFS Spanning
CHAPTERcontact
6
For more detail
us
2
4
5
6
7
BFS Spanning
54