Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
12 views

Data Structures Unit-4 Notes

Data structures unit 4 lecture notes

Uploaded by

jukuntlaananth
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Data Structures Unit-4 Notes

Data structures unit 4 lecture notes

Uploaded by

jukuntlaananth
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

UNIT-IV

Graph is a non-linear data structure. It contains a set of points known as nodes (or vertices) and
a set of links known as edges (or Arcs). Here edges are used to connect the vertices.

A graph is defined as follows...

Graph is a collection of nodes and edges in which nodes are connected with edges.
Generally, a graph G is represented as G = ( V , E ), where V is set of vertices and E is set of
edges.

Example
The following is a graph with 5 vertices and 7 edges.
This graph G can be defined as G = ( V , E )
Where V = {A,B,C,D,E} and E = {(A,B),(A,C)(A,D),(B,D),(C,D),(B,E),(E,D)}.

Graph Terminology
We use the following terms in graph data structure...
Vertex
Individual data element of a graph is called as Vertex. Vertex is also known as node. In above
example graph, A, B, C, D & E are known as vertices.
Edge
An edge is a connecting link between two vertices. Edge is also known as Arc. An edge is
represented as (startingVertex, endingVertex).
For example, in above graph the link between vertices A and B is represented as (A,B). In above
example graph, there are 7 edges (i.e., (A,B), (A,C), (A,D), (B,D), (B,E), (C,D), (D,E)).

Edges are three types.


1.Undirected Edge - An undirected egde is a bidirectional edge. If there is undirected edge
between vertices A and B then edge (A , B) is equal to edge (B , A).
Undirected Edge

V1 V2

2.Directed Edge - A directed egde is a unidirectional edge. If there is directed edge between
vertices A and B then edge (A , B) is not equal to edge (B , A).

Directed Edge

V1 V2
3.Weighted Edge - A weighted egde is a edge with value (cost) on it.

weighted Edge
3
V1 V2
Undirected Graph
A graph with only undirected edges is said to be undirected graph.

Directed Graph
A graph with only directed edges is said to be directed graph.

Mixed Graph
A graph with both undirected and directed edges is said to be mixed graph.
End vertices or Endpoints
The two vertices joined by edge are called end vertices (or endpoints) of that edge.
Origin
If a edge is directed, its first endpoint is said to be the origin of it.
Destination
If a edge is directed, its first endpoint is said to be the origin of it and the other endpoint is said
to be the destination of that edge.
Adjacent
If there is an edge between vertices A and B then both A and B are said to be adjacent. In other
words, vertices A and B are said to be adjacent if there is an edge between them.
Incident
Edge is said to be incident on a vertex if the vertex is one of the endpoints of that edge.
Outgoing Edge
A directed edge is said to be outgoing edge on its origin vertex.
Incoming Edge
A directed edge is said to be incoming edge on its destination vertex.
Degree
Total number of edges connected to a vertex is said to be degree of that vertex.
Indegree
Total number of incoming edges connected to a vertex is said to be indegree of that vertex.

Outdegree
Total number of outgoing edges connected to a vertex is said to be outdegree of that vertex.

Parallel edges or Multiple edges


If there are two undirected edges with same end vertices and two directed edges with same
origin and destination, such edges are called parallel edges or multiple edges.

Self-loop
Edge (undirected or directed) is a self-loop if its two endpoints coincide with each other.

Simple Graph
A graph is said to be simple if there are no parallel and self-loop edges.

Path
A path is a sequence of alternate vertices and edges that starts at a vertex and ends at other
vertex such that each edge is incident to its predecessor and successor vertex.
GRAPH REPRESENTATIONS
Graph data structure is represented using following representations...
1. Adjacency Matrix
2. Incidence Matrix
3. Adjacency List

1.Adjacency Matrix
• In this representation, the graph is represented using a matrix of size total number of
vertices by a total number of vertices.
• That means a graph with 4 vertices is represented using a matrix of size 4X4.
• In this matrix, both rows and columns represent vertices.
• This matrix is filled with either 1 or 0. Here, 1 represents that there is an edge from row
vertex to column vertex and 0 represents that there is no edge from row vertex to
column vertex.

For example, consider the following undirected graph representation...

Directed graph representation...


2.Incidence Matrix

• In this representation, the graph is represented using a matrix of size total number of
vertices by a total number of edges.
• That means graph with 4 vertices and 6 edges is represented using a matrix of size 4X6.
• In this matrix, rows represent vertices and columns represents edges.
• This matrix is filled with 0 or 1 or -1.
• Here, 0 represents that the row edge is not connected to column vertex, 1 represents
that the row edge is connected as the outgoing edge to column vertex and -1 represents
that the row edge is connected as the incoming edge to column vertex.

