CS205M Graph1
CS205M Graph1
Chapter 8 (Rosen)
Applications (examples)
Computer networks
Distinguish between two chemical compounds
with the same molecular formula but different
structures
Solve shortest path problems between cities
Scheduling exams
Assign channels to television stations, etc.
Definitions
A Graph G=(V, E) consists a finite nonempty set of
vertices (or nodes) denoted by V(G), or simply by V
and a set of edges E(G), or simply by E (which is a
subset of V × V).
u v
u v
Definitions – Edge Type
Loop: A loop is an edge whose endpoints are equal
i.e., an edge joining a vertex to itself is called a loop.
Example: G(V, E), V = {u, v, w}, E = {(u, v), (v, w), (u, w)}
u v
w
Definitions – Graph Type
Multigraphs: Graphs that may have multiple edges
connecting the same vertices
u
e1 e2 w
e3
v
Definitions – Graph Type
Pseudograph: Graphs that may include loops and possibly
multiple edges connecting the same pair of vertices.
u
e1 w e4
e2
v e3
Definitions – Graph Type
Directed Graph: G(V, E), set of vertices V, and set of Edges E,
that are ordered pair of elements of V (directed edges)
Example: G(V, E), V = {u, v, w}, E = {(u, v), (v, w), (w, u)}
u v
w
Definitions – Graph Type
Directed Multigraph:
Example: V = {u, v, w}, E = {e1, e2, e3, e4}
u
w e4
e1 e2
v e3
Simple Undirected graphs
u and v are adjacent (or neighbours) if e = (u, v) is an edge; e is called
incident with (or on/to) u and v. u and v are called endpoints of (u, v). Edge
e is said to join u and v.
Degree of a Vertex, deg (v), is the number of edges incident on a vertex.
A loop contributes twice to the degree (why?). [deg(v) or simply d(v)]
u v
k
w
Directed graphs
For the directed edge (u, v), u is adjacent to v OR v is adjacent from u.
u – Initial vertex, v – Terminal vertex
In-degree, deg- (u), is the number of edges for which u is terminal vertex
Out-degree, deg+ (u), is the number of edges for which u is initial vertex
u v
w
Theorem: Undirected Graphs
Theorem 1
The Handshaking theorem:
2e deg( v )
vV
Why?
Every edge connects 2 vertices
Theorem: Undirected Graphs
Theorem 2:
An undirected graph has even number of vertices with odd degree
Pr oof V 1 is the set of even degree vertices and V2 refers to odd degree vertices
2e deg(v) deg(u) deg(v)
vV u V1 v V2
K1 K2 K3
K4
Simple graphs – special cases
Cycle: Cn , n ≥ 3 consists of n vertices v1, v2, v3 … vn and edges
(v1, v2), (v2, v3), (v3, v4) … (vn-1, vn), (vn, v1)
C3 C4
Simple graphs – special cases
Wheels: Wn, obtained by adding an additional vertex to Cn and
connecting all vertices to this new vertex by new edges.
Representation Example: W3, W4
W3 W4
Simple graphs – special cases
N-cubes: Qn, vertices represented by 2n bit strings of length n.
Two vertices are adjacent if and only if the bit strings that they
represent differ by exactly one bit positions
Representation Example: Q1, Q2
10 11
0 1
00 01
Q1 Q2
Bipartite graphs
A simple graph G(V, E), is called bipartite if V can be partitioned
into two disjoint sets V1 and V2 such that every edge in the graph
connects a vertex in V1 and a vertex V2 (so that no edge in G
connects either two vertices in V1 or two vertices in V2)
v4
v1
v5
v2
v6
v3
V1 V2
Complete Bipartite graphs
Km,n : It is the graph that has its vertex set portioned into two
subsets of m and n vertices such that there is an edge between
two vertices if and only if one vertex is in the first subset and
the other vertex is in the second subset.
Representation example: K2,3, K3,3
K2,3 K3,3
Subgraphs
A subgraph of a graph G = (V, E) is a graph H =(V’, E’) where V’ is a
subset of V and E’ is a subset of E
u u u
v w v w v
G H1 H2
Union
G = G1 U G2 wherein E = E1 U E2 and V = V1 U V2
G, G1 and G2 are simple graphs
u
u
w v
w w v
G1 G2 G
Subgraphs
A spanning subgraph is a subgraph containing all the vertices of G.
u v w u v w u v w
x y z y x y z
G G1 G2
Subgraphs
The removal of a vertex vi from a graph G results in that subgraph G - vi
of G consisting of all vertices of G except vi and all edges incident with vi.
The removal of an edge ej from G yields the spanning subgraph G – ej of
G containing all edges of G except ej.
u
u
w v
w v w v
G1 G2 G
Weighted Graphs
In a weighted graph, each edge has a weight or cost
Typically numeric
Agra 120
Delhi
500
Jaipur
Representation
e1 e2 e3
u
v 1 0 1
e1 e2
u 1 1 0
v w w 0 1 1
e3
Representation- Adjacency Matrix
The Adjacency Matrix A of a graph G= (V, E), is of dimension N x N ,
where |V | = N , A = [aij]
When there are relatively few edges in the graph the adjacency
matrix is a sparse matrix.
v u w
u
v 0 1 1
u 1 0 1
v w
w 1 1 0
Representation- Adjacency Matrix
Example: directed Graph G (V, E)
v u w
u
v 0 1 0
u 0 0 1
v w
w 1 0 0
Representation- Adjacency List
Each node (vertex) has a list of which nodes (vertex) it is adjacent
u
node Adjacency List
u v,w
v w, u
v w
w u,v
Graph - Isomorphism
G1=(V1, E2) and G2=(V2, E2) are isomorphic if:
There is a bijection f from V1 to V2 with the property
that
Vertices a and b are adjacent in G1 if and only if f(a) and f(b)
are adjacent in G2, for all a and b in V1
(preserves the adjacency)
Function f is called isomorphism
Application Example:
In chemistry, to find if two compounds have the same structure
Graph - Isomorphism
u1 u2 v1 v2
u3 u4 v4
v3
Complement graph
The complement G’ of a graph G=(V, E) also has V as its vertex
set, but two vertices are adjacent in G’ iff they are non-adjacent
in G.
u1 u2 v1 v2
u3 u4
v3 v4
Complement graph
A graph G is said to be self-complementary if G is isomorphic to
its complement G’.
Example: A graph and its complement where they are isomorphic too.
u1 u2 v1 v2
u3 u4
v3 v4
Connectivity
In a graph reachability among vertices by
traversing the edges.
Application Examples:
In a city to city road-network, if one city can be reached from
another city.
b
Example: a, b, c, d, c, b, e e
Walk, trail, path
If no edge is repeated more than once in a walk, then it is a
“trail” (however, a vertex may repeat).
a d
b
Example: a, c, b, e, c, d e
Walk, trail, path
A walk (or trail) in which no vertex appears more than once
is a “path”.
Pn – a path with n vertices
Path length – no. of edges in a d
the path
c
b
Example: a, b, e, c, d e
(a path of length 4)
Closed walk, Cycle
A closed walk is a walk that begins and ends with the same vertex
(otherwise, open).
A closed path is called circuit or cycle, where no vertex (except the initial
and final vertex) appears more that once.
a d
Cycle: 1) a, b, e, d, a b
e
2) b, a, c, d, e, b
Paths and Cycles in Directed Graphs
Example: D
C
A
Example: G (V, E) is connected since for V = {v1, v2, v3, v4, v5},
there exists a path between vi and vj, 1 ≤ i, j ≤ 5
v4
v1 v3
v2 v5
Connected components
Undirected Graph
A graph that is not connected is called disconnected.
v2 v5
Undirected Graph Connectivity
An undirected graph is connected if for all pairs of vertices u≠v,
there exists a path from u to v
n – k ≤ m ≤ (n – k)(n – k + 1)/2
Connectivity – Connectedness
v3
v5
v1
v2
v4
Connectivity – Connectedness
Directed Graph
A directed graph is strongly connected if there is a path from
a to b and from b to a whenever a and b are vertices in the
graph.
A directed graph is weakly connected if there is a
(undirected) path between every two vertices in the underlying
undirected path.
G1 G2 G3
Directed Graph Connectivity
G G1
The Seven Bridges of Königsberg, Germany
Euler:
a b
c d e
Euler Graph
a b
g c d e
The problem in our language:
a b
f c d
Building a cycle:
e {a,b}, {b,c}, {c,f}, {f,a}
c d
e
Delete the cycle:
{a,b}, {b,c}, {c,f}, {f,a}
c d
a b
f c d
“Splice” the circuits in the 2 graphs:
{a,b}, {b,c}, {c,f}, {f,a}
“+”
e {c,d}, {d,e}, {e,c}
“=“
{a,b}, {b,c}, {c,d}, {d,e}, {e,c}, {c,f}
{f,a}
Euler Tour
1. Cycle C := a cycle in G beginning at an arbitrary
vertex v.
1. Add edges successively to form a path that returns to this
vertex (such a cycle exists since degree of every vertex is at least two)
2. H := G – above cycle C
3. While H has edges
1. Sub-cycle sc := a cycle that begins at a vertex in H that
is also in C (e.g., vertex “c ”)
2. H := H – sc (- all isolated vertices)
3. Tour := cycle C “spliced” with sub-cycle sc
4. Tour C has the Euler tour.
Hamiltonian Graph
Hamiltonian path (also called traceable path) is a path that visits
each vertex exactly once.
Is it Hamiltonian?
Yes
.
Hamiltonian Graph