DSA Lec11 Graph
DSA Lec11 Graph
Data
Structures and
Algorithms
Chapter 11:
Graph
2nd semester AY2024
School of Applied
Digital Technology
3
Image source: http://www.csit.parkland.edu/~mbrandyberry/CS2Java/Lessons/TreesRecursionGraphs/Graphs.shtml
Graph Terminology
• A graph can be represented by G=(V,E).
• V is a set of vertices or nodes and E is a set of
edges.
• If eE, then e={a,b} or e = ab = ba
– a and b are incident upon e
– a and b are adjacent
• Size of a graph --> number of elements or
vertices.
4
Graph Terminology
• A simple graph (V,E) of size 4
– V={a,b,c,d}--> (4 vertices)
– E={ab,ac,ad,bd,cd}-->(5 edges)
– No loops
A simple graph
– and no multiple edges
– Ex.
7
Graph Terminology
• No. of edges in the complete graph on n vertices
is n(n-1)/2
8
Paths
• A walk from vertex a to b in a graph is
– a sequence of edges (a0a1, a1a2,…, ak-1ak)
9
Paths
• If p = a0a1a2 … ak-1ak is a walk,
– p connects a0 to ak
10
Paths
Path Example
• A path is a walk whose vertices are
all distinct.
– abcfde or (ab,bc,cf,fd,de) is a a b c
path of length 5
d e
– The walk abefdbc of length 6 is not a
path because b appears twice.
f
G3, is the
G, G1, G2 are the connected graphs. disconnected
graphs.
12
Cycles
• Graph Cycles:
– A walk is closed if its 2 end points are the same
vertex.
– A cycle is a closed walk of length at least 3 whose
internal vertices are all distinct.
Ex.
• The walk abefa is a cycle.
a b c
• The walk abedbcfa is not a path and
not a cycle. (duplicate vertex b).
• The path abef is not a cycle (it is not d e
closed).
• The walk aba is not a cycle (length of 2) f
13
Cycles
• A graph is acyclic if it contains no cycles.
G
G1 G2
14
Isomorphic Graphs
• Isomorphic means “same form”.
15
Isomorphic Graphs
They must have the same number of:
Vertices, Edges, Connected components, Vertices of each
degree, Cycles of each length
16
Isomorphic Graphs (Ex.)
Are two graphs isomorphic?
17
Isomorphic Graphs (Ex.)
Are two graphs isomorphic?
• Not isomorphic
• G1 has only 14 edges and G
has 16 edges
18
Isomorphic Graphs (Ex.)
Are two graphs isomorphic?
• Not isomorphic
G • G2 has 2 connected components
• G has only 1 connected
components
19
Isomorphic Graphs (Ex.)
Are two graphs isomorphic?
G
• Not isomorphic
• G3 has some vertices of degree 3
and some of degree 5.
• G has degree 4
20
Isomorphic Graphs (Ex.)
The two graphs shown below are isomorphic
21
Image source: http://en.wikipedia.org/wiki/Graph_isomorphism
The Adjacency Matrix for a graph
• An adjacency matrix for a graph (V,E) is a two
dimensional boolean array
boolean[ ][ ] a;
– Assigning true to a[i][j] if vertex vi is adjacent
to vertex vj.
• Ex.
22
The Adjacency Matrix for a graph
• Adjacency matrices are often expressed with
0s and 1s instead of trues and falses.
• a is adjacent to b, c, and d
• b is adjacent to a, and d
• c is adjacent to a and d
• d is adjacent to a, b, and c
23
Excercises b c
Find the following for the graph shown
a f
a) Its size n
d e
b) Its vertex set V
e) A path of length 3
f) A path of length 5
g) A cycle of length 4
24
Exercises (Lecture Assignment)
b c
d e
a b c d e f
a
b
c
d
e
f
25
Graph Class and Method
26
Vertex Methods
• Vertex(char label) →set initial value of vertex title
and visited
27
Graph Methods
• Graph() → initialize list of vertices, adjacency
matrix, num of vertices, vertices' titles
29
Digraphs
• A digraph with V = {a,b,c,d}
30
Digraphs
• If G is a digraph with m edges,
– The sum of all outdegrees = m
– The sum of all indegrees = m
• Ex.
• This digraph has 5 edges
outdegree of a = 2 indegree of a = 1
outdegree of b = 1 indegree of b = 1
outdegree of c = 1 indegree of c = 1
outdegree of d = 1 indegree of d = 2
Sum of outdegree = 5 Sum of indegree= 5
31
Digraphs
• The number of edges in the complete
digraph on n vertices is n(n-1)
• Ex.
The complete graph on 6 vertices
has 6(6-1) = 30
A complete digraph
32
Digraphs
• The number of edges in any digraph on n
vertices is m <= n(n-1)
• Ex.
a b c d
a 0 1 0 1
b 0 0 0 1
c 1 0 0 0
d 0 0 1 0
36
Paths in a Digraph
• Ex.
37
Weighted Digraphs and Graphs
• A weight digraph is a pair (V,w)
– V is set of vertices.
– w is a function assigned to (x,y)
• w(x,y) can be the cost (time or distance)
• w(x,y) = 0 (no edge from x to y)
38
Weighted Digraphs and Graphs
• The weighted path length of the path
cabd is |cabd| = 2+3+2=7
39
Weighted Digraphs and Graphs
A weighted digraph
a b c d
a 0 3 0 4
b 0 0 0 2
c 2 0 0 0
d 0 0 1 0
40
Exercise
• Let G1 be the graph whose adjacency matrix is shown in
the figure. A B C D E F
A 0 0 0 0 0 1
B 0 0 3 0 0 0
a. Draw a directed graph G1
C 0 0 0 0 0 0
D 2 5 0 0 0 0
E 0 0 4 2 0 0
F 0 0 0 0 8 0
41
Exercise A
1
B
F
a. Is a AFEC closed?
2 3
8 5
4 C
E
2
b. Is a AFEDAF cycle? D
c. Is G1 acyclic?
42
Euler Path and cycle
• An euler path is a walk including each edge exactly
once. (not mentioned on vertices)
– G is eulerian.
45
Hamiltonian Path and Cycle
• A Hamiltonian path in a graph is a path including
each vertex exactly once.
• A Hamiltonian cycle is a cycle including each
vertex exactly once.
• A Hamiltonian graph is a graph having a
hamiltonian cycle.
48
Table to show a trace of the BFS algorithm
Q x L (list of vertices) y T (set of edges)
A A A B AB
B E AB, AE
B, E B C AB, AE, BC
E, C A, B F AB, AE, BC, BF
E, C, F E A, B, E
C, F C A, B, E, E D AB, AE, BC, BF, CD
F, D G AB, AE, BC, BF, CD, CG
F, D, G F A, B, E, C, F
D, G D A, B, E, C, F, D
G G A, B, E, C, F, D, G
49
BFS demonstration
A B C D
E F G
Q x y T
(adjacency of x) (set of edges)
A A B AB
B E AB, AE
B, E B C AB, AE, BC
E, C F AB, AE, BC, BF
E, C, F E
C, F C D AB, AE, BC, BF, CD
F, D G AB, AE, BC, BF, CD, CG
F, D, G F
D, G D
G G
50
Graph Traversal Algorithms
• The Depth-first search algorithm
Let T be an empty set of edges.
Let L be an empty list of vertices.
1. Initialize an empty stack S for temporary storage of vertices.
2. Add v0 to L.
3. Push v0 onto S.
4. Mark v0 visited.
5. Repeat steps 6-8 while S is not empty.
6. Let x be the top element on S.
7. If x has any adjacent unvisited vertices, do steps 9-13.
8. Otherwise, pop the stack S and go back to step 5.
9. Let y be an unvisited vertex that is adjacent to x.
10. Add the edge xy to T.
11. Add y to L.
12. Push y onto S.
13. Mark y visited.
51
L S x y T
A A A B AB
A, B A,B B C AB, BC
A, B, C A, B, C C D AB, BC, CD
A, B, C, D A, B, C, D D G AB, BC, CD, DG
A, B, C, D, G A, B, C, D, G G
A, B, C, D D
A, B, C C F AB, BC, CD, DG, CF
A, B, C, D, G, F A, B, C, F F
A, B, C C
A, B B E AB, BC, CD, DG, CF, BE
A, B, C, D, G, F, E A, B, E E
A, B B
A A
52
DFS demonstration
A B C D
E F G
S x y T
(Stack) (top of stack) (spanning tree)
A A B AB
A,B B C AB, BC
A, B, C C D AB, BC, CD
A, B, C, D D G AB, BC, CD, DG
A, B, C, D, G G
A, B, C, D D
A, B, C C F AB, BC, CD, DG, CF
A, B, C, F F
A, B, C C
A, B B E AB, BC, CD, DG, CF, BE
A, B, E E
A, B B
A A 53
Summary
• Why we use graphs?
• Explain the graph terminologies.
• What are different between paths and cycles?
• What is isomorphic graph?
• How to represent the graphs?
• What are digraphs?
• What are weighted digraphs and graphs?
• What is euler paths and hamiltonian cycles?
• Explain BFS and DFS Algorithms.
54