HND in Computing and Software
Engineering
SEC5213: Data Structures and Algorithm
Level: 5
Credit Value: 20
Lesson 16 – Graph Data Structure
1
Lecturer: Ms. Sathananthy 06/04/2022
Learning Outcomes 04
• Apply algorithms and data structures to solve programming problems.
2 06/04/2022
Outline
Fundamentals of graph DS
Terminologies
Adjacency matrix and Adjacency List
Primary operations
Depth first search
Breadth first search
Weighted graph
Develop an algorithm and implementation
Complexity Measures
7
GRAPH
Definition : A graph G can be defined as an ordered set G(V, E) where V(G) represents the
set of vertices and E(G) represents the set of edges which are used to connect these vertices.
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.
A Graph G(V, E) with 5 vertices (A, B, C, D, E) and six edges ((A,B), (B,C), (C,E), (E,D),
(D,B), (D,A)) is shown in the following figure.
4 06/04/2022
GRAPH
Formally, 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.
Take a look at the following graph
In the given graph,
V = {a, b, c, d, e}
E = {ab, ac, bd, cd, de}
5
06/04/2022
Graph Data Structure: Terminologies
Mathematical graphs can be represented in data structure
Vertex − Each node of the graph is represented as a vertex.
Ex: In the following example, A to G are vertices.
Edge − Edge represents a path between two vertices or a line
between two vertices. In the following example, the lines
from A to B, B to C, and so on represents edges.
Adjacency − Two node or vertices are adjacent if they are
connected to each other through an edge. In the following
example, B is adjacent to A, C is adjacent to B, and so on.
Path − Path represents a sequence of edges between the two
vertices. In the following example, ABCD represents a path
from A to D.
6 06/04/2022
Graph Data Structure: Terminologies
Path : A path can be defined as the sequence of nodes that are followed in order to reach some
terminal node V from the initial node U.
Closed Path : A path will be called as closed path if the initial node is same as terminal node. A
path will be closed path if V0=VN.
Simple Path : If all the nodes of the graph are distinct with an exception V 0=VN, then such path P
is called as closed simple path.
Cycle : A cycle can be defined as the path which has no repeated edges or vertices except the first
and last vertices.
Loop : An edge that is associated with the similar end points can be called as Loop.
Connected Graph : A connected graph is the one in which some path exists between every two
7 vertices (u, v) in V. There are no isolated nodes in connected graph. 06/04/2022
Graph Data Structure: Terminologies
Complete Graph : A complete graph is the one in which every node is connected with all other
nodes. A complete graph contain n(n-1)/2 edges where n is the number of nodes in the graph.
Weighted Graph : In a weighted graph, each edge is assigned with some data such as length or
weight. The weight of an edge e can be given as w(e) which must be a positive (+) value indicating
the cost of traversing the edge.
Digraph : A digraph is a directed graph in which each edge of the graph is associated with some
direction and the traversing can be done only in the specified direction.
Adjacent Nodes : If two nodes u and v are connected via an edge e, then the nodes u and v are
called as neigh bours or adjacent nodes.
Degree of the Node : A degree of a node is the number of edges that are connected with that node.
A node with degree 0 is called as isolated node.
8 06/04/2022
Basic Operations
Following are basic primary operations of a Graph −
Add Vertex − Adds a vertex to the graph.
Add Edge − Adds an edge between the two vertices of the graph.
Display Vertex − Displays a vertex of the graph.
9 06/04/2022
Directed and Undirected Graph
A graph can be directed or undirected. However, in an undirected graph, edges are not
associated with the directions with them. An undirected graph is shown in the above
figure since its edges are not attached with any of the directions. If an edge exists
between vertex A and B then the vertices can be traversed from B to A as well as A to B.
In a directed graph, edges form an ordered pair. Edges represent a specific path from
some vertex A to another vertex B. Node A is called initial node while node B is called
terminal node.
A directed graph is shown in the following figure.
10 06/04/2022
Directed and Undirected Graph
11 06/04/2022
Sequential Representation
In sequential representation, we use adjacency matrix to store the mapping represented by vertices
and edges. In adjacency matrix, the rows and columns are represented by the graph vertices. A
graph having n vertices, will have a dimension n x n.
An entry Mij in the adjacency matrix representation of an undirected graph G will be 1 if there
exists an edge between Vi and Vj.
An undirected graph and its adjacency matrix representation is shown in the following figure.
12 06/04/2022
Sequential Representation
In the previous figure, we can see the mapping among the vertices (A, B, C, D, E) is represented
by using the adjacency matrix which is also shown in the figure.
There exists different adjacency matrices for the directed and undirected graph. In directed graph,
an entry Aij will be 1 only when there is an edge directed from V i to Vj.
A directed graph and its adjacency matrix representation is shown in the following figure.
13 06/04/2022
Sequential Representation
Representation of weighted directed graph is different. Instead of filling the entry by 1, the Non-
zero entries of the adjacency matrix are represented by the weight of respective edges.
The weighted directed graph along with the adjacency matrix representation is shown in the
following figure.
14 06/04/2022
Linked Representation
In the linked representation, an adjacency list is used to store the Graph into the computer's
memory.
Consider the undirected graph shown in the following figure and check the adjacency list
representation.
An adjacency list is maintained for each node present in previous graph which stores the node value and a pointer
to the next adjacent node to the respective node. If all the adjacent nodes are traversed then store the NULL in the
pointer field of last node of the list. The sum of the lengths of adjacency lists is equal to the twice of the number
15 06/04/2022
of edges present in an undirected graph.
Linked Representation
Consider the directed graph shown in the following figure and check the adjacency list
representation of the graph.
In a directed graph, the sum of lengths of all the adjacency lists is equal to the number of edges present in the
graph.
In the case of weighted directed graph, each node contains an extra field that is called the weight of the node.
The adjacency list representation of a directed graph is shown in the following figure.
16 06/04/2022
Linked Representation
In a directed graph, the sum of lengths of all the adjacency lists is equal to the number of edges present in the
graph.
In the case of weighted directed graph, each node contains an extra field that is called the weight of the node.
The adjacency list representation of a directed graph is shown in the following figure.
17 06/04/2022
Graph Traversal Algorithm
Traversing the graph means examining all the nodes and vertices of the graph.
There are two standard methods by using which, we can traverse the graphs.
Lets discuss each one of them in detail.
Breadth First Search
Depth First Search
18 06/04/2022
Breadth First Search (BFS)
Breadth First Search (BFS) algorithm traverses a graph in a breadth-ward
motion and uses a queue to remember to get the next vertex to start a search,
when a dead end occurs in any iteration.
Breadth first search is a graph traversal algorithm that starts traversing the graph
from root node and explores all the neighboring nodes. Then, it selects the
nearest node and explore all the unexplored nodes. The algorithm follows the
same process for each of the nearest node until it finds the goal.
19 06/04/2022
Breadth First Search (BFS)
As in the example given above, BFS algorithm
traverses from A to B to E to F first then to C
and G lastly to D. It employs the following
rules.
Rule 1 − Visit the adjacent unvisited vertex.
Mark it as visited. Display it. Insert it in a
queue.
Rule 2 − If no adjacent vertex is found,
remove the first vertex from the queue.
Rule 3 − Repeat Rule 1 and Rule 2 until the
20 06/04/2022
queue is empty
21 06/04/2022
22 06/04/2022
23 06/04/2022
Breadth First Search (BFS)
Minimum Path P can be found by applying breadth first search algorithm that will begin at node A and will end at
E. the algorithm uses two queues, namely QUEUE1 and QUEUE2. QUEUE1 holds all the nodes that are to be
processed while QUEUE2 holds all the nodes that are processed and deleted from QUEUE1.
Lets start examining the graph from Node A.
24 06/04/2022
Breadth First Search (BFS)
1. Add A to QUEUE1 and NULL to QUEUE2. 5. Delete the node C from QUEUE1 and insert all its
QUEUE1 = {A} neighbours. Add node C to QUEUE2.
QUEUE2 = {NULL} QUEUE1 = {F, E, G}
QUEUE2 = {A, B, D, C}
2. Delete the Node A from QUEUE1 and insert all its
neighbours. Insert Node A into QUEUE2 6. Remove F from QUEUE1 and add all its neighbours. Since
QUEUE1 = {B, D} all of its neighbours has already been added, we will not add
QUEUE2 = {A} them again. Add node F to QUEUE2.
QUEUE1 = {E, G}
3. Delete the node B from QUEUE1 and insert all its QUEUE2 = {A, B, D, C, F}
neighbours. Insert node B into QUEUE2.
QUEUE1 = {D, C, F} 7. Remove E from QUEUE1, all of E's neighbours has already
QUEUE2 = {A, B} been added to QUEUE1 therefore we will not add them again.
All the nodes are visited and the target node i.e. E is
4. Delete the node D from QUEUE1 and insert all its encountered into QUEUE2.
neighbours. Since F is the only neighbour of it which has QUEUE1 = {G}
been inserted, we will not insert it again. Insert node D QUEUE2 = {A, B, D, C, F, E}
into QUEUE2.
QUEUE1 = {C, F} Now, backtrack from E to A, using the nodes available in
QUEUE2 = { A, B, D} QUEUE2.
25 The minimum path will be A → B → C → E. 06/04/2022
Depth First Search (DFS) Algorithm
Depth first search (DFS) algorithm starts with the initial node of the graph G,
and then goes to deeper and deeper until we find the goal node or the node
which has no children. The algorithm, then backtracks from the dead end
towards the most recent node that is yet to be completely unexplored.
The data structure which is being used in DFS is stack. The process is similar to
BFS algorithm. In DFS, the edges that leads to an unvisited node are called
discovery edges while the edges that leads to an already visited node are called
block edges.
26 06/04/2022
Depth First Search (DFS) Algorithm
Depth First Search (DFS) algorithm traverses a graph in a depth-ward motion and uses a stack
to remember to get the next vertex to start a search, when a dead end occurs in any iteration.
As in the example given above, DFS algorithm traverses from S to A
to D to G to E to B first, then to F and lastly to C. It employs the
following rules.
Rule 1 − Visit the adjacent unvisited vertex. Mark it as visited.
Display it. Push it in a stack.
Rule 2 − If no adjacent vertex is found, pop up a vertex from the
stack. (It will pop up all the vertices from the stack, which do not
27 06/04/2022
have adjacent vertices.)
DFS
28 06/04/2022
DFS
29 06/04/2022
DFS
As C does not have any unvisited
adjacent node so we keep popping the
stack until we find a node that has an
unvisited adjacent node.
30 06/04/2022
Algorithm
Step 1: SET STATUS = 1 (ready state) for each node in G
Step 2: Push the starting node A on the stack and set its STATUS = 2 (waiting state)
Step 3: Repeat Steps 4 and 5 until STACK is empty
Step 4: Pop the top node N. Process it and set its STATUS = 3 (processed state)
Step 5: Push on the stack all the neighbours of N that are in the ready state (whose
STATUS = 1) and set their
STATUS = 2 (waiting state)
[END OF LOOP]
Step 6: EXIT
31 06/04/2022
DFS
Example : Consider the graph G along with its adjacency list, given in the figure below. Calculate
the order to print all the nodes of the graph starting from node H, by using depth first search
(DFS) algorithm
32 06/04/2022
DFS
Solution : Push H onto the stack Stack : B, F
STACK : H Pop the top element of the stack i.e. F, print it and push
POP the top element of the stack i.e. H, print it and all the neighbours of F onto the stack that are in ready
push all the neighbours of H onto the stack that are is
state.
ready state.
Print H
Print F
STACK : A
Stack : B
Pop the top element of the stack i.e. A, print it and push
Pop the top of the stack i.e. B and push all the neighbours
all the neighbours of A onto the stack that are in ready Print B
state. Stack : C
Print A Pop the top of the stack i.e. C and push all the
Stack : B, D neighbours.
Pop the top element of the stack i.e. D, print it and Print C
push all the neighbours of D onto the stack that are in Stack : E, G
ready state.
Pop the top of the stack i.e. G and push all its neighbours.
Print D
33 06/04/2022
34 06/04/2022
35 06/04/2022
Weighted Graph
A weighted graph is a graph in which each branch is given a
numerical weight. A weighted graph is therefore a special type of labeled
graph in which the labels are numbers (which are usually taken to be
positive).
A Graph is called weighted graph when it has weighted edges which
means there are some cost associated with each edge in graph.
36 06/04/2022
Weighted Graph
37 06/04/2022
Weighted Graph
The weight of an edge can represent:
Cost or distance = the amount of effort needed to travel from one place to
another
Capacity = the maximum amount of flow that can be transported from one place
38
to another 06/04/2022
Weighted Graph : Implementation
Each edge of a graph has an associated numerical value, called a weight.
Usually, the edge weights are non-negative integers.
Weighted graphs may be either directed or undirected.
The weight of an edge is often referred to as the “cost” of the edge.
Will create an Edge class to put weight on each edge
39 06/04/2022
END
40 06/04/2022