SlideShare a Scribd company logo
1 of 263
SRM
INSTITUTE OF SCIENCE ANDTECHNOLOGY,
CHENNAI.
DATA STRUCTURES AND
ALGORITHMS
(18CSC162J)
PREPARED BY
D.KAVITHA,AP/CSE,SRMIST,
RAMAPURAM CAMPUS
SRM
INSTITUTE OF SCIENCE ANDTECHNOLOGY,
CHENNAI.
DATA STRUCTURE - GRAPH
UNIT IV
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
TOPICS TO BE COVERED (THEORY)
Session Topics
1 Graph Terminology, Traversal
2 Topological sorting, Minimal spanning tree Intro
3 Prim’s
4 Kruskal’s
5 Djikstra’s
6 Searching – Linear, binary
7 Breadth, Depth First searches
8 Sorting – Bubble, insertion, selection
9 Shell, merge, quick, heap
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
TOPICS FOR LAB SESSION
Session Topics
1 Implementation of Minimal spanning trees – Prim’s
2 Implementation of Minimal spanning trees – Kruskal’s
3 Implementation of shortest path algorithms - Dijkstra’s
Algorithm
4 Implementation of shortest path algorithms
5 Implementation of B Trees
6
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
LEARNING RESOURCES
• Fundamentals of Data Structures, E. Horowitz and S. Sahni, 1977.
• Data Structures and Algorithms, Alfred V. Aho, John E. Hopperoft, Jeffrey D.
UIlman.
• Mark Allen Weiss, Data Structures and Algorithm Analysis in C, 2nd ed.,
Pearson Education, 2015
• Reema Thareja, Data Structures Using C, 1st ed., Oxford Higher Education,
2011
• Thomas H Cormen, Charles E Leiserson, Ronald L Revest, Clifford Stein,
Introduction to Algorithms 3rd ed., The MIT Press Cambridge, 2014
INTRODUCTION
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
GRAPHS - TERMINOLOGY AND
REPRESENTATION
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.
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
GRAPHS - TERMINOLOGY AND
REPRESENTATION
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 example graph, there are 7 edges (i.e., (A,B), (A,C), (A,D),
(B,D), (B,E), (C,D), (D,E)).
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
GRAPHS - TERMINOLOGY AND
REPRESENTATION
Types of Edges
• Undirected Edge: An undirected edge is a bidirectional edge.
If there is undirected edge between vertices A and B then edge
(A , B) is equal to edge (B , A).
• Directed Edge: - A directed edge 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).
• Weighted Edge: - A weighted edge is a edge with value (cost)
on it.
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
GRAPHS - TERMINOLOGY AND
REPRESENTATION
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.
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
GRAPHS - TERMINOLOGY AND
REPRESENTATION
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.
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
GRAPHS - TERMINOLOGY AND
REPRESENTATION
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.
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
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
GRAPHS - TERMINOLOGY AND
REPRESENTATION
Individual data element of a graph is called as Vertex.
Vertex is also known as node.
Denote vertices with labels
In above example graph, A, B, C, D & E are known as vertices.
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
GRAPHS - TERMINOLOGY AND
REPRESENTATION
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.
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
GRAPHS - TERMINOLOGY AND
REPRESENTATION
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.
Cycle
a path that starts and ends on the same vertex
Length of a path: Number of edges in the path
Sometimes the sum of the weights of the edges
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
GRAPHS - TERMINOLOGY AND
REPRESENTATION
Motivation:
Many algorithms use a graph representation to represent data or the problem to be solved
Examples:
• Cities with distances between
• Roads with distances between intersection points
• Course prerequisites
• Network
• Social networks
• Program call graph and variable dependency graph
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
GRAPH CLASSIFICATIONS
There are several common kinds of graphs
• Weighted or unweighted
• Directed or undirected
• Cyclic or acyclic
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
WEIGHTED AND UNWEIGHTED
Graphs can be classified by whether or not their edges have weights
Weighted graph: edges have a weight
• Weight typically shows cost of traversing
• Example: weights are distances between cities
Unweighted graph: edges have no weight
• Edges simply show connections
• Example: course prereqs
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
DIRECTED AND UNDIRECTED
Graphs can be classified by whether or their edges are have direction
• Undirected Graphs: each edge can be traversed in either direction
• Directed Graphs: each edge can be traversed only in a specified direction
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
CYCLIC AND ACYCLIC GRAPHS
A Cyclic graph contains cycles
Example: roads (normally)
An acyclic graph contains no cycles
Example: Course prereqs!
Examples - Are these cyclic or acyclic?
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
CONNECTED, UNCONNECTED
GRAPHS AND CONNECTED
COMPONENTS
An undirected graph is connected if every pair of vertices has a path between it
• Otherwise it is unconnected
• Give an example of a connected graph
An unconnected graph can be broken in to connected components
A directed graph is strongly connected if every pair of vertices has a path between them, in
both directions
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
DATA STRUCTURES FOR
REPRESENTING GRAPHS
Two common data structures for representing graphs:
• Adjacency lists
• Adjacency matrix
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
ADJACENCY LIST REPRESENTATION
Adjacency List Representation
Each node has a list of adjacent nodes
Example (undirected graph):
A: B, C, D
B: A, D
C: A, D
D: A, B, C
Example (directed graph):
A: B, C, D
B: D
C: Nil
D: C
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
ADJACENCY MATRIX
REPRESENTATION
Adjacency Matrix: 2D array containing weights on edges
• Row for each vertex
• Column for each vertex
• Entries contain weight of edge from row vertex to column vertex
• Entries contain ∞ (i.e. Integer‘ last) if no edge from row vertex to column vertex
• Entries contain 0 on diagonal (if self edges not allowed)
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
ADJACENCY MATRIX
REPRESENTATION
Example directed graph (assume self-edges allowed)
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
GRAPH TRAVERSAL
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...
• DFS (Depth First Search)
• BFS (Breadth First Search)
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
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.
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
DFS (DEPTH FIRST SEARCH)
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
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
DFS (DEPTH FIRST SEARCH)
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
DFS (DEPTH FIRST SEARCH)
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
DFS (DEPTH FIRST SEARCH)
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
DFS (DEPTH FIRST SEARCH)
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
DFS (DEPTH FIRST SEARCH)
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
DFS (DEPTH FIRST SEARCH)
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
DFS (DEPTH FIRST SEARCH)
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
DFS (DEPTH FIRST SEARCH)
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
DFS (DEPTH FIRST SEARCH)
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
DFS (DEPTH FIRST SEARCH)
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
DFS (DEPTH FIRST SEARCH)
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
DFS (DEPTH FIRST SEARCH)
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
DFS (DEPTH FIRST SEARCH)
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
DFS (DEPTH FIRST SEARCH)
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
DFS (DEPTH FIRST SEARCH)
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
DFS (DEPTH FIRST SEARCH)
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
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.
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
BFS (BREADTH FIRST SEARCH)
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
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
BFS (BREADTH FIRST SEARCH)
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
BFS (BREADTH FIRST SEARCH)
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
BFS (BREADTH FIRST SEARCH)
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
BFS (BREADTH FIRST SEARCH)
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
BFS (BREADTH FIRST SEARCH)
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
BFS (BREADTH FIRST SEARCH)
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
BFS (BREADTH FIRST SEARCH)
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
BFS (BREADTH FIRST SEARCH)
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
BFS (BREADTH FIRST SEARCH)
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
BFS (BREADTH FIRST SEARCH)
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
GRAPH - SPANNING TREE
• A tree that contains every vertex of a connected
graph is called spanning tree. It is an undirected tree
consisting of only those edges that are necessary to
connect all the vertices of the original graph G.
• CHARACTERISTICS
1. It is a subgraph of a graph G that contain all the vertex of
graph G.
2. For any pair of vertices, there exists only one path between
them.
3. It is a connected graph with no cycles.
4. A graph may have many spanning tree.
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
EXAMPLE
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
GRAPH - MINIMUM SPANNING TREE
• Let G=(V,E,W) be any weighted graph. Then a spanning
tree whose cost is minimum is called minimum spanning
tree. The cost of the spanning tree is defined as the sum of
the costs of the edges in that tree.
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
MINIMUM SPANNING TREE
• A spanning tree is a subset of Graph G, which has all the
vertices covered with minimum possible number of edges.
• A complete undirected graph can have maximum nn-2 number
of spanning trees, where n is number of nodes.
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
GENERAL PROPERTIES OF SPANNING
TREE
• A connected graph G can have more than one spanning
tree.
• All possible spanning trees of graph G, have same number
of edges and vertices.
• Spanning tree does not have any cycle (loops)
• Removing one edge from spanning tree will make the
graph disconnected i.e. spanning tree is minimally
connected.
• Adding one edge to a spanning tree will create a cycle or
loop i.e. spanning tree is maximally acyclic.
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
MATHEMATICAL PROPERTIES OF
SPANNING TREE
• Spanning tree has n-1 edges, where n is number of nodes
(vertices)
• From a complete graph, by removing maximum
e-n+1 edges, we can construct a spanning tree.
• A complete graph can have maximum nn-2 number of
spanning trees.
• So we can conclude here that spanning trees are subset of
a connected Graph G and disconnected Graphs do not
have spanning tree.
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
APPLICATION OF SPANNING TREE
• Spanning tree is basically used to find minimum paths to
connect all nodes in a graph.
• Civil Network Planning
• Computer Network Routing Protocol
• Cluster Analysis
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
MINIMUM SPANNING TREE (MST)
• In a weighted graph, a minimum spanning tree is a
spanning tree that has minimum weight that all other
spanning trees of the same graph.
MST Algorithm
• Kruskal’s Algorithm
• Prim’s Algorithm
Both are greedy algorithms.
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
PRIM’S ALGORITHM
• Create a set mstSet that keeps track of vertices already included
in MST.
• Assign a key value to all vertices in the input graph. Initialize all
key values as INFINITE. Assign key value as 0 for the first vertex
so that it is picked first.
• While mstSet doesn’t include all vertices
• Pick a vertex u which is not there in mstSet and has minimum key value.
• Include u to mstSet.
• Update key value of all adjacent vertices of u. To update the key values, iterate
through all adjacent vertices. For every adjacent vertex v, if weight of edge u-v is
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
A
F
B
C
D
E
2
7
4
5
8 6
4
5
3
8
Select any vertex
A
Select the shortest
edge connected to
that vertex
AB 3
PRIM’S ALGORITHM
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
A
F
B
C
D
E
2
7
4
5
8 6
4
5
3
8
Select the shortest
edge connected to
any vertex already
connected.
AE 4
PRIM’S ALGORITHM
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
Select the shortest
edge connected to
any vertex already
connected.
ED 2
A
F
B
C
D
E
2
7
4
5
8 6
4
5
3
8
PRIM’S ALGORITHM
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
Select the shortest
edge connected to
any vertex already
connected.
DC 4
A
F
B
C
D
E
2
7
4
5
8 6
4
5
3
8
PRIM’S ALGORITHM
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
Select the shortest
edge connected to
any vertex already
connected.
EF 5
A
F
B
C
D
E
2
7
4
5
8 6
4
5
3
8
PRIM’S ALGORITHM
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
A
F
B
C
D
E
2
7
4
5
8 6
4
5
3
8
All vertices have been
connected.
The solution is
AB 3
AE 4
ED 2
DC 4
EF 5
Total weight of tree: 18
PRIM’S ALGORITHM
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
A
F
B
C
D
E
2
4
5
4
3
All vertices have been
connected.
The solution is
AB 3
AE 4
ED 2
DC 4
EF 5
Total weight of tree: 18
PRIM’S ALGORITHM
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
KRUSKAL ALGORITHM
Algorithm
• Remove all loops & Parallel Edges from the given graph
• Sort all the edges in non-decreasing order of their weight.
• Pick the smallest edge. Check if it forms a cycle with the
spanning tree formed so far. If cycle is not formed, include this
edge. Else, discard it.
• Repeat step#2 until there are (V-1) edges in the spanning tree.
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
EXAMPLE
Avonford Fingley
Brinleigh Cornwell
Donster
2
7
4
5
8 6
4
5
3
8
A cable company want to
connect five villages to their
network which currently
extends to the market town of
Avonford. What is the minimum
length of cable needed?
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
A F
B C
D
2
7
4
5
8 6
4
5
3
8
WE MODEL THE SITUATION AS A NETWORK,
THEN THE PROBLEM IS TO FIND THE MINIMUM
CONNECTOR FOR THE NETWORK
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
A
F
B
C
D
E
2
7
4
5
8 6
4
5
3
8
List the edges in
order of size:
ED 2
AB 3
AE 4
CD 4
BC 5
EF 5
CF 6
AF 7
BF 8
CF 8
KRUSKAL’S ALGORITHM
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
Select the shortest
edge in the network
ED 2
A
F
B
C
D
E
2
7
4
5
8 6
4
5
3
8
KRUSKAL’S ALGORITHM
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
Select the next shortest
edge which does not
create a cycle
ED 2
AB 3
A
F
B
C
D
E
2
7
4
5
8 6
4
5
3
8
KRUSKAL’S ALGORITHM
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
Select the next shortest
edge which does not
create a cycle
ED 2
AB 3
CD 4 (or AE 4)
A
F
B
C
D
E
2
7
4
5
8 6
4
5
3
8
KRUSKAL’S ALGORITHM
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
Select the next shortest
edge which does not
create a cycle
ED 2
AB 3
CD 4
AE 4
A
F
B
C
D
E
2
7
4
5
8 6
4
5
3
8
KRUSKAL’S ALGORITHM
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
Select the next shortest
edge which does not
create a cycle
ED 2
AB 3
CD 4
AE 4
BC 5 – forms a cycle
EF 5
A
F
B
C
D
E
2
7
4
5
8 6
4
5
3
8
KRUSKAL’S ALGORITHM
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
All vertices have been
connected.
The solution is
ED 2
AB 3
CD 4
AE 4
EF 5
Total weight of tree: 18
A
F
B
C
D
E
2
7
4
5
8 6
4
5
3
8
KRUSKAL’S ALGORITHM
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
All vertices have been
connected.
The solution is
ED 2
AB 3
CD 4
AE 4
EF 5
Total weight of tree: 18
A
F
B
C
D
E
2
4
5
4
3
KRUSKAL’S ALGORITHM
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
Kruskal’s algorithm
1. Select the shortest edge in a
network
2. Select the next shortest edge
which does not create a cycle
3. Repeat step 2 until all vertices
have been connected
Prim’s algorithm
1. Select any vertex
2. Select the shortest edge
connected to that vertex
3. Select the shortest edge
connected to any vertex already
connected
4. Repeat step 3 until all vertices
have been connected
MINIMUM SPANNING TREE
ALGORITHMS
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
PRIMS ALGORITHM & KRUSKAL
ALGORITHM
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
FIND MST USING KRUSKAL’S AND
PRIM’S ALGORITHM
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
MST USING KRUSKAL’S ALGORITHM
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
INTRODUCTION
• Single-Source Shortest Path Problem - The problem of finding
shortest path from a source vertex v to all other vertices in the graph.
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
DIJKSTRA'S ALGORITHM
Dijkstra's algorithm - is a solution to the single-source
shortest path problem in graph theory.
Works on both directed and undirected graphs. However, all
edges must have nonnegative weights.
Input: Weighted graph G={E,V} and source vertex v∈V, such
that all edge weights are nonnegative
Output: Lengths of shortest paths (or the shortest paths
themselves) from a given source vertex v∈V to all other
vertices
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
APPROACH
• The algorithm computes for each vertex u the
distance to u from the start vertex v, that is, the weight
of a shortest path between v and u.
• The algorithm keeps track of the set of vertices for
which the distance has been computed, called the
cloud C
• Every vertex has a label D associated with it. For any
vertex u, D[u] stores an approximation of the distance
between v and u. The algorithm will update a D[u]
value when it finds a shorter path from v to u.
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
DIJKSTRA PSEUDOCODE
Dijkstra(v1, v2):
for each vertex v: //
Initialization
v's distance := infinity.
v's previous := none.
v1's distance := 0.
List := {all vertices}.
while List is not empty:
v := remove List vertex with minimum
distance.
mark v as known.
for each unknown neighbor n of v:
dist := v's distance + edge (v,
n)'s weight.
if dist is smaller than n's
distance:
n's distance := dist.
n's previous := v.
reconstruct path from v2 back to v1,
following previous pointers.
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
EXAMPLE: INITIALIZATION
92
A
G
F
B
E
C D
4 1
2
10
3
6
4
2
2
8
5
1
0 ∞
∞ ∞
∞
Pick vertex in List with minimum distance.
∞ ∞
Distance(source) =
0
Distance (all vertices
but source) = ∞
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
93
EXAMPLE: UPDATE NEIGHBORS'
DISTANCE
A
G
F
B
E
C D
4 1
2
10
3
6
4
2
2
8
5
1
0 2
∞ ∞
1
∞ ∞
Distance(B) = 2
Distance(D) = 1
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
94
EXAMPLE: REMOVE VERTEX WITH
MINIMUM DISTANCE
Pick vertex in List with minimum distance, i.e., D
A
G
F
B
E
C D
4 1
2
10
3
6
4
2
2
8
5
1
0 2
∞ ∞
1
∞ ∞
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
95
EXAMPLE: UPDATE NEIGHBORS
A
G
F
B
E
C D
4 1
2
10
3
6
4
2
2
8
5
1
0 2
3 3
1
9 5
Distance(C) = 1 + 2 = 3
Distance(E) = 1 + 2 = 3
Distance(F) = 1 + 8 = 9
Distance(G) = 1 + 4 = 5
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
96
EXAMPLE: CONTINUED...
A
G
F
B
E
C D
4 1
2
10
3
6
4
2
2
8
5
1
0 2
3 3
1
Pick vertex in List with
minimum distance (B) and
update neighbors
9 5
Note : distance(D) not
updated since D is
already known and
distance(E) not updated
since it is larger than
previously computed
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
97
EXAMPLE: CONTINUED...
A
G
F
B
E
C D
4 1
2
10
3
6
4
2
2
8
5
1
0 2
3 3
1
9 5
No updating
Pick vertex List with minimum
distance (E) and update
neighbors
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
98
EXAMPLE: CONTINUED...
A
G
F
B
E
C D
4 1
2
10
3
6
4
2
2
8
5
1
0 2
3 3
1
8 5
Pick vertex List with minimum
distance (C) and update
neighbors
Distance(F) = 3 + 5 = 8
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
99
EXAMPLE: CONTINUED...
A
G
F
B
E
C D
4 1
2
10
3
6
4
2
2
8
5
1
0 2
3 3
1
6 5
Distance(F) = min (8, 5+1) = 6
Previous distance
Pick vertex List with minimum
distance (G) and update
neighbors
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
10
0
EXAMPLE (END)
A
G
F
B
E
C D
4 1
2
10
3
6
4
2
2
8
5
1
0 2
3 3
1
Pick vertex not in S with lowest cost (F) and update neighbors
6 5
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
ANOTHER EXAMPLE
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
ANOTHER EXAMPLE
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
ANOTHER EXAMPLE
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
ANOTHER EXAMPLE
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
ANOTHER EXAMPLE
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
ANOTHER EXAMPLE
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
ANOTHER EXAMPLE
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
ANOTHER EXAMPLE
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
ANOTHER EXAMPLE
SEARCHING TECHNIQUES
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
PROBLEM: SEARCH
• We are given a list of records.
• Each record has an associated key.
• Give efficient algorithm for searching for a record containing a particular
key.
• Efficiency is quantified in terms of average time analysis (number of
comparisons) to retrieve an item.
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
SEARCH
[ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 700 ]
Number 506643548
Number 233667136
Number 281942902
Number 155778322
Number 580625685
Number 701466868
…
Number 580625685
Each record in list has an associated key.
In this example, the keys are ID numbers.
Given a particular key, how can we efficiently retrieve the record from
the list?
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
LINEAR SEARCH
• Step through array of records, one at a time.
• Look for record with matching key.
• Search stops when
• record with matching key is found
• or when search has examined all records without success.
PSEUDOCODE FOR LINEAR SEARCH
// Search for a desired item in the n array elements
// starting at a[first].
// Returns pointer to desired record if found.
// Otherwise, return NULL
…
for(i = first; i < n; ++i )
if(a[first+i] is desired item)
return &a[first+i];
// if we drop through loop, then desired item was not found
return NULL;
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
SERIAL SEARCH ANALYSIS
• What are the worst and average case running times for serial search?
• We must determine the O-notation for the number of operations
required in search.
• Number of operations depends on n, the number of entries in the list.
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
WORST CASE TIME FOR LINEAR SEARCH
• For an array of n elements, the worst case time for linear
search requires n array accesses: O(n).
• Consider cases where we must loop over all n records:
• desired record appears in the last position of the array
• desired record does not appear in the array at all
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
AVERAGE CASE FOR LINEAR SEARCH
Assumptions:
1. All keys are equally likely in a search
2. We always search for a key that is in the array
Example:
• We have an array of 10 records.
• If search for the first record, then it requires 1 array access; if the second,
then 2 array accesses. etc.
The average of all these searches is:
(1+2+3+4+5+6+7+8+9+10)/10 = 5.5
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
AVERAGE CASE TIME FOR LINEAR SEARCH
Generalize for array size n.
Expression for average-case running time:
(1+2+…+n)/n = n(n+1)/2n = (n+1)/2
Therefore, average case time complexity for serial search is O(n).
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
BINARY SEARCH
• Perhaps we can do better than O(n) in the average case?
• Assume that we are give an array of records that is sorted. For
instance:
• an array of records with integer keys sorted from smallest to largest (e.g., ID
numbers), or
• an array of records with string keys sorted in alphabetical order (e.g., names).
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
BINARY SEARCH PSEUDOCODE
…
if(size == 0)
found = false;
else {
middle = index of approximate midpoint of array segment;
if(target == a[middle])
target has been found!
else if(target < a[middle])
search for target in area before midpoint;
else
search for target in area after midpoint;
}
…
BINARY SEARCH
[ 0 ] [ 1 ]
Example: sorted array of integer keys. Target=7.
3 6 7 11 32 33 53
[ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ]
BINARY SEARCH
[ 0 ] [ 1 ]
Example: sorted array of integer keys. Target=7.
3 6 7 11 32 33 53
[ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ]
Find approximate midpoint
BINARY SEARCH
[ 0 ] [ 1 ]
Example: sorted array of integer keys. Target=7.
3 6 7 11 32 33 53
[ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ]
Is 7 = midpoint key? NO.
BINARY SEARCH
[ 0 ] [ 1 ]
Example: sorted array of integer keys. Target=7.
3 6 7 11 32 33 53
[ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ]
Is 7 < midpoint key? YES.
BINARY SEARCH
[ 0 ] [ 1 ]
Example: sorted array of integer keys. Target=7.
3 6 7 11 32 33 53
[ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ]
Search for the target in the area before midpoint.
BINARY SEARCH
[ 0 ] [ 1 ]
Example: sorted array of integer keys. Target=7.
3 6 7 11 32 33 53
[ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ]
Find approximate midpoint
BINARY SEARCH
[ 0 ] [ 1 ]
Example: sorted array of integer keys. Target=7.
3 6 7 11 32 33 53
[ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ]
Target = key of midpoint? NO.
BINARY SEARCH
[ 0 ] [ 1 ]
Example: sorted array of integer keys. Target=7.
3 6 7 11 32 33 53
[ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ]
Target < key of midpoint? NO.
BINARY SEARCH
[ 0 ] [ 1 ]
Example: sorted array of integer keys. Target=7.
3 6 7 11 32 33 53
[ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ]
Target > key of midpoint? YES.
BINARY SEARCH
[ 0 ] [ 1 ]
Example: sorted array of integer keys. Target=7.
3 6 7 11 32 33 53
[ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ]
Search for the target in the area after midpoint.
BINARY SEARCH
[ 0 ] [ 1 ]
Example: sorted array of integer keys. Target=7.
3 6 7 11 32 33 53
[ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ]
Find approximate midpoint.
Is target = midpoint key? YES.
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
BINARY SEARCH IMPLEMENTATION
void search(const int a[ ], size_t first, size_t size, int target, bool& found, size_t& location)
{
size_t middle;
if(size == 0) found = false;
else {
middle = first + size/2;
if(target == a[middle]){
location = middle;
found = true;
}
else if (target < a[middle])
// target is less than middle, so search subarray before middle
search(a, first, size/2, target, found, location);
else
// target is greater than middle, so search subarray after middle
search(a, middle+1, (size-1)/2, target, found, location);
}
}
RELATION TO BINARY SEARCH TREE
Corresponding complete binary search tree
3 6 7 11 32 33 53
3
6
7
11
32
33
53
Array of previous example:
SEARCH FOR TARGET = 7
Start at root:
Find midpoint:
3 6 7 11 32 33 53
3
6
7
11
32
33
53
Search left subarray:
SEARCH FOR TARGET = 7
Search left subtree:
3 6 7 11 32 33 53
3
6
7
11
32
33
53
Find approximate midpoint of subarray:
SEARCH FOR TARGET = 7
Visit root of subtree:
3 6 7 11 32 33 53
3
6
7
11
32
33
53
Search right subarray:
SEARCH FOR TARGET = 7
Search right subtree:
3 6 7 11 32 33 53
3
6
7
11
32
33
53
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
BINARY SEARCH: ANALYSIS
• Worst case complexity?
• What is the maximum depth of recursive calls in binary search as
function of n?
• Each level in the recursion, we split the array in half (divide by two).
• Therefore maximum recursion depth is floor(log2n) and worst case =
O(log2n).
• Average case is also = O(log2n).
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
DEPTH-FIRST SEARCH
• DFS follows the following rules:
1. Select an unvisited node x, visit it, and treat as the current node
2. Find an unvisited neighbor of the current node, visit it, and make it
the new current node;
3. If the current node has no unvisited neighbors, backtrack to the its
parent, and make that parent the new current node;
4. Repeat steps 3 and 4 until no more nodes can be visited.
5. If there are still unvisited nodes, repeat from step 1.
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
0 1
2
4
5
6
7
8
9
10
11
0
1
4
2
5
6
7
8
9
11
10
DFS Tree
Graph G
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
• Observations:
• the last node visited is the first node from which to proceed.
• Also, the backtracking proceeds on the basis of "last visited, first to backtrack too".
• This suggests that a stack is the proper data structure to remember the current node and how to backtrack.
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
DFS (PSEUDO CODE)
DFS(input: Graph G) {
Stack S; Integer x, t;
while (G has an unvisited node x){
visit(x); push(x,S);
while (S is not empty){
t := peek(S);
if (t has an unvisited neighbor y){ visit(y);
push(y,S); }
else
pop(S);
}
}
}
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
BREADTH-FIRST SEARCH
• BFS follows the following rules:
1. Select an unvisited node x, visit it, have it be the root in a BFS tree
being formed. Its level is called the current level.
2. From each node z in the current level, in the order in which the level
nodes were visited, visit all the unvisited neighbors of z. The newly
visited nodes from this level form a new level that becomes the next
current level.
3. Repeat step 2 until no more nodes can be visited.
4. If there are still unvisited nodes, repeat from Step 1.
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
0 1
2
4
5
6
7
8
9
10
11
0
1 4
2
5
6 7 8
9
11
10
BFS Tree Graph G
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
BFS (PSEUDO CODE)
BFS(input: graph G) {
Queue Q; Integer x, z, y;
while (G has an unvisited node x) {
visit(x); Enqueue(x,Q);
while (Q is not empty){
z := Dequeue(Q);
for all (unvisited neighbor y of z){ visit(y);
Enqueue(y,Q);
}
}
}
}
SORTING TECHNIQUES
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
SORTING
• Sorting takes an unordered collection and makes it an
ordered one.
5
12
35
42
77 101
1 2 3 4 5 6
5 12 35 42 77 101
1 2 3 4 5 6
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
"BUBBLING UP" THE LARGEST
ELEMENT
• Traverse a collection of elements
• Move from the front to the end
• “Bubble” the largest value to the end using pair-wise comparisons and
swapping
5
12
35
42
77 101
1 2 3 4 5 6
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
"BUBBLING UP" THE LARGEST
ELEMENT
• Traverse a collection of elements
• Move from the front to the end
• “Bubble” the largest value to the end using pair-wise comparisons and
swapping
5
12
35
42
77 101
1 2 3 4 5 6
Swap
42 77
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
"BUBBLING UP" THE LARGEST
ELEMENT
• Traverse a collection of elements
• Move from the front to the end
• “Bubble” the largest value to the end using pair-wise comparisons and
swapping
5
12
35
77
42 101
1 2 3 4 5 6
Swap
35 77
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
"BUBBLING UP" THE LARGEST
ELEMENT
• Traverse a collection of elements
• Move from the front to the end
• “Bubble” the largest value to the end using pair-wise comparisons and
swapping
5
12
77
35
42 101
1 2 3 4 5 6
Swap
12 77
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
"BUBBLING UP" THE LARGEST
ELEMENT
• Traverse a collection of elements
• Move from the front to the end
• “Bubble” the largest value to the end using pair-wise comparisons and
swapping
5
77
12
35
42 101
1 2 3 4 5 6
No need to swap
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
"BUBBLING UP" THE LARGEST
ELEMENT
• Traverse a collection of elements
• Move from the front to the end
• “Bubble” the largest value to the end using pair-wise comparisons and
swapping
5
77
12
35
42 101
1 2 3 4 5 6
Swap
5 101
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
"BUBBLING UP" THE LARGEST
ELEMENT
• Traverse a collection of elements
• Move from the front to the end
• “Bubble” the largest value to the end using pair-wise comparisons and
swapping
77
12
35
42 5
1 2 3 4 5 6
101
Largest value correctly placed
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
BUBBLE SORT ALGORITHM
index <- 1
last_compare_at <- n – 1
loop
exitif(index > last_compare_at)
if(A[index] > A[index + 1]) then
Swap(A[index], A[index + 1])
endif
index <- index + 1
endloop
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
NO, SWAP ISN’T BUILT IN.
Procedure Swap(a, b isoftype in/out Num)
t isoftype Num
t <- a
a <- b
b <- t
endprocedure // Swap
LB
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
ITEMS OF INTEREST
• Notice that only the largest value is correctly placed
• All other values are still out of order
• So we need to repeat this process
77
12
35
42 5
1 2 3 4 5 6
101
Largest value correctly placed
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
REPEAT “BUBBLE UP” HOW MANY TIMES?
• If we have N elements…
• And if each time we bubble an element, we place it in
its correct location…
• Then we repeat the “bubble up” process N – 1 times.
• This guarantees we’ll correctly
place all N elements.
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
“BUBBLING” ALL THE ELEMENTS
77
12
35
42 5
1 2 3 4 5 6
101
5
42
12
35 77
1 2 3 4 5 6
101
42
5
35
12 77
1 2 3 4 5 6
101
42
35
5
12 77
1 2 3 4 5 6
101
42
35
12
5 77
1 2 3 4 5 6
101
N
-
1
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
REDUCING THE NUMBER OF
COMPARISONS
12
35
42
77 101
1 2 3 4 5 6
5
77
12
35
42 5
1 2 3 4 5 6
101
5
42
12
35 77
1 2 3 4 5 6
101
42
5
35
12 77
1 2 3 4 5 6
101
42
35
5
12 77
1 2 3 4 5 6
101
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
REDUCING THE NUMBER OF COMPARISONS
• On the Nth “bubble up”, we only need to
do MAX-N comparisons.
• For example:
• This is the 4th “bubble up”
• MAX is 6
• Thus we have 2 comparisons to do
42
5
35
12 77
1 2 3 4 5 6
101
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
N is … // Size of Array
Arr_Type definesa Array[1..N] of Num
Procedure Swap(n1, n2 isoftype in/out Num)
temp isoftype Num
temp <- n1
n1 <- n2
n2 <- temp
endprocedure // Swap
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
procedure Bubblesort(A isoftype in/out Arr_Type)
to_do, index isoftype Num
to_do <- N – 1
loop
exitif(to_do = 0)
index <- 1
loop
exitif(index > to_do)
if(A[index] > A[index + 1]) then
Swap(A[index], A[index + 1])
endif
index <- index + 1
endloop
to_do <- to_do - 1
endloop
endprocedure // Bubblesort
Inner
loop
Outer
loop
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
ALREADY SORTED COLLECTIONS?
• What if the collection was already sorted?
• What if only a few elements were out of place and after a couple of “bubble ups,” the
collection was sorted?
• We want to be able to detect this
and “stop early”!
42
35
12
5 77
1 2 3 4 5 6
101
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
USING A BOOLEAN “FLAG”
• We can use a boolean variable to determine if any swapping
occurred during the “bubble up.”
• If no swapping occurred, then we know that the collection is
already sorted!
• This boolean “flag” needs to be reset after each “bubble up.”
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
did_swap isoftype Boolean
did_swap <- true
loop
exitif ((to_do = 0) OR NOT(did_swap))
index <- 1
did_swap <- false
loop
exitif(index > to_do)
if(A[index] > A[index + 1]) then
Swap(A[index], A[index + 1])
did_swap <- true
endif
index <- index + 1
endloop
to_do <- to_do - 1
endloop
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
AN ANIMATED EXAMPLE
67
45
23 14 6 33
98 42
1 2 3 4 5 6 7 8
to_do
index
7
N 8 did_swap true
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
67
45
23 14 6 33
98 42
1 2 3 4 5 6 7 8
to_do
index
7
1
N 8 did_swap false
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
67
45
23 14 6 33
98 42
1 2 3 4 5 6 7 8
to_do
index
7
1
N 8
Swap
did_swap false
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
67
45
98 14 6 33
23 42
1 2 3 4 5 6 7 8
to_do
index
7
1
N 8
Swap
did_swap true
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
67
45
98 14 6 33
23 42
1 2 3 4 5 6 7 8
to_do
index
7
2
N 8 did_swap true
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
67
45
98 14 6 33
23 42
1 2 3 4 5 6 7 8
to_do
index
7
2
N 8
Swap
did_swap true
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
67
98
45 14 6 33
23 42
1 2 3 4 5 6 7 8
to_do
index
7
2
N 8
Swap
did_swap true
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
67
98
45 14 6 33
23 42
1 2 3 4 5 6 7 8
to_do
index
7
3
N 8 did_swap true
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
67
98
45 14 6 33
23 42
1 2 3 4 5 6 7 8
to_do
index
7
3
N 8
Swap
did_swap true
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
67
14
45 98 6 33
23 42
1 2 3 4 5 6 7 8
to_do
index
7
3
N 8
Swap
did_swap true
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
67
14
45 98 6 33
23 42
1 2 3 4 5 6 7 8
to_do
index
7
4
N 8 did_swap true
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
67
14
45 98 6 33
23 42
1 2 3 4 5 6 7 8
to_do
index
7
4
N 8
Swap
did_swap true
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
67
14
45 6 98 33
23 42
1 2 3 4 5 6 7 8
to_do
index
7
4
N 8
Swap
did_swap true
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
67
14
45 6 98 33
23 42
1 2 3 4 5 6 7 8
to_do
index
7
5
N 8 did_swap true
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
67
14
45 6 98 33
23 42
1 2 3 4 5 6 7 8
to_do
index
7
5
N 8
Swap
did_swap true
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
98
14
45 6 67 33
23 42
1 2 3 4 5 6 7 8
to_do
index
7
5
N 8
Swap
did_swap true
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
98
14
45 6 67 33
23 42
1 2 3 4 5 6 7 8
to_do
index
7
6
N 8 did_swap true
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
98
14
45 6 67 33
23 42
1 2 3 4 5 6 7 8
to_do
index
7
6
N 8
Swap
did_swap true
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
33
14
45 6 67 98
23 42
1 2 3 4 5 6 7 8
to_do
index
7
6
N 8
Swap
did_swap true
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
33
14
45 6 67 98
23 42
1 2 3 4 5 6 7 8
to_do
index
7
7
N 8 did_swap true
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
33
14
45 6 67 98
23 42
1 2 3 4 5 6 7 8
to_do
index
7
7
N 8
Swap
did_swap true
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
33
14
45 6 67 42
23 98
1 2 3 4 5 6 7 8
to_do
index
7
7
N 8
Swap
did_swap true
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
AFTER FIRST PASS OF OUTER LOOP
33
14
45 6 67 42
23 98
1 2 3 4 5 6 7 8
to_do
index
7
8
N 8
Finished first “Bubble Up”
did_swap true
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
THE SECOND “BUBBLE UP”
33
14
45 6 67 42
23 98
1 2 3 4 5 6 7 8
to_do
index
6
1
N 8 did_swap false
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
THE SECOND “BUBBLE UP”
33
14
45 6 67 42
23 98
1 2 3 4 5 6 7 8
to_do
index
6
1
N 8 did_swap false
No Swap
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
THE SECOND “BUBBLE UP”
33
14
45 6 67 42
23 98
1 2 3 4 5 6 7 8
to_do
index
6
2
N 8 did_swap false
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
THE SECOND “BUBBLE UP”
33
14
45 6 67 42
23 98
1 2 3 4 5 6 7 8
to_do
index
6
2
N 8 did_swap false
Swap
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
THE SECOND “BUBBLE UP”
33
45
14 6 67 42
23 98
1 2 3 4 5 6 7 8
to_do
index
6
2
N 8 did_swap true
Swap
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
THE SECOND “BUBBLE UP”
33
45
14 6 67 42
23 98
1 2 3 4 5 6 7 8
to_do
index
6
3
N 8 did_swap true
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
THE SECOND “BUBBLE UP”
33
45
14 6 67 42
23 98
1 2 3 4 5 6 7 8
to_do
index
6
3
N 8 did_swap true
Swap
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
THE SECOND “BUBBLE UP”
33
6
14 45 67 42
23 98
1 2 3 4 5 6 7 8
to_do
index
6
3
N 8 did_swap true
Swap
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
THE SECOND “BUBBLE UP”
33
6
14 45 67 42
23 98
1 2 3 4 5 6 7 8
to_do
index
6
4
N 8 did_swap true
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
THE SECOND “BUBBLE UP”
33
6
14 45 67 42
23 98
1 2 3 4 5 6 7 8
to_do
index
6
4
N 8 did_swap true
No Swap
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
THE SECOND “BUBBLE UP”
33
6
14 45 67 42
23 98
1 2 3 4 5 6 7 8
to_do
index
6
5
N 8 did_swap true
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
THE SECOND “BUBBLE UP”
33
6
14 45 67 42
23 98
1 2 3 4 5 6 7 8
to_do
index
6
5
N 8 did_swap true
Swap
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
THE SECOND “BUBBLE UP”
67
6
14 45 33 42
23 98
1 2 3 4 5 6 7 8
to_do
index
6
5
N 8 did_swap true
Swap
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
THE SECOND “BUBBLE UP”
67
6
14 45 33 42
23 98
1 2 3 4 5 6 7 8
to_do
index
6
6
N 8 did_swap true
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
THE SECOND “BUBBLE UP”
67
6
14 45 33 42
23 98
1 2 3 4 5 6 7 8
to_do
index
6
6
N 8 did_swap true
Swap
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
THE SECOND “BUBBLE UP”
42
6
14 45 33 67
23 98
1 2 3 4 5 6 7 8
to_do
index
6
6
N 8 did_swap true
Swap
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
AFTER SECOND PASS OF OUTER LOOP
42
6
14 45 33 67
23 98
1 2 3 4 5 6 7 8
to_do
index
6
7
N 8 did_swap true
Finished second “Bubble Up”
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
THE THIRD “BUBBLE UP”
42
6
14 45 33 67
23 98
1 2 3 4 5 6 7 8
to_do
index
5
1
N 8 did_swap false
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
THE THIRD “BUBBLE UP”
42
6
14 45 33 67
23 98
1 2 3 4 5 6 7 8
to_do
index
5
1
N 8 did_swap false
Swap
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
THE THIRD “BUBBLE UP”
42
6
23 45 33 67
14 98
1 2 3 4 5 6 7 8
to_do
index
5
1
N 8 did_swap true
Swap
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
THE THIRD “BUBBLE UP”
42
6
23 45 33 67
14 98
1 2 3 4 5 6 7 8
to_do
index
5
2
N 8 did_swap true
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
THE THIRD “BUBBLE UP”
42
6
23 45 33 67
14 98
1 2 3 4 5 6 7 8
to_do
index
5
2
N 8 did_swap true
Swap
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
THE THIRD “BUBBLE UP”
42
23
6 45 33 67
14 98
1 2 3 4 5 6 7 8
to_do
index
5
2
N 8 did_swap true
Swap
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
THE THIRD “BUBBLE UP”
42
23
6 45 33 67
14 98
1 2 3 4 5 6 7 8
to_do
index
5
3
N 8 did_swap true
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
THE THIRD “BUBBLE UP”
42
23
6 45 33 67
14 98
1 2 3 4 5 6 7 8
to_do
index
5
3
N 8 did_swap true
No Swap
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
THE THIRD “BUBBLE UP”
42
23
6 45 33 67
14 98
1 2 3 4 5 6 7 8
to_do
index
5
4
N 8 did_swap true
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
THE THIRD “BUBBLE UP”
42
23
6 45 33 67
14 98
1 2 3 4 5 6 7 8
to_do
index
5
4
N 8 did_swap true
Swap
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
THE THIRD “BUBBLE UP”
42
23
6 33 45 67
14 98
1 2 3 4 5 6 7 8
to_do
index
5
4
N 8 did_swap true
Swap
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
THE THIRD “BUBBLE UP”
42
23
6 33 45 67
14 98
1 2 3 4 5 6 7 8
to_do
index
5
5
N 8 did_swap true
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
THE THIRD “BUBBLE UP”
42
23
6 33 45 67
14 98
1 2 3 4 5 6 7 8
to_do
index
5
5
N 8 did_swap true
Swap
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
THE THIRD “BUBBLE UP”
45
23
6 33 42 67
14 98
1 2 3 4 5 6 7 8
to_do
index
5
5
N 8 did_swap true
Swap
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
AFTER THIRD PASS OF OUTER LOOP
45
23
6 33 42 67
14 98
1 2 3 4 5 6 7 8
to_do
index
5
6
N 8 did_swap true
Finished third “Bubble Up”
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
THE FOURTH “BUBBLE UP”
45
23
6 33 42 67
14 98
1 2 3 4 5 6 7 8
to_do
index
4
1
N 8 did_swap false
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
THE FOURTH “BUBBLE UP”
45
23
6 33 42 67
14 98
1 2 3 4 5 6 7 8
to_do
index
4
1
N 8 did_swap false
Swap
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
THE FOURTH “BUBBLE UP”
45
23
14 33 42 67
6 98
1 2 3 4 5 6 7 8
to_do
index
4
1
N 8 did_swap true
Swap
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
THE FOURTH “BUBBLE UP”
45
23
14 33 42 67
6 98
1 2 3 4 5 6 7 8
to_do
index
4
2
N 8 did_swap true
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
THE FOURTH “BUBBLE UP”
45
23
14 33 42 67
6 98
1 2 3 4 5 6 7 8
to_do
index
4
2
N 8 did_swap true
No Swap
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
THE FOURTH “BUBBLE UP”
45
23
14 33 42 67
6 98
1 2 3 4 5 6 7 8
to_do
index
4
3
N 8 did_swap true
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
THE FOURTH “BUBBLE UP”
45
23
14 33 42 67
6 98
1 2 3 4 5 6 7 8
to_do
index
4
3
N 8 did_swap true
No Swap
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
THE FOURTH “BUBBLE UP”
45
23
14 33 42 67
6 98
1 2 3 4 5 6 7 8
to_do
index
4
4
N 8 did_swap true
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
THE FOURTH “BUBBLE UP”
45
23
14 33 42 67
6 98
1 2 3 4 5 6 7 8
to_do
index
4
4
N 8 did_swap true
No Swap
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
AFTER FOURTH PASS OF OUTER LOOP
45
23
14 33 42 67
6 98
1 2 3 4 5 6 7 8
to_do
index
4
5
N 8 did_swap true
Finished fourth “Bubble Up”
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
THE FIFTH “BUBBLE UP”
45
23
14 33 42 67
6 98
1 2 3 4 5 6 7 8
to_do
index
3
1
N 8 did_swap false
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
THE FIFTH “BUBBLE UP”
45
23
14 33 42 67
6 98
1 2 3 4 5 6 7 8
to_do
index
3
1
N 8 did_swap false
No Swap
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
THE FIFTH “BUBBLE UP”
45
23
14 33 42 67
6 98
1 2 3 4 5 6 7 8
to_do
index
3
2
N 8 did_swap false
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
THE FIFTH “BUBBLE UP”
45
23
14 33 42 67
6 98
1 2 3 4 5 6 7 8
to_do
index
3
2
N 8 did_swap false
No Swap
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
THE FIFTH “BUBBLE UP”
45
23
14 33 42 67
6 98
1 2 3 4 5 6 7 8
to_do
index
3
3
N 8 did_swap false
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
THE FIFTH “BUBBLE UP”
45
23
14 33 42 67
6 98
1 2 3 4 5 6 7 8
to_do
index
3
3
N 8 did_swap false
No Swap
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
AFTER FIFTH PASS OF OUTER LOOP
45
23
14 33 42 67
6 98
1 2 3 4 5 6 7 8
to_do
index
3
4
N 8 did_swap false
Finished fifth “Bubble Up”
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
FINISHED “EARLY”
45
23
14 33 42 67
6 98
1 2 3 4 5 6 7 8
to_do
index
3
4
N 8 did_swap false
We didn’t do any swapping,
so all of the other elements
must be correctly placed.
We can “skip” the last two
passes of the outer loop.
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
SORTING
• INSERTION SORT
• SHELL SORT
• MERGE SORT
• QUICK SORT
• HEAP SORT
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
INSERTION
• Insertion sort is a simple sorting algorithm that works similar to the way you sort
playing cards in your hands. The array is virtually split into a sorted and an
unsorted part. Values from the unsorted part are picked and placed at the
correct position in the sorted part.
Algorithm
To sort an array of size n in ascending order:
1: Iterate from arr[1] to arr[n] over the array.
2: Compare the current element (key) to its predecessor.
3: If the key element is smaller than its predecessor, compare it to the elements
before. Move the greater elements one position up to make space for the
swapped element.
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
INSERTION SORT-EXAMPLE
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
SELECTION
• The selection sort algorithm sorts an array by repeatedly finding the minimum
element (considering ascending order) from unsorted part and putting it at the
beginning.
• The algorithm maintains two subarrays in a given array.
1) The subarray which is already sorted.
2) Remaining subarray which is unsorted.
In every iteration of selection sort, the minimum element (considering ascending
order) from the unsorted subarray is picked and moved to the sorted subarray.
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
SELECTION SORT-EXAMPLE
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
EXAMPLE
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
SHELL
• Shell sort is a highly efficient sorting algorithm
and is based on insertion sort algorithm.
• This algorithm avoids large shifts as in case of
insertion sort, if the smaller value is to the far right
and has to be moved to the far left.
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
SHELL SORT
• ShellSort is mainly a variation of Insertion Sort. In
insertion sort, we move elements only one position
ahead. When an element has to be moved far ahead,
many movements are involved.
• The idea of shellSort is to allow exchange of far items.
In shellSort, we make the array h-sorted for a large
value of h. We keep reducing the value of h until it
becomes 1. An array is said to be h-sorted if all
sublists of every h’th element is sorted.
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
EXAMPLE-SHELL SORT
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
MERGE
• Like QuickSort, 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.
• The merge() function is used for merging two halves.
• The merge(arr, l, m, r) is a key process that assumes that arr[l..m]
and arr[m+1..r] are sorted and merges the two sorted sub-arrays
into one.
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
MERGE SORT
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
QUICK
• Like Merge Sort, QuickSort is a Divide and Conquer algorithm. It picks an
element as pivot and partitions the given array around the picked pivot.
There are many different versions of quickSort that pick pivot in different
ways.
1.Always pick first element as pivot.
2.Always pick last element as pivot.
3.Pick a random element as pivot.
4.Pick median as pivot.
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
QUICK SORT
• The key process in quickSort is partition().
• Target of partitions is, given an array and an element
x of array as pivot, put x at its correct position in sorted
array and put all smaller elements (smaller than x)
before x, and put all greater elements (greater than x)
after x.
• All this should be done in linear time.
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
QUICK SORT-EXAMPLE
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
HEAP
Definitions of heap:
• A large area of memory from which the programmer can
allocate blocks as needed, and deallocate them (or allow
them to be garbage collected) when no longer needed
• A balanced, left-justified binary tree in which no node has a
value greater than the value in its parent
• These two definitions have little in common
• Heapsort uses the second definition
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
BALANCED BINARY TREES
• Recall:
• The depth of a node is its distance from the root
• The depth of a tree is the depth of the deepest node
• A binary tree of depth n is balanced if all the nodes at depths 0 through n-2 have
two children
Balanced
Balanced Not balanced
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
LEFT-JUSTIFIED BINARY TREES
• A balanced binary tree is left-justified if:
• all the leaves are at the same depth, or
• all the leaves at depth n+1 are to the left of all the
nodes at depth n
Left-justified
Not left-justified
Not balanced
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
PLAN OF OUTBREAK
• First, we will learn how to turn a binary tree into a heap
• Next, we will learn how to turn a binary tree back into a heap
after it has been changed in a certain way
• Finally (this is the cool part) we will see how to use these
ideas to sort an array
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
HEAP
• All leaf nodes automatically have the heap property
• A binary tree is a heap if all nodes in it have the heap property
12
8 3
Blue node has
heap property
12
8 12
Blue node has
heap property
12
8 14
Blue node does not have
heap property
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
SIFTUP
• Given a node that does not have the heap property, you can give it the heap property by
exchanging its value with the value of the larger child
12
8 14
Blue node does not
have heap property
14
8 12
Blue node has heap
property
• This is sometimes called sifting up
Notice that the child may have lost the heap property
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
CONSTRUCTING A HEAP I
• A tree consisting of a single node is automatically a heap
• We construct a heap by adding nodes one at a time:
• Add the node just to the right of the rightmost node in the deepest level
• If the deepest level is full, start a new level
• Examples:
Add a new
node here
Add a new
node here
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
CONSTRUCTING A HEAP II
• Each time we add a node, we may destroy the heap property of
its parent node
• To fix this, we sift up
• But each time we sift up, the value of the topmost node in the sift
may increase, and this may destroy the heap property of its
parent node
• We repeat the sifting up process, moving up in the tree, until
either
• We reach nodes whose values don’t need to be swapped (because
the parent is still larger than both children), or
• We reach the root
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
CONSTRUCTING A HEAP III
• A 3-ary max heap is like a binary max heap, but instead of 2 children,
nodes have 3 children.
• A 3-ary heap can be represented by an array as follows: The root is
stored in the first location, a[0], nodes in the next level, from left to right,
is stored from a[1] to a[3].
• The nodes from the second level of the tree from left to right are stored
from a[4] location onward. An item x can be inserted into a 3-ary heap
containing n items by placing x in the location a[n] and pushing it up the
tree to satisfy the heap property.
•
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
A SAMPLE HEAP
19
14
18
22
3
21
14
11
9
15
25
17
22

