0% found this document useful (0 votes)
28 views2 pages

Feature

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views2 pages

Feature

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Feature Greedy Approach Dynamic Programming

Guarantees an optimal solution if the


May not always provide an optimal
problem exhibits the principle of
solution.
Optimality optimality.

Subproblem Does not reuse solutions to Reuses solutions to overlapping


Reuse subproblems. subproblems.

May involve backtracking, especially in


Does not involve backtracking.
Backtracking top-down implementations.

Typically simpler and faster to May be more complex and slower to


Complexity implement. implement.

Suitable for problems where local


Suitable for problems with overlapping
optimization leads to global
subproblems and optimal substructure.
Application optimization.

Minimum Spanning Tree, Shortest Fibonacci sequence, Huffman,


Examples Path algorithms. BFA Knapsack TSP

Feature Graph Tree

A collection of nodes (vertices) A hierarchical data structure consisting


and edges, where edges of nodes connected by edges with a
Definition connect nodes. single root node.

No cycles; connected structure with


Can have cycles (loops) and
exactly one path between any two
disconnected components.
Structure nodes.

No root node; nodes may have


Has a designated root node that has no
multiple parents or no parents at
parent.
Root Node all.

Node Relationships between nodes Parent-child relationship; each node


Relationship are arbitrary. (except the root) has exactly one parent.

Each node can have any If there is n nodes then there would
Edges number of edges. be n-1 number of edges

Traversal can be complex due to


Traversal is straightforward and can be
Traversal cycles and disconnected
done in linear time.
Complexity components.
Parameters BFS DFS

BFS stands for Breadth First


DFS stands for Depth First Search.
Stands for Search.

BFS(Breadth First Search) uses


DFS(Depth First Search) uses Stack data
Data Queue data structure for finding
structure.
Structure the shortest path.

DFS is also a traversal approach in which


BFS is a traversal approach in
the traverse begins at the root node and
which we first walk through all
proceeds through the nodes as far as
nodes on the same level before
possible until we reach the node with no
moving on to the next level.
Definition unvisited nearby nodes.

Conceptual BFS builds the tree level by


DFS builds the tree sub-tree by sub-tree.
Difference level.

Approach It works on the concept of FIFO It works on the concept of LIFO (Last In
used (First In First Out). First Out).

BFS is more suitable for


DFS is more suitable when there are
searching vertices closer to the
solutions away from source.
Suitable for given source.

BFS is used in various DFS is used in various applications such


applications such as bipartite as acyclic graphs and finding strongly
Applications graphs, shortest paths, etc. connected components etc

Recursion is technique used in computer science to solve big problems by breaking them into
smaller, similar problems. The process in which a function calls itself directly or indirectly is
called recursion and the corresponding function is called a recursive function. Using a recursive
algorithm, certain problems can be solved quite easily.
What is a Recursive Algorithm?
A recursive algorithm is an algorithm that uses recursion to solve a problem. Recursive
algorithms typically have two parts:
1. Base case: Which is a condition that stops the recursion.
2. Recursive case: Which is a call to the function itself with a smaller version of the problem.
Types of Recursion
There are several different recursion types and terms. These include:
 Direct recursion: This is typified by the factorial implementation where the methods call
itself.
 In-Direct recursion: This happens where one method, say method A, calls another
method B, which then calls method A. This involves two or more methods that eventually
create a circular call sequence.
 Head recursion: The recursive call is made at the beginning of the method.
 Tail recursion: The recursive call is the last statement.
Example 1: Factorial: The factorial of a number n is the product of all the integers from 1 to n.
The factorial of n can be defined recursively as:
factorial(n) = n * factorial(n-1)

You might also like