Module 5 Graphs
Module 5 Graphs
MODULE - 5
Graphs: Definitions, Terminologies, Matrix and Adjacency List Representation Of Graphs,
Elementary Graph operations, Traversal methods: Breadth First Search and Depth First Search.
Sorting and Searching: Insertion Sort, Radix sort, Address Calculation Sort.
Hashing: Hash Table organizations, Hashing Functions, Static and Dynamic Hashing.
Files and Their Organization: Data Hierarchy, File Attributes Text Files and Binary Files, Basic
File Operations, File Organizations and Indexing.
2. Multigraph: A graph with self-loops or parallel edges(multiple edge) between any two vertices is
called a multigraph.
3. Complete graph: A graph is said to be a complete graph, if there exists an edge between every
pair of vertices.
4. Weighted graph: A graph in which a number is assigned to each edge in graph is called weighted
graph. Weight typically shows cost or distance of traversing.
Example: weights are distances between cities.
Unweighted graph: A graph in which a number is not assigned to edge in graph is called
unweighted. Edges simply show connections.
Example: Traverse the following graph by BFS and print all the vertices reachable from start vertex
a.
5.6 Sorting
Sorting means arranging the elements of an array so that they are placed in some relevant order
which may be either ascending or descending.
Example: Sort the following list using insertion sort – 77, 33, 44, 11, 88, 22, 66, 55
To insert an element A[K] in a sorted list A[0], A[1], ..., A[K–1], we need to compare A[K] with
A[K–1], then with A[K–2], A[K–3], and so on until we meet an element A[J] such that A[J] <=
A[K]. In order to insert A[K] in its correct position, we need to move elements A[K– 1], A[K–2], ...,
A[J] by one position and then A[K] is inserted at the (J+1)th location.
Prof. Shrikant Pujar, Dept. of CS&E 8
DATA STRUCTURES AND APPLICATIONS
5.6.2 Radix Sort
Radix sort is a linear sorting algorithm for integers and uses the concept of sorting names in
alphabetical order.
When we have a list of sorted names, the radix is 26 (or 26 buckets) because there are 26 letters in
the English alphabet. So radix sort is also known as bucket sort. Observe that words are first sorted
according to the first letter o/f the name. That is, 26 classes are used to arrange the names, where the
first class stores the names that begin with A, the second class contains the names with B, and so on.
During the second pass, names are grouped according to the second letter. After the second pass,
names are sorted on the first two letters. This process is continued till the nth pass, where n is the
length of the name with maximum number of letters.
After every pass, all the names are collected in order of buckets. That is, first pick up the names in
the first bucket that contains the names beginning with A. In the second pass, collect the name from
the second bucket, and so on.
When radix sort is used on integers, sorting is done on each of the digits in the number. The sorting
procedure proceeds by sorting the least significant to the most significant digit. While sorting the
numbers, we have ten buckets, each for one digit (0, 1, 2, …, 9) and the number of passes will
depend on the length of the number having maximum number of digits.
Example: Sort the following list using Radix sort – 348, 143, 361, 423, 538, 128, 321, 543,366
Sorted numbers are- 128, 143, 321, 348, 361, 366, 423, 538, 543.
Difference between Graph and Tree
2. Mid square method: In this method firstly key is squared and then mid part of the result is taken
as the index.
H(key) = Key2 = mid(key)
For example: consider that if we want to place a record of 3101 and the size of table is 1000. So
3101*3101=9616201 i.e. h (3101) = 162 (middle 3 digit).
Collision
It is a situation in which the hash function returns the same hash key for more than one
record, it is called as collision. Sometimes when we are going to resolve the collision it may
lead to a overflow condition and this overflow and collision condition makes the poor hash
function.
Collision resolution technique
If there is a problem of collision occurs then it can be handled by apply some technique. These
techniques are called as collision resolution techniques.
1) Chaining: It is a method in which additional field with data i.e. chain is introduced. A chain is
maintained at the home bucket. In this when a collision occurs then a linked list is maintained for
colliding data.
Example: Let us consider a hash table of size 10 and we apply a hash function of H(key)=key %
size of table. Let us take the keys to be inserted are 31,33,77,61. In the above diagram we can see at
same bucket 1 there are two records which are maintained by linked list or we can say by chaining
method.
Prof. Shrikant Pujar, Dept. of CS&E 12
DATA STRUCTURES AND APPLICATIONS
2) Linear probing: It is very easy and simple method to resolve or to handle the collision. In this
collision can be solved by placing the second record linearly down, whenever the empty place is
found. In this method there is a problem of clustering which means at some place block of a data is
formed in a hash table.
Example: Let us consider a hash table of size 10 and hash function is defined as H(key)=key %
table size. Consider that following keys are to be inserted that are 56,64,36,71.
In this diagram we can see that 56 and 36 need to be placed at same bucket but by linear probing
technique the records linearly placed downward if place is empty i.e. it can be seen 36 is placed
at index 7.
Dynamic Hashing: This is the process of mapping large amounts of data into a table whose size is
assigned during run time.
File Attributes:
File attributes are settings associated with computer files that grant or deny certain rights to how a
user or the operating system can access that file. For example, IBM compatible computers running
MS-DOS or Microsoft Windows have capabilities of having read, archive, system, and hidden
attributes.
Read-only - Allows a file to be read, but nothing can be written to the file or changed.
Archive - Tells Windows Backup to backup the file.
System - System file.
Hidden - File is not shown when doing a regular directory from DOS.
Prof. Shrikant Pujar, Dept. of CS&E 13
DATA STRUCTURES AND APPLICATIONS
In operating systems like Linux, there are three main file attributes: read (r), write (w), execute (x).
Read - Designated as an "r"; allows a file to be read, but nothing can be written to or changed in
the file.
Write - Designated as a "w"; allows a file to be written to and changed.
Execute - Designated as an "x"; allows a file to be executed by users or the operating system.
QUESTION BANK