Professional Documents
Culture Documents
ALGORITHMS
CHAPTER VII
Graph Data Structure
By: Mr.KENNETH H M
BNCS 2
What is a Graph
Rule 1 − Visit adjacent unvisited vertex. Mark it visited. Display it. Push it in a
stack.
Rule 2 − If no adjacent vertex found, pop up a vertex from stack. (It will pop up
all the vertices from the stack which do not have adjacent vertices.)
Rule 3 − Repeat Rule 1 and Rule 2 until stack is empty.
1.
2.
4.
6.
We start from
visiting S(starting node), and
mark it visited.
2
We then see unvisited
adjacent node from S. In this
example, we have three
nodes but alphabetically we
choose A mark it visited and
3 enqueue it.
4
Next unvisited adjacent node
5 from S is C. We mark it visited
and enqueue it.
Examples:
Citieswith distances between
Roads with distances between intersection points
Course prerequisites
Network
Social networks
5 is adjacent to 7
7 is adjacent from 5
Path: a sequence of vertices that connect two
nodes in a graph
Complete graph: a graph in which every vertex
is directly connected to every other vertex
A path is a list of edges such that each node (but the last) is the
predecessor of the next node in the list
A cycle is a path whose first and last nodes are the same
Example: (London,
60
Birmingham Rugby Bristol, Birmingham,
150 London, Dover) is a path
190 100
140 Cambridge Example: (London,
London Bristol, Birmingham,
190 120
London) is a cycle
Bristol 110 Dover
A cyclic graph contains at
least one cycle
Southhampton
An acyclic graph does not
contain any cycles
© ISBAT UNIVERSITY – 2020. 6/5/2021
Un-directed Graph Directed Graph A
A
B C
B C
D E
D E
Set of vertices = { A, B, C, D, E } Set of vertices = { A, B, C, D, E }
Set of edges = { ( A, B ) , ( A, C ) , ( B, Set of edges = { ( A, B ) , ( A, C ) , ( B, B ), ( B,
D ), ( C, D ),( C, E ) , ( D, E ) } D ), ( C, A ), ( C, D ), ( C, E ) , ( E, D )
A graph in which every edge is ,(D,F), (F,E)}
undirected is known as Un directed A graph in which every edge is directed is
graph. known as Directed graph.
The set of edges are unordered pairs. The set of edges are ordered pairs.
Edge ( A, B ) and ( B, A ) are treated Edge ( A, B ) and ( B, A ) are treated as
as same edge. different edges.
The edge connected to same vertex ( B, B ) is
called loop.
Out degree : no of edges originating at a given
node.
In degree : no of edges terminating at a given
node.
© ISBAT UNIVERSITY – 2020. For node C - In degree is 1 and out degree 6/5/2021
is 3 and
total degree is 4.
Representation of Graph
A Set of vertices = { A, B, C, D, E }
Set of edges = { ( A, B ) , ( A, C ) , ( A, D ), ( B, D ), ( B, D ),
( C, D ),( C, E ) , ( D, E ) }
B C There are two ways of representing a graph in memory.
i) Sequential Representation by means of Adjacency Matrix.
ii) Linked Representation by means of Linked List.
D
E
Linked List
Adjacency Matrix
A B C D NULL
A B C D E
A 0 1 1 1 0 B A C D NULL
B 1 0 1 1 0
C 1 1 0 1 1 E NULL
C A B D
D 1 1 1 0 1
E 0 0 1 1 0
C E NULL
D A B
Adjacency Matrix is a bit matrix which
contains entries of only 0 and 1 D NULL
N E C
The connected edge between to vertices is
represented by 1 and absence of edge is
represented by 0.
Adjacency matrix of an undirected graph is
symmetric.
© ISBAT UNIVERSITY – 2020. 6/5/2021
Adjacency-matrix representation I
34
q nodeSet = {A, B, C, D, E, F, G}
A
p B C
t r
D E edgeSet = { p: (A, E),
v
s u
w F q: (B, C), r: (E, B),
G
s: (D, D), t: (D, A),
Here we have a set of nodes, u: (E, G), v: (F, E),
and each node contains only
w: (G, D) }
its element (not shown)
q
A A {p} p: (A, E)
p B C
t r
B {q} q: (B, C)
D E
v C {} r: (E, B)
s u
w F
G D { s, t } s: (D, D)
Thank you