Graphs
Nitin Upadhyay
February 25, 2006
Discussion
What is a Graph?
Applications of Graphs
Categorization
Terminology
Discussion
Hamiltonian Cycle
Special Graph Structures
Graph Representations
Hamiltonian Cycle
Hamiltonian Cycle: A Cycle that contains all
the vertices of the graph
1 3
1,4,5,3,2,1 is a Hamiltonian
2 Cycle
4
5
Special Graph Structures
Special cases of undirected graph structures:
Complete graphs K
n
Cycles Cn
Wheels Wn
n-Cubes Qn
Bipartite graphs
Complete bipartite graphs Km,n
Revisiting Paths and
cycles
A path is a sequence of nodes
v1, v2, …, vN such that (vi,vi+1)∈E for 0<i<N
The length of the path is N-1.
Simple path: all vi are distinct, 0<i<N
A cycle is a path such that v1=vN
An acyclic graph has no cycles
Complete Graphs
For any n∈N, a complete graph on n vertices,
Kn, is a simple graph with n nodes in which
every node is adjacent to every other node: ∀
u,v∈V: u≠v↔{u,v}∈E.
K1 K2 K3 K4
K5 K6
n −1
n(n − 1)
Note that Kn has ∑i = 2 edges.
i =1
Cycles
For any n≥3, a cycle on n vertices, Cn, is a
simple graph where V={v1,v2,… ,vn} and
E={{v1,v2},{v2,v3},…,{vn−1,vn},{vn,v1}}.
C3 C4 C5 C6 C8
C7
How many edges are there in Cn?
Wheels
For any n≥3, a wheel Wn, is a simple graph
obtained by taking the cycle Cn and adding
one extra vertex vhub and n extra edges
{{vhub,v1}, {vhub,v2},…,{vhub,vn}}.
W3 W4 W5 W6 W8
W7
How many edges are there in Wn?
n-cubes (hypercubes)
For any n∈N, the hypercube Qn is a simple
graph consisting of two copies of Qn-1
connected together at corresponding nodes.
Q0 has 1 node.
Q0
Q1 Q2 Q4
Q3
Number of vertices: 2n. Number of edges:Exercise to try!
Bipartite Graphs
Let G=(V,E) be a graph, G is said to be
bipartite graph:
If its vertex set V can be partitioned into two
nonempty disjoint subsets V1 and V2, called
a bipartition.
And edge connecting one of its end in V1 and
other with V2.
Complete Bipartite Graphs
A complete bipartite graph is a bipartite graph
with partition V1 and V2 in which each vertex
of V1 is joined by an edge to each vertex of
V2.
A complete bipartite graph with | V1 | = m and
| V2 | = n is denoted as Km,n.
Subgraphs
A subgraph of a graph G=(V, E) is a graph
H=(W, F) where W⊆V and F⊆E.
G H
Graph Representations
Adjacency-matrix representation
Incidence Matrix representation
Edge-set representation
Adjacency-set representation
Adjacency List
Adjacency-matrix
representation
Adjacency Matrix Representation
A
B C One simple way of
D E representing a graph
F is the adjacency
G
matrix
AB CD EF G A 2-D array has a
A
B mark at [i][j] if there
C is an edge from node i
D to node j
E
F
The adjacency matrix
G is symmetric about
the main diagonal
Adjacency-matrix
representation
Adjacency Matrix (A)
The Adjacency Matrix A=(ai,j) of a graph G=(V,E)
with n nodes is an nXn matrix
Each element of A is either 0 or 1, depending on
the adjacency of the nodes.
1 if (i,j) Є E
aij =
0 otherwise
Adjacency-matrix
representation
Find the adjacency matrices of the following
graphs 1 2 3 4 5
1 3 1 0 1 1 1 0
2 1 0 1 0 0
2 3 1 1 0 0 1
4 4 1 0 0 0 1
5 5 0 0 1 1 0
1 2 3
1 3 1 0 1 0
2 0 0 1
2 3 1 1 0
Adjacency-matrix
representation
Representation
An adjacency matrix
A
B C can equally well be
E used for digraphs
D
(directed graphs)
F
G A 2-D array has a
AB CD EF G mark at [i][j] if there
A
B
is an edge from node i
C to node j
D
E
F
G
Incidence Matrices
Let G = (V, E) be an undirected graph and v1,
v2, …, vn be the vertices and e1, e2, …, em be
the edges of G. Then the incidence matrix
with respect to this ordering of V and E is the
n x m matrix M = [mij], where
1 when edge ej is incident with vi,
mij =
0 otherwise
Incidence Matrix Example
Represent the graph shown below with an
incidence matrix.
e1 e2 e3 e4 e5 e6 e7 e8 e9
v1 v2 v3
v 1 1 1 0 0 0 0 0 0 0
v 2 1 0
e1 e3
0 1 1 1 1 0 0
e6
v3 0 0
e7
0 1 0 0 0 1 1
e4
e2
e5
e8
v4 x x x x x x x x x
v4 e9 v5 v6 v5 x x x x x x x x x
v6 x x x x x x x x x
For your exercise
Edge-set representation
An edge-set representation uses a set of nodes and a set of
edges
The sets might be represented by, say, linked lists
The set links are stored in the nodes and edges themselves
The only other information in a node is its element (that is, its
value)—it does not hold information about its edges
The only other information in an edge is its source and
destination (and attribute, if any)
If the graph is undirected, we keep links to both nodes, but don’t
distinguish between source and destination
This representation makes it easy to find nodes from an edge,
but you must search to find an edge from a node
This is seldom a good representation
Edge-set representation
nodeSet = {A, B, C, D, E, F, G}
edge
q
A
p B C edgeSet = { p: (A, E),
t r
D E q: (B, C), r: (E, B),
v
s u
w F s: (D, D), t: (D, A),
G
u: (E, G), v: (F, E),
Here we have a set of nodes,
and each node contains only its w: (G, D) }
element (not shown)
Each edge contains references to its source
and its destination (and its attribute, if any)
Adjacency-set
representation
An adjacency-set representation uses a set
of nodes
Each node contains a reference to the set of its
edges
For a directed graph, a node might only know
about (have references to) its out-edges
Thus, there is not one single edge set, but
rather a separate edge set for each node
Each edge would contain its attribute (if any) and
its destination (and possibly its source)
Adjacency-set
representation
A {p} p: (A, E)
Adjacency B {q} q: (B, C)
q
A C {} r: (E, B)
p B C
t r
D { s, t } s: (D, D)
D E
v t: (D, A)
s u E { r, u }
w F
G F {v} u: (E, G)
Here we have a set of v: (F, E)
G {w}
nodes, and each node
w: (G, D)
refers to a set of edges
Each edge contains references to its
source and its destination (and its
attribute, if any)
Adjacency-set
representation
If the edges have no associated attribute, there is no
need for a separate Edge class
Instead, each node can refer to a set of its neighbors
In this representation, the edges would be implicit in the
connections between nodes, not a separate data structure
For an undirected graph, the node would have
references to all the nodes adjacent to it
For a directed graph, the node might have:
references to all the nodes adjacent to it, or
references to only those adjacent nodes connected by an
out-edge from this node
Adjacency-set
representation A {E}
Adjacency B {C}
A
B C C {}
D E D { D, A }
F E { B, G }
G
F {E}
Here we have a set of
G {D}
nodes, and each node
refers to a set of other
(pointed to) nodes
The edges are implicit
Adjacency List
An Adjacency list is an array of lists, each list
showing the vertices a given vertex is
adjacent to
1 3 1 2
2 1 3
2 3 1 5
4 4 1 5
5 5
Adjacency List
Adjacency List of a Weighted Graph
The weight is included in the list
9 1 2 4
1 3
2 3 5
4 2 5 3 2 5
1 9
Questions
Questions ?