0% found this document useful (0 votes)
2 views8 pages

Algorithm

The document explains key concepts in algorithms, including time complexity, space complexity, and the definition of an algorithm. It also covers various algorithmic strategies such as divide and conquer, dynamic programming, and specific algorithms like matrix multiplication and the Traveling Salesperson Problem. Additionally, it discusses Huffman coding for data compression.

Uploaded by

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

Algorithm

The document explains key concepts in algorithms, including time complexity, space complexity, and the definition of an algorithm. It also covers various algorithmic strategies such as divide and conquer, dynamic programming, and specific algorithms like matrix multiplication and the Traveling Salesperson Problem. Additionally, it discusses Huffman coding for data compression.

Uploaded by

Md. Emon Hasan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

ALGORITHM

What Is Time Complexity?

Time complexity is defined in terms of how many times it takes to run a


given algorithm, based on the length of the input. Time complexity is not
a measurement of how much time it takes to execute a particular
algorithm because such factors as programming language, operating
system, and processing power are also considered.

What Is Space Complexity?

When an algorithm is run on a computer, it necessitates a certain amount


of memory space. The amount of memory used by a program to execute
it is represented by its space complexity. Because a program requires
memory to store input data and temporal values while running, the space
complexity is auxiliary and input space.

What is an Algorithm?

 An algorithm is a set of commands that must be followed for a


computer to perform calculations or other problem-solving
operations.
 According to its formal definition, an algorithm is a finite set of
instructions carried out in a specific order to perform a particular
task.

 It is not the entire program or code; it is simple logic to a problem


represented as an informal description in the form of a flowchart
or pseudocode.

Subarray max sum___________

#include<bits/stdc++.h>

using namespace std;

int main()

int ar[200];

int n;

cin>>n;

for(int i=0;i<n;i++) cin>>ar[i];

int ans =-10000;

vector<int>vt;

for(int i=0;i<n;i++)
{

int sum=0;

for(int j=i;j<n;j++)

sum=sum+ar[j];

if(sum>ans)

[Link]();

ans=sum;

for(int k=i;k<=j;k++)

vt.push_back(ar[k]);

cout<<ans<<endl;

for(int i=0;i<[Link]();i++) cout<<vt[i]<<' ';

[Link] is Sorting?

Sorting is the process of arranging elements either in ascending (or) descending order.

#include <iostream>

#include <vector>

// Function to perform matrix multiplication

std::vector<std::vector<int>> matrixMultiply(const std::vector<std::vector<int>>& matrix1, const


std::vector<std::vector<int>>& matrix2) {

int rows1 = [Link]();


int cols1 = matrix1[0].size();

int cols2 = matrix2[0].size();

std::vector<std::vector<int>> result(rows1, std::vector<int>(cols2, 0));

for (int i = 0; i < rows1; ++i) {

for (int j = 0; j < cols2; ++j) {

for (int k = 0; k < cols1; ++k) {

result[i][j] += matrix1[i][k] * matrix2[k][j];

return result;

// Function to print a matrix

void printMatrix(const std::vector<std::vector<int>>& matrix) {

for (const auto& row : matrix) {

for (int element : row) {

std::cout << element << " ";

std::cout << std::endl;

int main() {

// Example matrices

std::vector<std::vector<int>> matrix1 = { {1, 2, 3},

{4, 5, 6} };
std::vector<std::vector<int>> matrix2 = { {7, 8},

{9, 10},

{11, 12} };

// Multiply matrices

std::vector<std::vector<int>> result = matrixMultiply(matrix1, matrix2);

// Print the result

printMatrix(result);

return 0;

Divide and Conquer Approach: The Divide and Conquer approach is a problem-
solving strategy that involves breaking down a complex problem into smaller
subproblems, solving them independently, and then combining their solutions to
obtain the final result. It follows a recursive process that can be summarized in
three steps:

 Divide: Break the problem into smaller subproblems that are easier
to solve.
 Conquer: Solve each subproblem independently, typically through
recursive calls.
 Combine: Combine the solutions of the subproblems to obtain the
final solution to the original problem.

Dynamic Programming is an algorithmic technique used to solve optimization


problems by breaking them down into smaller overlapping subproblems and
storing the solutions to these subproblems in a table (usually a matrix or an
array). The stored solutions can be reused when needed, eliminating redundant
computations and improving efficiency.
// C++ program to print largest contiguous array sum

#include <bits/stdc++.h>

using namespace std;

int maxSubArraySum(int a[], int size)

int s=0;

int t=0;

int e=0;

int max_so_far = INT_MIN, max_ending_here = 0;

for (int i = 0; i < size; ++i)

max_ending_here = max_ending_here + a[i];

if (max_ending_here < 0)

max_ending_here = 0;

t=i+1;

// cout<<t<<endl;

if (max_so_far < max_ending_here)

max_so_far = max_ending_here;

s=t;

e=i;

// cout<<s<<' '<<t<<' '<<e<<endl;

cout<<s<<' '<<e<<endl;
return max_so_far;

// Driver Code

int main()

int a[] = { -2, 1, -3, 4, -1, 2, 1, -5, 4 };

int n = sizeof(a) / sizeof(a[0]);

// Function Call

int max_sum = maxSubArraySum(a, n);

cout << "Maximum contiguous sum is " << max_sum;

return 0;

The Traveling Salesperson Problem (TSP) is a well-known optimization


problem in computer science and operations research. It is an NP-hard
problem that seeks to find the shortest possible route or tour that visits a
set of cities exactly once and returns to the starting city.

The problem is formulated as follows: Given a list of cities and the


distances between each pair of cities, the goal is to find a tour that
minimizes the total distance traveled. The tour must visit each city exactly
once and return to the starting city.

When binary search can not give you optimal solution

Unsorted Data Missing Values Duplicate Values

Dynamic Data: Binary search is most effective when the data remains static or
changes infrequently. If the data is dynamic and frequently updated or modified,
binary search might not be the best choice. Maintaining a sorted order can
become inefficient, and other data structures like balanced search trees or hash
tables may provide better performance.
Huffman Coding is a technique of compressing data to reduce its size
without losing any of the details. It was first developed by David Huffman.

Huffman Coding is generally useful to compress the data in which there are
frequently occurring characters.

Huffman coding first creates a tree using the frequencies of the character
and then generates code for each character.

You might also like