Final Term Course (Week No. 07 - 16)
Final Term Course (Week No. 07 - 16)
Prepared by: Arshad Iqbal, Lecturer (CS/IT), The University of Agriculture, Peshawar
1
Week No. 07: SEARCHING
Introduction to Searching:
Computer systems are often used to store large amounts of data from which individual records
are retrieved according to some search criteria.
The process of finding a specific data item or record from a list is called searching.
OR
It is the process by which we can find the specific location of a given item in a list.
OR
Searching is a technique of finding an element from the given list or a set of elements like
arrays, list or trees.
If the item exists in the given list then search is said to be successful otherwise if the element is
not found in the given list then search is said to be unsuccessful.
Internal search: The search in which the whole list resides in the main memory is called
internal search.
External search: The search in which the most of the list resides in the secondary memory
is called external search.
In this searching we will concentrate on internal searching. The complexity of any searching
method is determined from the number of comparison performed among the collected elements
in order to find the elements. The time required for operation is depends on the complexity of the
operation or algorithm.
• Best Case: The best case is that in which the element is found during the first
comparison.
• Worst Case: The worst case is that in which the element is found only at the end or the
last comparison or not found.
• Average Case: The average case is that in which the element is found in comparisons
more than best case but less than worst case.
Prepared by: Arshad Iqbal, Lecturer (CS/IT), The University of Agriculture, Peshawar
2
Types of Searching:
1. Linear Search
2. Binary Search
1. Linear Search:
Linear Search is also called sequential search. It is the simplest way for finding an element in a
list. In Linear Search, the elements find sequentially in a list, no matter whether the list is
sorted or unsorted.
In case of sorted list, the search is started from 1st element and continuous until the element is
found or the element whose value is greater than the value being searched.
In case of unsorted list, the search is started from 1st location and continuous until the element
is found or the end of the list is reached.
Linear Search is a very slow process. It is used for small amounts of data. This method is not
recommended for large amount of data.
Example:
Data
0 1 2 3 4 5 6 7 8 9 10 11 12
A [13] = 10 20 30 40 50 60 70 80 90 100 110 120 130
Prepared by: Arshad Iqbal, Lecturer (CS/IT), The University of Agriculture, Peshawar
3
Algorithm for Linear Search: This algorithm is used for linear searching. ITEM is to be
searched in a linear array A having N numbers of elements. LOC is variable which store the
location in case of successful search and ‘I’ is a counter variable.
Algorithm for Sorted Array: LINEAR SEARCH (A, LOC, I, N, ITEM)
Step 1: [Initialization]
I = 0 and LOC = -1
Step 2: [Starting Loop to Search]
Repeat step 3 & 4 ... While (I < N)
Step 3: [Comparing given element]
If (ITEM == A [I]) then
LOC: = I
Break
Else if (ITEM < A [I]) then
Break
[End of IF Structure]
Step 4: [Increment counter]
I = I+1
Step 5: [Display result]
If (LOC == -1) then
Write (“Element is not Found”)
Else:
Write (“Search is Successful”)
Write (“ITEM is found at Location”,
LOC)
[End of IF Structure]
Step 6: [Finish]
Exit
2. Binary Search:
Binary search is the most efficient searching technique for finding an element in a sorted
list/array. In binary search, first, find the middle index of the list/array, then compare the
element (which you want to search) with the middle element of the list/array. If they are equal,
the search is successful otherwise if the element is greater than the middle element of the
list/array, then right side of the list/array will be searched and if the element is less than the
middle element, then left side of the list/array will be searched in similar way.
Example:
Data
0 1 2 3 4 5 6 7 8 9 10 11 12
A [13] = 10 20 30 40 50 60 70 80 90 100 110 120 130
Find the ITEM = 50?
1. BEG = 0 & END = 12 MID = INT ((0+12)/2) = 6 so A [MID] = 70
2. Since 50 < 70 therefore BEG = 0 & END = MID - 1 = 6 – 1 = 5
MID = INT ((0 + 5)/2) = 2.5=2 so A [MID] = 30
3. Since 50 > 30 therefore BEG = MID + 1 = 2 + 1 = 3 & END = 5
MID = INT ((3 + 5)/2) = 4 so A [MID] = 50
Thus search is successful, the ITEM is found at location 4.
Prepared by: Arshad Iqbal, Lecturer (CS/IT), The University of Agriculture, Peshawar
4
Algorithm for Binary Search: This algorithm is used for binary searching. ITEM is to
be searched in a linear sorted array A having N numbers of filled memory locations. BEG, END
and MID variables are used to store array index numbers.
ALGORITHM: BINARY SEARCH (A, ITEM, UB, LB, BEG, END, MID)
Step 1: [Initialization]
BEG = LB & END = UB
Step 2: [Starting loop to search]
Repeat step 3, 4 & 5 ….. While (BEG < = END)
Step 3: [Calculate MID]
MID = INT ((BEG + END)/2)
Step 4: [Check element]
If (ITEM == A [MID]) Then
Break
[End of IF Structure]
Step 5: [Set BEG & END]
If (ITEM < A [MID]) then
END = MID - 1
Else:
BEG = MID + 1
[End of IF Structure]
Step 6: [Display message]
If (ITEM == A [MID]) then
Write (“Element found at location”, MID)
Else:
Write (“Element not found”)
[End of IF Structure]
Step 7: [Finish]
Exit
Limitations of Binary Search: In binary search we have required two conditions first is:
the list must be sorted and the second is: one must have the direct access to the middle elements
in any sub list. But keeping data in a sorted array is normally very expensive when there are
many insertions and deletions.
Prepared by: Arshad Iqbal, Lecturer (CS/IT), The University of Agriculture, Peshawar
5
Week No. 08: SORTING
Introduction to sorting: Sorting is the fundamental operation in computer science. It is refers
to the operation of arranging data in some given order such as increasing or decreasing with
numerical data or alphabetically with character data. In real life we come across several
examples of sorted information e.g. in the telephone directory the names of the telephone owners
are written in alphabetical order.
Example: Consider we have six numbers as 1 3 5 2 6 4 we can arrange it in ascending order
as 1 2 3 4 5 6 or in descending order as 6 5 4 3 2 1. Similarly if we have alphabets B A D E C F
we can arrange it in ascending order (A to Z) as A B C D E F or in descending order (Z to A)
F E D C B A.
A sort can be classified as internal sort if the elements are sorted in main memory
or external sort if some of an element that is sorting in auxiliary memory. We will discuss
internal sort only.
Sorting Methods: There are many sorting methods which are used but some of them are the
following:
1. Selection sort
2. Bubble sort
3. Insertion sort
1. Selection Sort:
In selection sort, select the first element and compare it with the rest of the elements.
For ascending order, if the next compared element is less than the selected element, then swap
the selected element with the compared element. Otherwise compare the selected element with
the next element of array. This process will find the 1st smallest element and put it on the first
position in the first pass. In second pass, it will find the second smallest element of array and
put it on the second position and so on.
Similarly for descending order, if the next compared element is greater than the selected
element, then swap the selected element with the compared element. Otherwise compare the
selected element with the next element of array. This process will find the 1st greatest element
and put it on the first position in the first pass. In second pass, it will find the second greatest
element of array and put it on the second position and so on.
The selection sort is simple to implement. It is however, insufficient for large lists. It is usually
used to sort lists no more than 1000 items. For sorting N elements, N-1 passes are required.
Example for ascending order:
40 59 36 23 65 46
Pass 1: 40 59 36 23 65 46
40 59 36 23 65 46
36 59 40 23 65 46
23 59 40 36 65 46
23 59 40 36 65 46
23 59 40 36 65 46
Prepared by: Arshad Iqbal, Lecturer (CS/IT), The University of Agriculture, Peshawar
6
Pass 2: 23 59 40 36 65 46
23 40 59 36 65 46
23 36 59 40 65 46
23 36 59 40 65 46
23 36 59 40 65 46
Pass 3: 23 36 59 40 65 46
23 36 40 59 65 46
23 36 40 59 65 46
23 36 40 59 65 46
Pass 4: 23 36 40 59 65 46
23 36 40 59 65 46
23 36 40 46 65 59
Pass 5: 23 36 40 46 65 59
23 36 40 46 59 65
Algorithm for Selection Sort: This algorithm is used for selection sort of a linear array A
having N filled elements. S and C are the control variables for loops and TEMP is used for
swapping process.
ALGORITM (For Ascending Order): SELECTION SORT (A, N, S, C, TEMP)
2. Bubble Sort: Bubble sort bubble up the largest value to the end.
In bubble sort, two adjacent memory cells are compared. If 1st is greater than the 2nd then
exchange them. To arrange an array in ascending order, through exchange of elements, the
largest value slowly floats or bubbles up to the top or end. Similarly to arrange an array
in descending order, the smaller value slowly bubbles up to the top.
Bubble sort is a slow method therefore it is used for sorting limited amount of data. Bubble sort
is easily programmable. For sorting N number of elements, N-1 passes are required.
17 35 37 15
17 35 37 15
17 35 15 37
Pass 3: 17 35 15
17 35 15
17 15 35
Pass 4: 17 15
15 17
Thus after 4 passes we have following sorted elements:
15 17 35 37 49
Prepared by: Arshad Iqbal, Lecturer (CS/IT), The University of Agriculture, Peshawar
8
Algorithm for Bubble Sort: Algorithm for bubble sort of a linear array A having N filled
elements. P and C are the control variables for loops and TEMP is used for swapping process.
ALGORITHM (For Ascending Order): BUBBLE SORT (A, N, P, C, TEMP)
Step 1: [Starting passes loop]
Repeat step 2 for P = 1 to N - 1 by 1
Step 2: [Starting comparison loop]
Repeat step 3 for C = 1 to N - P by 1
Step 3: [Compare elements]
If (A[C] > A [C + 1]) then
TEMP = A [C]
A [C] = A [C+1]
A [C + 1] = TEMP
[End of if structure]
Step 4: [Finish]
Exit
ALGORITHM (For Descending Order): BUBBLE SORT (A, N, P, C, TEMP)
Step 1: [Starting passes loop]
Repeat step 2 for P = 1 to N - 1 by 1
Step 2: [Starting comparison loop]
Repeat step 3 for C = 1 to N - P by 1
Step 3: [Compare elements]
If (A[C] < A [C + 1]) then
TEMP = A [C]
A [C] = A [C+1]
A [C + 1] = TEMP
[End of if structure]
Step 4: [Finish]
Exit
3. Insertion Sort: Insertion sort is performed by inserting each element at the appropriate position.
In insertion sort, the first pass starts with the comparison of 1st element with itself. In second
pass, the 2nd element is compared with 1st element. In 3rd pass, the 3rd element is compared with
1st and 2nd element. In general in every pass elements are compared with all elements to its left.
If at any point it is found that element can be inserted at a position then space is created for it by
shifting the right elements to the right side and inserting the element at a suitable position.
For sorting N number of elements, N passes are required.
Example for ascending order:
25 57 48 37 12 92
Pass 1: 25 57 48 37 12 92
Pass 2: 25 57 48 37 12 92
Pass 3: 25 57 48 37 12 92
25 48 57 37 12 92
25 48 57 37 12 92
Pass 4: 25 48 57 37 12 92
25 48 37 57 12 92
25 37 48 57 12 92
25 37 48 57 12 92
Prepared by: Arshad Iqbal, Lecturer (CS/IT), The University of Agriculture, Peshawar
9
Pass 5: 25 37 48 57 12 92
25 37 48 12 57 92
25 37 12 48 57 92
25 12 37 48 57 92
12 25 37 48 57 92
Pass 6: 12 25 37 48 57 92
12 25 37 48 57 92
A stack is a list of elements in which an element may be inserted or deleted only at the one
end, which is called top of the stack. It means that elements of stack can be removed in reverse
order, in which they are inserted into stack.
Insertion of data or element into the stack top is called PUSH or “stacking”. And deletion of
data or element from the stack top is called POP or “Un stacking”.
The items in the stack are stored and retrieved in LIFO and FILO manner. LIFO means Last In
First Out and FILO means First In Last Out. Other names used for stack are “PILES” and
“Push Down List”.
15 4 Top of stack
14 3
13 2
12 1
11 0
Stack Implementation:
Stack can be implemented in the following two ways:
1. Static
2. Dynamic
1. Static Implementation:
In static implementation of stack is done through the linear array e.g. “stack”. A pointer
variable which contains the location of the Top element of the stack and also a variable
MAXSTK which gives us maximum number of elements that can be inserted or Pushed on the
stack e.g.
Stack
10 20 30 40
0 1 2 3 4 5 6 7
Top 3 MAXSTK 7
There are some limitations in the implementation using arrays such as, when a size of an array
is declared, its size cannot be modified during program execution. It is also inefficient for
utilization of memory i.e. when we declare an array the memory is allocated which is equal to
the array size. But when we need more space in memory at the execution time the array does not
provide the facility to reserve the memory at the execution time. And also when we declare an
array with maximum size but the elements stored in stack is less than the maximum then the
remaining memory will be wasted.
Prepared by: Arshad Iqbal, Lecturer (CS/IT), The University of Agriculture, Peshawar
11
2. Dynamic Implementation:
Pointers can also be used for implementation of stack. The link list is an example of this
implementation. Using pointer implementation at run time, there is no restriction on the number
of elements. The stack may be expendable. The memory is efficiently utilized with pointers.
Memory is allocated only when element is pushed to the stack.
Operation on Stack:
PUSH Operation:
PUSH operation is used to insert an element to the stack. To insert an element first of all the
stack top is monitored if it is equal to the maximum size of stack then it shows the message of
overflow of stack. Otherwise the top pointer is incremented and the item is pushed in the top of
the stack e.g.
10 Top
8 Top 8
Top 2 Top 2 2
Empty stack Push 2 Push 8 Push 10
Algorithm for PUSH operation: PUSH algorithm is used pushes an ITEM onto the stack.
TOP is the pointer pointing to the top of the stack. MAXSTK is the maximum numbers of
elements.
Prepared by: Arshad Iqbal, Lecturer (CS/IT), The University of Agriculture, Peshawar
12
Algorithm to PUSH multiple elements at a time: This algorithm is used to PUSH multiple
elements at a time in an empty stack.
POP Operation:
POP operation is used to delete an element from the stack. To delete an element first of all the
tack top is monitored it is equal to the empty stack (0 or -1 in C) then it shows the message of
“Underflow” or “Empty Stack”. Otherwise the top pointer is decremented and the item is
returned e.g.
10 TOP
8 8 TOP
2 2 2 TOP
Stack initially POP 10 POP 8
Algorithm for POP Operation: POP algorithm deletes the TOP element of STACK and
assigns it to the variable ITEM.
Prepared by: Arshad Iqbal, Lecturer (CS/IT), The University of Agriculture, Peshawar
13
Week No. 10: QUEUE
Introduction to Queue:
A queue is a linear list of elements in which deletion can take place only at one end, called
front, and insertion can take place at other end, called rear. The term front and rear are used
in describing a linear list only when it is implemented as a queue.
The queue is also called First In First Out (FIFO) lists, since the first element in the queue will
be the first element that can get out of the queue. In other words, the order in which the elements
enter a queue is the order in which they will leave.
The purpose of queue is to provide some form of buffering. In a computer system, queues are
used for:
Process Management: For example, in a timesharing system in computer, programs are added
to a queue and are executed one after the other.
Buffer between the fast computer and a slow printer: Documents sent to the printer for
printing is added to a queue. The document sent first is printed first and document sent last is
printed last.
Queues abound in everyday life. The automobiles is waiting to pass through an intersection
from a queue in which the first car in line is the first can through; the people is waiting in line at
a bank from a queue, where the first person in line is the first person to be waited on and so on.
For example:
Deletion 10 20 30 40 50 Insertion
Front Rear
Operation on Queue:
Four primitives operation applied to a queue.
1. Insertion: The insertion operation insert (QUEUE, ITEM) inserts an ITEM at the
REAR of a QUEUE.
3. Empty: The third operation empty (QUEUE) return FALSE when the QUEUE is
not empty otherwise it return TRUE if the QUEUE is empty.
Prepared by: Arshad Iqbal, Lecturer (CS/IT), The University of Agriculture, Peshawar
14
Simple example for non-Circular QUEUE: To explain the above operations we have a
simple example as follow:
FRONT = REAR = 0 0 1 2 3 4
Insert (QUEUE, A) A
0 1 2 3 4
FRONT = 0, REAR = 1
A B
Insert (QUEUE, B)
0 1 2 3 4
FRONT = 0, REAR = 2
A B C
Insert (QUEUE, C)
full (QUEUE) = ? It will return FALSE because the QUEUE is not full.
FRONT = 1, REAR = 2 0 1 2 3 4
ITEM = remove (QUEUE) B C
FRONT = 1, REAR = 3 0 1 2 3 4
Insert (QUEUE, D) B C D
FRONT = 1, REAR = 4 0 1 2 3 4
B C D E
Insert (QUEUE, E)
0 1 2 3 4
FRONT = 2, REAR = 4
C D E
ITEM = remove (QUEUE)
Representation of Queues:
The queue is represented in the computer in various ways, usually by means of one-way link list
or linear arrays. Let takes the example of linear array.
Consider a linear array QUEUE and two pointers variables FRONT and REAR. FRONT will
contain the location number of the front element of the queue and REAR will contain the
location number of the rear element. The condition FRONT = 0 will indicate that queue is
empty. If the array QUEUE has N elements, then whenever a new element is added the value of
REAR will be incremented by 1 e.g.
REAR = REAR + 1
Similarly when an item is deleted the value of the FRONT will be incremented by 1e.g.
FRONT = FRONT + 1
Prepared by: Arshad Iqbal, Lecturer (CS/IT), The University of Agriculture, Peshawar
15
Example of Circular Queue: To explain the above operations we have a simple example as
follow:
0 1 2 3 4
FRONT = REAR = 0 0 1 2 3 4
Insert (QUEUE, A) A
FRONT = 0, REAR = 1 0 1 2 3 4
Insert (QUEUE, B) A B
FRONT = 0, REAR = 2 0 1 2 3 4
Insert (QUEUE, C) A B C
FRONT = 1, REAR = 2 0 1 2 3 4
ITEM = remove (QUEUE) B C
FRONT = 1, REAR = 3 0 1 2 3 4
Insert (QUEUE, D) B C D
FRONT = 1, REAR = 4 0 1 2 3 4
Insert (QUEUE, E) B C D E
FRONT = 2, REAR = 4 0 1 2 3 4
ITEM = remove (QUEUE) C D E
full (QUEUE) = ? It will return FALSE because the QUEUE is not full.
FRONT = 2, REAR = 0 0 1 2 3 4
Insert (QUEUE, F) F C D E
0 1 2 3 4
FRONT = 2, REAR = 1
F G C D E
Insert (QUEUE, G)
In circular queue the QUEUE [0] comes after the QUEUE [N] in linear array i.e. the first
element stored after the last element of the queue if the space is a variable. With this assumption
we insert ITEM into the QUEUE by assigning ITEM to QUEUE [1]. Instead of incrementing
REAR to N+1 we reset REAR = 1 and then assign the item.
Similarly if FRONT = N and the element of QUEUE is deleted then we have reset FRONT = 1
instead of increasing FRONT to N+1. Suppose that QUEUE contains only one element i.e.
FRONT = REAR and the element is deleted then we will reset FRONT = NULL, (or -1) and
REAR = NULL (or -1) indicating that queue is empty.
Prepared by: Arshad Iqbal, Lecturer (CS/IT), The University of Agriculture, Peshawar
16
Insertion Algorithm for non Circular QUEUE: This algorithm is used to insert an ITEM to
non-Circular QUEUE.
ALGORITHM: INSERTION (FRONT, REAR, ITEM, N, QUEUE)
Step 1: [Check overflow condition]
If (REAR > = N - 1) Then
Write (“Overflow”)
Return
[End of If structure]
Step 2: [Set REAR Pointer]
If (REAR = -1) Then
REAR = 0
Else
REAR = REAR +1
[End of If structure]
Step 3: [Insert the Item]
QUEUE [REAR] = ITEM
Step 4: [Set the FRONT Pointer]
If (FRONT = -1) Then
FRONT = 0
[End of If structure]
Step 5: [Finish]
Exit
Insertion Algorithm for Circular QUEUE: This algorithm is used to insert an ITEM to
Circular QUEUE.
Deletion Algorithm for Circular QUEUE: This algorithm is used to delete an ITEM
from Circular QUEUE.
Prepared by: Arshad Iqbal, Lecturer (CS/IT), The University of Agriculture, Peshawar
18
DEQUEUE:
In a simple queue the elements are deleted at one end and inserted at the other end. But
DeQueue is a linear list in which elements can be added or removed at either end but not at the
middle. The DeQueue is pronounced as Deck or DeQueue. The term DeQueue is a contraction
of the name double-ended queue.
1. Input Restricted DeQueue: It is the type of DeQueue, which allows insertion from one
end and deletion from both ends.
2. Output Restricted DeQueue: It is the type of DeQueue, which allows deletion from one
end and insertion from both ends.
1. Insertion: In case of insertion if element is added at left the LEFT is decreased by 1, and
if element is added at right the RIGHT is increased by 1.
2. Deletion: In case of deletion if the element is deleted from left the LEFT is increased by
1 and if the element is deleted from right the RIGHT is decreased by 1.
Prepared by: Arshad Iqbal, Lecturer (CS/IT), The University of Agriculture, Peshawar
19
Week No. 11: ONE WAY LINKED LIST
Introduction:
A list is an ordered collection of data. One way to use the list is sequential array. In this type of
lists it is easy to compute the address of an element for storage and retrieval purposes. On the
other hand these have certain limitations. There are many situations in which there is a need for a
data structure in which data can be updated, inserted and deleted continuously and the data
should be in sorted format at run time. That is, insertion and deletion of data items frequently
occurs. But it is relatively expensive to insert or delete elements from sequential list e.g. array.
Ø When a number of users share main memory, there may not be enough adjacent memory
locations left to hold an array. But there could be enough memory in the shape of small
free blocks.
Ø The second major problem with array is when we have a large list of data elements and
exact number of elements cannot be known in advance while an array have a fixed size
and we cannot increase the size of array on run time when additional memory is required;
therefore arrays are called static data structure.
Ø The data access speed is becomes slow when the size of the array becomes large.
To overcome these limitations Linked Lists are used. In a link list the elements are logically
adjacent needs not to be physically adjacent in the memory but they should be linked or
connected through a pointer.
Single Link List or One Way Link List:
The link list is a dynamic data structure i.e. the size is not fixed and it will expand during
program execution. Also a link list is a linear data structure having unlimited elements, each
element of a link list is called a “node”. The node of one way link list have at least two fields,
the first one is the data or information field (more than one data fields can be used in a node to
store information) which contain the actual data of a list and the other field is called link field or
next-pointer field which contain the address of the next node of a link list.
Example:
Data/Information Pointer
In the following diagram of a one-way link list have four nodes. Each node has two parts. The
left part represents the information part of the node and the right part represents pointer field or
link field, which contains the memory address of the next node. In the list we have a start node
whose address is stored in a pointer START. We need a START pointer to trace the list. A
special case is the list that when the list have no nodes. Such a list is called a null list or empty
list and denoted by null pointer in the START pointer. The pointer field of the last node will
contain the null pointer to show the end of the list.
START
1000
1. Front Insertion:
It is the easiest way to insert a node in the link list. Following algorithm is used to insert a node
in the front of the LIST.
ALGORITHM: FRONT INSERTION (Insert, Node, First, Data, Info)
This algorithm is used to insert a node insert at the start of a one-way link list.
In step 1, 2 & 3 we create a node insert, store information in it and assign to the first node
address. In step 4, we assign insert to first pointer to make newly inserted node the first node of
the LIST. The following figure shows the above mechanism:
FIRST
1004
1004
05 1000
INSERT
One Way Link List Front Insertion
2. Middle Insertion: There are two methods, which can be used to insert node at the
middle if a one way link list.
Method 1: If the nodes of the given list are unsorted then we insert node in a list after a given
node.
Method 2: If the nodes of the given list are sorted in some particular order then the node is
inserted at a particular position so that the sorted order could be maintained.
We will discuss the second method. The following algorithm inserts a node at a proper place.
Prepared by: Arshad Iqbal, Lecturer (CS/IT), The University of Agriculture, Peshawar
21
ALGORITHM: MIDDLE INSERTION (Prev, First, Cur, X, Insert)
This algorithm is used to insert a node at the proper location in as ordered LIST.
In above algorithm step 4, 5 & 6 are used to find out a proper position for a new node. In step 7
& 8, we create a new node, assign its address to insert pointer and store information. In step 9,
10 & 11, the new node is linked to the current and previous nodes.
FIRST
1000
1004
25 1002
INSERT
Prepared by: Arshad Iqbal, Lecturer (CS/IT), The University of Agriculture, Peshawar
22
3. End Insertion:
By end insertion, means to insert a node at the end or last of a link list. The algorithm is given
as follow:
ALGORITHM: END INSERTION (Prev, First, Cur, X, Insert)
This algorithm is used to insert a node insert at the end of a one-way link list.
Step 1: [Set Prev pointer to First Node]
Prev = First
Step 2: [Set Cur to prev link]
Cur = Prev -> link
Step 3: [Read value for insertion]
Read (X)
Step 4: [Start loop to reach to the end of list]
Repeat step 5 & 6 ------- while (Cur -> link != Null)
Step 5: [Set Cur as Prev]
Prev = Cur
Step 6: [Set Cur to Cur -> link]
Cur = Cur -> link
Step 7: [Create a new node and set it to insert]
Insert = (struct node *) malloc (size of (struct node))
Step 8: [Store Information]
Insert -> data = X
Step 9: [Set Insert link as Null]
Insert -> link = null or Insert -> link = Cur -> link
Step 10: [Set Prev link as Insert]
Cur -> link = Insert
Step 11: [Finish]
Exit
In the above algorithm step 2 & 3 are used to reach to the end of link list. When end of list is
found then in step 4, 5 & 6, we create a new node; assign its address to insert pointer, store
information and set insert pointer as NULL, because null link pointer shows the end of the list.
In step 7, the new node is linked to previous node.
The following figure shows the above mechanism.
FIRST
1000
1004
50 NULL
INSERT
One Way Link List End Insertion
Prepared by: Arshad Iqbal, Lecturer (CS/IT), The University of Agriculture, Peshawar
23
One Way Link List Deletion Algorithm:
Let LIST be a given link list with N successive nodes and we want to delete a node X from a
link list. Then we can delete that node from three different location of a link list, which is given
as:
1. Front Deletion
2. Middle Deletion
3. End Deletion
1. Front Deletion: Front deletion is the easiest way to delete a node from a link list.
Following is the algorithm is used to delete a node from one-way link list from the front or
start.
This algorithm is used to delete a node from the front of a one-way link list.
In the above algorithm in step 1, we store the address of first node in current pointer. In step 2
we update the first to the next node to make the second node as first. In step 3, we delete the
node by free () function provided in C language.
FIRST
1001
Prepared by: Arshad Iqbal, Lecturer (CS/IT), The University of Agriculture, Peshawar
24
2. Middle Deletion: In middle deletion if we want to delete node X from a linked list
which is not at the first or last location. For this we start searching for that node in a list. If found
then we should update successor and predecessor and then delete the node. The following
algorithm is used to delete a node from middle form one-way link list.
In the above algorithm in step 1, 2 we assign previous pointer to first node and cur to the next of
first node. In step 4, 5, 6 & 7 we start loop for searching for the node, if it is found it is deleted
and exited otherwise the prev is updated to cur and cur is updated to the next node.
FIRST
1000
Prepared by: Arshad Iqbal, Lecturer (CS/IT), The University of Agriculture, Peshawar
25
3. End Deletion: To delete the end node of a one-way link list first we have to reach to the
end of the list.
In the above algorithm in step 1 & 2 we assign prev to first node and cur to the next node to the
prev. In step 3, 4 & 5, we reach to the end of the list. In step 6 & 7, we assign Null to prev to
make it the end node and free cur pointer to delete the end node.
FIRST
1000
Prepared by: Arshad Iqbal, Lecturer (CS/IT), The University of Agriculture, Peshawar
26
Week No. 12: TWO WAY LINKED LIST
Introduction to Two Way Linked List/ Double Linked List:
One of the big disadvantages of one-way link list is that only possible way to traverse the data
is in the list is the forward traversing. There is no backward traversing of a list in one-way link
list. The problem is handled by the double or two-way link list. In two way link list each node
is linked to both its successor and predecessor. The two-way link list is traversable from either
direction i.e. forward and backward. On two-way link list a node is divided into three parts.
Information part: It contains the data of a node.
Right or next pointer: It points to the successor node.
Left or previous pointer: Pointer to the predecessor node.
Left Information Right
In the following diagram of two-way link list with three nodes, each node has three parts. The
left part contains the address of predecessor node while the right part contains the address of the
successor node and the information part contains the information about the element. There are
two pointers also used i.e. first and last. The first pointer points to the first or start node of the
two-way link list and the last pointer points to the last or end node of the two-way link list.
FIRST LAST
100 102
FIRST LAST
103 102
103
NULL 5 100
2. Middle Insertion:
There are two methods that can be used to insert node at the middle of a Two-way link list.
Method 1: If the nodes of the given list are unsorted then we insert node in a list after a given
node.
Method 2: If the nodes of the given list are sorted in some particular order then the node is
inserted at a particular position so that the sorted order could be maintained.
We will discuss the second method. The following algorithm inserts a node at a proper place in
Two-way link list.
Prepared by: Arshad Iqbal, Lecturer (CS/IT), The University of Agriculture, Peshawar
28
ALGORITHM: MIDDLE INSERTION (Prev, Cur, First, Insert)
This algorithm is used to insert a node at the proper location in an ordered LIST in Two-way
link list.
In the above algorithm step 4, 5 & 6 are used to find out a proper position for a new node. In
step 7 & 8, we create a new node, assign its address to insert pointer and store information. In
step 9, 10 & 11, we link the new node to the current and previous nodes respectively.
The following figure shows the above mechanism.
FIRST LAST
100 102
103
101
25 102
INSERT
Prepared by: Arshad Iqbal, Lecturer (CS/IT), The University of Agriculture, Peshawar
29
3. End Insertion:
End insertion in the Two-way link list is much easier than the one –way link list because we
have a direct access to the end node of the Two-way link list node.
This algorithm is used to insert a node at the end of the Two-way link list.
FIRST LAST
100 103
103
102 5 NULL
INSERT
Prepared by: Arshad Iqbal, Lecturer (CS/IT), The University of Agriculture, Peshawar
30
Two Way Linked List Deletion Algorithm:
Let LIST be a given Two-way link list with N successive nodes and we want to delete a node X
from LIST. Then we can delete that node from three different location of a Two-way link list
which is given as:
1. Front Deletion
2. Middle Deletion
3. End Deletion
1. Front Deletion:
Front deletion is the easiest way to delete a node from a link list. Following is the algorithm is
used to delete a node from Two-way link list from the front or start.
This algorithm is used to delete a node from the front of a Two-way link list.
In the above algorithm in step 1, we store the address of first node in current pointer. In step 2,
we update the first to the next node to make the second node as first. In step 3, we delete the
node by free () function provided in C language. In step 4, again we set the current pointer to
point to first node and now current node is the first node in the list. In step 5, we assign NULL
to current left pointer to make it first node of the list.
FIRST LAST
101 102
Prepared by: Arshad Iqbal, Lecturer (CS/IT), The University of Agriculture, Peshawar
31
2. Middle Deletion:
Let LIST be a Two-way link list with N successive nodes and we want to delete a node X from
a linked list which is not at the first or last location. For this we start searching for that node in
the list. If found then one should update the successor and predecessor node pointers.
The following algorithm inserts a node at a proper place in Two-way link list.
This algorithm is used to delete a node at the proper location in Two-way link list.
In the above algorithm in step 1, 2, we assign previous pointer to first node and cur to the next of
first node. In step 4, 5, 6 & 7, we start loop for searching for the node, if it is found it is deleted
and exited otherwise the prev is updated to cur and cur is updated to the next node at right side.
The following figure shows the above mechanism:
FIRST LAST
100 102
End deletion is also very easy as front deletion in Two-way link list.
This algorithm is used to delete the end node of a Two-way link list.
In step 2 of the above algorithm we updated the last pointer to point the 2nd last node in the list.
In step 3, memory is freed occupied by the last node. In step 4, again set current pointer to last
node, and now current node is the last node in the list. Therefore, assign NULL to current right
pointer.
FIRST LAST
100 101
Prepared by: Arshad Iqbal, Lecturer (CS/IT), The University of Agriculture, Peshawar
33
Week No. 13: TREES
Introduction to Tree:
A tree is a non linear data structure. Each object of a tree starts with a root and extends into
several branches. Each branch may extend into other branches. Tree is mainly used to represent
the data containing a hierarchal relationship between elements e.g. family tree, table of contents,
organization chart of a company.
Types of Tree: The following are two types of Tree:
1. General Tree
2. Binary Tree
1. General Tree:
A general tree (sometimes called a tree) is defined to be a non empty finite set of elements
called nodes, such that:
i. Tree contains a distinguished element called the root of the tree.
ii. The remaining elements of tree form an ordered collection of zero or more
disjoint trees.
And in General Tree, a node can have any number of children.
A typical tree is shown below:
Figure 1:
A
B C D E
F G H I J K L M
2. Binary Tree:
A binary tree is a non linear data structure in which each node has only 0, 1, or 2 children
(mostly has two children). Binary tree can be empty or contains one node that is called root of
the tree. Typically the child nodes are called left & right nodes (Child).
B C
D E F
Prepared by: Arshad Iqbal, Lecturer (CS/IT), The University of Agriculture, Peshawar
34
Similar Trees:-
Two or more than two trees are said to be similar, if they have the same shape. Consider the
following diagram:
Figure 2: B C
F D E
Figure 3:
K L
M N O
In the above two trees, we see that each root node has two children nodes. The rightmost child
has again two children nodes in each tree. The leftmost node of each tree B and K has one child
node each. It should be noted that B and K has one child node but these child nodes appear to the
left of each node.
Hence the shapes of each tree are the same and thus they are known to be similar.
Copies of trees:-
Two or more than two trees are said to be copies of each other, if they are similar as well as the
contents of each node are also same. Consider the following tree diagram:
Figure 4: B C
F D E
Figure 5: B C
F D E
Prepared by: Arshad Iqbal, Lecturer (CS/IT), The University of Agriculture, Peshawar
35
In the above trees, we see that both the trees have same shapes. So they are similar trees. But we
also see that all the contents of the two trees are also the same, i.e. A is the root node in each tree,
B and C lies at the same level of each tree and so on. So they are also called copies of each
other.It should be noted that the two similar trees cannot be copies of each other, but it is
necessary that the two copied trees are always similar to each other.
Tree Terminology:
Family relationships terminology is frequently used to describe relationship between the nodes of
a tree.
Root Node: The node at the top e.g. in the above figure 1, A is the root node.
Parent: Those nodes that have either the child nodes as well as one parent node is called
parent node. E.g. in the above figure 1, B is a parent node.
Children: The node that is directly connected to parent node is called the child node. E.g. in
the above figure 1, F is a child of B.
Sibling: The nodes having same parent is called sibling or brother nodes. E.g. in the
above figure 1, I, J and K are sibling nodes because they have same parent D.
Sub tree: The child node of the root that has its own child nodes is called sub tree e.g. in
the above figure 1, B, C, D & E are sub trees.
Level: The root of a tree has a level 0 and the level of any other nodes in the tree is one
more than the level of its father e.g. in the above figure 1, node J is on level 2.
Edge: The line drowns from a node of tree to a successor is called edge or connection
between one node to another.
Leaf: The node with no successor is called leaf node or terminal node. E.g. in the
above figure 1, M is a leaf node because it has no successor (child nodes) or a
node with no children.
Depth or Height: The maximum numbers of node in a branch is called depth or height of
the tree e.g. Depth or Height of the tree in the above figure 1 is three.
Degree: Maximum number of children possible for a node or number of sub trees of a
node e.g. in the above figure 1, degree of A is 4, degree of D is 3, degree of C is
1 and degree of F is 0 etc.
Prepared by: Arshad Iqbal, Lecturer (CS/IT), The University of Agriculture, Peshawar
36
Week No. 14: TYPES AND OPERATIONS OF BINARY TREE
Types of Binary Tree: A binary tree has following types:
1. Strictly Binary Tree:
If every non leaf/non terminal node in a binary tree has non empty left and right subtrees/childs
then such a tree is called Strictly Binary Tree e.g.
F G
B C Level 1 2n = 21 = 2 nodes
D E F G Level 2 2n = 22 = 4 nodes
A Level 0 2n = 20 = 1 nodes
B C Level 1 2n = 21 = 2 nodes
D E F G Level 2 2n = 22 = 4 nodes
H I J K
R A
A B OR B C
C D D F G
H I J K
Prepared by: Arshad Iqbal, Lecturer (CS/IT), The University of Agriculture, Peshawar
37
Binary Search Tree:
A binary tree in which the left child node of a tree is less than the value of its root and the right
child node value is greater than its root node value is called binary search tree.
Due to these properties, the elements traversed using in order will yield a sorted list of the
elements of tree. Main advantage of binary search tree is that it is easy to create new tree,
insertion, searching and deletion.
Following is an example of binary search tree: {49 30 36 25 75 60 55 68 80 28 12}
49
30 75
25 36 60 80
12 28 55 68
16 19 15 9 8 66 12 61
16
15 19
9 66
8 12 61
8 9 12 15 16 19 61 66
2. Insertion:
A new node is inserted after searching other nodes if the new value exist in any node then
insertion operation fails otherwise the new node is inserted when searching terminates. The
following algorithm finds the location of item in the Binary Search Tree:
• Compare item with Root node of Tree
– If item < N move to Left child
– If item > N move to right child
• Repeat the above until the item inserted into correct place.
Prepared by: Arshad Iqbal, Lecturer (CS/IT), The University of Agriculture, Peshawar
38
For example 01: To insert the item 20 in the following binary search tree:
38
14 56
8 23 45 85
18 70
20
Example 02: To insert the item 20 in the following binary search tree:
9
5 11
4 7 10 12
6 8 20
3. Deletion:
To delete a node involves three conditions:
I. A node with no Childs i.e. leaf node
II. A node with one child
III. A node with two Childs.
I. Leaf Node Deletion:
To delete a leaf node first of all search the tree if element to delete is found then check the
position of leaf node then delete leaf node.
For example: Delete item 8 from the following binary search tree:
9
5 11
4 7 10 12
6 8 20
Before deleting
5 11
4 7 10 12
6 20
After deleting
Prepared by: Arshad Iqbal, Lecturer (CS/IT), The University of Agriculture, Peshawar
39
II. Deleting Node with one Child:
First of all search node if search is successful then check the position of deleting node and its
child if the deleting node is left of its parent node then child node of deleting node become left
child of the parent of deleting node or vice versa (in other words, deletion of non leaf node with
1 child, child node takes place of disposed node).
For example: Delete item 11 from the following binary search tree:
9
5 11
4 7 12
6 8 10 20
Before deleting
5 12
4 7 10 20
6 8
After deleting
III. Deleting Node with two Childs:
– Replace the deleting node with largest element of its left sub tree
OR
– Replace the deleting node with smallest element of its right sub tree
For example: Delete item 11 from the following binary search tree:
9
5 11
4 7 10 13
6 8 12 20
Before deleting
5 12 OR 10
4 7 10 13 13
6 8 20 12 20
After deleting
Prepared by: Arshad Iqbal, Lecturer (CS/IT), The University of Agriculture, Peshawar
40
Week No. 15: TRAVERSING OF GENERAL TREE & BINARY TREE
Traversing of General Tree:
Accessing the nodes of a tree:
There are three basic ways of accessing the nodes of a tree:
1. Level by Level Traversing
2. Pre Order Traversing
3. Post Order Traversing/Suffix Walk Traversing
The order in which the nodes or element of a linear list are visited in a traversal is clearly from
first to last however, there is no such natural linear order for the nodes of tree so different
orderings are used for traversal in different cases.
Q R S
T U V
W X
3. Postfix Traversing: T U Q R W X V S P
Prepared by: Arshad Iqbal, Lecturer (CS/IT), The University of Agriculture, Peshawar
41
Traversing of Binary Tree:
In traversing, each node of a binary search tree is accessed for processing exactly once. It is also
called visiting different methods are used to visit a binary search tree i.e.
1. Inorder Traversal
2. Preorder Traversal
3. Post Order Traversal
Note:
• If a tree T is null then the empty list is the Preorder, Inorder and Post Order listing of T.
• If a tree T consist a single node then that node by itself is the Preorder, Inorder and Post
Order listing of T.
1. Inorder Traversal:
In inorder traversal, the left child is traversal first then the root and after it the right child is
accessed.
For example:
6
2 8
1 4 7 9
3 5
2. Preorder Traversal:
In preorder traversal, first root is processed then left child and then right child.
For example:
1
2 7
3 4 8 9
5 6
5 8
1 4 6 7
2 3
Polish Notation:
1. Infix to Prefix:
Convert the following Infix notations to the prefix notation:
i. (A + B) * C
= [+AB] * C
= *+ABC
ii. A + (B * C)
= A + [*BC]
= +A*BC
iii. (A + B)/(C – D)
= [+AB]/[ –CD]
= /+AB–CD
2. Infix to Postfix:
Convert the following Infix notations to the Postfix notation:
i. (A + B) * C
= [AB+] * C
= AB+C*
ii. A + (B * C)
= A + [BC*]
= ABC*+
iii. (A + B)/(C – D)
= [AB+]/[CD–]
= AB+CD–/
Prepared by: Arshad Iqbal, Lecturer (CS/IT), The University of Agriculture, Peshawar
43
3. Infix to Prefix & Postfix:
To Prefix:
((a + b) + c * (d + e) + f) * (g +h)
= ([+ab] + c * [+de] + f) * [+gh]
= ([+ab] + [*c+de] + f) * [+gh]
= ([++ab*c+de] + f) * [+gh]
= [+++ab*c+def] * [+gh]
= *+++ab*c+def+gh
To Postfix:
((a + b) + c * (d + e) + f) * (g +h)
= ([ab+] + c * [de+] + f) * [gh+]
= ([ab+] + [cde+*] + f) * [gh+]
= ([ab+cde+*+] + f) * [gh+]
= [ab+cde+*+f+] * [gh+]
= ab+cde+*+f+gh+*
Prepared by: Arshad Iqbal, Lecturer (CS/IT), The University of Agriculture, Peshawar
44
Week No. 16: GRAPH
Introduction to Graph:
Tree data structure is used to represent the one to many relationships. In real life, we frequently
come across the problem can be best described by many to many relationships. Such a problem
cannot be solved using tree or other data structures. To solve this problem we use a non linear
data structure called graph.
A graph is a non linear data structure which is made up of sets of nodes and lines. Nodes are
called Vertices or Points and lines are called Edges of arcs. Lines are used to connect vertices
with each other. An edge of a graph is represented as follow:
e = [u, v]
‘u’ and ‘v’ denote the start and end nodes of an edge ‘e’. They are also called head and tail nodes
of edge ‘e’.
Graphs are used to represents essentially any relationship. Graphs are used to study the problems
in a wide variety of areas including computer science, electrical engineering, chemistry etc. for
example it is used to represent and study transport networks, communication networks and
electrical circuits.
In transportation networks, graph vertices represent the location between which people or goods
can be moved. Location may be cities, airports, terminals etc. The edge represents path between
the vertices which may be roods, railway tracks etc, though which the communication between
cities takes place. Following graph represents a transport links between file cities. The vertices
represent the city and the edge represents the roods between these cities.
Charsadda
Mardan Nowshera
Peshawar Islamabad
Graph Terminologies:
1. Degree of a Node:
The number of edges a node contains is called the degree of the node. For example, in the
following figure A has a degree 3, B has a degree 2, C has a degree 2 and D has a degree 3.
A C
B D
A node that has 0 degree is called Isolated Node. And a graph having only one isolated node is
called Null Graph.
Prepared by: Arshad Iqbal, Lecturer (CS/IT), The University of Agriculture, Peshawar
45
Out – degree & In – degree:
The number of edges beginning from a node is called out – degree of the node. For example in
the following figure: A has 3 out – degree, C has 1 out – degree, D has 1 out – degree and B has
0 out – degree. A node having 0 out – degree is called terminal node or leaf node and other
nodes are called Branch Nodes.
A C
B D
The number of edges ending at a node is called in – degree of the node. In the graph shown
above, the in – degree of A is 0, B has in – degree 2.
The sum of the out – degree and in – degree is the Total Degree. The total degree of a loop node
is 2 and that of isolated node is 0.
A C
B D
The node that has 0 out – degree but have positive in – degree is called Sink Node. For example
in above figure B is a sink node because it has 0 out – degree and positive in – degree 2.
3. Pendent Node:
A node is said to be a pendent node if it has total degree equal to 1. In the given figure A is a
pendent node because it has out – degree 1 and in – degree 0, so its total degree becomes 1. All
the other nodes have more then 1 total degree so they are not pendent nodes.
A B C
Pendent node
D E
4. Loop Edge:
An edge ‘e’ is said to be a loop edge if the same node is its tail and head. A loop edge is shown
in the following figure:
B
A
Prepared by: Arshad Iqbal, Lecturer (CS/IT), The University of Agriculture, Peshawar
46
5. Multiple Edges:
A graph is said to have multiple edges, if it has more than one edge have the same tail and head
nodes. An example of multiple edges is given below:
B
A
C
The edges that have the same tail and head nodes are known as Parallel Edges.
A path which repeats no node is known as the Simple Path. A path is usually assumed to be a
simple path unless otherwise defined. For example, in the following figure, a path from A to B is
a Simple Path.
The number of edges in a path of a graph is called Length of the Graph. For a path consisting
of ‘n’ nodes is of length n – 1. For example, in the following figure, a path from A to B has two
nodes so the length of that path is n – 1 = 2 – 1 = 1 and a path from A to C, D, B has four nodes
so the length of that path is n – 1 = 4 – 1 = 3. So the Length of the following Graph is 3.
A C
B D
A C
B D
A path in which start node & end node are different is called Acyclic Path. Following figure is
an example of Acyclic Path:
A C
B D
The directed graph that has no cycles is called Acyclic Graph. A directed acyclic graph is also
referred to as DAG (Directed Acyclic Graph).
Prepared by: Arshad Iqbal, Lecturer (CS/IT), The University of Agriculture, Peshawar
47
Types of Graph:
1. Undirected Graph:
An undirected graph has edges that have no directions. An undirected graph is also called
undigraph e.g.
A C
B D
2. Directed Graph:
A directed graph has edges that are unidirectional. A directed graph is also called digraph e.g.
A C
B D
3. Weighted Graph:
A graph which has a weight or number associated with each edge is called a weighted graph.
Weight of an edge is sometimes called its cast. The weight of and edge usually represents some
conditions or situations associated with the edge. For example, in the following weighted graph,
the weights represent the distance between the cities.
Charsadda
30 28
18
Mardan Nowshera
B D B D
- Undirected Complete Graph - Directed Complete Graph
Prepared by: Arshad Iqbal, Lecturer (CS/IT), The University of Agriculture, Peshawar
48
5. Regular Graph:
A graph in which each node has equal total degree is called regular graph. Consider the
following figure in which there are three nodes. Each node of them has equal in – degree and
out – degree that total degree of the nodes is also equal. Hence the graph is said to be a regular
graph.
A
B C
6. Isomorphic Graph:
Two graphs are said to be isomorphic if they have same behavior in terms of graphical property.
All the edges of the two graphs must be incident at their corresponding nodes. The conditions for
isomorphism are:
For example in the following two graphs there are 5 nodes v1, v2, v3, v4, v5 and 5 edges in the
first graph and in the second one there also 5 nodes and 5 edges. The behavior of the two graphs
is same, because e1 lies in v4 and v5. Similarly e1 lies in v4 and v5. So in graph 1 and 2 numbers
of nodes are equal, the numbers of in – degree and out – degree are same. So they are isomorphic
graphs.
V5
e1
e2 e5
V4 V1 V2 V4
e5 e3 e2 e4 e1
V2 V3 V1 V3
e4 e3 V5
Graph 1 Graph 2
In graph 3 & 4, the number of edges and nodes are equal. Also the numbers of in – degree and
out – degree of v2 are same as v2. But v1 and v3 have not same in – degree and out – degree in
v1 and v3. Hence graph 3 & 4 are not isomorphic.
V1 V1
e1 e2 e1 e2
e3 e4
V2 V3 V2 V3
e4 e3
Graph 3 Graph 4
Prepared by: Arshad Iqbal, Lecturer (CS/IT), The University of Agriculture, Peshawar
49
Graph Representation:
Graphs are unstructured. One vertex in a graph might be adjacent to every other vertex. Similarly, a
vertex might be adjacent to just one vertex. This property of adjacency is used to represent graph in
computer memory.
Link representation of graph or Adjacency List:
Let figure G be a directed graph with 5 nodes look the following figure. The following table shows each
node in figure G followed by its adjacency list which is its list of adjacent nodes, also called its successors
or neighbors.
100
A 102 104 106 Ø
102
B 104 Ø
104
C Ø
106
D 104 108 Ø
108
E 104 Ø
A. Node List: Each element of in the list NODE will correspond to a node in graph, and it will
be a record of the form.
Here NODE will be the name of key value of the node, NEXT will be a pointer to the next node
in the list NODE and ADJ will be a pointer to the first element in adjacency list of the node,
which is maintained in the list EDGE. The shaded area indicates that there may be other
information in the record like in – degree, out – degree etc.
B. Edge List: Each element in the list EDGE will correspond to an edge of graph and will be a
record of the form.
The field DEST will point to the location in the list NODE of the destination or terminal node of
edge. The field LINK will link together the edges with the same initial node, that is, the nodes in
the same adjacency list. The shaded area indicates that there may be other information in the
record corresponding to the edge, such as weight etc. THE END
Prepared by: Arshad Iqbal, Lecturer (CS/IT), The University of Agriculture, Peshawar
50