More Related Content

Similar to UNIT 4_DSA_KAVITHA_RMP.ppt

Unit-6 Graph.ppsx ppt
Unit-6 Graph.ppsx                                       pptUnit-6 Graph.ppsx                                       ppt
Unit-6 Graph.ppsx pptDhruvilSTATUS
 
Graph Data Structure
Graph Data StructureGraph Data Structure
Graph Data StructureKeno benti
 
Lecture 5b graphs and hashing
Lecture 5b graphs and hashingLecture 5b graphs and hashing
Lecture 5b graphs and hashingVictor Palmar
 
Graphical reprsentation dsa (DATA STRUCTURE ALGORITHM)
Graphical reprsentation dsa (DATA STRUCTURE ALGORITHM)Graphical reprsentation dsa (DATA STRUCTURE ALGORITHM)
Graphical reprsentation dsa (DATA STRUCTURE ALGORITHM)Abdul Naqashbandi
 
8.-Graphs information technologies graph
8.-Graphs information technologies graph8.-Graphs information technologies graph
8.-Graphs information technologies graphiloveyoucarlo0923
 
Talk on Graph Theory - I
Talk on Graph Theory - ITalk on Graph Theory - I
Talk on Graph Theory - IAnirudh Raja
 
Graphs, Trees, Paths and Their Representations
Graphs, Trees, Paths and Their RepresentationsGraphs, Trees, Paths and Their Representations
Graphs, Trees, Paths and Their RepresentationsAmrinder Arora
 
