More Related Content
Similar to Ch-17-Data Structures.ppt.pptx
Similar to Ch-17-Data Structures.ppt.pptx (20)
Ch-17-Data Structures.ppt.pptx
- 1. © Oxford University Press 2012. All rights reserved.
Computer Fundamentals and Programming in C
Reema Thareja, Assistant Professor,
Institute of Information Technology and
Management
- 3. © Oxford University Press 2012. All rights reserved.
INTRODUCTION
● A data structure is nothing but an arrangement of data either in
computer's memory or on the disk storage.
● Some common examples of data structure includes arrays, linked lists,
queues, stacks, binary trees, and hash tables.
● Data structures are widely applied in areas such as:
a. Compiler design
b. Operating system
c. Statistical analysis package
d. DBMS
e. Numerical analysis
f. Simulation
g. Artificial Intelligence
h. Graphics
- 4. © Oxford University Press 2012. All rights reserved.
INTRODUCTION
● A linked list in simple terms is a linear collection of data elements. These data
elements are called nodes.
● Linked list is a data structure which in turn can be used to implement other data
structures. Thus, it acts as building block to implement data structures like
stacks, queues and their variations.
● A linked list can be perceived as a train or a sequence of nodes in which each
node contain one or more data fields and a pointer to the next node.
1 2 3 4 5 6 7 X
START
In the above linked list, every node contains two parts- one integer and the
other a pointer to the next node. The left part of the node which contains data
may include a simple data type, an array or a structure. The right part of the
node contains a pointer to the next node (or address of the next node in
sequence). The last node will have no next node connected to it, so it will store
a special value called NULL.
- 5. © Oxford University Press 2012. All rights reserved.
INTRODUCTION contd.
● Linked list contains a pointer variable, START which stores the address of
the first node in the list.
● We can traverse the entire list using a single pointer variable START. The
START node will contain the address of the first node; the next part of the
first node will in turn store the address of its succeeding node.
● Using this technique the individual nodes of the list will form a chain of
nodes. If START = NULL, this means that the linked list is empty and
contains no nodes.
● In C, we will implement a linked list using the following code:
struct node
{
int data;
struct node *next;
};
- 6. © Oxford University Press 2012. All rights reserved.
1 DATA
NEXT
H 4
E 7
L 8
L 10
O -1
1
2
3
4
5
6
7
8
9
10
START
START pointing
to the first
element of the
linked list in
memory
9
AVAIL
If we want to add a node to an already existing
linked list in the memory, we will first find any free
space in the memory and then use it to store the
information.
The operating system maintains a free pool which is
a linked list of a of all free memory cells. It maintains
a pointer variable AVAIL which stores the address of
the first free space.
When you delete a node from the linked list, the
operating system adds the freed memory to the free
pool.
The operating system will perform this operation
whenever it finds the CPU idle or whenever the
programs are falling short of memory. The operating
system scans through all the memory cells and mark
the cells that are being used by some or the other
program. Then, it collects all those cells which are
not being used and add their address to the free pool
so that it can be reused by the programs. This
process is called garbage collection. The whole
process of collecting unused memory cells (garbage
collection) is transparent to the programmer.
- 7. © Oxford University Press 2012. All rights reserved.
Singly Linked List
● A singly linked list is the simplest type of linked list in which every node
contains some data and a pointer to the next node of the same data
type. By saying that the node contains a pointer to the next node we
mean that the node stores the address of the next node in sequence.
1 2 3 4 5 6 7 X
START
Algorithm for traversing a linked list
Step 1: [INITIALIZE] SET PTR = START
Step 2: Repeat Steps 3 and 4 while PTR != NULL
Step 3: Apply Process to PTR->DATA
Step 4: SET PTR = PTR->NEXT
[END OF LOOP]
Step 5: EXIT
- 8. © Oxford University Press 2012. All rights reserved.
Algorithm to print the information stored in each node of the linked list
Step 1: [INITIALIZE] SET PTR = START
Step 2: Repeat Steps 3 and 4 while PTR != NULL
Step 3: Write PTR->DATA
Step 4: SET PTR = PTR->NEXT
[END OF LOOP]
Step 5: EXIT
Algorithm to print the number of nodes in the linked list
Step 1: [INITIALIZE] SET Count = 0
Step 2: [INITIALIZE] SET PTR = START
Step 3: Repeat Steps 4 and 5 while PTR != NULL
Step 4: SET Count = Count + 1
Step 5: SET PTR = PTR->NEXT
[END OF LOOP]
Step 6: EXIT
- 9. © Oxford University Press 2012. All rights reserved.
Algorithm to search an unsorted linked list
Step 1: [INITIALIZE] SET PTR = START
Step 2: Repeat Steps 3 while PTR != NULL
Step 3: IF VAL = PTR->DATA
SET POS = PTR
Go To Step 5
ELSE
SET PTR = PTR->NEXT
[END OF IF]
[END OF LOOP]
Step 4: SET POS = NULL
Step 5: EXIT
1 7 3 4 2 6 5 X
PTR
1 7 3 4 2 6 5 X
PTR
1 7 3 4 2 6 5 X
PTR
1 7 3 4 2 6 5 X
PTR
- 10. © Oxford University Press 2012. All rights reserved.
1 7 3 4 2 6 5 X
START
START
Algorithm to insert a new node in the beginning of the linked list
Step 1: IF AVAIL = NULL, then
Write OVERFLOW
Go to Step 7
[END OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL->NEXT
Step 4: SET New_Node->DATA = VAL
Step 5: SET New_Node->Next = START
Step 6: SET START = New_Node
Step 7: EXIT
9 1 7 3 4 2 6 5 X
- 11. © Oxford University Press 2012. All rights reserved.
Algorithm to insert a new node at the end of the linked list
Step 1: IF AVAIL = NULL, then
Write OVERFLOW
Go to Step 10
[END OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL->NEXT
Step 4: SET New_Node->DATA = VAL
Step 5: SET New_Node->Next = NULL
Step 6: SET PTR = START
Step 7: Repeat Step 8 while PTR->NEXT != NULL
Step 8: SET PTR = PTR ->NEXT
[END OF LOOP]
Step 9: SET PTR->NEXT = New_Node
Step 10: EXIT
1 7 3 4 2 6 5 X
START, PTR
1 7 3 4 2 6 5 9 X
PTR
START
- 12. © Oxford University Press 2012. All rights reserved.
Algorithm to insert a new node after a node that has value NUM
Step 1: IF AVAIL = NULL, then
Write OVERFLOW
Go to Step 12
[END OF IF]
Step 2: SET New_Node = AVAIL
Step 3: SET AVAIL = AVAIL->NEXT
Step 4: SET New_Node->DATA = VAL
Step 5: SET PTR = START
Step 6: SET PREPTR = PTR
Step 7: Repeat Step 8 and 9 while PREPTR->DATA != NUM
Step 8: SET PREPTR = PTR
Step 9: SET PTR = PTR->NEXT
[END OF LOOP]
Step 10: PREPTR->NEXT = New_Node
Step 11: SET New_Node->NEXT = PTR
Step 12: EXIT
5
1 7 3 4 2 6 X
START, PTR, PREPTR
1 7 3 4 2 6 5 X
START PREPTR PTR
1 7 3 9 4 2 6 5 X
START
- 13. © Oxford University Press 2012. All rights reserved.
Algorithm to delete the first node from the linked list
Step 1: IF START = NULL, then
Write UNDERFLOW
Go to Step 5
[END OF IF]
Step 2: SET PTR = START
Step 3: SET START = START->NEXT
Step 4: FREE PTR
Step 5: EXIT
1 7 3 4 2 6 5 X
7 3 4 2 6 5 X
START
START
- 14. © Oxford University Press 2012. All rights reserved.
Algorithm to delete the last node of the linked list
Step 1: IF START = NULL, then
Write UNDERFLOW
Go to Step 8
[END OF IF]
Step 2: SET PTR = START
Step 3: Repeat Step 4 and 5 while PTR->NEXT != NULL
Step 4: SET PREPTR = PTR
Step 5: SET PTR = PTR->NEXT
[END OF LOOP]
Step 6: SET PREPTR->NEXT = NULL
Step 7: FREE PTR
Step 8: EXIT
1 7 3 4 2 6 5 X
START, PREPTR, PTR
1 7 3 4 2 6 X 5 X
PREPTR PTR
START
- 15. © Oxford University Press 2012. All rights reserved.
Algorithm to delete the node after a given node from the linked list
Step 1: IF START = NULL, then
Write UNDERFLOW
Go to Step 10
[END OF IF]
Step 2: SET PTR = START
Step 3: SET PREPTR = PTR
Step 4: Repeat Step 5 and 6 while PRETR->DATA != NUM
Step 5: SET PREPTR = PTR
Step 6: SET PTR = PTR->NEXT
[END OF LOOP]
Step7: SET TEMP = PTR->NEXT
Step 8: SET PREPTR->NEXT = TEMP->NEXT
Step 9: FREE TEMP
Step 10: EXIT
1 7 3 4 2 6 5 X
START, PREPTR, PTR
1 7 3 4 2 6 5 X
PREPTR PTR
START
1 7 3 4 2 6 5 X
START
1 7 3 4 6 5 X
START
- 16. © Oxford University Press 2012. All rights reserved.
Introduction to Stacks
● Stack is an important data structure which stores its elements
in an ordered manner. Take an analogy of a pile of plates where
one plate is placed on top of the other. A plate can be removed
from the topmost position. Hence, you can add and remove the
plate only at/from one position that is, the topmost position.
The topmost plate
will be removed first
Another plate
will be added on
top of this plate
Same is the case with stack. A stack
is a linear data structure which can
be implemented either using an array
or a linked list. The elements in a
stack are added and removed only
from one end, which is called top.
Hence, a stack is called a LIFO (Last
In First Out) data structure as the
element that was inserted last is the
first one to be taken out.
- 17. © Oxford University Press 2012. All rights reserved.
Array Representation Of Stacks
● In computer’s memory stacks can be represented as a linear
array.
● Every stack has a variable TOP associated with it. TOP is used
to store the address of the topmost element of the stack. It is
this position from where the element will be added or deleted.
● There is another variable MAX which will be used to store the
maximum number of elements that the stack can hold.
● If TOP = NULL, then it indicates that the stack is empty and if
TOP = MAX -1, then the stack is full.
- 18. © Oxford University Press 2012. All rights reserved.
Push Operation
● The push operation is used to insert an element in to the stack. The
new element is added at the topmost position of the stack. However,
before inserting the value, we must first check if TOP=MAX-1, because
if this is the case then it means the stack is full and no more insertions
can further be done. If an attempt is made to insert a value in a stack
that is already full, an OVERFLOW message is printed.
1 2 3 4 5
0 1 2 3 TOP = 4 5 6 7 8 9
1 2 3 4 5 6
0 1 2 3 TOP = 4 5 6 7 8 9
- 19. © Oxford University Press 2012. All rights reserved.
Pop Operation
● The pop operation is used to delete the topmost element from the
stack. However, before deleting the value, we must first check if
TOP=NULL, because if this is the case then it means the stack is
empty so no more deletions can further be done. If an attempt is made
to delete a value from a stack that is already empty, an UNDERFLOW
message is printed.
1 2 3 4 5
0 1 2 3 TOP = 4 5 6 7 8 9
1 2 3 4
0 1 2 TOP = 3 4 5 6 7 8 9
- 20. © Oxford University Press 2012. All rights reserved.
Peep Operation
● Peep is an operation that returns the value of the topmost element of the stack
without deleting it from the stack.
● However, the peep operation first checks if the stack is empty or contains
some elements. For this, a condition is checked. If TOP = NULL, then an
appropriate message is printed else the value is returned.
1 2 3 4 5
0 1 2 3 TOP = 4 5 6 7 8 9
Here Peep operation will return 5, as it is the value of the
topmost element of the stack.
- 21. © Oxford University Press 2012. All rights reserved.
Algorithm to PUSH an element in to the stack
Step 1: IF TOP = MAX-1, then
PRINT “OVERFLOW”
[END OF IF]
Step 2: SET TOP = TOP + 1
Step 3: SET STACK[TOP] = VALUE
Step 4: END
Algorithm to POP an element from the stack
Step 1: IF TOP = NULL, then
PRINT “UNDERFLOW”
[END OF IF]
Step 2: SET VAL = STACK[TOP]
Step 3: SET TOP = TOP - 1
Step 4: END
Algorithm for Peep Operation
Step 1: IF TOP =NULL, then
PRINT “STACK IS EMPTY”
Go TO Step 3
Step 2: RETURN STACK[TOP]
Step 3: END
- 22. © Oxford University Press 2012. All rights reserved.
QUEUES
● Queue is an important data structure which stores its elements in
an ordered manner. Take for example the analogies given below.
● People moving on an escalator. The people who got on the
escalator first will be the first one to step out of it.
● People waiting for bus. The first person standing in the line will be
the first one to get into the bus.
●A queue is a FIFO (First In First Out) data
structure in which the element that was
inserted first is the first one to be taken out.
The elements in a queue are added at one end
called the rear and removed from the other one
end called front.
- 23. © Oxford University Press 2012. All rights reserved.
Array Representation Of Queue
● Queues can be easily represented using linear arrays. As
stated earlier, every queue will have front and rear variables
that will point to the position from where deletions and
insertions can be done respectively.
● Consider a queue shown in figure
12 9 7 18 14 36
0 1 2 3 4 5 6 7 8 9
Here, front = 0 and rear = 5. If we want to add one more value in the
list say with value 45, then rear would be incremented by 1 and the
value would be stored at the position pointed by rear. The queue
after addition would be as shown in figure
12 9 7 18 14 36 45
0 1 2 3 4 5 6 7 8 9
- 24. © Oxford University Press 2012. All rights reserved.
Array Representation Of Queue contd.
● Here, front = 0 and rear = 6. Every time a new element has to be added, we will
repeat the same procedure.
● Now, if we want to delete an element from the queue, then the value of front will be
incremented. Deletions are done from only this end of the queue. The queue after
deletion will be as shown in figure
Here, front = 1 and rear = 6.
However, before inserting an element in the queue we must check for overflow
conditions. An overflow will occur when we will try to insert an element into a
queue that is already full. When Rear = MAX – 1, where MAX is the size of the queue
that is, MAX specifies the maximum number of elements that the queue can hold.
Similarly, before deleting an element from the queue, we must check for underflow
condition. An underflow condition occurs when we try to delete an element from a
queue that is already empty. If front = -1 and rear = -1, this means there is no
element in the queue.
9 7 18 14 36 45
0 1 2 3 4 5 6 7 8 9
- 25. © Oxford University Press 2012. All rights reserved.
Algorithms to insert and delete an
element from the Queue
Algorithm to insert an element in the queue
Step 1: IF REAR=MAX-1, then;
Write OVERFLOW
[END OF IF]
Step 2: IF FRONT == -1 and REAR = -1, then;
SET FRONT = REAR = 0
ELSE
SET REAR = REAR + 1
[END OF IF]
Step 3: SET QUEUE[REAR] = NUM
Step 4: Exit
Algorithm to delete an element from the queue
Step 1: IF FRONT = -1 OR FRONT > REAR, then;
Write UNDERFLOW
ELSE
SET FRONT = FRONT + 1
SET VAL = QUEUE[FRONT]
[END OF IF]
Step 2: Exit
- 26. © Oxford University Press 2012. All rights reserved.
BINARY TREES
● A binary tree is a data structure which is defined as a collection of
elements called nodes. Every node contains a "left" pointer, a "right"
pointer, and a data element. Every binary tree has a root element pointed
by a "root" pointer. The root element is the topmost node in the tree. If
root = NULL, then it means the tree is empty.
● If the root node R is not NULL, then the two trees T1 and T2 are called the
left and right subtrees of R. if T1 is non-empty, then T1 is said to be the
left successor of R. likewise, if T2 is non-empty then, it is called the right
successor of R.
8
1
3
2
4
5 6 7
1
2
1
0
1
1
ROOT NODE
T2
T1
9
In a binary tree every node has 0, 1 or at the
most 2 successors. A node that has no
successors or 0 successors is called the leaf
node or the terminal node.
- 27. © Oxford University Press 2012. All rights reserved.
KEY TERMS
● Sibling: If N is any node in T that has left successor S1 and right successor S2, then
N is called the parent of S1 and S2. Correspondingly, S1 and S2 are called the left
child and the right child of N. Also, S1 and S2 are said to be siblings. Every node
other than the root node has a parent. In other words, all nodes that are at the same
level and share the same parent are called siblings (brothers).
● Level number: Every node in the binary tree is assigned a level number. The root
node is defined to be at level 0. The left and right child of the root node has a level
number 1. Similarly, every node is at one level higher than its parents. So all child
nodes are defined to have level number as parent’s level number + 1.
● Degree: Degree of a node is equal to the number of children that a node has. The
degree of a leaf node is zero.
● In-degree of a node is the number of edges arriving at that node. The root node is
the only node that has an in-degree equal to zero. Similarly,
● Out-degree of a node is the number of edges leaving that node.
- 28. © Oxford University Press 2012. All rights reserved.
KEY TERMS contd.
Leaf node: A leaf node has no children.
Directed edge: Line drawn from a node N to any of its successor is called a directed
edge. A binary tree of n nodes have exactly n – 1 edges
Path: A sequence of consecutive edges is called a path.
Depth: The depth of a node N is given as the length of the path from the root R to the
node N. The depth of the root node is zero. A binary tree of height h, has at least h
nodes and at most 2h – 1 nodes. This is because every level will have at least one
node and can have at most 2 nodes. So, if every level has two nodes then a tree with
height h will have at the most 2h – 1 nodes as at level 0, there is only one element
called the root. The height of a binary tree with n nodes is at least n and at most
log2(n+1)
Ancestor and descendant nodes: Ancestors of a node are all the nodes along the path
from the root to that node. Similarly, descendants of a node are all the nodes along
the path from that node to the leaf node.
- 29. © Oxford University Press 2012. All rights reserved.
Representation of Binary Trees
in Memory
● In computer’s memory, a binary tree can be maintained either using a linked
representation (as in case of a linked list) or using sequential representation
(as in case of single arrays).
Linked Representation of Binary Trees
● In linked representation of binary tree, every node will have three parts, the
data element, a pointer to the left node and a pointer to the right node. So in
C, the binary tree is built with a node type given as below.
struct node {
struct node* left;
int data;
struct node* right;
};
1
2 3
4 5 6 7
X 8 X X 9 X X 10 X X 11 X X 12 X
- 30. © Oxford University Press 2012. All rights reserved.
Sequential Representation of
Binary Trees
● Sequential representation of trees is done using 1D array. Though,
simple it is very inefficient as it requires a lot of memory space. A
sequential binary tree follows the rules given below:
● One dimensional array called TREE, will be used.
● The root of the tree will be stored in the first location. That is,
TREE[0] will store the data of the root element.
● The children of a node K, will be stored in location (2*K) and (2*K+1).
● The maximum size of the array TREE is given as (2d+1-1), where d is
the depth of the tree.
● An empty tree or sub-tree is specified using NULL. If TREE[0] =
NULL, then the tree is empty.
35
15
12
17 2
1
3
9
4
5
1
6
1
8
3
6
20
0 20
1
2 15
3 35
4 12
5 17
6 21
7 39
8
9
1
0
16
1
1
18
1
2
1
3
1
4
36
1
5
45
1
- 31. © Oxford University Press 2012. All rights reserved.
TRAVERSING OF A BINARY TREE
● Traversing a binary tree is the process of visiting each node in the tree exactly
once, in a systematic way. Unlike linear data structures in which the elements
are traversed sequentially, tree is a non-linear data structure in which the
elements can be traversed in many different ways. There are different
algorithms for tree traversals. These algorithms differ in the order in which the
nodes are visited. In this section, we will read about these algorithms.
● Pre-order algorithm
To traverse a non-empty binary tree in preorder, the following
operations are performed recursively at each node. The algorithm
starts with the root node of the tree and continues by,
● Visiting the root node.
● Traversing the left subtree.
● Traversing the right subtree.
A
C
B
D E
F
I
H
G
A, B, D, C, E, F, G, H and I
- 32. © Oxford University Press 2012. All rights reserved.
In-order algorithm
To traverse a non-empty binary tree in in-order, the following operations
are performed recursively at each node. The algorithm starts with the root
node of the tree and continues by,
● Traversing the left subtree.
● Visiting the root node.
● Traversing the right subtree.
Post-order algorithm
To traverse a non-empty binary tree in post-order, the following operations
are performed recursively at each node. The algorithm starts with the root
node of the tree and continues by,
● Traversing the left subtree.
● Traversing the right subtree.
● Visiting the root node.
A
C
B
D E
F
I
H
G
B, D, A, E, H, G, I, F AND C.
D, B, H, I, G, F, E, C and A.
- 33. © Oxford University Press 2012. All rights reserved.
INTRODUCTION
● A graph is an abstract data structure that is used to implement the graph concept from
mathematics. A graph is basically, a collection of vertices (also called nodes) and edges that
connect these vertices. A graph is often viewed as a generalization of the tree structure, where
instead of a having a purely parent-to-child relationship between tree nodes, any kind of
complex relationships between the nodes can be represented.
Why graphs are useful?
● Graphs are widely used to model any situation where entities or things are related to each other
in pairs; for example, the following information can be represented by graphs:
● Family trees in which the member nodes have an edge from parent to each of their children.
● Transportation networks in which nodes are airports, intersections, ports, etc. The edges can be
airline flights, one-way roads, shipping routes, etc.
- 34. © Oxford University Press 2012. All rights reserved.
Definition
● A graph G is defined as an ordered set (V, E), where V(G) represent the set of vertices and
E(G) represents the edges that connect the vertices.
● The figure given shows a graph with V(G) = { A, B, C, D and E} and E(G) = { (A, B), (B, C),
(A, D), (B, D), (D, E), (C, E) }. Note that there are 5 vertices or nodes and 6 edges in the
graph.
A B C
D E
A graph can be directed or undirected. In an undirected graph, the edges do not have any direction
associated with them. That is, if an edge is drawn between nodes A and B, then the nodes can be
traversed from A to B as well as from B to A. The above figure shows an undirected graph because it
does not gives any information about the direction of the edges.
The given figure shows a directed graph. In a directed graph, edges form an ordered pair. If there is
an edge from A to B, then there is a path from A to B but not from B to A. The edge (A, B) is said to
initiate from node A (also known as initial node) and terminate at node B (terminal node).
A B C
D E
- 35. © Oxford University Press 2012. All rights reserved.
Graph Terminology
● Adjacent Nodes or Neighbors: For every edge, e = (u, v) that connects nodes u and v; the
nodes u and v are the end-points and are said to be the adjacent nodes or neighbors.
● Degree of a node: Degree of a node u, deg(u), is the total number of edges containing the
node u. If deg(u) = 0, it means that u does not belong to any edge and such a node is
known as an isolated node.
● Regular graph: Regular graph is a graph where each vertex has the same number of
neighbors. That is every node has the same degree. A regular graph with vertices of degree
k is called a k-regular graph or regular graph of degree k.
O regular graph
1 regular graph 2 regular graph
Path: A path P, written as P = {v0, v1, v2,….., vn), of length n from a node u to v is defined as a
sequence of (n+1) nodes. Here, u = v0, v = vn and vi-1 is adjacent to vi for i = 1, 2, 3, …, n.
Closed path: A path P is known as a closed path if the edge has the same end-points. That is, if v0 = vn.
Simple path: A path P is known as a simple path if all the nodes in the path are distinct with an
exception that v0 may be equal to vn. If v0 = vn, then the path is called a closed simple path.
- 36. © Oxford University Press 2012. All rights reserved.
● Cycle: A closed simple path with length 3 or more is known as a cycle. A cycle of length
k is called a k – cycle.
● Connected graph: A graph in which there exists a path between any two of its nodes is
called a connected graph. That is to say that there are no isolated nodes in a connected
graph. A connected graph that does not have any cycle is called a tree.
● Complete graph: A graph G is said to be a complete, if all its nodes are fully connected,
that is, there is a path from one node to every other node in the graph. A complete graph
has n(n-1)/2 edges, where n is the number of nodes in G.
● Labeled graph or weighted graph: A graph is said to be labeled if every edge in the graph
is assigned some data. In a weighted graph, the edges of the graph are assigned some
weight or length. Weight of the edge, denoted by w(e) is a positive value which indicates
the cost of traversing the edge.
● Multiple edges: Distinct edges which connect the same end points are called multiple
edges. That is, e = {u, v) and e’ = (u, v) are known as multiple edges of G.
● Loop: An edge that has identical end-points is called a loop. That is, e = (u, u).
● Multi- graph: A graph with multiple edges and/or a loop is called a multi-graph.
● Size of the graph: The size of a graph is the total number of edges in it.
(a) Multi-graph
(b) Tree (c) Weighted Graph
A B
C
e1
e2
e3
e4
e5
e6
e7
A
D E
B C
F
B
A
D
C
B
3
2
4
7 1
3
- 37. © Oxford University Press 2012. All rights reserved.
Directed Graph
A directed graph G, also known as a digraph, is a graph in which every edge has a direction assigned
to it. An edge of a directed graph is given as an ordered pair (u, v) of nodes in G. For an edge (u, v)-
● the edge begins at u and terminates at v
● u is known as the origin or initial point of e. Correspondingly, v is known as the destination or
terminal point of e
● u is the predecessor of v. Correspondingly, v is the successor of u
● nodes u and v are adjacent to each other.
Terminology of a directed graph
● Out-degree of a node: The out degree of a node u, written as outdeg(u), is the number of edges
that originate at u.
● In-degree of a node: The in degree of a node u, written as indeg(u), is the number of edges that
terminate at u.
● Degree of a node: Degree of a node written as deg(u) is equal to the sum of in-degree and out-
degree of that node. Therefore, deg(u) = indeg(u) + outdeg(u)
● Source: A node u is known as a source if it has a positive out-degree but an in-degree = 0.
● Sink: A node u is known as a sink if it has a positive in degree but a zero out-degree.
- 38. © Oxford University Press 2012. All rights reserved.
(a): Directed Acyclic Graph (b): Strongly Connected Directed Acyclic Graph
A B
C D
e8
e5
e6
e3
e2
e7
e4
A B
C D
Reachability: A node v is said to be reachable from node u, if and only if there exists a
(directed) path from node u to node v.
Strongly connected directed graph: A digraph is said to be strongly connected if and only if
there exists a path from every pair of nodes in G. That is, if there is a path from node u to v,
then there must be a path from node v to u.
Unilaterally connected graph: A digraph is said to be unilaterally connected if there exists a
path from any pair of nodes u, v in G such that there is a path from u to v or a path from v to
u but not both.
Parallel/Multiple edges: Distinct edges which connect the same end points are called
multiple edges. That is, e = {u, v) and e’ = (u, v) are known as multiple edges of G.
Simple directed graph: A directed graph G is said to be a simple directed graph if and only if
it has no parallel edges. However, a simple directed graph may contain cycle with an
- 39. © Oxford University Press 2012. All rights reserved.
REPRESENTATION OF GRAPHS
There are two common ways of storing graphs in computer’s memory. They
are:
■ Sequential representation by using an adjacency matrix
■ Linked representation by using an adjacency list that stores the neighbors
of a node using a linked list
- 40. © Oxford University Press 2012. All rights reserved.
Adjacency Matrix Representation
● An adjacency matrix is used to represent which nodes are adjacent to one another. By
definition, we have learnt that, two nodes are said to be adjacent if there is an edge
connecting them.
● In a directed graph G, if node v is adjacent to node u, then surely there is an edge from u
to v. That is, if v is adjacent to u, we can get from u to v by traversing one edge. For any
graph G having n nodes, the adjacency matrix will have dimensions of n X n.
● In an adjacency matrix, the rows and columns are labeled by graph vertices. An entry aij
in the adjacency matrix will contain 1, if vertices vi and vj are adjacent to each other.
However, if the nodes are not adjacent, aij will be set to zero.
1 if vi is adjacent to vj, that is there is an edge (vi, vj)
0 otherwise
aij
Since an adjacency matrix contains only 0s and 1s, it is called a bit matrix or a Boolean matrix.
The entries in the matrix depend on the ordering of the nodes in G. therefore, a change in the
order of nodes will result in a different adjacency matrix.
- 42. © Oxford University Press 2012. All rights reserved.
From adjacency matrix A1, we have learnt that an entry 1 in the ith row and jth column means that
there exists a path of length 1 from vi to vj. Now consider, A2, A3 and A4
aij
2 = ∑ aik akj
Any entry aij = 1 if aik = akj = 1. That is, if there is an edge (vi, vk) and (vk, vj). This implies that there
is a path from vi to vj of length 2.
Similarly, every entry in the ith row and jth column of A3 gives the number of paths of length 3
from node vi to vj.
In general terms, we can conclude that every entry in the ith row and jth column of An (where n is
the number of nodes in the graph) gives the number of paths of length n from node vi to vj.
- 43. © Oxford University Press 2012. All rights reserved.
Adjacency List
The adjacency list is another way in which graphs can be represented in computer’s memory.
This structure consists of a list of all nodes in G. Furthermore, every node is in turn linked to its
own list that contains the names of all other nodes that are adjacent to itself.
The key advantage of using an adjacency list includes:
■ It is easy to follow, and clearly shows the adjacent nodes of a particular node
■ It is often used for storing graphs that have a small to moderate number of edges. That is an
Adjacency list is preferred for representing sparse graphs in computer’s memory; otherwise, an
adjacency matrix is a good choice.
Adding new nodes in G is easy and straightforward when G is represented using an Adjacency
list. Adding new nodes in an Adjacency matrix is a difficult task as size of the matrix needs to be
changed and existing nodes may have to be reordered.
- 44. © Oxford University Press 2012. All rights reserved.
A B
C D
A
B
C
D
C X
B
B D X
D X
B X
A
Graph G and its adjacency list
For a directed graph, the sum of lengths of all adjacency lists is equal to the number of edges in G.
However, for an undirected graph, the sum of lengths of all adjacency lists is equal to twice the
number of edges in G because an edge (u, v) means an edge from node u to v as well as an edge v to
u. The adjacency list can also be modified to store weighted graphs.
A B C
D E
A
B
C
D
E
B D X
A D X
C
B E X
A E X
B
C D X