Graphs: Nitin Upadhyay
Graphs: Nitin Upadhyay
Nitin Upadhyay
February 25, 2006
Discussion
What is a Graph?
Applications of Graphs
Categorization
Terminology
Discussion
Hamiltonian Cycle
Graph Representations
Hamiltonian Cycle
1 3
1,4,5,3,2,1 is a Hamiltonian
2 Cycle
4
5
Special Graph Structures
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
K1 K2 K3 K4
K5 K6
n −1
n(n − 1)
Note that Kn has ∑i = 2 edges.
i =1
Cycles
C3 C4 C5 C6 C8
C7
How many edges are there in Cn?
Wheels
W3 W4 W5 W6 W8
W7
How many edges are there in Wn?
n-cubes (hypercubes)
Q0
Q1 Q2 Q4
Q3
Number of vertices: 2n. Number of edges:Exercise to try!
Bipartite Graphs
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
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
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
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
1 3 1 2
2 1 3
2 3 1 5
4 4 1 5
5 5
Adjacency List
9 1 2 4
1 3
2 3 5
4 2 5 3 2 5
1 9
Questions
Questions ?