In this chapter we will compare the data structures we have learned so far by the performance (execution speed) of the basic operations (addition, search, deletion, etc.). We will give specific tips in what situations what data structures to use.
In this chapter we are going to get familiar with some of the basic presentations of data in programming: lists and linear data structures. Very often in order to solve a given problem we need to work with a sequence of elements. For example, to read completely this book we have to read sequentially each page, i.e. to traverse sequentially each of the elements of the set of the pages in the book. Depending on the task, we have to apply different operations on this set of data. In this chapter we will introduce the concept of abstract data types (ADT) and will explain how a certain ADT can have multiple different implementations. After that we shall explore how and when to use lists and their implementations (linked list, doubly-linked list and array-list). We are going to see how for a given task one structure may be more convenient than another. We are going to consider the structures "stack" and "queue", as well as their applications. We are going to get familiar with some implementations of these structures.
In this chapter we will learn about arrays as a way to work with sequences of elements of the same type. We will explain what arrays are, how we declare, create, instantiate and use them. We will examine one-dimensional and multidimensional arrays. We will learn different ways to iterate through the array, read from the standard input and write to the standard output. We will give many example exercises, which can be solved using arrays and we will show how useful they really are.
In this chapter we will analyze more complex data structures like dictionaries and sets, and their implementations with hash-tables and balanced trees. We will explain in more details what hashing and hash-tables mean and why they are such an important part of programming. We will discuss the concept of "collisions" and how they might happen when implementing hash-tables. Also we will offer you different types of approaches for solving this type of issues. We will look at the abstract data structure set and explain how it can be implemented with the ADTs dictionary and balanced search tree. Also we will provide you with examples that illustrate the behavior of these data structures with real world examples.
In this chapter we will review how to work with text files in C#. We will explain what a stream is, what its purpose is, and how to use it. We will explain what a text file is and how can you read and write data to a text file and how to deal with different character encodings. We will demonstrate and explain the good practices for exception handling when working with files. All of this will be demonstrated with many examples in this chapter
In this chapter we will explore strings. We are going to explain how they are implemented in Java and in what way we can process text content. Additionally, we will go through different methods for manipulating a text: we will learn how to compare strings, how to search for substrings, how to extract substrings upon previously settled parameters and last but not least how to split a string by separator chars. We will demonstrate how to correctly build strings with the StringBuilder class. We will provide a short but very useful information for the most commonly used regular expressions.
In this chapter we are going to get familiar with some of the basic presentations of data in programming: lists and linear data structures. Very often in order to solve a given problem we need to work with a sequence of elements. For example, to read completely this book we have to read sequentially each page, i.e. to traverse sequentially each of the elements of the set of the pages in the book. Depending on the task, we have to apply different operations on this set of data. In this chapter we will introduce the concept of abstract data types (ADT) and will explain how a certain ADT can have multiple different implementations. After that we shall explore how and when to use lists and their implementations (linked list, doubly-linked list and array-list). We are going to see how for a given task one structure may be more convenient than another. We are going to consider the structures "stack" and "queue", as well as their applications. We are going to get familiar with some implementations of these structures.
In this chapter we will learn about arrays as a way to work with sequences of elements of the same type. We will explain what arrays are, how we declare, create, instantiate and use them. We will examine one-dimensional and multidimensional arrays. We will learn different ways to iterate through the array, read from the standard input and write to the standard output. We will give many example exercises, which can be solved using arrays and we will show how useful they really are.
In this chapter we will analyze more complex data structures like dictionaries and sets, and their implementations with hash-tables and balanced trees. We will explain in more details what hashing and hash-tables mean and why they are such an important part of programming. We will discuss the concept of "collisions" and how they might happen when implementing hash-tables. Also we will offer you different types of approaches for solving this type of issues. We will look at the abstract data structure set and explain how it can be implemented with the ADTs dictionary and balanced search tree. Also we will provide you with examples that illustrate the behavior of these data structures with real world examples.
In this chapter we will review how to work with text files in C#. We will explain what a stream is, what its purpose is, and how to use it. We will explain what a text file is and how can you read and write data to a text file and how to deal with different character encodings. We will demonstrate and explain the good practices for exception handling when working with files. All of this will be demonstrated with many examples in this chapter
In this chapter we will explore strings. We are going to explain how they are implemented in Java and in what way we can process text content. Additionally, we will go through different methods for manipulating a text: we will learn how to compare strings, how to search for substrings, how to extract substrings upon previously settled parameters and last but not least how to split a string by separator chars. We will demonstrate how to correctly build strings with the StringBuilder class. We will provide a short but very useful information for the most commonly used regular expressions.
In this chapter we are going to get familiar with recursion and its applications. Recursion represents a powerful programming technique in which a method makes a call to itself from within its own method body. By means of recursion we can solve complicated combinatorial problems, in which we can easily exhaust different combinatorial configurations, e.g. generating permutations and variations and simulating nested loops. We are going to demonstrate many examples of correct and incorrect usage of recursion and convince you how useful it can be.
Chapter 22. Lambda Expressions and LINQIntro C# Book
In this chapter we will become acquainted with some of the advanced capabilities of the C# language. To be more specific, we will pay attention on how to make queries to collections, using lambda expressions and LINQ, and how to add functionality to already created classes, using extension methods. We will get to know the anonymous types, describe their usage briefly and discuss lambda expressions and show in practice how most of the built-in lambda functions work. Afterwards, we will pay more attention to the LINQ syntax – we will learn what it is, how it works and what queries we can build with it. In the end, we will get to know the meaning of the keywords in LINQ, and demonstrate their capabilities with lots of examples.
Here we are going to take a look how to use for loop, foreach loop and while loop. Also we are going to learn how to use and invoke methods and how to define classes in Java programming language.
In this chapter we will explore strings. We are going to explain how they are implemented in C# and in what way we can process text content. Additionally, we will go through different methods for manipulating a text: we will learn how to compare strings, how to search for substrings, how to extract substrings upon previously settled parameters and last but not least how to split a string by separator chars. We will demonstrate how to correctly build strings with the StringBuilder class. We will provide a short but very useful information for the most commonly used regular expressions. We will discuss some classes for efficient construction of strings. Finally, we will take a look at the methods and classes for achieving more elegant and stricter formatting of the text content.
19. Data Structures and Algorithm ComplexityIntro C# Book
In this chapter we will compare the data structures we have learned so far by the performance (execution speed) of the basic operations (addition, search, deletion, etc.). We will give specific tips in what situations what data structures to use. We will explain how to choose between data structures like hash-tables, arrays, dynamic arrays and sets implemented by hash-tables or balanced trees. Almost all of these structures are implemented as part of NET Framework, so to be able to write efficient and reliable code we have to learn to apply the most appropriate structures for each situation.
In this chapter we will examine the loop programming constructs through which we can execute a code snippet repeatedly. We will discuss how to implement conditional repetitions (while and do-while loops) and how to work with for-loops. We will give examples of different possibilities to define loops, how to construct them and some of their key usages. Finally, we will discuss the foreach-loop construct and how we can use multiple loops placed inside each other (nested loops).
In this chapter we will get familiar with primitive types and variables in Java – what they are and how to work with them. First we will consider the data types – integer types, real types with floating-point, Boolean, character, string and object type. We will continue with the variables, with their characteristics, how to declare them, how they are assigned a value and what is variable initialization.
Learn how to use arrays in Java, how to enter array, how to traverse an array, how to print array and more array operations.
Watch the video lesson and access the hands-on exercises here: https://softuni.org/code-lessons/java-foundations-certification-arrays
Java Foundations: Data Types and Type ConversionSvetlin Nakov
Learn how to use data types and variables in Java, how variables are stored in the memory and how to convert from one data type to another.
Watch the video lesson and access the hands-on exercises here: https://softuni.org/code-lessons/java-foundations-certification-data-types-and-variables
This is the slides for the data science workshop at CDIPS, UC Berkeley on 06-28-2017. It is about general machine learning with a focus on scikit-learn. You can find all the related material: https://github.com/qingkaikong/20170628_ML_sklearn
Learn how to use lists in Java, how to use List<T> and ArrayList<T>, how to process lists of elements.
Watch the video lesson and access the hands-on exercises here: https://softuni.org/code-lessons/java-foundations-certification-lists
03 and 04 .Operators, Expressions, working with the console and conditional s...Intro C# Book
In this chapter we will get acquainted with the operators in Java and the actions they can perform on the different data types. We will explain the priorities of the operators and we will analyze the different types of operators, according to the count of the arguments they can take and the actions they can perform. In the second part we will examine the conversion of data types.
In this lesson you will learn how to use basic syntax, conditions, if-else statements and loops (for-loop, while-loop and do-while-loop) in Java and how to use the debugger.
Watch the video lesson and access the hands-on exercises here: https://softuni.org/code-lessons/java-foundations-certification-basic-syntax-conditions-and-loops
In this chapter we are going to get familiar with recursion and its applications. Recursion represents a powerful programming technique in which a method makes a call to itself from within its own method body. By means of recursion we can solve complicated combinatorial problems, in which we can easily exhaust different combinatorial configurations, e.g. generating permutations and variations and simulating nested loops. We are going to demonstrate many examples of correct and incorrect usage of recursion and convince you how useful it can be.
Chapter 22. Lambda Expressions and LINQIntro C# Book
In this chapter we will become acquainted with some of the advanced capabilities of the C# language. To be more specific, we will pay attention on how to make queries to collections, using lambda expressions and LINQ, and how to add functionality to already created classes, using extension methods. We will get to know the anonymous types, describe their usage briefly and discuss lambda expressions and show in practice how most of the built-in lambda functions work. Afterwards, we will pay more attention to the LINQ syntax – we will learn what it is, how it works and what queries we can build with it. In the end, we will get to know the meaning of the keywords in LINQ, and demonstrate their capabilities with lots of examples.
Here we are going to take a look how to use for loop, foreach loop and while loop. Also we are going to learn how to use and invoke methods and how to define classes in Java programming language.
In this chapter we will explore strings. We are going to explain how they are implemented in C# and in what way we can process text content. Additionally, we will go through different methods for manipulating a text: we will learn how to compare strings, how to search for substrings, how to extract substrings upon previously settled parameters and last but not least how to split a string by separator chars. We will demonstrate how to correctly build strings with the StringBuilder class. We will provide a short but very useful information for the most commonly used regular expressions. We will discuss some classes for efficient construction of strings. Finally, we will take a look at the methods and classes for achieving more elegant and stricter formatting of the text content.
19. Data Structures and Algorithm ComplexityIntro C# Book
In this chapter we will compare the data structures we have learned so far by the performance (execution speed) of the basic operations (addition, search, deletion, etc.). We will give specific tips in what situations what data structures to use. We will explain how to choose between data structures like hash-tables, arrays, dynamic arrays and sets implemented by hash-tables or balanced trees. Almost all of these structures are implemented as part of NET Framework, so to be able to write efficient and reliable code we have to learn to apply the most appropriate structures for each situation.
In this chapter we will examine the loop programming constructs through which we can execute a code snippet repeatedly. We will discuss how to implement conditional repetitions (while and do-while loops) and how to work with for-loops. We will give examples of different possibilities to define loops, how to construct them and some of their key usages. Finally, we will discuss the foreach-loop construct and how we can use multiple loops placed inside each other (nested loops).
In this chapter we will get familiar with primitive types and variables in Java – what they are and how to work with them. First we will consider the data types – integer types, real types with floating-point, Boolean, character, string and object type. We will continue with the variables, with their characteristics, how to declare them, how they are assigned a value and what is variable initialization.
Learn how to use arrays in Java, how to enter array, how to traverse an array, how to print array and more array operations.
Watch the video lesson and access the hands-on exercises here: https://softuni.org/code-lessons/java-foundations-certification-arrays
Java Foundations: Data Types and Type ConversionSvetlin Nakov
Learn how to use data types and variables in Java, how variables are stored in the memory and how to convert from one data type to another.
Watch the video lesson and access the hands-on exercises here: https://softuni.org/code-lessons/java-foundations-certification-data-types-and-variables
This is the slides for the data science workshop at CDIPS, UC Berkeley on 06-28-2017. It is about general machine learning with a focus on scikit-learn. You can find all the related material: https://github.com/qingkaikong/20170628_ML_sklearn
Learn how to use lists in Java, how to use List<T> and ArrayList<T>, how to process lists of elements.
Watch the video lesson and access the hands-on exercises here: https://softuni.org/code-lessons/java-foundations-certification-lists
03 and 04 .Operators, Expressions, working with the console and conditional s...Intro C# Book
In this chapter we will get acquainted with the operators in Java and the actions they can perform on the different data types. We will explain the priorities of the operators and we will analyze the different types of operators, according to the count of the arguments they can take and the actions they can perform. In the second part we will examine the conversion of data types.
In this lesson you will learn how to use basic syntax, conditions, if-else statements and loops (for-loop, while-loop and do-while-loop) in Java and how to use the debugger.
Watch the video lesson and access the hands-on exercises here: https://softuni.org/code-lessons/java-foundations-certification-basic-syntax-conditions-and-loops
17. Java data structures trees representation and traversalIntro C# Book
In this chapter we will discuss tree data structures, like trees and graphs. The abilities of these data structures are really important for the modern programming. Each of this data structures is used for building a model of real life problems, which are efficiently solved using this model.
In this chapter we will discuss one recommended practice for efficiently solving computer programming problems and make a demonstration with appropriate examples. We will discuss the basic engineering principles of problem solving, why we should follow them when solving computer programming problems (the same principles can also be applied to find the solutions of many mathematical and scientific problems as well) and we will make an example of their use. We will describe the steps, in which we should go in order to solve some sample problems and show the mistakes that can occur when we do not follow these same steps. We will pay attention to some important steps from the methodology of problem solving, that we usually skip, e.g. the testing. We hope to be able to prove you, with proper examples, that the solving of computer programming problems has a "recipe" and it is very useful.
In this chapter we review the basic rules and recommendations for writing quality program code. We pay attention to naming the identifiers in the program (variables, methods, parameters, classes, etc.), formatting and code organization rules, good practices for composing methods, and principles for writing quality documentation.
Here we are going to learn why is a good practice to use interfaces and how they are different from abstraction classes. Further more we are going to see how which one of them to use.
In this chapter we will understand how to define custom classes and their elements. We will learn to declare fields, constructors and properties for the classes. We will revise what a method is and we will broaden our knowledge about access modifiers and methods.
In this chapter we will discuss exceptions in the object-oriented programming and in Java in particular. We will learn how to handle exceptions using the try-catch construct, how to pass them to the calling methods and how to throw standard or our own exceptions using the throw construct.
01. Introduction to programming with javaIntro C# Book
In this chapter we will take a look at the basic programming terminology and we will write our first Java program. We will familiarize ourselves with programming – what it means and its connection to computers and programming languages.
In this chapter we will discuss one recommended practice for efficiently solving computer programming problems and make a demonstration with appropriate examples. We will discuss the basic engineering principles of problem solving, why we should follow them when solving computer programming problems (the same principles can also be applied to find the solutions of many mathematical and scientific problems as well) and we will make an example of their use. We will describe the steps, in which we should go in order to solve some sample problems and show the mistakes that can occur when we do not follow these same steps. We will pay attention to some important steps from the methodology of problem solving, that we usually skip, e.g. the testing. We hope to be able to prove you, with proper examples, that the solving of computer programming problems has a "recipe" and it is very useful
In this chapter we review the basic rules and recommendations for writing quality program code. We pay attention to naming the identifiers in the program (variables, methods, parameters, classes, etc.), formatting and code organization rules, good practices for composing methods, and principles for writing quality documentation. We describe the official "Design Guidelines for Developing Class Libraries for .NET" from Microsoft. In the meantime we explain how the programming environment can automate operations such as code formatting and refactoring.
This chapter is a kind of continuation of the previous one – “Object-Oriented Programming Principles”. The reader is expected to be familiar with the basic OOP principles: abstraction, inheritance, polymorphism, encapsulation and exception handling. Those do greatly affect the quality of the code.
In this chapter we will discuss tree data structures, like trees and graphs. The abilities of these data structures are really important for the modern programming. Each of this data structures is used for building a model of real life problems, which are efficiently solved using this model. We will explain what tree data structures are and will review their main advantages and disadvantages. We will present example implementations and problems showing their practical usage. We will focus on binary trees, binary search trees and self-balancing binary search tree. We will explain what graph is, the types of graphs, how to represent a graph in the memory (graph implementation) and where graphs are used in our life and in the computer technologies. We will see where in .NET Framework self-balancing binary search trees are implemented and how to use them.
In this chapter we will understand how to define custom classes and their elements. We will learn to declare fields, constructors and properties for the classes. We will revise what a method is and we will broaden our knowledge about access modifiers and methods. We will observe the characteristics of the constructors and we will set out how the program objects coexist in the dynamic memory and how their fields are initialized. Finally, we will explain what the static elements of a class are – fields (including constants), properties and methods and how to use them properly. In this chapter we will also introduce generic types (generics), enumerated types (enumerations) and nested classes.
This 7-second Brain Wave Ritual Attracts Money To You.!nirahealhty
Discover the power of a simple 7-second brain wave ritual that can attract wealth and abundance into your life. By tapping into specific brain frequencies, this technique helps you manifest financial success effortlessly. Ready to transform your financial future? Try this powerful ritual and start attracting money today!
Bridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptxBrad Spiegel Macon GA
Brad Spiegel Macon GA’s journey exemplifies the profound impact that one individual can have on their community. Through his unwavering dedication to digital inclusion, he’s not only bridging the gap in Macon but also setting an example for others to follow.
APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024APNIC
Ellisha Heppner, Grant Management Lead, presented an update on APNIC Foundation to the PNG DNS Forum held from 6 to 10 May, 2024 in Port Moresby, Papua New Guinea.
# Internet Security: Safeguarding Your Digital World
In the contemporary digital age, the internet is a cornerstone of our daily lives. It connects us to vast amounts of information, provides platforms for communication, enables commerce, and offers endless entertainment. However, with these conveniences come significant security challenges. Internet security is essential to protect our digital identities, sensitive data, and overall online experience. This comprehensive guide explores the multifaceted world of internet security, providing insights into its importance, common threats, and effective strategies to safeguard your digital world.
## Understanding Internet Security
Internet security encompasses the measures and protocols used to protect information, devices, and networks from unauthorized access, attacks, and damage. It involves a wide range of practices designed to safeguard data confidentiality, integrity, and availability. Effective internet security is crucial for individuals, businesses, and governments alike, as cyber threats continue to evolve in complexity and scale.
### Key Components of Internet Security
1. **Confidentiality**: Ensuring that information is accessible only to those authorized to access it.
2. **Integrity**: Protecting information from being altered or tampered with by unauthorized parties.
3. **Availability**: Ensuring that authorized users have reliable access to information and resources when needed.
## Common Internet Security Threats
Cyber threats are numerous and constantly evolving. Understanding these threats is the first step in protecting against them. Some of the most common internet security threats include:
### Malware
Malware, or malicious software, is designed to harm, exploit, or otherwise compromise a device, network, or service. Common types of malware include:
- **Viruses**: Programs that attach themselves to legitimate software and replicate, spreading to other programs and files.
- **Worms**: Standalone malware that replicates itself to spread to other computers.
- **Trojan Horses**: Malicious software disguised as legitimate software.
- **Ransomware**: Malware that encrypts a user's files and demands a ransom for the decryption key.
- **Spyware**: Software that secretly monitors and collects user information.
### Phishing
Phishing is a social engineering attack that aims to steal sensitive information such as usernames, passwords, and credit card details. Attackers often masquerade as trusted entities in email or other communication channels, tricking victims into providing their information.
### Man-in-the-Middle (MitM) Attacks
MitM attacks occur when an attacker intercepts and potentially alters communication between two parties without their knowledge. This can lead to the unauthorized acquisition of sensitive information.
### Denial-of-Service (DoS) and Distributed Denial-of-Service (DDoS) Attacks
Multi-cluster Kubernetes Networking- Patterns, Projects and GuidelinesSanjeev Rampal
Talk presented at Kubernetes Community Day, New York, May 2024.
Technical summary of Multi-Cluster Kubernetes Networking architectures with focus on 4 key topics.
1) Key patterns for Multi-cluster architectures
2) Architectural comparison of several OSS/ CNCF projects to address these patterns
3) Evolution trends for the APIs of these projects
4) Some design recommendations & guidelines for adopting/ deploying these solutions.
19. Java data structures algorithms and complexity
1. SoftUni Team
Technical Trainers
Software University
http://softuni.bg
Data Structures,
Algorithms and Complexity
Analyzing Algorithm Complexity.
Asymptotic Notation
2. Table of Contents
1. Data Structures
Linear Structures, Trees, Hash Tables, Others
2. Algorithms
Sorting and Searching, Combinatorics,
Dynamic Programming, Graphs, Others
3. Complexity of Algorithms
Time and Space Complexity
Mean, Average and Worst Case
Asymptotic Notation O(g)
2
6. 6
Examples of data structures:
Person structure (first name + last name + age)
Array of integers – int[]
List of strings – List<string>
Queue of people – Queue<Person>
What is a Data Structure?
“In computer science, a data structure is a particular
way of storing and organizing data in a computer so
that it can be used efficiently.” -- Wikipedia
7. 7
Student Data Structure
struct Student {
string Name { get; set; }
short Age { get; set; } // Student age (< 128)
Gender Gender { get; set; }
int FacultyNumber { get; set; }
};
enum Gender
{
Male,
Female,
Other
}
Student
Name Maria Smith
Age 23
Gender Female
FacultyNumber SU2007333
9. 9
Stack
class Stack<T>
{
// Pushes an elements at the top of the stack
void Push(T data);
// Extracts the element at the top of the stack
T Pop();
// Checks for empty stack
bool IsEmpty();
}
// The type T can be any data structure like
// int, string, DateTime, Student
15. 15
Queue
class Queue<T>
{
// Appends an elements at the end of the queue
void Enqueue(T data);
// Extracts the element from the start of the queue
T Dequeue();
// Checks for empty queue
bool IsEmpty();
}
// The type T can be any data structure like
// int, string, DateTime, Student
20. 20
Binary Tree
data
left link
.......
NULL
tree root
right link
data
left link
right link
data
left link
right link
data
left link
right link
data
left link
right link
.......
NULL
NULL
.......
.......
27. 27
Data structures and algorithms are the foundation of computer
programming
Algorithmic thinking, problem solving and data structures are
vital for software engineers
C# developers should know when to use T[], LinkedList<T>,
List<T>, Stack<T>, Queue<T>, Dictionary<K, T>,
HashSet<T>, SortedDictionary<K, T> and SortedSet<T>
Computational complexity is important for algorithm design and
efficient programming
Why Are Data Structures So Important?
28. Primitive data types
Numbers: int, float, double, decimal, …
Text data: char, string, …
Simple structures
A group of primitive fields stored together
E.g. DateTime, Point, Rectangle, Color, …
Collections
A set / sequence of elements (of the same type)
E.g. array, list, stack, queue, tree, hashtable, bag, …
Primitive Types and Collections
29. An Abstract Data Type (ADT) is
A data type together with the operations, whose properties are
specified independently of any particular implementation
ADT is a set of definitions of operations
Like the interfaces in C# / Java
Defines what we can do with the structure
ADT can have several different implementations
Different implementations can have different efficiency, inner
logic and resource needs
Abstract Data Types (ADT)
30. Linear structures
Lists: fixed size and variable size sequences
Stacks: LIFO (Last In First Out) structures
Queues: FIFO (First In First Out) structures
Trees and tree-like structures
Binary, ordered search trees, balanced trees, etc.
Dictionaries (maps, associative arrays)
Hold pairs (key value)
Hash tables: use hash functions to search / insert
Basic Data Structures
31. 31
Sets, multi-sets and bags
Set – collection of unique elements
Bag – collection of non-unique elements
Ordered sets and dictionaries
Priority queues / heaps
Special tree structures
Suffix tree, interval tree, index tree, trie, rope, …
Graphs
Directed / undirected, weighted / unweighted,
connected / non-connected, cyclic / acyclic, …
Basic Data Structures (2)
33. 33
The term "algorithm" means "a sequence of steps"
Derived from Muḥammad Al-Khwārizmī', a Persian mathematician
and astronomer
He described an algorithm for solving quadratic equations in 825
What is an Algorithm?
“In mathematics and computer science, an algorithm is
a step-by-step procedure for calculations. An algorithm
is an effective method expressed as a finite list of well-
defined instructions for calculating a function.”
-- Wikipedia
34. 34
Algorithms are fundamental in programming
Imperative (traditional, algorithmic) programming means to
describe in formal steps how to do something
Algorithm == sequence of operations (steps)
Can include branches (conditional blocks) and repeated logic (loops)
Algorithmic thinking (mathematical thinking, logical thinking,
engineering thinking)
Ability to decompose the problems into formal sequences of steps
(algorithms)
Algorithms in Computer Science
35. 35
Algorithms can be expressed as pseudocode, through flowcharts or
program code
Pseudocode and Flowcharts
BFS(node)
{
queue node
while queue not empty
v queue
print v
for each child c of v
queue c
}
Pseudo-code Flowchart
public void DFS(Node node)
{
Print(node.Name);
for (int i = 0; i < node.
Children.Count; i++)
{
if (!visited[node.Id])
DFS(node.Children[i]);
}
visited[node.Id] = true;
}
Source code
36. Sorting and searching
Dynamic programming
Graph algorithms
DFS and BFS traversals
Combinatorial algorithms
Recursive algorithms
Other algorithms
Greedy algorithms, computational geometry, randomized
algorithms, parallel algorithms, genetic algorithms
36
Some Algorithms in Programming
39. 39
Why should we analyze algorithms?
Predict the resources the algorithm will need
Computational time (CPU consumption)
Memory space (RAM consumption)
Communication bandwidth consumption
The expected running time of an algorithm is:
The total number of primitive operations executed
(machine independent steps)
Also known as algorithm complexity
Algorithm Analysis
40. 40
What to measure?
CPU time
Memory consumption
Number of steps
Number of particular operations
Number of disk operations
Number of network packets
Asymptotic complexity
Algorithmic Complexity
41. 41
Worst-case
An upper bound on the running time for any input of given size
Typically algorithms performance is measured for their worst case
Average-case
The running time averaged over all possible inputs
Used to measure algorithms that are repeated many times
Best-case
The lower bound on the running time (the optimal case)
Time Complexity
42. Sequential search in a list of size n
Worst-case:
n comparisons
Best-case:
1 comparison
Average-case:
n/2 comparisons
The algorithm runs in linear time
Linear number of operations
42
Time Complexity: Example
… … … … … … …
n
45. 45
Algorithm complexity is a rough estimation of the number of steps
performed by given computation, depending on the size of the input
Measured with asymptotic notation
O(g) where g is a function of the size of the input data
Examples:
Linear complexity O(n)
All elements are processed once (or constant number of times)
Quadratic complexity O(n2)
Each of the elements is processed n times
Algorithms Complexity
46. 46
Asymptotic upper bound
O-notation (Big O notation)
For a given function g(n), we denote by O(g(n)) the set of functions
that are different than g(n) by a constant
Examples:
3 * n2 + n/2 + 12 ∈ O(n2)
4*n*log2(3*n+1) + 2*n-1 ∈ O(n * log n)
Asymptotic Notation: Definition
O(g(n)) = {f(n): there exist positive constants c and n0
such that f(n) <= c*g(n) for all n >= n0}
49. 49
Complexity Notation Description
constant O(1)
Constant number of operations, not
depending on the input data size, e.g.
n = 1 000 000 1-2 operations
logarithmic O(log n)
Number of operations proportional to
log2(n) where n is the size of the input
data, e.g.
n = 1 000 000 000 30 operations
linear O(n)
Number of operations proportional to the
input data size, e.g. n = 10 000 5 000
operations
Typical Complexities
50. 50
Complexity Notation Description
quadratic O(n2)
Number of operations proportional to the
square of the size of the input data, e.g.
n = 500 250 000 operations
cubic O(n3)
Number of operations propor-tional to
the cube of the size of the input data, e.g.
n = 200 8 000 000 operations
exponential
O(2n),
O(kn),
O(n!)
Exponential number of operations, fast
growing, e.g. n = 20 1 048 576
operations
Typical Complexities (2)
52. 52
Time Complexity and Program Speed
Complexity 10 20 50 100 1 000 10 000 100 000
O(1) < 1 s < 1 s < 1 s < 1 s < 1 s < 1 s < 1 s
O(log(n)) < 1 s < 1 s < 1 s < 1 s < 1 s < 1 s < 1 s
O(n) < 1 s < 1 s < 1 s < 1 s < 1 s < 1 s < 1 s
O(n*log(n)) < 1 s < 1 s < 1 s < 1 s < 1 s < 1 s < 1 s
O(n2) < 1 s < 1 s < 1 s < 1 s < 1 s 2 s 3-4 min
O(n3) < 1 s < 1 s < 1 s < 1 s 20 s 5 hours 231 days
O(2n) < 1 s < 1 s 260 days hangs hangs hangs hangs
O(n!) < 1 s hangs hangs hangs hangs hangs hangs
O(nn) 3-4 min hangs hangs hangs hangs hangs hangs
54. 54
Complexity can be expressed as a formula of multiple variables, e.g.
Algorithm filling a matrix of size n * m with the natural numbers 1,
2, … n*m will run in O(n*m)
Algorithms to traverse a graph with n vertices and m edges will
take O(n + m) steps
Memory consumption should also be considered, for example:
Running time O(n) & memory requirement O(n2)
n = 50 000 OutOfMemoryException
Time and Memory Complexity
BTW: Is it possible in
practice? In theory?
56. 56
A linear algorithm could be slower than a quadratic algorithm
The hidden constant could be significant
Example:
Algorithm A performs 100*n steps O(n)
Algorithm B performs n*(n-1)/2 steps O(n2)
For n < 200, algorithm B is faster
Real-world example:
The "insertion sort" is faster than "quicksort" for n < 9
The Hidden Constant
57. 57
Amortized Analysis
Worst case: O(n2)
Average case: O(n) – Why?
void AddOne(char[] chars, int m)
{
for (int i = 0; 1 != chars[i] && i < m; i++)
{
c[i] = 1 - c[i];
}
}
59. 59
Just calculate and print sum = n * (n + 1) * (2n + 1) / 6
Using a Barometer
uint sum = 0;
for (i = 0; i < n; i++)
{
for (j = 0; j < i*i; j++)
{
sum++;
}
}
Console.WriteLine(sum);
60. 60
A polynomial-time algorithm has worst-case time complexity
bounded above by a polynomial function of its input size
Examples:
Polynomial time: 2n, 3n3 + 4n
Exponential time: 2n, 3n, nk, n!
Exponential algorithms hang for large input data sets
Polynomial Algorithms
W(n) ∈ O(p(n))
64. Runs in O(n) where n is the size of the array
The number of elementary steps is ~ n
Complexity Examples
int FindMaxElement(int[] array)
{
int max = array[0];
for (int i=1; i<array.length; i++)
{
if (array[i] > max)
{
max = array[i];
}
}
return max;
}
65. Runs in O(n2) where n is the size of the array
The number of elementary steps is ~ n * (n+1) / 2
Complexity Examples (2)
long FindInversions(int[] array)
{
long inversions = 0;
for (int i=0; i<array.Length; i++)
for (int j = i+1; j<array.Length; i++)
if (array[i] > array[j])
inversions++;
return inversions;
}
66. Runs in cubic time O(n3)
The number of elementary steps is ~ n3
Complexity Examples (3)
decimal Sum3(int n)
{
decimal sum = 0;
for (int a=0; a<n; a++)
for (int b=0; b<n; b++)
for (int c=0; c<n; c++)
sum += a*b*c;
return sum;
}
67. Runs in quadratic time O(n*m)
The number of elementary steps is ~ n*m
Complexity Examples (4)
long SumMN(int n, int m)
{
long sum = 0;
for (int x=0; x<n; x++)
for (int y=0; y<m; y++)
sum += x*y;
return sum;
}
68. Runs in quadratic time O(n*m)
The number of elementary steps is ~ n*m + min(m,n)*n
Complexity Examples (5)
long SumMN(int n, int m)
{
long sum = 0;
for (int x=0; x<n; x++)
for (int y=0; y<m; y++)
if (x==y)
for (int i=0; i<n; i++)
sum += i*x*y;
return sum;
}
69. Runs in exponential time O(2n)
The number of elementary steps is ~ 2n
Complexity Examples (6)
decimal Calculation(int n)
{
decimal result = 0;
for (int i = 0; i < (1<<n); i++)
result += i;
return result;
}
70. Runs in linear time O(n)
The number of elementary steps is ~ n
Complexity Examples (7)
decimal Factorial(int n)
{
if (n==0)
return 1;
else
return n * Factorial(n-1);
}
71. Runs in exponential time O(2n)
The number of elementary steps is ~ Fib(n+1)
where Fib(k) is the kth Fibonacci's number
Complexity Examples (8)
decimal Fibonacci(int n)
{
if (n == 0)
return 1;
else if (n == 1)
return 1;
else
return Fibonacci(n-1) + Fibonacci(n-2);
}
78. 78
Data structures organize data in computer systems for efficient use
Abstract data types (ADT) describe a set of operations
Collections hold a group of elements
Algorithms are sequences of steps for calculating / doing something
Algorithm complexity is a rough estimation of the number of steps
performed by given computation
Can be logarithmic, linear, n log n, square, cubic, exponential, etc.
Complexity predicts the speed of given code before its execution
Summary
81. License
This course (slides, examples, labs, videos, homework, etc.)
is licensed under the "Creative Commons Attribution-
NonCommercial-ShareAlike 4.0 International" license
81
Attribution: this work may contain portions from
"Fundamentals of Computer Programming with C#" book by Svetlin Nakov & Co. under CC-BY-SA license
"Data Structures and Algorithms" course by Telerik Academy under CC-BY-NC-SA license
82. Free Trainings @ Software University
Software University Foundation – softuni.org
Software University – High-Quality Education,
Profession and Job for Software Developers
softuni.bg
Software University @ Facebook
facebook.com/SoftwareUniversity
Software University @ YouTube
youtube.com/SoftwareUniversity
Software University Forums – forum.softuni.bg
Editor's Notes
39##
40##
41##
42##
46##
60##
63##
64##
65##
66##
67##
68##
69##
70##
71##
(c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*