DECREASE AND CONQUER
• This technique is based on exploiting the
relationship between a solution to a given
instance of a problem and a solution to a
smaller instance of the same problem.
• Three major variations:
-decrease by a constant
-decrease by a constant factor
-variable size decrease
• Decrease by a constant
• Eg: Computation of an for positive integer
• an = an-1. a
• ∴Function f(n)= an can be computed using
recursive relation
• Decrease by a Constant Factor
• Eg: Computation of an the exponential problem
• an = (an/2)2 n is a integer exponent
• Variable Size Decrease
Eg: Computing GCD(m,n) using Euclids algorithm
GCD(m,n)=GCD(n, m mod n)
Insertion Sort
• Efficiency:
• Basic operation is the Key Comparison A[j]>v
• Worst Case: If the input array is in descending
order
• Best Case: If the input array is already in
ascending order
• Average case: If the elements are randomly
ordered in an array
DFS(Depth First Search)
• DFS problem is to traverse the graph
• Method:
Complexity
• In adjacency list representation, depth-first
search visits every vertex once and checks
every edge in the graph once. Therefore, DFS
complexity is ϴ(|V| + |E|)
Breadth First Search(BFS)
• BFS is used to traverse a graph
• BFS works as follows:
• Conventions for construction of BFS forest
• BFS Forest or BFS Tree: The traversal’s starting
vertex serves as the root of the first tree in such a
forest.
• Tree Edge: Whenever a new unvisited vertex is
reached for the first time, the vertex is attached as
a child to the vertex it is being reached from with
an edge called a tree edge.
• Cross Edge: If an edge leading to a previously
visited vertex other than its immediate
predecessor (i.e., its parent in the tree) is
encountered, the edge is called as cross edge
indicated by dotted lines.
• Main facts about DFS and BFS:
DFS BFS
Data Structure Stack Queue
Number of vertex 2 Orderings 1 Orderings
orderings
Edge Types(undirected Tree and Back Edges Tree and Cross Edges
Graph)
Applications Connectivity, Acyclicity, Connectivity, Acyclicity,
Minimum Edge paths
Efficiency for Adjacency Θ(|v|)2 Θ(|v|)2
Matrix
Efficiency for Adjacency Θ(|v|+|E|) Θ(|v|+|E|)
Linked List
Topological Sorting
• A directed graph or digraph, is a graph with directions specified
for all its edges.
• The 2 ways of representing digraph are:
-adjacency matrix
- adjacency linked lists
• The differences between undirected and directed graphs in
representing them are:
(1) the adjacency matrix of a directed graph does not have to be
symmetric;
(2) an edge in a directed graph has just one (not two)
corresponding nodes in the digraph’s adjacency lists.
Traversing the digraph – BFS and DFS
• Tree Edges- ab, bc, de
• Back edges – ba
• Forward edges – ac
• Cross edges – dc
• DFS forest of a digraph that has no back edge is called dag(Directed Acyclic Graph)
• Topological Sorting for DAG is a linear ordering
of vertices such that for every directed edge
uv, vertex u comes before v in the ordering.
• Two algorithms for solving the topological
sorting problems :
- based on depth first search
- based on implementation of decrease by constant
technique or source removal method
• Eg: consider a set of five required courses {C1, C2, C3,
C4, C5} a part-time student has to take in some degree
program. The courses can be taken in any order as long
as the following course prerequisites are met:
C1 and C2 have no prerequisites,
C3 requires C1 and C2,
C4 requires C3
C5 requires C3 and C4.
The student can take only one course per term. In which
order should the student take the courses?
Source Removal Method:
• Apply the DFS-based algorithm to solve the
topological sorting problem for the following
digraphs:
Algorithm - Source Removal Method
Topological sort - complexity
Topological sort –DFS method