## The Mastery of Computer Programming: Primary Algorithms - Sykalo Eugene 2023

# Fundamental concepts

Introduction to Algorithms

## Basic Concepts

An algorithm is a set of instructions that are followed to solve a problem or complete a task. The basic operations used to describe algorithms include assignment, comparison, arithmetic, and input/output operations. Notations such as Big O, Big Omega, and Big Theta are used to describe the time complexity of algorithms, which is the amount of time required to execute an algorithm as a function of the size of its input data.

Efficiency and complexity analysis are important considerations when designing and selecting algorithms. The time and space complexity of an algorithm can be calculated and used to determine the efficiency of an algorithm. Best-case, worst-case, and average-case scenarios are analyzed to determine the expected run time of an algorithm.

Common algorithm design techniques include divide-and-conquer and dynamic programming. Divide-and-conquer involves breaking down a problem into smaller sub-problems that can be solved independently, and then combining the solutions to solve the original problem. Dynamic programming involves breaking down a problem into sub-problems, solving each sub-problem only once, and storing the solutions in a table to avoid redundant calculations.

## Searching and Sorting Algorithms

Searching and sorting algorithms are two of the most fundamental types of algorithms in computer science. They are used to organize and manipulate data in a variety of applications, from databases to web search engines.

### Searching Algorithms

Searching algorithms are used to find a specific element in a collection of data. There are two main types of searching algorithms: linear search and binary search.

Linear search, also known as sequential search, is a simple algorithm that checks each element of a collection in turn until it finds the desired element or reaches the end of the collection. Linear search is easy to implement, but it can be slow for large collections.

Binary search is a more efficient algorithm that works by dividing the collection in half repeatedly until the desired element is found. Binary search only works on collections that are already sorted, but it is much faster than linear search for large collections.

### Sorting Algorithms

Sorting algorithms are used to arrange data in a specific order. There are many different sorting algorithms, but some of the most common ones include insertion sort, selection sort, bubble sort, merge sort, and quicksort.

Insertion sort works by iterating through the collection and inserting each element in its proper place in a new, sorted collection. Selection sort works by selecting the smallest unsorted element and swapping it with the first unsorted element. Bubble sort works by repeatedly swapping adjacent elements if they are in the wrong order.

Merge sort and quicksort are more efficient sorting algorithms that use divide-and-conquer techniques to sort the collection. Merge sort works by dividing the collection in half repeatedly until each sub-collection contains only one element, and then merging the sub-collections back together in order. Quicksort works by selecting a pivot element, partitioning the collection into two sub-collections based on the pivot, and then recursively sorting the sub-collections.

Sorting algorithms can be compared in terms of their efficiency and complexity. The time complexity of a sorting algorithm is typically measured by the number of comparisons and swaps required to sort a collection of data. The space complexity of a sorting algorithm is the amount of memory required to perform the sort.

## Data Structures

Data structures are used to store and organize data in a way that allows for efficient access and modification. There are many different data structures, each with its own strengths and weaknesses. Some of the most basic data structures include arrays, linked lists, stacks, queues, and trees.

### Arrays

An array is a collection of elements, each identified by an index or a key. Arrays are used to store data that can be accessed and modified quickly, as each element can be accessed in constant time. However, arrays have a fixed size and can be difficult to modify if the size needs to be changed.

### Linked Lists

A linked list is a collection of elements, each containing a reference to the next element in the list. Linked lists are used to store data that can be easily added or removed, as elements can be inserted or deleted in constant time. However, linked lists can be slower to access than arrays, as each element must be accessed sequentially.

### Stacks and Queues

Stacks and queues are data structures that are used to store collections of elements. A stack is a collection of elements that are stored and accessed in a last-in, first-out (LIFO) order. A queue is a collection of elements that are stored and accessed in a first-in, first-out (FIFO) order. Both stacks and queues can be implemented using arrays or linked lists.

### Trees

A tree is a collection of elements that are organized hierarchically. Each element in a tree is called a node, and each node can have zero or more child nodes. Trees are used to store data that can be easily searched and modified, as elements can be added or removed in logarithmic time. There are many different types of trees, including binary trees, AVL trees, and B-trees.

Choosing the right data structure for a particular algorithm or application is an important consideration. The efficiency and complexity of an algorithm can depend heavily on the choice of data structure, and selecting the right data structure can greatly improve the performance of an algorithm.

## Graph Algorithms

Graph algorithms are used to analyze and manipulate data that is organized as a graph. A graph is a collection of vertices (nodes) and edges that connect the vertices. Graphs are used to model a wide variety of systems, including computer networks, social networks, and transportation networks.

### Breadth-First Search

Breadth-first search (BFS) is a simple graph traversal algorithm that visits all the vertices in a graph in breadth-first order. BFS starts at a specific vertex and explores all the vertices at the same level before moving on to the next level. BFS is often used to find the shortest path between two vertices in an unweighted graph.

### Depth-First Search

Depth-first search (DFS) is another graph traversal algorithm that visits all the vertices in a graph. DFS explores the graph by visiting as far as possible along each branch before backtracking. DFS is often used to find cycles in a graph and to perform topological sorting.

### Shortest Path Algorithms

Shortest path algorithms are used to find the shortest path between two vertices in a graph. There are many different algorithms for finding the shortest path, including Dijkstra's algorithm, Bellman-Ford algorithm, and Floyd-Warshall algorithm. Dijkstra's algorithm is often used to find the shortest path in a weighted graph, while Bellman-Ford algorithm is used to find the shortest path in a graph with negative edge weights.

### Minimum Spanning Tree Algorithms

Minimum spanning tree algorithms are used to find the minimum spanning tree of a graph, which is a tree that connects all the vertices in the graph with the minimum possible total edge weight. There are many different algorithms for finding the minimum spanning tree, including Kruskal's algorithm and Prim's algorithm.