DS Lecture 17 Graphs
DS Lecture 17 Graphs
Lahore Campus
Lecture Outline
• Graphs
• Special Graph Structures
• Sub Graph
• Adjacency List and Adjacency Matrix
• Incidence Matrix
• Path and Distance Matrix
12/19/2021 Discrete Structures 3
Complete Graphs
• For any 𝑛𝑛 ∈ 𝑁𝑁, a complete graph on n vertices, denoted
by 𝐾𝐾𝑛𝑛 , is a simple graph with n nodes in which every node
is adjacent to every other node.∀𝑢𝑢,𝑣𝑣∈𝑉𝑉 : 𝑢𝑢 ≠ 𝑣𝑣 ↔ {𝑢𝑢, 𝑣𝑣} ∈ 𝐸𝐸.
Cycles
• For any 𝑛𝑛 ≥ 3, a cycle on n vertices, denoted by 𝐶𝐶𝑛𝑛 , is a
simple graph where 𝑉𝑉 = 𝑣𝑣1 , 𝑣𝑣2 , … , 𝑣𝑣𝑛𝑛 𝑎𝑎𝑎𝑎𝑎𝑎 𝐸𝐸 = {{𝑣𝑣1 ,
𝑣𝑣2 }, {𝑣𝑣2 , 𝑣𝑣3 }, … , {𝑣𝑣𝑛𝑛−1 , 𝑣𝑣𝑛𝑛 }, {𝑣𝑣𝑛𝑛 , 𝑣𝑣1 }}.
Wheels
• For any 𝑛𝑛 ≥ 3, a wheel 𝑊𝑊𝑛𝑛 , is a simple graph obtained by
taking the cycle 𝐶𝐶𝑛𝑛 and adding an extra vertex 𝑉𝑉ℎ𝑢𝑢𝑢𝑢 and n
extra edges where {{𝑣𝑣ℎ𝑢𝑢𝑢𝑢 , 𝑣𝑣1 }, {𝑣𝑣ℎ𝑢𝑢𝑢𝑢 , 𝑣𝑣2 }, … , {𝑣𝑣ℎ𝑢𝑢𝑢𝑢 , 𝑣𝑣𝑛𝑛 }}.
Bipartite Graph
• A graph G = {V, E} is bipartite graph (two parts) if and only
if 𝑉𝑉 = 𝑉𝑉1 𝑈𝑈 𝑉𝑉2 𝑤𝑤𝑤𝑤𝑤𝑤𝑤𝑤𝑤 𝑉𝑉1 ∩ 𝑉𝑉2 = ∅ and ∀𝑒𝑒∈𝐸𝐸 : ∃𝑣𝑣1 ∈ 𝑉𝑉1 , 𝑣𝑣2 ∈
𝑉𝑉2 : 𝑒𝑒 = {𝑣𝑣1 , 𝑣𝑣2 }
• The graph vertices can be divided into two parts in such a
way that all edges go between the two parts. (No edge in
G connects either two vertices in V1 or two vertices in
V2).
12/19/2021 Discrete Structures 8
Example
12/19/2021 Discrete Structures 9
Theorem
• A simple graph is bipartite if and only if it is possible to
assign one of two different colors to each vertex of the
graph so that no two adjacent vertices are assigned the
same color.
12/19/2021 Discrete Structures 10
Example
• Is this bipartite?
a b
g
c
f
e d
12/19/2021 Discrete Structures 11
Example
• Bipartite Graph
12/19/2021 Discrete Structures 12
Example
12/19/2021 Discrete Structures 13
Example
12/19/2021 Discrete Structures 14
Sub-graphs
• A sub-graph of a graph G = (V ,E) is a graph H = (W, F),
where W ⊆ V and F ⊆ E.
• A sub-graph H of G is a proper sub-graph of G if 𝐻𝐻 ≠ 𝐺𝐺.
G H
12/19/2021 Discrete Structures 16
• V = { a, b, c, d, e, f }
• E = {{a,b},{a,c},{b,c},
{b,e},{b,f},{c,f}}
12/19/2021 Discrete Structures 17
Adjacency List
• A table with 1 row per vertex, listing its adjacent vertices.
12/19/2021 Discrete Structures 18
Adjacency Matrix
• Matrix 𝐴𝐴 = [𝑎𝑎𝑖𝑖𝑖𝑖 ], where 𝑎𝑎𝑖𝑖𝑖𝑖 is 1 if {𝑣𝑣𝑖𝑖 , 𝑣𝑣𝑗𝑗 } is an edge of G,
and is 0 otherwise.
12/19/2021 Discrete Structures 19
Example
Draw a undirected graph with the adjacency matrix
0 1 1 0
1 0 0 1
1 0 0 1
0 1 1 0
Example
Incidence Matrix
• Let 𝐺𝐺 = (𝑉𝑉, 𝐸𝐸) be an undirected graph. Suppose that
1,2, … , 𝑛𝑛 are the vertices and 𝑒𝑒1 , 𝑒𝑒2 , … , 𝑒𝑒𝑚𝑚 are the edges of
G. Then the incidence matrix with respect to this ordering
of V and E is 𝑛𝑛 × 𝑚𝑚 matrix M = [𝑚𝑚𝑖𝑖𝑖𝑖 ], where
1 𝑤𝑤𝑤𝑤𝑤𝑤𝑤 𝑒𝑒𝑒𝑒𝑒𝑒𝑒𝑒 𝑒𝑒𝑗𝑗 𝑖𝑖𝑖𝑖 𝑖𝑖𝑖𝑖𝑖𝑖𝑖𝑖𝑖𝑖𝑖𝑖𝑖𝑖𝑖𝑖 𝑜𝑜𝑜𝑜 𝑖𝑖.
• 𝑚𝑚𝑖𝑖𝑖𝑖 = �
0 𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜
12/19/2021 Discrete Structures 25
Example
12/19/2021 Discrete Structures 26
Path
• A path is a sequence of edges that begins at a vertex of a
graph and travels from vertex to vertex along edges of the
graph.
Path
Find a path between a and e.
Graph Distance
• The graph distance between two vertices in a graph is the
number of edges in a shortest path connecting them.
Eccentricity
• The eccentricity 𝒆𝒆(𝒗𝒗) of a vertex 𝒗𝒗 in a connected graph
𝑮𝑮(𝑽𝑽, 𝑬𝑬) is max 𝒅𝒅(𝒖𝒖, 𝒗𝒗), for all 𝒖𝒖 ∈ 𝑽𝑽. In other words, a
vertex’s eccentricity is equal to the distance from itself to
the vertex farthest away.
Example
Draw a undirected graph with the adjacency matrix
0 1 1 0
1 0 1 1
1 1 0 1
0 1 1 0
Example (Solution)
Un-directed Graph