graph representation.pdf
graph representation.pdfgraph representation.pdf
graph representation.pdfamitbhachne
 
Skiena algorithm 2007 lecture10 graph data strctures
Skiena algorithm 2007 lecture10 graph data strcturesSkiena algorithm 2007 lecture10 graph data strctures
Skiena algorithm 2007 lecture10 graph data strctureszukun
 
Graph Basic In Data structure
Graph Basic In Data structureGraph Basic In Data structure
Graph Basic In Data structureIkhlas Rahman
 
VANU no sql ppt.pptx
VANU no sql ppt.pptxVANU no sql ppt.pptx
VANU no sql ppt.pptxMJeyavarthini
 
Graph_data_structure_information_engineering.pptx
Graph_data_structure_information_engineering.pptxGraph_data_structure_information_engineering.pptx
Graph_data_structure_information_engineering.pptxsahilpawar2426
 

Similar to UNIT 4_DSA_KAVITHA_RMP.ppt (20)

Unit-6 Graph.ppsx ppt
Unit-6 Graph.ppsx                                       pptUnit-6 Graph.ppsx                                       ppt
Unit-6 Graph.ppsx ppt
 
Graph Data Structure
Graph Data StructureGraph Data Structure
Graph Data Structure
 
Graph 1
Graph 1Graph 1
Graph 1
 
