Unit 4 Algotihm Mca First Year

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

Unit 3 amit gupta

Backtracking Algorithm
In this tutorial, you will learn what a backtracking algorithm is. Also, you will
find an example of a backtracking approach.

A backtracking algorithm is a problem-solving algorithm that uses a brute


force approach for finding the desired output.
The term backtracking suggests that if the current solution is not suitable, then
backtrack and try other solutions. Thus, recursion is used in this approach.

This approach is used to solve problems that have multiple solutions. If you
want an optimal solution, you must go for dynamic programming.

Join our newsletter for the latest updates.


Subscribe

Backtracking Algorithm
In this tutorial, you will learn what a backtracking algorithm is. Also, you will
find an example of a backtracking approach.

A backtracking algorithm is a problem-solving algorithm that uses a brute


force approach for finding the desired output.

The Brute force approach tries out all the possible solutions and chooses the
desired/best solutions.
The term backtracking suggests that if the current solution is not suitable, then
backtrack and try other solutions. Thus, recursion is used in this approach.

This approach is used to solve problems that have multiple solutions. If you
want an optimal solution, you must go for dynamic programming.

State Space Tree

A space state tree is a tree representing all the possible states (solution or
nonsolution) of the problem from the root as an initial state to the leaf as a
terminal state.

State Space Tree

Join our newsletter for the latest updates.


Subscribe

Backtracking Algorithm
In this tutorial, you will learn what a backtracking algorithm is. Also, you will
find an example of a backtracking approach.

A backtracking algorithm is a problem-solving algorithm that uses a brute


force approach for finding the desired output.

The Brute force approach tries out all the possible solutions and chooses the
desired/best solutions.

The term backtracking suggests that if the current solution is not suitable, then
backtrack and try other solutions. Thus, recursion is used in this approach.

This approach is used to solve problems that have multiple solutions. If you
want an optimal solution, you must go for dynamic programming.

State Space Tree

A space state tree is a tree representing all the possible states (solution or
nonsolution) of the problem from the root as an initial state to the leaf as a
terminal state.
State Space Tree

Backtracking Algorithm

Backtrack(x)

if x is not a solution

return false

if x is a new solution

add to list of solutions

backtrack(expand x)

Example Backtracking Approach

Problem: You want to find all the possible ways of arranging 2 boys and 1 girl
on 3 benches. Constraint: Girl should not be on the middle bench.

Solution: There are a total of 3! = 6 possibilities. We will try all the possibilities
and get the possible solutions. We recursively try all the possibilities.
All the possibilities are:

All the possibilities

The following state space tree shows the possible solutions.

State tree with all the solutions


Unit 4
What is a Graph?
A graph consists of a finite set of vertices or nodes and a set
of edges connecting these vertices. Two vertices are said to
be adjacent if they are connected to each other by the same edge.

Some basic definitions related to graphs are given below. You can
refer to Figure 1 for examples.

 Order: The number of vertices in the graph

 Size: The number of edges in the graph

 Vertex degree: The number of edges that are incident to a


vertex

 Isolated vertex: A vertex that is not connected to any other


vertices in the graph

 Self-loop: An edge from a vertex to itself

 Directed graph: A graph where all the edges have a direction


indicating what is the start vertex and what is the end vertex

 Undirected graph: A graph with edges that have no direction

 Weighted graph: Edges of the graph has weights


 Unweighted graph: Edges of the graph has no weights

1. Breadth-first search

Traversing or searching is one of the fundamental operations


which can be performed on graphs. In breadth-first search (BFS),
we start at a particular vertex and explore all of its neighbours at
the present depth before moving on to the vertices in the next
level. Unlike trees, graphs can contain cycles (a path where the
first and last vertices are the same). Hence, we have to keep track
of the visited vertices. When implementing BFS, we use a queue
data structure.
Applications

 Used to determine the shortest paths and minimum spanning


trees.

 Used by search engine crawlers to build indexes of web pages.

 Used to search on social networks.

 Used to find available neighbour nodes in peer-to-peer


networks such as BitTorrent.
Depth-first search

In depth-first search (DFS) we start from a particular vertex and


explore as far as possible along each branch before retracing back
(backtracking). In DFS also we have to keep track of the visited
vertices. When implementing DFS, we use a stack data structure to
support backtracking.

Applications

 Used to find a path between two vertices.

 Used to detect cycles in a graph.

 Used in topological sorting.

 Used to solve puzzles having only one solution (e.g., mazes)


Algorithms

1. Prim’s algorithm

2. Kruskal’s algorithm

Applications

 Used to construct trees for broadcasting in computer networks.

 Used in graph-based cluster analysis.

 Used in image segmentation.

 Used in regionalisation of socio-geographic areas, where


regions are grouped into contiguous regions.
Algorithms

1. Prim’s algorithm

2. Kruskal’s algorithm

Applications

 Used to construct trees for broadcasting in computer networks.

 Used in graph-based cluster analysis.

 Used in image segmentation.

 Used in regionalisation of socio-geographic areas, where


regions are grouped into contiguous regions.
Dijkstra's Shortest Path Algorithm- single
Source stoutest path algorithm
One algorithm for finding the shortest path from a starting node to a target node in a
weighted graph is Dijkstra’s algorithm. The algorithm creates a tree of shortest paths from
the starting vertex, the source, to all other points in the graph.

Dijkstra’s algorithm, published in 1959 and named after its creator Dutch computer scientist
Edsger Dijkstra, can be applied on a weighted graph. The graph can either be directed or
undirected. One stipulation to using the algorithm is that the graph needs to have a
nonnegative weight on every edge

Initialize distances according to the algorithm.


Pick first node and calculate distances to adjacent nodes.

Pick next node with minimal distance; repeat adjacent node distance calculations.
Final result of shortest-path tree

You might also like