0% found this document useful (0 votes)
7 views54 pages

DSA Lec11 Graph

Chapter 11 of the document covers graphs, including terminology, types of graphs (digraphs, weighted graphs), paths, cycles, and traversal algorithms. It explains concepts such as isomorphic graphs, adjacency matrices, Euler paths, and Hamiltonian cycles. The chapter also includes exercises for practical understanding of graph theory.

Uploaded by

6731503077
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views54 pages

DSA Lec11 Graph

Chapter 11 of the document covers graphs, including terminology, types of graphs (digraphs, weighted graphs), paths, cycles, and traversal algorithms. It explains concepts such as isomorphic graphs, adjacency matrices, Euler paths, and Hamiltonian cycles. The chapter also includes exercises for practical understanding of graph theory.

Uploaded by

6731503077
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 54

1501118

Data
Structures and
Algorithms

Chapter 11:
Graph
2nd semester AY2024
School of Applied
Digital Technology

Copyright 2025 School of ADT


Outline
• Introduction
• Graph Terminology
– Paths and Cycles
– Isomorphic Graphs
– The adjacency and incidence matrix
• Digraphs
• Weighted Digraphs and Graphs
• Euler Paths and Hamiltonian Cycles
• Graph Traversal Algorithms (self-study)
• Summary
2
Why Graphs?
• More general structure than the trees.
• Ex. of graph representation:
– Systems of roads, airline flights from city to city, how
the Internet is connected, and prerequisites and other
interdependencies among courses

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 eE, 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

Not a simple graph,


but it is a graph
5
Image source: JOHN R. HUBBARD Data Structure with Java.
Graph Terminology
• Degree of a vertex = no. of edges that are incident
upon it
– Ex.
G2 G1

a has degree 2 and a and b have degree 1


b has degree 1

– The sum of the degrees of the vertices with m


edges is 2m --> e.g. G2 has 6 degrees
6
Graph Terminology
• A complete graph :
– a simple graph in which every pair of vertices is
connected by an edge.

– Ex.

The complete graph on a set of 4 vertices


• V = {a,b,c,d} and E = {ab,ac,ad,bc,bd,cd}

7
Graph Terminology
• No. of edges in the complete graph on n vertices
is n(n-1)/2

– there are n(n-1)/2 unordered pairs i.e. {a,b}, {a,c},{a,d},


{b,c}, {b,d}, and {c,d}

• No. of edges in any graph on n vertices is


m <= 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)

– or more simply as a0a1a2 … ak-1ak

– where a0 = a and ak= b

– that forming a chain of connected vertices from a to b.

– Length of a walk = no. of k edges that form the walk.

9
Paths
• If p = a0a1a2 … ak-1ak is a walk,

– then p is a walk from a0 to ak (or from ak


to a0 ).

– p connects a0 to ak

– a0 and ak are the terminal or end points of the


walk.

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

– The walk abefa of length 4 is not a


path.

– abf is not a walk (bf is not an edge). 11


Paths
• A graph is connected if every pair of its vertices are
connected by some path.

• A graph not connected is called disconnected.


G G1 G2 G3

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

Cyclic graph Acyclic graph Acyclic graph

• A tree is defined as a connected acyclic graph.

14
Isomorphic Graphs
• Isomorphic means “same form”.

“The 1st graph can be twisted around to the same shape as


the 2nd graph without breaking any of the edge connections”

• Two graphs which are isomorphic must have the same


number of:
• Vertices
• Edges
• Connected components
• Vertices of each degree
• Cycles of each length

15
Isomorphic Graphs
They must have the same number of:
Vertices, Edges, Connected components, Vertices of each
degree, Cycles of each length

Two graphs that are isomorphic

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

c) Its edge set E

d) The degree d(x) of each vertex x

e) A path of length 3

f) A path of length 5

g) A cycle of length 4

24
Exercises (Lecture Assignment)
b c

h. Its adjacency matrix a f

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

• getVisit() → return if this vertex is visited

• setVisit(boolean ok) → set this vertex as visited

