You are on page 1of 40

ECE215 –DATA STRACTURES AND

ALGORITHMS
CHAPTER VII
Graph Data Structure

By: Mr.KENNETH H M

BNCS 2
What is a Graph

 A graph is a pictorial representation of a set of


objects where some pairs of objects are connected
by links.
 The interconnected objects are represented by
points termed as vertices
 and the links that connect the vertices are called
edges.

© ISBAT UNIVERSITY – 2020. 6/5/2021


 A graph is a pair of sets (V, E), where V is the set
of vertices and E is the set of edges, connecting the
pairs of vertices.

In the above graph,


V = {a, b, c, d, e}
E = {ab, ac, bd, cd,
de}

© ISBAT UNIVERSITY – 2020. 6/5/2021


Graph Data Structure
We can represent a graph using an array of vertices and a
two dimensional array of edges.
 Vertex
Each node of the graph is represented as a vertex.
 Path − Path represents a sequence of edges between two
vertices

© ISBAT UNIVERSITY – 2020. 6/5/2021


Basic Operations

 Add Vertex − add a vertex to a graph.


 Add Edge − add an edge between two vertices of a
graph.
 Display Vertex − display a vertex of a graph.

© ISBAT UNIVERSITY – 2020. 6/5/2021


Graph searching

 Problem: find a path between two nodes of the


graph (e.g., Austin and Washington)
 Methods: Depth-First-Search (DFS) or Breadth-
First-Search (BFS)

© ISBAT UNIVERSITY – 2020. 6/5/2021


Depth-First-Search (DFS)

 What is the idea behind DFS?


 Travel as far as you can down a path
 Back up as little as possible when you reach a "dead
end" (i.e., next vertex has been "marked" or there is no
next vertex)
 DFS can be implemented efficiently using a
stack

© ISBAT UNIVERSITY – 2020. 6/5/2021


Breadth-First-Searching (BFS)

 What is the idea behind BFS?


 Look at all possible paths at the same depth before
you go at a deeper level
 Back up as far as possible when you reach a "dead
end" (i.e., next vertex has been "marked" or there is
no next vertex)
 BFS can be implemented efficiently using a queue
BFS can be implemented efficiently using a queue
© ISBAT UNIVERSITY – 2020. 6/5/2021
Depth First Traversal

 Depth First Search algorithm(DFS) traverses a graph in a


depthward motion and uses a stack to remember to get the next
vertex to start a search when a dead end occurs in any iteration.

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.

© ISBAT UNIVERSITY – 2020. 6/5/2021


Step Traversal Description

1.

Initialize the stack

2.

Mark S as visited and put it onto the


stack. Explore any unvisited adjacent
node from S. We have three nodes
and we can pick any of them. For this
example, we shall take the node in
alphabetical order.
3.

Mark A as visited and put it onto


the stack. Explore any unvisited
adjacent node from A.
Both Sand D are adjacent to A but
we are concerned for unvisited
nodes only.

4.

Visit D and mark it visited and put


onto the stack. Here we
have B and C nodes which are
adjacent to D and both are
unvisited. But we shall again
choose in alphabetical order.
5.

We choose B, mark it visited


and put onto stack.
Here B does not have any
unvisited adjacent node. So
we pop B from the stack.

6.

We check stack top for


return to previous node and
check if it has any unvisited
nodes. Here, we find D to
be on the top of stack.
7.

Only unvisited adjacent


node is from D is C now. So
we visit C, mark it visited
and put it onto the stack.
Breadth First Traversal

 Breadth First Search algorithm(BFS) traverses a graph


in a breadthwards motion and uses a queue to
remember to get the next vertex to start a search when
a dead end occurs in any iteration.

 Rule 1 − Visit adjacent unvisited vertex. Mark it visited. Display


it. Insert it in a queue.
 Rule 2 − If no adjacent vertex found, remove the first vertex
from queue.
 Rule 3 − Repeat Rule 1 and Rule 2 until queue is empty.

© ISBAT UNIVERSITY – 2020. 6/5/2021


Initialize the queue.

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.

Next unvisited adjacent node


from S is B. We mark it
visited and enqueue it.

4
Next unvisited adjacent node
5 from S is C. We mark it visited
and enqueue it.

Now S is left with no unvisited


6 adjacent nodes. So we
dequeue and find A.
From A we have D as
unvisited adjacent node. We
7
mark it visited and enqueue
it.
Motivation

 Many algorithms use a graph representation to


represent data or the problem to be solved

