HND in Computing and Software Engineering: Lesson 16 - Graph Data Structure
HND in Computing and Software Engineering: Lesson 16 - Graph Data Structure
Engineering
SEC5213: Data Structures and Algorithm
Level: 5
Credit Value: 20
1
Lecturer: Ms. Sathananthy 06/04/2022
Learning Outcomes 04
2 06/04/2022
Outline
Fundamentals of graph DS
Terminologies
Primary operations
Weighted graph
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
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
Simple Path : If all the nodes of the graph are distinct with an exception V 0=VN, then such path P
Connected Graph : A connected graph is the one in which some path exists between every two
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
Add Edge − Adds an edge between the two vertices 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
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
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
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.
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)
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.
following rules.
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
30 06/04/2022
Algorithm
Step 2: Push the starting node A on the stack and set its STATUS = 2 (waiting state)
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
means there are some cost associated with each edge in graph.
36 06/04/2022
Weighted Graph
37 06/04/2022
Weighted Graph
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
39 06/04/2022
END
40 06/04/2022