Lecture 5b graphs and hashing
Lecture 5b graphs and hashingLecture 5b graphs and hashing
Lecture 5b graphs and hashing
 
Graphical reprsentation dsa (DATA STRUCTURE ALGORITHM)
Graphical reprsentation dsa (DATA STRUCTURE ALGORITHM)Graphical reprsentation dsa (DATA STRUCTURE ALGORITHM)
Graphical reprsentation dsa (DATA STRUCTURE ALGORITHM)
 
Graphs
GraphsGraphs
Graphs
 
8.-Graphs information technologies graph
8.-Graphs information technologies graph8.-Graphs information technologies graph
8.-Graphs information technologies graph
 
Talk on Graph Theory - I
Talk on Graph Theory - ITalk on Graph Theory - I
Talk on Graph Theory - I
 
Graphs, Trees, Paths and Their Representations
Graphs, Trees, Paths and Their RepresentationsGraphs, Trees, Paths and Their Representations
Graphs, Trees, Paths and Their Representations
 
Spanningtreesppt
SpanningtreespptSpanningtreesppt
Spanningtreesppt
 
graph representation.pdf
graph representation.pdfgraph representation.pdf
graph representation.pdf
 
Skiena algorithm 2007 lecture10 graph data strctures
Skiena algorithm 2007 lecture10 graph data strcturesSkiena algorithm 2007 lecture10 graph data strctures
Skiena algorithm 2007 lecture10 graph data strctures
 
Graph Basic In Data structure
Graph Basic In Data structureGraph Basic In Data structure
Graph Basic In Data structure
 
Graphs
GraphsGraphs
Graphs
 
Techniques for graph
Techniques for graphTechniques for graph
Techniques for graph
 
Graph
GraphGraph
Graph
 
Vanmathy no sql
Vanmathy no sql Vanmathy no sql
Vanmathy no sql
 
VANU no sql ppt.pptx
VANU no sql ppt.pptxVANU no sql ppt.pptx
VANU no sql ppt.pptx
 
Graph_data_structure_information_engineering.pptx
Graph_data_structure_information_engineering.pptxGraph_data_structure_information_engineering.pptx
Graph_data_structure_information_engineering.pptx
 
Unit ix graph
Unit   ix    graph Unit   ix    graph
Unit ix graph
 

Recently uploaded

College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAbhinavSharma374939
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learningmisbanausheenparvam
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 

Recently uploaded (20)

College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog Converter
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learning
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 

UNIT 4_DSA_KAVITHA_RMP.ppt

  • 1. SRM INSTITUTE OF SCIENCE ANDTECHNOLOGY, CHENNAI. DATA STRUCTURES AND ALGORITHMS (18CSC162J) PREPARED BY D.KAVITHA,AP/CSE,SRMIST, RAMAPURAM CAMPUS
  • 2. SRM INSTITUTE OF SCIENCE ANDTECHNOLOGY, CHENNAI. DATA STRUCTURE - GRAPH UNIT IV
  • 3. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. TOPICS TO BE COVERED (THEORY) Session Topics 1 Graph Terminology, Traversal 2 Topological sorting, Minimal spanning tree Intro 3 Prim’s 4 Kruskal’s 5 Djikstra’s 6 Searching – Linear, binary 7 Breadth, Depth First searches 8 Sorting – Bubble, insertion, selection 9 Shell, merge, quick, heap
  • 4. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. TOPICS FOR LAB SESSION Session Topics 1 Implementation of Minimal spanning trees – Prim’s 2 Implementation of Minimal spanning trees – Kruskal’s 3 Implementation of shortest path algorithms - Dijkstra’s Algorithm 4 Implementation of shortest path algorithms 5 Implementation of B Trees 6
  • 5. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. LEARNING RESOURCES • Fundamentals of Data Structures, E. Horowitz and S. Sahni, 1977. • Data Structures and Algorithms, Alfred V. Aho, John E. Hopperoft, Jeffrey D. UIlman. • Mark Allen Weiss, Data Structures and Algorithm Analysis in C, 2nd ed., Pearson Education, 2015 • Reema Thareja, Data Structures Using C, 1st ed., Oxford Higher Education, 2011 • Thomas H Cormen, Charles E Leiserson, Ronald L Revest, Clifford Stein, Introduction to Algorithms 3rd ed., The MIT Press Cambridge, 2014
  • 7. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. GRAPHS - TERMINOLOGY AND REPRESENTATION 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.
  • 8. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. GRAPHS - TERMINOLOGY AND REPRESENTATION 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 example graph, there are 7 edges (i.e., (A,B), (A,C), (A,D), (B,D), (B,E), (C,D), (D,E)).
  • 9. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. GRAPHS - TERMINOLOGY AND REPRESENTATION Types of Edges • Undirected Edge: An undirected edge is a bidirectional edge. If there is undirected edge between vertices A and B then edge (A , B) is equal to edge (B , A). • Directed Edge: - A directed edge 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). • Weighted Edge: - A weighted edge is a edge with value (cost) on it.
  • 10. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. GRAPHS - TERMINOLOGY AND REPRESENTATION 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.
  • 11. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. GRAPHS - TERMINOLOGY AND REPRESENTATION 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.
  • 12. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. GRAPHS - TERMINOLOGY AND REPRESENTATION 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. 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
  • 13. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. GRAPHS - TERMINOLOGY AND REPRESENTATION Individual data element of a graph is called as Vertex. Vertex is also known as node. Denote vertices with labels In above example graph, A, B, C, D & E are known as vertices.
  • 14. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. GRAPHS - TERMINOLOGY AND REPRESENTATION 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.
  • 15. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. GRAPHS - TERMINOLOGY AND REPRESENTATION 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. Cycle a path that starts and ends on the same vertex Length of a path: Number of edges in the path Sometimes the sum of the weights of the edges
  • 16. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. GRAPHS - TERMINOLOGY AND REPRESENTATION Motivation: Many algorithms use a graph representation to represent data or the problem to be solved Examples: • Cities with distances between • Roads with distances between intersection points • Course prerequisites • Network • Social networks • Program call graph and variable dependency graph
  • 17. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. GRAPH CLASSIFICATIONS There are several common kinds of graphs • Weighted or unweighted • Directed or undirected • Cyclic or acyclic
  • 18. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. WEIGHTED AND UNWEIGHTED Graphs can be classified by whether or not their edges have weights Weighted graph: edges have a weight • Weight typically shows cost of traversing • Example: weights are distances between cities Unweighted graph: edges have no weight • Edges simply show connections • Example: course prereqs
  • 19. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. DIRECTED AND UNDIRECTED Graphs can be classified by whether or their edges are have direction • Undirected Graphs: each edge can be traversed in either direction • Directed Graphs: each edge can be traversed only in a specified direction
  • 20. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. CYCLIC AND ACYCLIC GRAPHS A Cyclic graph contains cycles Example: roads (normally) An acyclic graph contains no cycles Example: Course prereqs! Examples - Are these cyclic or acyclic?
  • 21. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. CONNECTED, UNCONNECTED GRAPHS AND CONNECTED COMPONENTS An undirected graph is connected if every pair of vertices has a path between it • Otherwise it is unconnected • Give an example of a connected graph An unconnected graph can be broken in to connected components A directed graph is strongly connected if every pair of vertices has a path between them, in both directions
  • 22. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. DATA STRUCTURES FOR REPRESENTING GRAPHS Two common data structures for representing graphs: • Adjacency lists • Adjacency matrix
  • 23. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. ADJACENCY LIST REPRESENTATION Adjacency List Representation Each node has a list of adjacent nodes Example (undirected graph): A: B, C, D B: A, D C: A, D D: A, B, C Example (directed graph): A: B, C, D B: D C: Nil D: C
  • 24. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. ADJACENCY MATRIX REPRESENTATION Adjacency Matrix: 2D array containing weights on edges • Row for each vertex • Column for each vertex • Entries contain weight of edge from row vertex to column vertex • Entries contain ∞ (i.e. Integer‘ last) if no edge from row vertex to column vertex • Entries contain 0 on diagonal (if self edges not allowed)
  • 25. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. ADJACENCY MATRIX REPRESENTATION Example directed graph (assume self-edges allowed)
  • 26. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. GRAPH TRAVERSAL 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... • DFS (Depth First Search) • BFS (Breadth First Search)
  • 27. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. 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.
  • 28. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. DFS (DEPTH FIRST SEARCH) 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
  • 29. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. DFS (DEPTH FIRST SEARCH)
  • 30. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. DFS (DEPTH FIRST SEARCH)
  • 31. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. DFS (DEPTH FIRST SEARCH)
  • 32. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. DFS (DEPTH FIRST SEARCH)
  • 33. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. DFS (DEPTH FIRST SEARCH)
  • 34. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. DFS (DEPTH FIRST SEARCH)
  • 35. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. DFS (DEPTH FIRST SEARCH)
  • 36. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. DFS (DEPTH FIRST SEARCH)
  • 37. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. DFS (DEPTH FIRST SEARCH)
  • 38. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. DFS (DEPTH FIRST SEARCH)
  • 39. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. DFS (DEPTH FIRST SEARCH)
  • 40. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. DFS (DEPTH FIRST SEARCH)
  • 41. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. DFS (DEPTH FIRST SEARCH)
  • 42. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. DFS (DEPTH FIRST SEARCH)
  • 43. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. DFS (DEPTH FIRST SEARCH)
  • 44. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. DFS (DEPTH FIRST SEARCH)
  • 45. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. 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.
  • 46. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. BFS (BREADTH FIRST SEARCH) 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
  • 47. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. BFS (BREADTH FIRST SEARCH)
  • 48. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. BFS (BREADTH FIRST SEARCH)
  • 49. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. BFS (BREADTH FIRST SEARCH)
  • 50. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. BFS (BREADTH FIRST SEARCH)
  • 51. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. BFS (BREADTH FIRST SEARCH)
  • 52. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. BFS (BREADTH FIRST SEARCH)
  • 53. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. BFS (BREADTH FIRST SEARCH)
  • 54. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. BFS (BREADTH FIRST SEARCH)
  • 55. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. BFS (BREADTH FIRST SEARCH)
  • 56. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. BFS (BREADTH FIRST SEARCH)
  • 57. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. GRAPH - SPANNING TREE • A tree that contains every vertex of a connected graph is called spanning tree. It is an undirected tree consisting of only those edges that are necessary to connect all the vertices of the original graph G. • CHARACTERISTICS 1. It is a subgraph of a graph G that contain all the vertex of graph G. 2. For any pair of vertices, there exists only one path between them. 3. It is a connected graph with no cycles. 4. A graph may have many spanning tree.
  • 58. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. EXAMPLE
  • 59. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. GRAPH - MINIMUM SPANNING TREE • Let G=(V,E,W) be any weighted graph. Then a spanning tree whose cost is minimum is called minimum spanning tree. The cost of the spanning tree is defined as the sum of the costs of the edges in that tree.
  • 60. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. MINIMUM SPANNING TREE • A spanning tree is a subset of Graph G, which has all the vertices covered with minimum possible number of edges. • A complete undirected graph can have maximum nn-2 number of spanning trees, where n is number of nodes.
  • 61. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. GENERAL PROPERTIES OF SPANNING TREE • A connected graph G can have more than one spanning tree. • All possible spanning trees of graph G, have same number of edges and vertices. • Spanning tree does not have any cycle (loops) • Removing one edge from spanning tree will make the graph disconnected i.e. spanning tree is minimally connected. • Adding one edge to a spanning tree will create a cycle or loop i.e. spanning tree is maximally acyclic.
  • 62. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. MATHEMATICAL PROPERTIES OF SPANNING TREE • Spanning tree has n-1 edges, where n is number of nodes (vertices) • From a complete graph, by removing maximum e-n+1 edges, we can construct a spanning tree. • A complete graph can have maximum nn-2 number of spanning trees. • So we can conclude here that spanning trees are subset of a connected Graph G and disconnected Graphs do not have spanning tree.
  • 63. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. APPLICATION OF SPANNING TREE • Spanning tree is basically used to find minimum paths to connect all nodes in a graph. • Civil Network Planning • Computer Network Routing Protocol • Cluster Analysis
  • 64. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. MINIMUM SPANNING TREE (MST) • In a weighted graph, a minimum spanning tree is a spanning tree that has minimum weight that all other spanning trees of the same graph. MST Algorithm • Kruskal’s Algorithm • Prim’s Algorithm Both are greedy algorithms.
  • 65. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. PRIM’S ALGORITHM • Create a set mstSet that keeps track of vertices already included in MST. • Assign a key value to all vertices in the input graph. Initialize all key values as INFINITE. Assign key value as 0 for the first vertex so that it is picked first. • While mstSet doesn’t include all vertices • Pick a vertex u which is not there in mstSet and has minimum key value. • Include u to mstSet. • Update key value of all adjacent vertices of u. To update the key values, iterate through all adjacent vertices. For every adjacent vertex v, if weight of edge u-v is
  • 66. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. A F B C D E 2 7 4 5 8 6 4 5 3 8 Select any vertex A Select the shortest edge connected to that vertex AB 3 PRIM’S ALGORITHM
  • 67. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. A F B C D E 2 7 4 5 8 6 4 5 3 8 Select the shortest edge connected to any vertex already connected. AE 4 PRIM’S ALGORITHM
  • 68. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. Select the shortest edge connected to any vertex already connected. ED 2 A F B C D E 2 7 4 5 8 6 4 5 3 8 PRIM’S ALGORITHM
  • 69. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. Select the shortest edge connected to any vertex already connected. DC 4 A F B C D E 2 7 4 5 8 6 4 5 3 8 PRIM’S ALGORITHM
  • 70. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. Select the shortest edge connected to any vertex already connected. EF 5 A F B C D E 2 7 4 5 8 6 4 5 3 8 PRIM’S ALGORITHM
  • 71. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. A F B C D E 2 7 4 5 8 6 4 5 3 8 All vertices have been connected. The solution is AB 3 AE 4 ED 2 DC 4 EF 5 Total weight of tree: 18 PRIM’S ALGORITHM
  • 72. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. A F B C D E 2 4 5 4 3 All vertices have been connected. The solution is AB 3 AE 4 ED 2 DC 4 EF 5 Total weight of tree: 18 PRIM’S ALGORITHM
  • 73. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. KRUSKAL ALGORITHM Algorithm • Remove all loops & Parallel Edges from the given graph • Sort all the edges in non-decreasing order of their weight. • Pick the smallest edge. Check if it forms a cycle with the spanning tree formed so far. If cycle is not formed, include this edge. Else, discard it. • Repeat step#2 until there are (V-1) edges in the spanning tree.
  • 74. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. EXAMPLE Avonford Fingley Brinleigh Cornwell Donster 2 7 4 5 8 6 4 5 3 8 A cable company want to connect five villages to their network which currently extends to the market town of Avonford. What is the minimum length of cable needed?
  • 75. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. A F B C D 2 7 4 5 8 6 4 5 3 8 WE MODEL THE SITUATION AS A NETWORK, THEN THE PROBLEM IS TO FIND THE MINIMUM CONNECTOR FOR THE NETWORK
  • 76. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. A F B C D E 2 7 4 5 8 6 4 5 3 8 List the edges in order of size: ED 2 AB 3 AE 4 CD 4 BC 5 EF 5 CF 6 AF 7 BF 8 CF 8 KRUSKAL’S ALGORITHM
  • 77. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. Select the shortest edge in the network ED 2 A F B C D E 2 7 4 5 8 6 4 5 3 8 KRUSKAL’S ALGORITHM
  • 78. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. Select the next shortest edge which does not create a cycle ED 2 AB 3 A F B C D E 2 7 4 5 8 6 4 5 3 8 KRUSKAL’S ALGORITHM
  • 79. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. Select the next shortest edge which does not create a cycle ED 2 AB 3 CD 4 (or AE 4) A F B C D E 2 7 4 5 8 6 4 5 3 8 KRUSKAL’S ALGORITHM
  • 80. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. Select the next shortest edge which does not create a cycle ED 2 AB 3 CD 4 AE 4 A F B C D E 2 7 4 5 8 6 4 5 3 8 KRUSKAL’S ALGORITHM
  • 81. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. Select the next shortest edge which does not create a cycle ED 2 AB 3 CD 4 AE 4 BC 5 – forms a cycle EF 5 A F B C D E 2 7 4 5 8 6 4 5 3 8 KRUSKAL’S ALGORITHM
  • 82. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. All vertices have been connected. The solution is ED 2 AB 3 CD 4 AE 4 EF 5 Total weight of tree: 18 A F B C D E 2 7 4 5 8 6 4 5 3 8 KRUSKAL’S ALGORITHM
  • 83. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. All vertices have been connected. The solution is ED 2 AB 3 CD 4 AE 4 EF 5 Total weight of tree: 18 A F B C D E 2 4 5 4 3 KRUSKAL’S ALGORITHM
  • 84. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. Kruskal’s algorithm 1. Select the shortest edge in a network 2. Select the next shortest edge which does not create a cycle 3. Repeat step 2 until all vertices have been connected Prim’s algorithm 1. Select any vertex 2. Select the shortest edge connected to that vertex 3. Select the shortest edge connected to any vertex already connected 4. Repeat step 3 until all vertices have been connected MINIMUM SPANNING TREE ALGORITHMS
  • 85. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. PRIMS ALGORITHM & KRUSKAL ALGORITHM
  • 86. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. FIND MST USING KRUSKAL’S AND PRIM’S ALGORITHM
  • 87. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. MST USING KRUSKAL’S ALGORITHM
  • 88. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. INTRODUCTION • Single-Source Shortest Path Problem - The problem of finding shortest path from a source vertex v to all other vertices in the graph.
  • 89. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. DIJKSTRA'S ALGORITHM Dijkstra's algorithm - is a solution to the single-source shortest path problem in graph theory. Works on both directed and undirected graphs. However, all edges must have nonnegative weights. Input: Weighted graph G={E,V} and source vertex v∈V, such that all edge weights are nonnegative Output: Lengths of shortest paths (or the shortest paths themselves) from a given source vertex v∈V to all other vertices
  • 90. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. APPROACH • The algorithm computes for each vertex u the distance to u from the start vertex v, that is, the weight of a shortest path between v and u. • The algorithm keeps track of the set of vertices for which the distance has been computed, called the cloud C • Every vertex has a label D associated with it. For any vertex u, D[u] stores an approximation of the distance between v and u. The algorithm will update a D[u] value when it finds a shorter path from v to u.
  • 91. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. DIJKSTRA PSEUDOCODE Dijkstra(v1, v2): for each vertex v: // Initialization v's distance := infinity. v's previous := none. v1's distance := 0. List := {all vertices}. while List is not empty: v := remove List vertex with minimum distance. mark v as known. for each unknown neighbor n of v: dist := v's distance + edge (v, n)'s weight. if dist is smaller than n's distance: n's distance := dist. n's previous := v. reconstruct path from v2 back to v1, following previous pointers.
  • 92. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. EXAMPLE: INITIALIZATION 92 A G F B E C D 4 1 2 10 3 6 4 2 2 8 5 1 0 ∞ ∞ ∞ ∞ Pick vertex in List with minimum distance. ∞ ∞ Distance(source) = 0 Distance (all vertices but source) = ∞
  • 93. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. 93 EXAMPLE: UPDATE NEIGHBORS' DISTANCE A G F B E C D 4 1 2 10 3 6 4 2 2 8 5 1 0 2 ∞ ∞ 1 ∞ ∞ Distance(B) = 2 Distance(D) = 1
  • 94. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. 94 EXAMPLE: REMOVE VERTEX WITH MINIMUM DISTANCE Pick vertex in List with minimum distance, i.e., D A G F B E C D 4 1 2 10 3 6 4 2 2 8 5 1 0 2 ∞ ∞ 1 ∞ ∞
  • 95. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. 95 EXAMPLE: UPDATE NEIGHBORS A G F B E C D 4 1 2 10 3 6 4 2 2 8 5 1 0 2 3 3 1 9 5 Distance(C) = 1 + 2 = 3 Distance(E) = 1 + 2 = 3 Distance(F) = 1 + 8 = 9 Distance(G) = 1 + 4 = 5
  • 96. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. 96 EXAMPLE: CONTINUED... A G F B E C D 4 1 2 10 3 6 4 2 2 8 5 1 0 2 3 3 1 Pick vertex in List with minimum distance (B) and update neighbors 9 5 Note : distance(D) not updated since D is already known and distance(E) not updated since it is larger than previously computed
  • 97. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. 97 EXAMPLE: CONTINUED... A G F B E C D 4 1 2 10 3 6 4 2 2 8 5 1 0 2 3 3 1 9 5 No updating Pick vertex List with minimum distance (E) and update neighbors
  • 98. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. 98 EXAMPLE: CONTINUED... A G F B E C D 4 1 2 10 3 6 4 2 2 8 5 1 0 2 3 3 1 8 5 Pick vertex List with minimum distance (C) and update neighbors Distance(F) = 3 + 5 = 8
  • 99. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. 99 EXAMPLE: CONTINUED... A G F B E C D 4 1 2 10 3 6 4 2 2 8 5 1 0 2 3 3 1 6 5 Distance(F) = min (8, 5+1) = 6 Previous distance Pick vertex List with minimum distance (G) and update neighbors
  • 100. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. 10 0 EXAMPLE (END) A G F B E C D 4 1 2 10 3 6 4 2 2 8 5 1 0 2 3 3 1 Pick vertex not in S with lowest cost (F) and update neighbors 6 5
  • 101. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. ANOTHER EXAMPLE
  • 102. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. ANOTHER EXAMPLE
  • 103. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. ANOTHER EXAMPLE
  • 104. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. ANOTHER EXAMPLE
  • 105. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. ANOTHER EXAMPLE
  • 106. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. ANOTHER EXAMPLE
  • 107. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. ANOTHER EXAMPLE
  • 108. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. ANOTHER EXAMPLE
  • 109. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. ANOTHER EXAMPLE
  • 111. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. PROBLEM: SEARCH • We are given a list of records. • Each record has an associated key. • Give efficient algorithm for searching for a record containing a particular key. • Efficiency is quantified in terms of average time analysis (number of comparisons) to retrieve an item.
  • 112. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. SEARCH [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 700 ] Number 506643548 Number 233667136 Number 281942902 Number 155778322 Number 580625685 Number 701466868 … Number 580625685 Each record in list has an associated key. In this example, the keys are ID numbers. Given a particular key, how can we efficiently retrieve the record from the list?
  • 113. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. LINEAR SEARCH • Step through array of records, one at a time. • Look for record with matching key. • Search stops when • record with matching key is found • or when search has examined all records without success.
  • 114. PSEUDOCODE FOR LINEAR SEARCH // Search for a desired item in the n array elements // starting at a[first]. // Returns pointer to desired record if found. // Otherwise, return NULL … for(i = first; i < n; ++i ) if(a[first+i] is desired item) return &a[first+i]; // if we drop through loop, then desired item was not found return NULL;
  • 115. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. SERIAL SEARCH ANALYSIS • What are the worst and average case running times for serial search? • We must determine the O-notation for the number of operations required in search. • Number of operations depends on n, the number of entries in the list.
  • 116. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. WORST CASE TIME FOR LINEAR SEARCH • For an array of n elements, the worst case time for linear search requires n array accesses: O(n). • Consider cases where we must loop over all n records: • desired record appears in the last position of the array • desired record does not appear in the array at all
  • 117. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. AVERAGE CASE FOR LINEAR SEARCH Assumptions: 1. All keys are equally likely in a search 2. We always search for a key that is in the array Example: • We have an array of 10 records. • If search for the first record, then it requires 1 array access; if the second, then 2 array accesses. etc. The average of all these searches is: (1+2+3+4+5+6+7+8+9+10)/10 = 5.5
  • 118. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. AVERAGE CASE TIME FOR LINEAR SEARCH Generalize for array size n. Expression for average-case running time: (1+2+…+n)/n = n(n+1)/2n = (n+1)/2 Therefore, average case time complexity for serial search is O(n).
  • 119. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. BINARY SEARCH • Perhaps we can do better than O(n) in the average case? • Assume that we are give an array of records that is sorted. For instance: • an array of records with integer keys sorted from smallest to largest (e.g., ID numbers), or • an array of records with string keys sorted in alphabetical order (e.g., names).
  • 120. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. BINARY SEARCH PSEUDOCODE … if(size == 0) found = false; else { middle = index of approximate midpoint of array segment; if(target == a[middle]) target has been found! else if(target < a[middle]) search for target in area before midpoint; else search for target in area after midpoint; } …
  • 121. BINARY SEARCH [ 0 ] [ 1 ] Example: sorted array of integer keys. Target=7. 3 6 7 11 32 33 53 [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ]
  • 122. BINARY SEARCH [ 0 ] [ 1 ] Example: sorted array of integer keys. Target=7. 3 6 7 11 32 33 53 [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] Find approximate midpoint
  • 123. BINARY SEARCH [ 0 ] [ 1 ] Example: sorted array of integer keys. Target=7. 3 6 7 11 32 33 53 [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] Is 7 = midpoint key? NO.
  • 124. BINARY SEARCH [ 0 ] [ 1 ] Example: sorted array of integer keys. Target=7. 3 6 7 11 32 33 53 [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] Is 7 < midpoint key? YES.
  • 125. BINARY SEARCH [ 0 ] [ 1 ] Example: sorted array of integer keys. Target=7. 3 6 7 11 32 33 53 [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] Search for the target in the area before midpoint.
  • 126. BINARY SEARCH [ 0 ] [ 1 ] Example: sorted array of integer keys. Target=7. 3 6 7 11 32 33 53 [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] Find approximate midpoint
  • 127. BINARY SEARCH [ 0 ] [ 1 ] Example: sorted array of integer keys. Target=7. 3 6 7 11 32 33 53 [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] Target = key of midpoint? NO.
  • 128. BINARY SEARCH [ 0 ] [ 1 ] Example: sorted array of integer keys. Target=7. 3 6 7 11 32 33 53 [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] Target < key of midpoint? NO.
  • 129. BINARY SEARCH [ 0 ] [ 1 ] Example: sorted array of integer keys. Target=7. 3 6 7 11 32 33 53 [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] Target > key of midpoint? YES.
  • 130. BINARY SEARCH [ 0 ] [ 1 ] Example: sorted array of integer keys. Target=7. 3 6 7 11 32 33 53 [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] Search for the target in the area after midpoint.
  • 131. BINARY SEARCH [ 0 ] [ 1 ] Example: sorted array of integer keys. Target=7. 3 6 7 11 32 33 53 [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] Find approximate midpoint. Is target = midpoint key? YES.
  • 132. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. BINARY SEARCH IMPLEMENTATION void search(const int a[ ], size_t first, size_t size, int target, bool& found, size_t& location) { size_t middle; if(size == 0) found = false; else { middle = first + size/2; if(target == a[middle]){ location = middle; found = true; } else if (target < a[middle]) // target is less than middle, so search subarray before middle search(a, first, size/2, target, found, location); else // target is greater than middle, so search subarray after middle search(a, middle+1, (size-1)/2, target, found, location); } }
  • 133. RELATION TO BINARY SEARCH TREE Corresponding complete binary search tree 3 6 7 11 32 33 53 3 6 7 11 32 33 53 Array of previous example:
  • 134. SEARCH FOR TARGET = 7 Start at root: Find midpoint: 3 6 7 11 32 33 53 3 6 7 11 32 33 53
  • 135. Search left subarray: SEARCH FOR TARGET = 7 Search left subtree: 3 6 7 11 32 33 53 3 6 7 11 32 33 53
  • 136. Find approximate midpoint of subarray: SEARCH FOR TARGET = 7 Visit root of subtree: 3 6 7 11 32 33 53 3 6 7 11 32 33 53
  • 137. Search right subarray: SEARCH FOR TARGET = 7 Search right subtree: 3 6 7 11 32 33 53 3 6 7 11 32 33 53
  • 138. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. BINARY SEARCH: ANALYSIS • Worst case complexity? • What is the maximum depth of recursive calls in binary search as function of n? • Each level in the recursion, we split the array in half (divide by two). • Therefore maximum recursion depth is floor(log2n) and worst case = O(log2n). • Average case is also = O(log2n).
  • 139. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. DEPTH-FIRST SEARCH • DFS follows the following rules: 1. Select an unvisited node x, visit it, and treat as the current node 2. Find an unvisited neighbor of the current node, visit it, and make it the new current node; 3. If the current node has no unvisited neighbors, backtrack to the its parent, and make that parent the new current node; 4. Repeat steps 3 and 4 until no more nodes can be visited. 5. If there are still unvisited nodes, repeat from step 1.
  • 140. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. 0 1 2 4 5 6 7 8 9 10 11 0 1 4 2 5 6 7 8 9 11 10 DFS Tree Graph G
  • 141. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. • Observations: • the last node visited is the first node from which to proceed. • Also, the backtracking proceeds on the basis of "last visited, first to backtrack too". • This suggests that a stack is the proper data structure to remember the current node and how to backtrack.
  • 142. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. DFS (PSEUDO CODE) DFS(input: Graph G) { Stack S; Integer x, t; while (G has an unvisited node x){ visit(x); push(x,S); while (S is not empty){ t := peek(S); if (t has an unvisited neighbor y){ visit(y); push(y,S); } else pop(S); } } }
  • 143. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. BREADTH-FIRST SEARCH • BFS follows the following rules: 1. Select an unvisited node x, visit it, have it be the root in a BFS tree being formed. Its level is called the current level. 2. From each node z in the current level, in the order in which the level nodes were visited, visit all the unvisited neighbors of z. The newly visited nodes from this level form a new level that becomes the next current level. 3. Repeat step 2 until no more nodes can be visited. 4. If there are still unvisited nodes, repeat from Step 1.
  • 144. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. 0 1 2 4 5 6 7 8 9 10 11 0 1 4 2 5 6 7 8 9 11 10 BFS Tree Graph G
  • 145. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. BFS (PSEUDO CODE) BFS(input: graph G) { Queue Q; Integer x, z, y; while (G has an unvisited node x) { visit(x); Enqueue(x,Q); while (Q is not empty){ z := Dequeue(Q); for all (unvisited neighbor y of z){ visit(y); Enqueue(y,Q); } } } }
  • 147. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. SORTING • Sorting takes an unordered collection and makes it an ordered one. 5 12 35 42 77 101 1 2 3 4 5 6 5 12 35 42 77 101 1 2 3 4 5 6
  • 148. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. "BUBBLING UP" THE LARGEST ELEMENT • Traverse a collection of elements • Move from the front to the end • “Bubble” the largest value to the end using pair-wise comparisons and swapping 5 12 35 42 77 101 1 2 3 4 5 6
  • 149. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. "BUBBLING UP" THE LARGEST ELEMENT • Traverse a collection of elements • Move from the front to the end • “Bubble” the largest value to the end using pair-wise comparisons and swapping 5 12 35 42 77 101 1 2 3 4 5 6 Swap 42 77
  • 150. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. "BUBBLING UP" THE LARGEST ELEMENT • Traverse a collection of elements • Move from the front to the end • “Bubble” the largest value to the end using pair-wise comparisons and swapping 5 12 35 77 42 101 1 2 3 4 5 6 Swap 35 77
  • 151. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. "BUBBLING UP" THE LARGEST ELEMENT • Traverse a collection of elements • Move from the front to the end • “Bubble” the largest value to the end using pair-wise comparisons and swapping 5 12 77 35 42 101 1 2 3 4 5 6 Swap 12 77
  • 152. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. "BUBBLING UP" THE LARGEST ELEMENT • Traverse a collection of elements • Move from the front to the end • “Bubble” the largest value to the end using pair-wise comparisons and swapping 5 77 12 35 42 101 1 2 3 4 5 6 No need to swap
  • 153. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. "BUBBLING UP" THE LARGEST ELEMENT • Traverse a collection of elements • Move from the front to the end • “Bubble” the largest value to the end using pair-wise comparisons and swapping 5 77 12 35 42 101 1 2 3 4 5 6 Swap 5 101
  • 154. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. "BUBBLING UP" THE LARGEST ELEMENT • Traverse a collection of elements • Move from the front to the end • “Bubble” the largest value to the end using pair-wise comparisons and swapping 77 12 35 42 5 1 2 3 4 5 6 101 Largest value correctly placed
  • 155. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. BUBBLE SORT ALGORITHM index <- 1 last_compare_at <- n – 1 loop exitif(index > last_compare_at) if(A[index] > A[index + 1]) then Swap(A[index], A[index + 1]) endif index <- index + 1 endloop
  • 156. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. NO, SWAP ISN’T BUILT IN. Procedure Swap(a, b isoftype in/out Num) t isoftype Num t <- a a <- b b <- t endprocedure // Swap LB
  • 157. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. ITEMS OF INTEREST • Notice that only the largest value is correctly placed • All other values are still out of order • So we need to repeat this process 77 12 35 42 5 1 2 3 4 5 6 101 Largest value correctly placed
  • 158. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. REPEAT “BUBBLE UP” HOW MANY TIMES? • If we have N elements… • And if each time we bubble an element, we place it in its correct location… • Then we repeat the “bubble up” process N – 1 times. • This guarantees we’ll correctly place all N elements.
  • 159. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. “BUBBLING” ALL THE ELEMENTS 77 12 35 42 5 1 2 3 4 5 6 101 5 42 12 35 77 1 2 3 4 5 6 101 42 5 35 12 77 1 2 3 4 5 6 101 42 35 5 12 77 1 2 3 4 5 6 101 42 35 12 5 77 1 2 3 4 5 6 101 N - 1
  • 160. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. REDUCING THE NUMBER OF COMPARISONS 12 35 42 77 101 1 2 3 4 5 6 5 77 12 35 42 5 1 2 3 4 5 6 101 5 42 12 35 77 1 2 3 4 5 6 101 42 5 35 12 77 1 2 3 4 5 6 101 42 35 5 12 77 1 2 3 4 5 6 101
  • 161. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. REDUCING THE NUMBER OF COMPARISONS • On the Nth “bubble up”, we only need to do MAX-N comparisons. • For example: • This is the 4th “bubble up” • MAX is 6 • Thus we have 2 comparisons to do 42 5 35 12 77 1 2 3 4 5 6 101
  • 162. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. N is … // Size of Array Arr_Type definesa Array[1..N] of Num Procedure Swap(n1, n2 isoftype in/out Num) temp isoftype Num temp <- n1 n1 <- n2 n2 <- temp endprocedure // Swap
  • 163. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. procedure Bubblesort(A isoftype in/out Arr_Type) to_do, index isoftype Num to_do <- N – 1 loop exitif(to_do = 0) index <- 1 loop exitif(index > to_do) if(A[index] > A[index + 1]) then Swap(A[index], A[index + 1]) endif index <- index + 1 endloop to_do <- to_do - 1 endloop endprocedure // Bubblesort Inner loop Outer loop
  • 164. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. ALREADY SORTED COLLECTIONS? • What if the collection was already sorted? • What if only a few elements were out of place and after a couple of “bubble ups,” the collection was sorted? • We want to be able to detect this and “stop early”! 42 35 12 5 77 1 2 3 4 5 6 101
  • 165. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. USING A BOOLEAN “FLAG” • We can use a boolean variable to determine if any swapping occurred during the “bubble up.” • If no swapping occurred, then we know that the collection is already sorted! • This boolean “flag” needs to be reset after each “bubble up.”
  • 166. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. did_swap isoftype Boolean did_swap <- true loop exitif ((to_do = 0) OR NOT(did_swap)) index <- 1 did_swap <- false loop exitif(index > to_do) if(A[index] > A[index + 1]) then Swap(A[index], A[index + 1]) did_swap <- true endif index <- index + 1 endloop to_do <- to_do - 1 endloop
  • 167. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. AN ANIMATED EXAMPLE 67 45 23 14 6 33 98 42 1 2 3 4 5 6 7 8 to_do index 7 N 8 did_swap true
  • 168. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. 67 45 23 14 6 33 98 42 1 2 3 4 5 6 7 8 to_do index 7 1 N 8 did_swap false
  • 169. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. 67 45 23 14 6 33 98 42 1 2 3 4 5 6 7 8 to_do index 7 1 N 8 Swap did_swap false
  • 170. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. 67 45 98 14 6 33 23 42 1 2 3 4 5 6 7 8 to_do index 7 1 N 8 Swap did_swap true
  • 171. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. 67 45 98 14 6 33 23 42 1 2 3 4 5 6 7 8 to_do index 7 2 N 8 did_swap true
  • 172. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. 67 45 98 14 6 33 23 42 1 2 3 4 5 6 7 8 to_do index 7 2 N 8 Swap did_swap true
  • 173. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. 67 98 45 14 6 33 23 42 1 2 3 4 5 6 7 8 to_do index 7 2 N 8 Swap did_swap true
  • 174. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. 67 98 45 14 6 33 23 42 1 2 3 4 5 6 7 8 to_do index 7 3 N 8 did_swap true
  • 175. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. 67 98 45 14 6 33 23 42 1 2 3 4 5 6 7 8 to_do index 7 3 N 8 Swap did_swap true
  • 176. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. 67 14 45 98 6 33 23 42 1 2 3 4 5 6 7 8 to_do index 7 3 N 8 Swap did_swap true
  • 177. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. 67 14 45 98 6 33 23 42 1 2 3 4 5 6 7 8 to_do index 7 4 N 8 did_swap true
  • 178. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. 67 14 45 98 6 33 23 42 1 2 3 4 5 6 7 8 to_do index 7 4 N 8 Swap did_swap true
  • 179. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. 67 14 45 6 98 33 23 42 1 2 3 4 5 6 7 8 to_do index 7 4 N 8 Swap did_swap true
  • 180. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. 67 14 45 6 98 33 23 42 1 2 3 4 5 6 7 8 to_do index 7 5 N 8 did_swap true
  • 181. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. 67 14 45 6 98 33 23 42 1 2 3 4 5 6 7 8 to_do index 7 5 N 8 Swap did_swap true
  • 182. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. 98 14 45 6 67 33 23 42 1 2 3 4 5 6 7 8 to_do index 7 5 N 8 Swap did_swap true
  • 183. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. 98 14 45 6 67 33 23 42 1 2 3 4 5 6 7 8 to_do index 7 6 N 8 did_swap true
  • 184. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. 98 14 45 6 67 33 23 42 1 2 3 4 5 6 7 8 to_do index 7 6 N 8 Swap did_swap true
  • 185. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. 33 14 45 6 67 98 23 42 1 2 3 4 5 6 7 8 to_do index 7 6 N 8 Swap did_swap true
  • 186. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. 33 14 45 6 67 98 23 42 1 2 3 4 5 6 7 8 to_do index 7 7 N 8 did_swap true
  • 187. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. 33 14 45 6 67 98 23 42 1 2 3 4 5 6 7 8 to_do index 7 7 N 8 Swap did_swap true
  • 188. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. 33 14 45 6 67 42 23 98 1 2 3 4 5 6 7 8 to_do index 7 7 N 8 Swap did_swap true
  • 189. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. AFTER FIRST PASS OF OUTER LOOP 33 14 45 6 67 42 23 98 1 2 3 4 5 6 7 8 to_do index 7 8 N 8 Finished first “Bubble Up” did_swap true
  • 190. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. THE SECOND “BUBBLE UP” 33 14 45 6 67 42 23 98 1 2 3 4 5 6 7 8 to_do index 6 1 N 8 did_swap false
  • 191. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. THE SECOND “BUBBLE UP” 33 14 45 6 67 42 23 98 1 2 3 4 5 6 7 8 to_do index 6 1 N 8 did_swap false No Swap
  • 192. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. THE SECOND “BUBBLE UP” 33 14 45 6 67 42 23 98 1 2 3 4 5 6 7 8 to_do index 6 2 N 8 did_swap false
  • 193. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. THE SECOND “BUBBLE UP” 33 14 45 6 67 42 23 98 1 2 3 4 5 6 7 8 to_do index 6 2 N 8 did_swap false Swap
  • 194. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. THE SECOND “BUBBLE UP” 33 45 14 6 67 42 23 98 1 2 3 4 5 6 7 8 to_do index 6 2 N 8 did_swap true Swap
  • 195. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. THE SECOND “BUBBLE UP” 33 45 14 6 67 42 23 98 1 2 3 4 5 6 7 8 to_do index 6 3 N 8 did_swap true
  • 196. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. THE SECOND “BUBBLE UP” 33 45 14 6 67 42 23 98 1 2 3 4 5 6 7 8 to_do index 6 3 N 8 did_swap true Swap
  • 197. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. THE SECOND “BUBBLE UP” 33 6 14 45 67 42 23 98 1 2 3 4 5 6 7 8 to_do index 6 3 N 8 did_swap true Swap
  • 198. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. THE SECOND “BUBBLE UP” 33 6 14 45 67 42 23 98 1 2 3 4 5 6 7 8 to_do index 6 4 N 8 did_swap true
  • 199. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. THE SECOND “BUBBLE UP” 33 6 14 45 67 42 23 98 1 2 3 4 5 6 7 8 to_do index 6 4 N 8 did_swap true No Swap
  • 200. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. THE SECOND “BUBBLE UP” 33 6 14 45 67 42 23 98 1 2 3 4 5 6 7 8 to_do index 6 5 N 8 did_swap true
  • 201. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. THE SECOND “BUBBLE UP” 33 6 14 45 67 42 23 98 1 2 3 4 5 6 7 8 to_do index 6 5 N 8 did_swap true Swap
  • 202. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. THE SECOND “BUBBLE UP” 67 6 14 45 33 42 23 98 1 2 3 4 5 6 7 8 to_do index 6 5 N 8 did_swap true Swap
  • 203. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. THE SECOND “BUBBLE UP” 67 6 14 45 33 42 23 98 1 2 3 4 5 6 7 8 to_do index 6 6 N 8 did_swap true
  • 204. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. THE SECOND “BUBBLE UP” 67 6 14 45 33 42 23 98 1 2 3 4 5 6 7 8 to_do index 6 6 N 8 did_swap true Swap
  • 205. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. THE SECOND “BUBBLE UP” 42 6 14 45 33 67 23 98 1 2 3 4 5 6 7 8 to_do index 6 6 N 8 did_swap true Swap
  • 206. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. AFTER SECOND PASS OF OUTER LOOP 42 6 14 45 33 67 23 98 1 2 3 4 5 6 7 8 to_do index 6 7 N 8 did_swap true Finished second “Bubble Up”
  • 207. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. THE THIRD “BUBBLE UP” 42 6 14 45 33 67 23 98 1 2 3 4 5 6 7 8 to_do index 5 1 N 8 did_swap false
  • 208. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. THE THIRD “BUBBLE UP” 42 6 14 45 33 67 23 98 1 2 3 4 5 6 7 8 to_do index 5 1 N 8 did_swap false Swap
  • 209. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. THE THIRD “BUBBLE UP” 42 6 23 45 33 67 14 98 1 2 3 4 5 6 7 8 to_do index 5 1 N 8 did_swap true Swap
  • 210. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. THE THIRD “BUBBLE UP” 42 6 23 45 33 67 14 98 1 2 3 4 5 6 7 8 to_do index 5 2 N 8 did_swap true
  • 211. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. THE THIRD “BUBBLE UP” 42 6 23 45 33 67 14 98 1 2 3 4 5 6 7 8 to_do index 5 2 N 8 did_swap true Swap
  • 212. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. THE THIRD “BUBBLE UP” 42 23 6 45 33 67 14 98 1 2 3 4 5 6 7 8 to_do index 5 2 N 8 did_swap true Swap
  • 213. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. THE THIRD “BUBBLE UP” 42 23 6 45 33 67 14 98 1 2 3 4 5 6 7 8 to_do index 5 3 N 8 did_swap true
  • 214. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. THE THIRD “BUBBLE UP” 42 23 6 45 33 67 14 98 1 2 3 4 5 6 7 8 to_do index 5 3 N 8 did_swap true No Swap
  • 215. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. THE THIRD “BUBBLE UP” 42 23 6 45 33 67 14 98 1 2 3 4 5 6 7 8 to_do index 5 4 N 8 did_swap true
  • 216. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. THE THIRD “BUBBLE UP” 42 23 6 45 33 67 14 98 1 2 3 4 5 6 7 8 to_do index 5 4 N 8 did_swap true Swap
  • 217. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. THE THIRD “BUBBLE UP” 42 23 6 33 45 67 14 98 1 2 3 4 5 6 7 8 to_do index 5 4 N 8 did_swap true Swap
  • 218. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. THE THIRD “BUBBLE UP” 42 23 6 33 45 67 14 98 1 2 3 4 5 6 7 8 to_do index 5 5 N 8 did_swap true
  • 219. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. THE THIRD “BUBBLE UP” 42 23 6 33 45 67 14 98 1 2 3 4 5 6 7 8 to_do index 5 5 N 8 did_swap true Swap
  • 220. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. THE THIRD “BUBBLE UP” 45 23 6 33 42 67 14 98 1 2 3 4 5 6 7 8 to_do index 5 5 N 8 did_swap true Swap
  • 221. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. AFTER THIRD PASS OF OUTER LOOP 45 23 6 33 42 67 14 98 1 2 3 4 5 6 7 8 to_do index 5 6 N 8 did_swap true Finished third “Bubble Up”
  • 222. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. THE FOURTH “BUBBLE UP” 45 23 6 33 42 67 14 98 1 2 3 4 5 6 7 8 to_do index 4 1 N 8 did_swap false
  • 223. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. THE FOURTH “BUBBLE UP” 45 23 6 33 42 67 14 98 1 2 3 4 5 6 7 8 to_do index 4 1 N 8 did_swap false Swap
  • 224. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. THE FOURTH “BUBBLE UP” 45 23 14 33 42 67 6 98 1 2 3 4 5 6 7 8 to_do index 4 1 N 8 did_swap true Swap
  • 225. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. THE FOURTH “BUBBLE UP” 45 23 14 33 42 67 6 98 1 2 3 4 5 6 7 8 to_do index 4 2 N 8 did_swap true
  • 226. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. THE FOURTH “BUBBLE UP” 45 23 14 33 42 67 6 98 1 2 3 4 5 6 7 8 to_do index 4 2 N 8 did_swap true No Swap
  • 227. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. THE FOURTH “BUBBLE UP” 45 23 14 33 42 67 6 98 1 2 3 4 5 6 7 8 to_do index 4 3 N 8 did_swap true
  • 228. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. THE FOURTH “BUBBLE UP” 45 23 14 33 42 67 6 98 1 2 3 4 5 6 7 8 to_do index 4 3 N 8 did_swap true No Swap
  • 229. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. THE FOURTH “BUBBLE UP” 45 23 14 33 42 67 6 98 1 2 3 4 5 6 7 8 to_do index 4 4 N 8 did_swap true
  • 230. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. THE FOURTH “BUBBLE UP” 45 23 14 33 42 67 6 98 1 2 3 4 5 6 7 8 to_do index 4 4 N 8 did_swap true No Swap
  • 231. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. AFTER FOURTH PASS OF OUTER LOOP 45 23 14 33 42 67 6 98 1 2 3 4 5 6 7 8 to_do index 4 5 N 8 did_swap true Finished fourth “Bubble Up”
  • 232. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. THE FIFTH “BUBBLE UP” 45 23 14 33 42 67 6 98 1 2 3 4 5 6 7 8 to_do index 3 1 N 8 did_swap false
  • 233. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. THE FIFTH “BUBBLE UP” 45 23 14 33 42 67 6 98 1 2 3 4 5 6 7 8 to_do index 3 1 N 8 did_swap false No Swap
  • 234. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. THE FIFTH “BUBBLE UP” 45 23 14 33 42 67 6 98 1 2 3 4 5 6 7 8 to_do index 3 2 N 8 did_swap false
  • 235. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. THE FIFTH “BUBBLE UP” 45 23 14 33 42 67 6 98 1 2 3 4 5 6 7 8 to_do index 3 2 N 8 did_swap false No Swap
  • 236. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. THE FIFTH “BUBBLE UP” 45 23 14 33 42 67 6 98 1 2 3 4 5 6 7 8 to_do index 3 3 N 8 did_swap false
  • 237. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. THE FIFTH “BUBBLE UP” 45 23 14 33 42 67 6 98 1 2 3 4 5 6 7 8 to_do index 3 3 N 8 did_swap false No Swap
  • 238. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. AFTER FIFTH PASS OF OUTER LOOP 45 23 14 33 42 67 6 98 1 2 3 4 5 6 7 8 to_do index 3 4 N 8 did_swap false Finished fifth “Bubble Up”
  • 239. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. FINISHED “EARLY” 45 23 14 33 42 67 6 98 1 2 3 4 5 6 7 8 to_do index 3 4 N 8 did_swap false We didn’t do any swapping, so all of the other elements must be correctly placed. We can “skip” the last two passes of the outer loop.
  • 240. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. SORTING • INSERTION SORT • SHELL SORT • MERGE SORT • QUICK SORT • HEAP SORT
  • 241. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. INSERTION • Insertion sort is a simple sorting algorithm that works similar to the way you sort playing cards in your hands. The array is virtually split into a sorted and an unsorted part. Values from the unsorted part are picked and placed at the correct position in the sorted part. Algorithm To sort an array of size n in ascending order: 1: Iterate from arr[1] to arr[n] over the array. 2: Compare the current element (key) to its predecessor. 3: If the key element is smaller than its predecessor, compare it to the elements before. Move the greater elements one position up to make space for the swapped element.
  • 242. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. INSERTION SORT-EXAMPLE
  • 243. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. SELECTION • The selection sort algorithm sorts an array by repeatedly finding the minimum element (considering ascending order) from unsorted part and putting it at the beginning. • The algorithm maintains two subarrays in a given array. 1) The subarray which is already sorted. 2) Remaining subarray which is unsorted. In every iteration of selection sort, the minimum element (considering ascending order) from the unsorted subarray is picked and moved to the sorted subarray.
  • 244. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. SELECTION SORT-EXAMPLE
  • 245. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. EXAMPLE
  • 246. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. SHELL • Shell sort is a highly efficient sorting algorithm and is based on insertion sort algorithm. • This algorithm avoids large shifts as in case of insertion sort, if the smaller value is to the far right and has to be moved to the far left.
  • 247. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. SHELL SORT • ShellSort is mainly a variation of Insertion Sort. In insertion sort, we move elements only one position ahead. When an element has to be moved far ahead, many movements are involved. • The idea of shellSort is to allow exchange of far items. In shellSort, we make the array h-sorted for a large value of h. We keep reducing the value of h until it becomes 1. An array is said to be h-sorted if all sublists of every h’th element is sorted.
  • 248. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. EXAMPLE-SHELL SORT
  • 249. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. MERGE • Like QuickSort, 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. • The merge() function is used for merging two halves. • The merge(arr, l, m, r) is a key process that assumes that arr[l..m] and arr[m+1..r] are sorted and merges the two sorted sub-arrays into one.
  • 250. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. MERGE SORT
  • 251. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. QUICK • Like Merge Sort, QuickSort is a Divide and Conquer algorithm. It picks an element as pivot and partitions the given array around the picked pivot. There are many different versions of quickSort that pick pivot in different ways. 1.Always pick first element as pivot. 2.Always pick last element as pivot. 3.Pick a random element as pivot. 4.Pick median as pivot.
  • 252. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. QUICK SORT • The key process in quickSort is partition(). • Target of partitions is, given an array and an element x of array as pivot, put x at its correct position in sorted array and put all smaller elements (smaller than x) before x, and put all greater elements (greater than x) after x. • All this should be done in linear time.
  • 253. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. QUICK SORT-EXAMPLE
  • 254. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. HEAP Definitions of heap: • A large area of memory from which the programmer can allocate blocks as needed, and deallocate them (or allow them to be garbage collected) when no longer needed • A balanced, left-justified binary tree in which no node has a value greater than the value in its parent • These two definitions have little in common • Heapsort uses the second definition
  • 255. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. BALANCED BINARY TREES • Recall: • The depth of a node is its distance from the root • The depth of a tree is the depth of the deepest node • A binary tree of depth n is balanced if all the nodes at depths 0 through n-2 have two children Balanced Balanced Not balanced
  • 256. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. LEFT-JUSTIFIED BINARY TREES • A balanced binary tree is left-justified if: • all the leaves are at the same depth, or • all the leaves at depth n+1 are to the left of all the nodes at depth n Left-justified Not left-justified Not balanced
  • 257. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. PLAN OF OUTBREAK • First, we will learn how to turn a binary tree into a heap • Next, we will learn how to turn a binary tree back into a heap after it has been changed in a certain way • Finally (this is the cool part) we will see how to use these ideas to sort an array
  • 258. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. HEAP • All leaf nodes automatically have the heap property • A binary tree is a heap if all nodes in it have the heap property 12 8 3 Blue node has heap property 12 8 12 Blue node has heap property 12 8 14 Blue node does not have heap property
  • 259. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. SIFTUP • Given a node that does not have the heap property, you can give it the heap property by exchanging its value with the value of the larger child 12 8 14 Blue node does not have heap property 14 8 12 Blue node has heap property • This is sometimes called sifting up Notice that the child may have lost the heap property
  • 260. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. CONSTRUCTING A HEAP I • A tree consisting of a single node is automatically a heap • We construct a heap by adding nodes one at a time: • Add the node just to the right of the rightmost node in the deepest level • If the deepest level is full, start a new level • Examples: Add a new node here Add a new node here
  • 261. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. CONSTRUCTING A HEAP II • Each time we add a node, we may destroy the heap property of its parent node • To fix this, we sift up • But each time we sift up, the value of the topmost node in the sift may increase, and this may destroy the heap property of its parent node • We repeat the sifting up process, moving up in the tree, until either • We reach nodes whose values don’t need to be swapped (because the parent is still larger than both children), or • We reach the root
  • 262. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. CONSTRUCTING A HEAP III • A 3-ary max heap is like a binary max heap, but instead of 2 children, nodes have 3 children. • A 3-ary heap can be represented by an array as follows: The root is stored in the first location, a[0], nodes in the next level, from left to right, is stored from a[1] to a[3]. • The nodes from the second level of the tree from left to right are stored from a[4] location onward. An item x can be inserted into a 3-ary heap containing n items by placing x in the location a[n] and pushing it up the tree to satisfy the heap property. •
  • 263. SRM INSTITUTE OF SCIENCE AND TECHNOLOGY, CHENNAI. A SAMPLE HEAP 19 14 18 22 3 21 14 11 9 15 25 17 22