• getTitle() → return title of this vertex e.g. 'A'

27
Graph Methods
• Graph() → initialize list of vertices, adjacency
matrix, num of vertices, vertices' titles

• addVertex(char title) → add this vertex to the list

• addEdge(int start, int end) → connect these two


vertices
• showVertex (String s, int v) → display vertex's title

• saveVertex (char label) → save this vertex's title


28
Digraphs

• A digraph (or directed graph) is a pair


G=(V,E).
• E is a set of ordered pairs of elements
of V.
• e = (a,b) or e = ab
– The edge e emanates from vertex a and
terminates at vertex b.
– The edge e is incident from a to b

29
Digraphs
• A digraph with V = {a,b,c,d}

• Edge set E = {ab,ad,bd,ca,dc}

• Vertex a has outdegree 2 and indegree 1.

• b and c each have outdegree 1 and indegree 1.

• A simple digraph is a digraph having no loops.

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.

– This graph on 4 vertices has 5 edges :


5 <= 4(4-1)
33
Digraphs
• An adjacency matrix for a digraph.
– 2D boolean array --> boolean[][] a

– Assign true to a[i][j],if there is an edge from vi to vj


• Ex.

A digraph G. The adjacency matrix for a digraph G.


34
Digraphs
• Adjacency matrices are often expressed with
0s and 1s instead of trues and falses.

a b c d
a 0 1 0 1
b 0 0 0 1
c 1 0 0 0
d 0 0 1 0

A digraph G. The adjacency matrix for a digraph G.


35
Paths in a Digraph
• A walk from vertex a to vertex b in a digraph
a b
– is a sequence of edges (a0a1, a1a2,…, ak-1ak )
– Path start at a and end at b.

• A walk is closed if it ends at the same vertex from


which it starts.
• A path is a walk with all distinct vertices.
• A cycle is a closed walk with all interval vertices
distinct.

36
Paths in a Digraph
• Ex.

– adcabdc is a walk of length 6 and not closed


– The walk abdcadca is closed, but not cycle (c and d
are repeated)
– The walk dcab is a path, but not closed.

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)

• Weighted path length is the sum of the weights of the edges


along the path.

38
Weighted Digraphs and Graphs
• The weighted path length of the path
cabd is |cabd| = 2+3+2=7

• The shortest distance from c to d is 6


(along the path cad).

• The shortest distance from c to d is 1


(along the path cd).

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

Its Adjacency matrix

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)

• An euler cycle is a closed walk including each edge


exactly once.

• An eulerian graph is a graph that has an euler


cycle.

• Euler paths and cycles do not need to have distinct


vertices. 43
Euler Path and cycle
• If G is a connected graph, the following
conditions are equivalent:

– G is eulerian.

– The degree of each vertex is even.

– The set of all edges of G can be partitioned into


cycles.
44
Example of Euler Paths
• The closed walk
acedabefdbcfa is an
euler cycle. a b c

– Every vertex has degree 4, d e

and its 12 edges are


partitioned into 3 circles. f

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.

A Hamiltonian graph Not a Hamiltonian graph


(a Hamiltonian path but
no Hamiltonian cycle)
46
Graph Traversal Algorithms
• Two general ways to traverse a graph.

– Breadth-First Search (BFS) algorithm.


• It starts at a node and explores the neighbor nodes
first, before moving to the next level neighbors.

– Depth-First Search (DFS) algorithm.


• it travel as deep as possible from neighbour to
neighbour before backtracking. What determines
how deep is possible is that you must follow edges,
and you don't visit any vertex twice. 47
Graph Traversal Algorithms
• The breadth-first search algorithm

Let T be an empty set of edges.


Let L be an empty list of vertices.
1. Initialize an empty queue Q for temporary storage of vertices.
2. Enqueue v0 into Q.
3. Repeat steps 4-6 while Q is not empty.
4. Dequeue Q into x.
5 Add x to L.
6. Do step 7 for each vertex y that is adjacent to x.
7. If y has not been visited, do steps 8-9.
8. Add the edge xy to T.
9. Enqueue y into Q.

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy