Professional Documents
Culture Documents
A) What Is An Algorithm? Write The Properties of The Algorithm
A) What Is An Algorithm? Write The Properties of The Algorithm
Properties of Algorithm:
It should terminate after a finite time.
It should produce at least one output.
It should take zero or more input.
It should be deterministic means giving the same output for the same input
case.
Every step in the algorithm must be effective i.e. every step should do some
work.
In a Perfect Binary Tree, the number of leaf nodes is the number of internal
nodes plus 1
L = I + 1 Where L = Number of leaf nodes, I = Number of internal nodes.
A Perfect Binary Tree of height h (where the height of the binary tree is the
number of edges in the longest path from the root node to any leaf node in
the tree, height of root node is 0) has 2 h+1 – 1 node.
An example of a Perfect binary tree is ancestors in the family. Keep a person
at root, parents as children, parents of parents as their children.
It is a type of binary tree in which the difference between the height of the left
and the right subtree for each node is either 0 or 1. In the figure above, the
root node having a value 0 is unbalanced with a depth of 2 units.
i] Sets:-
Sets, in the context of data structures and algorithms, refer to a fundamental abstract
data type that represents a collection of unique elements with no specific order. The
set data structure is designed to store elements such that each element occurs only
once within the set. Sets are widely used in various algorithms and applications due
to their ability to efficiently handle unique collections of data and perform set
operations.
Key Characteristics of Sets in Data Structures and Algorithms:
1. Uniqueness: Sets contain only unique elements. If an element is added to a set and
it already exists in the set, the set structure ensures that only one instance of the
element is stored.
2. No Specific Order: Unlike sequences or lists, sets do not impose any specific order
on the elements they contain. The order of elements within a set is arbitrary and
does not affect the set itself.
3. Membership Testing: Sets support efficient membership testing, which allows for
determining whether a given element belongs to the set or not. This operation is
typically performed in constant time (1)O(1), making sets suitable for quickly
checking the presence of elements.
4. Set Operations: Sets support various operations for performing set manipulations
and comparisons. Some of the common set operations include:
Union: Combining the elements of two sets to form a new set containing all
unique elements from both sets.
Intersection: Finding the common elements between two sets to form a new
set.
Difference: Obtaining the elements that are present in one set but not in
another set.
Subset: Checking if one set is a subset of another set.
Superset: Checking if one set contains all the elements of another set.
5. Dynamic Size: Sets can dynamically resize to accommodate a varying number of
elements. As elements are added or removed, the size of the set adjusts accordingly
to maintain efficiency in terms of memory usage and performance.
Data Deduplication: Sets are used to remove duplicate elements from data
collections efficiently.
Database Systems: Sets are utilized to represent relationships and ensure data
integrity in database management systems.
Graph Algorithms: Sets are employed in graph algorithms to represent sets of
vertices, edges, or other graph components.
Text Processing: Sets are used to perform operations such as finding unique words
in a document or determining common elements in multiple documents.
ii] Disjoint:-
the concept of disjoint primarily refers to sets or data structures that do not share
any common elements. Disjoint sets play a crucial role in various algorithms and data
structures, especially when dealing with relationships between elements, identifying
connectivity in graphs, or managing collections of elements efficiently.
44 33 11 55 77 90 40 60 99 22 88
1. 44 33 11 55 77 90 40 60 99 22 88
Let 44 be the Pivot element and scanning done from right to left
Comparing 44 to the right-side elements, and if right-side elements
are smaller than 44, then swap it. As 22 is smaller than 44 so swap them.
22 33 11 55 77 90 40 60 99 44 88
Now comparing 44 to the left side element and the element must be greater than
44 then swap them. As 55 are greater than 44 so swap them.
22 33 11 44 77 90 40 60 99 55 88
Recursively, repeating steps 1 & steps 2 until we get two lists one left from pivot
element 44 & one right from pivot element.
22 33 11 40 77 90 44 60 99 55 88
22 33 11 40 44 90 77 60 99 55 88
Now, the element on the right side and left side are greater than and smaller
than 44 respectively.
And these sublists are sorted under the same process as above done.
Step 1 - First, we have to choose a vertex from the above graph. Let's choose B.
Step 2 - Now, we have to choose and add the shortest edge from vertex B. There are
two edges from vertex B that are B to C with weight 10 and edge B to D with weight
4. Among the edges, the edge BD has the minimum weight. So, add it to the MST.
Step 3 - Now, again, choose the edge with the minimum weight among all the other
edges. In this case, the edges DE and CD are such edges. Add them to MST and
explore the adjacent of C, i.e., E and A. So, select the edge DE and add it to the MST.
Step 4 - Now, select the edge CD, and add it to the MST.
Step 5 - Now, choose the edge CA. Here, we cannot select the edge CE as it would
create a cycle to the graph. So, choose the edge CA and add it to the MST.
So, the graph produced in step 5 is the minimum spanning tree of the given graph.
The cost of the MST is given below -
Algorithm
1. Step 1: Select a starting vertex
2. Step 2: Repeat Steps 3 and 4 until there are fringe vertices
3. Step 3: Select an edge 'e' connecting the tree vertex and fringe vertex that has minim
um weight
4. Step 4: Add the selected edge and the vertex to the minimum spanning tree T
5. [END OF LOOP]
6. Step 5: EXIT
Data structure used for the minimum edge weight Time Complexity
o Time Complexity
Prim's algorithm can be simply implemented by using the adjacency matrix or
adjacency list graph representation, and to add the edge with the minimum weight
requires the linearly searching of an array of weights. It requires O(|V| 2) running time.
It can be improved further by using the implementation of heap to find the minimum
weight edges in the inner loop of the algorithm.
The time complexity of the prim's algorithm is O(E logV) or O(V logV), where E is the
no. of edges, and V is the no. of vertices.
1. Initialize: Begin with all vertices being isolated, forming single-vertex trees.
2. Sort Edges: Sort all the edges of the graph in non-decreasing order of their weights.
3. Iterate through Edges: Starting from the edge with the smallest weight, iterate
through each edge.
4. Check for Cycle: For each edge, check if adding it to the spanning tree creates a
cycle. If adding the edge does not create a cycle, include it in the MST.
5. Union-Find Data Structure: Use a union-find data structure to efficiently determine
whether adding an edge creates a cycle. This data structure maintains disjoint sets of
vertices and efficiently merges them while checking for connectivity.
6. Repeat: Repeat steps 4 and 5 until the MST contains V - 1 edges, where V is the
number of vertices in the graph.
ADVERTISEMENT
ADVERTISEMENT
The weight of the edges of the above graph is given in the below table -
Edge AB AC AD AE BC CD DE
Weight 1 7 10 5 3 4 2
Now, sort the edges given above in the ascending order of their weights.
Edge AB DE BC CD AE AC AD
Weight 1 2 3 4 5 7 10
Step 3 - Add the edge BC with weight 3 to the MST, as it is not creating any cycle or
loop.
Step 4 - Now, pick the edge CD with weight 4 to the MST, as it is not forming the
cycle.
ADVERTISEMENT
Step 5 - After that, pick the edge AE with weight 5. Including this edge will create
the cycle, so discard it.
Step 6 - Pick the edge AC with weight 7. Including this edge will create the cycle, so
discard it.
Step 7 - Pick the edge AD with weight 10. Including this edge will also create the
cycle, so discard it.
So, the final minimum spanning tree obtained from the given weighted graph by
using Kruskal's algorithm is -
Now, the number of edges in the above tree equals the number of vertices minus 1.
So, the algorithm stops here.
Algorithm
1. Step 1: Create a forest F in such a way that every vertex of the graph is a separate tre
e.
2. Step 2: Create a set E that contains all the edges of the graph.
3. Step 3: Repeat Steps 4 and 5 while E is NOT EMPTY and F is not spanning
4. Step 4: Remove an edge from E with minimum weight
5. Step 5: IF the edge obtained in Step 4 connects two different trees, then add it to the
forest F
6. (for combining two trees into one tree).
7. ELSE
8. Discard the edge
9. Step 6: END
Time Complexity:-
The time complexity of Kruskal's algorithm is O(E logE) or O(V logV), where E
is the no. of edges, and V is the no. of vertices.
In the Flow Shop Scheduling problem, each job consists of multiple operations that
must be performed sequentially, with each operation requiring processing on one of
the available machines. The objective is to find the optimal sequence of jobs and
operations to minimize the total completion time or another performance metric.
The problem is called "flow shop" because the jobs flow through the machines in a
fixed order, with each machine performing a specific operation on the job before
passing it to the next machine. Here are the key components and concepts
associated with the Flow Shop Scheduling algorithm:
1. Jobs: A set of tasks or jobs that need to be processed through the machines. Each
job consists of multiple operations that must be performed in a specific sequence.
2. Machines: A set of machines or workstations available to process the jobs. Each
machine is capable of performing specific operations, and jobs must be scheduled in
a way that minimizes idle time and maximizes machine utilization.
3. Processing Time: The time required to complete each operation of a job on a
specific machine. The processing times are usually predetermined and known in
advance.
4. Sequence: The sequence in which jobs are scheduled to be processed through the
machines. The goal is to find the optimal sequence that minimizes the total
completion time or makespan.
5. Objective Function: The performance metric used to evaluate the quality of the
scheduling solution. Common objective functions include minimizing the total
completion time, minimizing the maximum completion time (makespan), minimizing
the total flow time, or minimizing the total tardiness.
6. Constraints: Constraints may include precedence constraints (certain operations
must be completed before others can start), resource constraints (limited availability
of machines or workers), and job release times (jobs cannot start processing before a
certain time).
Flow Shop Scheduling algorithms can be classified into several categories, including
exact algorithms, heuristic algorithms, and metaheuristic algorithms. Some common
algorithms and techniques used to solve Flow Shop Scheduling problems include
Johnson's algorithm, Palmer's algorithm, Genetic algorithms, Simulated Annealing,
Tabu Search, and Ant Colony Optimization.
BFS algorithm
In this article, we will discuss the BFS algorithm in the data structure. Breadth-first
search is a graph traversal algorithm that starts traversing the graph from the root
node and explores all the neighboring nodes. Then, it selects the nearest node and
explores all the unexplored nodes. While using BFS for traversal, any node in the
graph can be considered as the root node.
There are many ways to traverse the graph, but among them, BFS is the most
commonly used approach. It is a recursive algorithm to search all the vertices of a
tree or graph data structure. BFS puts every vertex of the graph into two categories -
visited and non-visited. It selects a single node in a graph and, after that, visits all the
nodes adjacent to the selected node.
o BFS can be used to find the neighboring locations from a given source location.
o In a peer-to-peer network, BFS algorithm can be used as a traversal method to find
all the neighboring nodes. Most torrent clients, such as BitTorrent, uTorrent, etc.
employ this process to find "seeds" and "peers" in the network.
o BFS can be used in web crawlers to create web page indexes. It is one of the main
algorithms that can be used to index web pages. It starts traversing from the source
page and follows the links associated with the page. Here, every web page is
considered as a node in the graph.
o BFS is used to determine the shortest path and minimum spanning tree.
o BFS is also used in Cheney's technique to duplicate the garbage collection.
o It can be used in ford-Fulkerson method to compute the maximum flow in a flow
network.
Algorithm
The steps involved in the BFS algorithm to explore a graph are given as follows -
Step 4: Dequeue a node N. Process it and set its STATUS = 3 (processed state).
Step 5: Enqueue all the neighbours of N that are in the ready state (whose STATUS =
1) and set
their STATUS = 2
(waiting state)
[END OF LOOP]
Step 6: EXIT
The problem is named after the chess piece "queen," which is the most
powerful piece on the chessboard. The problem statement is as follows:
Given an N×N chessboard, place N queens on the board such that no two
queens threaten each other. This means that no two queens can share the
same row, column, or diagonal.
The N-Queens problem is not only of theoretical interest but also has
practical applications in various fields, including artificial intelligence,
constraint programming, and optimization. It serves as a benchmark
problem for evaluating and comparing different algorithms and techniques
for solving constraint satisfaction problems. Additionally, it demonstrates
the importance of problem-solving skills and algorithmic techniques in
computer science and mathematics.