For example, consider the following directed graph representation...

3.Adjacency List
In this representation, every vertex of a graph contains list of its adjacent vertices.
For example, consider the following directed graph representation implemented using linked
list...
This representation can also be implemented using an array as follows..

GRAPH TRAVERSAL TECHNIQUES


• Graph traversal is a technique used for a searching vertex in a graph.
• The graph traversal is also used to decide the order of vertices is visited in the search
process.
• A graph traversal finds the edges to be used in the search process without creating
loops.
• That means using graph traversal we visit all the vertices of the graph without getting
into looping path.

There are two graph traversal techniques and they are as follows...
1. DFS (Depth First Search)
2. BFS (Breadth First Search)

DFS (DEPTH FIRST SEARCH)


DFS traversal of a graph produces a spanning tree as final result.
Spanning Tree is a graph without loops.
We use Stack data structure with maximum size of total number of vertices in the graph to
implement DFS traversal.

We use the following steps to implement DFS traversal...


• Step 1 - Define a Stack of size total number of vertices in the graph.
• Step 2 - Select any vertex as starting point for traversal. Visit that vertex and push it on
to the Stack.
• Step 3 - Visit any one of the non-visited adjacent vertices of a vertex which is at the top
of stack and push it on to the stack.
• Step 4 - Repeat step 3 until there is no new vertex to be visited from the vertex which is
at the top of the stack.
• Step 5 - When there is no new vertex to visit then use back tracking and pop one vertex
from the stack.
• Step 6 - Repeat steps 3, 4 and 5 until stack becomes Empty.
• Step 7 - When stack becomes Empty, then produce final spanning tree by removing
unused edges from the graph

Back tracking is coming back to the vertex from which we reached the current vertex.
Example:
BFS (BREADTH FIRST SEARCH)
• BFS traversal of a graph produces a spanning tree as final result.
• Spanning Tree is a graph without loops.
• We use Queue data structure with maximum size of total number of vertices in the
graph to implement BFS traversal.

We use the following steps to implement BFS traversal...

• Step 1 - Define a Queue of size total number of vertices in the graph.


• Step 2 - Select any vertex as starting point for traversal. Visit that vertex and insert it
into the Queue.
• Step 3 - Visit all the non-visited adjacent vertices of the vertex which is at front of the
Queue and insert them into the Queue.
• Step 4 - When there is no new vertex to be visited from the vertex which is at front of
the Queue then delete that vertex.
• Step 5 - Repeat steps 3 and 4 until queue becomes empty.
• Step 6 - When queue becomes empty, then produce final spanning tree by removing
unused edges from the graph.

Example
SORTING
Sorting is nothing but arranging the elements in ascending or descending order.
Sorting arranges data in a sequence which makes searching easier.

HEAP SORTING
A Heap is a special Tree-based data structure in which the tree is a complete binary tree.
To perform heap sort,first create heap tree for the given set of elements and then sort heap tree.

Generally, Heaps can be of two types:


1.Max-Heap:
• In max heap, every node contains greater or equal value element than its child nodes.
• Thus, root node contains the largest value element.
Example
2.Min-Heap:
• In min heap, every node contains lesser value element than its child nodes.
• Thus, root node contains the smallest value element.

Example

Max Heap Construction:


Step 1: Convert the given array of elements into complete binary tree.
Step 2: Insert the newNode as last leaf from left to right.
Step 3: Compare newNode value with its Parent node.
Step 4: If newNode value is greater than its parent, then swap both of them.
Step 5: Repeat step 3 and step 4 until newNode value is less than its parent node (or) newNode
reaches to root.

Example:
Construct Heap tree for the following sequence of elements then sort the elements into
ascending order using heap sort technique.
50,25,30,75,100,45,80
1.Insert 50
• First element 50 is root.

2.Insert 25

• 25 is inserted at left of 50.


• The tree is satisfying heap condition.
50

25

3. Insert 30
• 30 is inserted at right of 50.
• The tree is satisfying heap condition.So it is a Heap tree.
50

25 30

4. Insert 75
• 75 is inserted at left of 25.
• The tree is not satisfying heap condition.Because 75 is greater than 25 as well as 50.
• So exchange it with its root 25 and again 50.
• After exchange it is a heap tree.

50 50 75

25 30 75 30 50 30

75 25 25

5. Insert 100
• 100 is inserted at right of 50.
• The tree is not satisfying heap condition.Because 100 is greater than 50 as well as 75.
• So exchange it with its root 50 and again 75.
• After exchange it is a heap tree.

75 75 100

50 30 100 30 75 30

25 100 25 50 25 50