Examples:
 Citieswith distances between
 Roads with distances between intersection points

 Course prerequisites

 Network

 Social networks

© ISBAT UNIVERSITY – 2020. 6/5/2021


Graph Classifications

 Graphs can be classified by whether or not their


edges have weights

Weighted graph: edges have a weight

 Weighttypically shows cost of traversing


 Example: weights are distances between cities

© ISBAT UNIVERSITY – 2020. 6/5/2021


 Unweighted graph: edges have no weight

 Edgessimply show connections


 Example: course prereqs

© ISBAT UNIVERSITY – 2020. 6/5/2021


Kinds of Graphs: Directed and Undirected
Graphs can be classified by whether or their edges
are have direction

 Undirected Graphs: each edge can be traversed


in either direction

Directed Graphs: each edge can be traversed only in
a specified direction

© ISBAT UNIVERSITY – 2020. 6/5/2021


Examples

© ISBAT UNIVERSITY – 2020. 6/5/2021


Graph terminology

 Adjacent nodes: two nodes are adjacent if they


are connected by an edge

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

© ISBAT UNIVERSITY – 2020. 6/5/2021


Graph terminology (cont.)
 What is the number of edges in a complete
directed graph with N vertices?
N * (N-1)

© ISBAT UNIVERSITY – 2020. 6/5/2021


Graph terminology (cont.)
 What is the number of edges in a complete
undirected graph with N vertices?
N * (N-1) / 2

© ISBAT UNIVERSITY – 2020. 6/5/2021


Data Structures for Representing Graphs

 Two common data structures for representing


graphs:
 Adjacency lists
 Adjacency matrix

© ISBAT UNIVERSITY – 2020. 6/5/2021


Graph terminology II
30

 The size of a graph is the number of nodes in it


 The empty graph has size zero (no nodes)
 If two nodes are connected by an edge, they are neighbors
(and the nodes are adjacent to each other)
 The degree of a node is the number of edges it has
 For directed graphs,
 If a directed edge goes from node S to node D, we call S the source
and D the destination of the edge
◼ The edge is an out-edge of S and an in-edge of D
◼ S is a predecessor of D, and D is a successor of S
 The in-degree of a node is the number of in-edges it has
 The out-degree of a node is the number of out-edges it has

© ISBAT UNIVERSITY – 2020. 6/5/2021


Graph terminology III
31

 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

A  One simple way of


B C
representing a graph is the
D E adjacency matrix
F  A 2-D array has a mark at
G
[i][j] if there is an edge from
AB CD EF G node i to node j
A
B
C
D
E
F
G

© ISBAT UNIVERSITY – 2020. 6/5/2021


Adjacency-matrix representation II
35

A  An adjacency matrix can


B C equally well be used for
D E digraphs (directed graphs)
F
 A 2-D array has a mark at
G [i][j] if there is an edge from
node i to node j
AB CD EF G
A
B
C
D
E
F
G

© ISBAT UNIVERSITY – 2020. 6/5/2021


Edge-set representation I
36

 An edge-set representation uses a set of nodes and a set of


edges
 The sets might be represented by, say, linked lists
 The set links are stored in the nodes and edges themselves
 The only other information in a node is its element (that is,
its value)—it does not hold information about its edges
 The only other information in an edge is its source and
destination (and attribute, if any)
 If the graph is undirected, we keep links to both nodes, but don’t
distinguish between source and destination
 This representation makes it easy to find nodes from an
edge, but you must search to find an edge from a node
© This
ISBAT is seldom
UNIVERSITY – 2020. a good representation 6/5/2021
Edge-set representation II
37

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)

 Each edge contains references to its source and its


destination (and its attribute, if any)
© ISBAT UNIVERSITY – 2020. 6/5/2021
Adjacency-set representation I
38

 An adjacency-set representation uses a set of nodes


 Each node contains a reference to the set of its edges
 For a directed graph, a node might only know about
(have references to) its out-edges
 Thus, there is not one single edge set, but rather a
separate edge set for each node
 Each edge would contain its attribute (if any) and its
destination (and possibly its source)

© ISBAT UNIVERSITY – 2020. 6/5/2021


Adjacency-set representation II
39

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)

 Here we have a set of nodes, E { r, u } t: (D, A)


and each node refers to a set F {v} u: (E, G)
of edges G {w} v: (F, E)
 Each edge contains references w: (G, D)
to its source and its destination
(and its attribute, if any)

© ISBAT UNIVERSITY – 2020. 6/5/2021


40

Thank you

© ISBAT UNIVERSITY – 2020. 6/5/2021

You might also like