6. Insert 45
• 45 is inserted at left of 30.
• The tree is not satisfying heap condition.Because 45 is greater than 30.
• So exchange 45 with its root 30.
• After exchange it is a heap tree.

100 100

75 30 75 45

25 50 25 50
45 30

7. Insert 80
• 80 is inserted at right of 45.
• The tree is not satisfying heap condition.Because 80 is greater than 45.
• So exchange 80 with its root 45.
• After exchange it is a heap tree.

100 100

75 45 75 80

25 50 25 50
30 80 30 45
SORT THE HEAP TREE

Heap Sort Algorithm for sorting in increasing order:


1. Build a max heap from the input data.
2. At this point, the largest item is stored at the root of the heap. Replace it with the last item
of the heap followed by reducing the size of heap by 1. Finally, heapify the root of the tree.

0
100 45
Exchange
1 2
75 80 75 80
a[0],a[6]

3 4 5 6 25
25 50 30 45 50 30 100

Initial Heap Sorted Path

0
80 30
Exchange
1 2
75 45 75 45
a[0],a[5]

3 4 5 6 25
25 50 30 100 50 80 100

Sorted Path

75 30

50 45 75 45

25 100 25 50
30 80 80 100
0
75 30
Exchange
1 2
50 45 50 45
a[0],a[4]

3 4 5 6 25
25 30 80 100 75 80 100

Sorted Path

0
50 25
Exchange
1 2
30 45 30 45
a[0],a[3]

3 4 5 6
25 75 80 100 50 75 80 100

Sorted Path

0
45 25
Exchange
1 2
30 25 30 45
a[0],a[2]

3 4 5 6
50 75 80 100 50 75 80 100

Sorted Path

25

30 45

50 75 80 100
0
30
Exchange 25
1 2
25 45 a[0],a[1]
30 45
3 4 5 6
50 75 80 100
50 75 80 100

The entire array sorted

Sorting Categories
The techniques of sorting can be divided into two categories. These are:
• Internal Sorting
• External Sorting
Internal Sorting: If all the data that is to be sorted can be adjusted at a time in the main
memory, the internal sorting method is being performed.
External Sorting: When the data that is to be sorted cannot be accommodated in the memory
at the same time and some has to be kept in auxiliary memory such as hard disk, floppy disk,
magnetic tapes etc, then external sorting methods are performed.

EXTERNAL SORTING
• All the internal sorting algorithms require that the input fit into main memory.
• There are, however, applications where the input is much too large to fit into memory.
• For those external sorting algorithms, which are designed to handle very large inputs.

Model for External Sorting


The wide variety of mass storage devices makes external sorting much more device dependent
than internal sorting.
The algorithms that we will consider work on tapes, which are probably the most restrictive
storage medium. Since access to an element on tape is done by winding the tape to the
correct location, tapes can be efficiently accessed only in sequential order (in either direction).

Example of External Sorting: 2-way Merge Sort


2-way Merging/ Basic External Sorting Algorithm
Assume unsorted data is on disk at start.
Let M=maximum number of records that can be sorted & sorted in
internal memory at one time.
Algorithm:

• Repeat
1. Read M records into main memory & sort internally.
2. Write this sorted sub-list into disk. (This is
one “run”). Until data is processed into runs.
• Repeat
1. Merge two runs into one sorted run twice as long.
2. Write this single run back onto
disk Until all runs processed into runs
twice as long
• Merge runs again as often as needed until only one large run: The sorted
list.

Example:

Unsorted Data on Disk

Assume M = 3 (M would actually be much larger, of course.) First


step is to read 3 data items at a time into main memory, sort them and
write them back to disk as runs of length 3.

Next step is to merge the runs of length 3 into runs of length 6.

Next step is to merge the runs of length 6 into runs of length 12.
Next step is to merge the runs of length 12 into runs of length 24.
Here we have less than 24, so we’re finished.

MERGE SORT
Merge Sort is a Divide and Conquer algorithm. It divides the input array into two halves, calls
itself for the two halves, and then merges the two sorted halves.

Merge Sort Algorithm


Merge Sort Algorithm works in the following steps-

• It divides the given unsorted array into two halves- left and right sub arrays.
• The sub arrays are divided recursively.
• This division continues until the size of each sub array becomes 1.
• After each sub array contains only a single element, each sub array is sorted trivially.
• The merge procedure combines these trivially sorted arrays to produce a final sorted
array.

Example:

• The following diagram shows the complete merge sort process for an example array
{38, 27, 43, 3, 9, 82, 10}.
• If we take a closer look at the diagram, we can see that the array is recursively divided
in two halves till the size becomes 1.
• Once the size becomes 1, the merge processes come into action and start merging
arrays back till the complete array is merged.

You might also like