Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
8 views41 pages

Week 1 - Chapter 1. Analysis and Design of Algorithms

Analysis and Design of Algorithms - DS&A

Uploaded by

congminhdang2011
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)
8 views41 pages

Week 1 - Chapter 1. Analysis and Design of Algorithms

Analysis and Design of Algorithms - DS&A

Uploaded by

congminhdang2011
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/ 41

FTU 91 Chua Lang, Hanoi, Vietnam [email protected].

vn

Week 1 - Chapter 1.
Analysis and Design of Algorithms

Dr. Pham Thi Kim Dung © 1



"Have you ever solved a real-world problem using
programming? How was that problem represented
in your program?"

Dr. Pham Thi Kim Dung © 2


Chapter 1: Analysis and Design of Algorithms (DSA)

1.1. Introduction to Data Structures and Algorithms

1.2. Introduction to Problems and Applications Related to Data


Structures

1.3. Algorithm Design Methods

Purpose ○

1.3.1. Modularization
1.3.2. Stepwise Refinement

1.4. Algorithm Analysis

○ 1.4.1. Problem Definition


○ 1.4.2. Algorithm Execution Time
○ 1.4.3. Computational Complexity of Algorithms
Hassan Khoury © 3
1.1. Introduction to Data Structures and
Algorithms
Data Structure Algorithm
Definition: A way of organizing and storing Definition: A set of steps or instructions followed
data in memory to facilitate efficient data in sequence to solve a specific problem.
access and manipulation.
Examples: Sorting algorithms, searching
Examples: Arrays, linked lists, stacks, queues, algorithms, recursion algorithms.
trees, graphs.
Role:
Role:
● Defines how to solve a specific problem
● Optimizes memory usage and data step-by-step.
processing time. ● Minimizes processing time through
● Helps programmers build and manage optimized steps.
complex programs easily.
Dr. Pham Thi Kim Dung © 4
Dr. Pham Thi Kim Dung ©

Data Structures & Algorithms == Tools & Methods

Example: “To search for an element in a dataset"


● If using an array → apply algorithms such as linear search or
binary search.
● If using a binary tree → apply tree traversal algorithms.

Key Insight: Choosing the right data structure can significantly


optimize an algorithm's performance.
Dr. Pham Thi Kim Dung © 6
Example of Data Structures
Common Examples:

● Array: A collection of elements, all of the same type, indexed for quick
access.
● Stack: A collection that follows the Last-In-First-Out (LIFO) principle.
● Queue: A collection that follows the First-In-First-Out (FIFO) principle.
● Linked List: A sequence of elements connected by pointers.
● Tree: A hierarchical structure with nodes, where each node may have
child nodes.
● Graph: A collection of nodes connected by edges, representing
relationships.
● …

Dr. Pham Thi Kim Dung © 7


11 Data Structures
Every Developer
Should Know

Dr. Pham Thi Kim Dung ©


Example of Algorithms
Common Examples:

● Sorting Algorithms: Arranging data in a specific order (e.g., Quick Sort, Bubble
Sort).
● Searching Algorithms: Locating specific data within a dataset (e.g., Binary
Search, Linear Search).
● Recursive Algorithms: A solution that calls itself in its own definition, often
simplifying complex problems.
● If the cities and routes are represented using a graph → use algorithms such as
Dijkstra’s algorithm or A (A-star) search*.
● …

Dr. Pham Thi Kim Dung © 9


Dr. Pham Thi Kim Dung © 10
Real-World Applications
Game Development:

● Graphs are used to find the shortest path for characters to navigate through a
game map.

Data Management Systems:

● Binary search trees are used to quickly organize and retrieve large sets of data.

Artificial Intelligence and Machine Learning:

● Priority queues are used in heuristic search algorithms, such as A* (A-star), to


prioritize the best potential solution paths.

Dr. Pham Thi Kim Dung © 11



"Nếu không tối ưu cấu trúc dữ liệu, chương trình sẽ
gặp phải vấn đề gì?"

"Có những tình huống nào mà thời gian thực hiện của
giải thuật là yếu tố quyết định?"

Dr. Pham Thi Kim Dung © 12


1.2. Introduction to Problems and
Applications Related to Data Structures

Dr. Pham Thi Kim Dung © 13


Sorting Problem Example:

● Sorting a list of students by their grades


in descending order.

Real-World Application:

● E-commerce websites displaying products


sorted by price (ascending/descending).

Suitable Data Structures:

● Arrays or Linked Lists.

Key Algorithms: Selection Sort, Insertion


Sort, Quick Sort.
Dr. Pham Thi Kim Dung © 14
Dr. Pham Thi Kim Dung ©
Problem Example:
Searching ● Searching for a phone
number in a contact list.

Real-World Application:

● Google search engine


retrieving information from
massive datasets.

Suitable Data Structures:

● Sorted Arrays, Binary


Search Trees (BST).

Key Algorithms: Linear Search,


Binary Search.
Dr. Pham Thi Kim Dung © 16
Problem Example:
Queue&Stack ● Handling undo/redo commands in a
text editor.
● Managing customer queues in a bank
system.

Real-World Application:

● Online ticket booking systems that


handle requests on a first-come,
first-served basis (FIFO - Queue).
● Browser history management (Stack).

Suitable Data Structures:

● Stack, Queue.
Dr. Pham Thi Kim Dung © 17
Problem Example:
Tree ● Organizing folders and files in a computer system.
● Representing parent-child relationships in a family tree or
organizational chart.

Dr. Pham Thi Kim Dung © 18


Tree Real-World Application:

● File system hierarchy in


operating systems.
● Hierarchical structures in
distributed databases.

Suitable Data Structures:

● Binary Tree, AVL Tree, B+


Tree.

Dr. Pham Thi Kim Dung © 19


Problem Example:

Shortest Path ● Finding the shortest route from the


current location to a destination on
a map.

Real-World Application:

● Navigation apps (Google Maps) for


optimal route suggestions.
● Network routing systems for
efficient data packet transfers.

Suitable Data Structures:

● Graphs.

Key Algorithms: Dijkstra, Bellman-Ford.


Dr. Pham Thi Kim Dung © 20
Real World "Explain, using an example, how to choose
the appropriate data structure and
Problem algorithm for an airport queue
management system."

⇒ Choosing the Appropriate Data Structure and Algorithm

Dr. Pham Thi Kim Dung © 21


Solution (1)
1. Problem Analysis:
An airport queue management system
must: ● Allow dynamic addition and
● Manage multiple queues for removal of passengers.
different purposes (e.g., check-in, ● Prioritize certain passengers (e.g.,
security screening, boarding). priority boarding for first-class
● Handle requests in the order they passengers).
arrive (FIFO – First In, First Out).

Dr. Pham Thi Kim Dung © 22


Solution
2. Data Structure Selection:
Primary Queue Management (FIFO Order): Priority Queue for Special Passengers:

● Data Structure: Queue ● Data Structure: Priority Queue


○ Reason: A queue follows the FIFO ○ Reason: Some passengers (e.g.,
principle, where passengers who elderly, VIPs) need priority service. A
arrive first are processed first. priority queue assigns a priority level
○ Implementation: Can use arrays, to each passenger, ensuring that
linked lists, or built-in queue those with higher priority are
structures depending on system processed before others, even if they
needs. arrived later.

Dr. Pham Thi Kim Dung © 23


Solution
3. Algorithm Selection:
Queue Operations: Priority Queue Algorithm:

● Enqueue (Add a passenger): Add the ● Use a heap-based priority queue


passenger to the rear of the queue. (e.g., Min-Heap or Max-Heap):
● Dequeue (Process the next passenger): ○ Insertion and deletion in
Remove the passenger from the front of the O(log n) time, where n is
queue. the number of elements.
○ Time Complexity: O(1) if using linked ○ Maintains order by adjusting
list-based queues. the tree structure efficiently.

Dr. Pham Thi Kim Dung © 24


Solution Execution:

1. A passenger, "John" (regular), is added


to the back of the regular queue.
Example
2. Later, "Anna" (VIP) arrives and is
Scenario: placed in the priority queue.
3. When calling the next passenger, the
● Regular passengers arrive at the
system checks both queues:
check-in desk and are added to the
○ If there are passengers in the
regular queue.
priority queue, "Anna" is
● Priority passenger arrives and
processed first.
should be placed ahead of regular
○ If the priority queue is empty, the
passengers.
system dequeues the next
passenger from the regular queue.
Dr. Pham Thi Kim Dung © 25
Pseudocode
-
Scenario 1:
FIFO Queue

Dr. Pham Thi Kim Dung © 26


Pseudocode -
Scenario 2:
Priority Queue

Dr. Pham Thi Kim Dung © 27


1.3. Algorithm Design Methods
1.3.1. Modularization
1.3.2. Stepwise Refinement

Dr. Pham Thi Kim Dung © 28


1.3.1. Modularization Python

Definition: Modularization is the


process of breaking an algorithm into
independent, smaller modules or
components that perform specific tasks.
Example: Calculate the total price of
items in a shopping cart.
Modularized Algorithm:

Dr. Pham Thi Kim Dung © 29


1.3.2. Stepwise Refinement
Step 1 (High-Level Plan):
Stepwise refinement is an iterative process
where the algorithm is refined step-by-step
by adding more details at each step until the
algorithm is complete.

● The approach begins with a high-level


solution and proceeds by refining into Step 2 (Refinement):
more detailed sub-steps.

Example: Print the Fibonacci sequence up


to n terms.

Dr. Pham Thi Kim Dung © 30


Python

Dr. Pham Thi Kim Dung ©


1.4. Algorithm Analysis
1.4.1. Problem Definition
1.4.2. Algorithm Execution Time
1.4.3. Computational Complexity of Algorithms

Dr. Pham Thi Kim Dung © 32


1.4.1. Problem Definition
Define a problem Example: "Design an algorithm to find the
largest number in a list of integers."
Inputs: What data do you need?
Inputs: A list of integers.
Outputs: What results should be produced?
Outputs: The largest integer.
Constraints: What are the limits (e.g.,
time, memory)? Constraints: The algorithm must work
for any size of the list.
Objectives: What do you want to achieve
(e.g., minimize time, maximize accuracy)?

Dr. Pham Thi Kim Dung © 33


1.4.2. Algorithm Execution Time

Measuring Execution Time:

● Use Big-O notation to describe how execution time scales with input size n.

Linear Search Algorithm: “Find a specific value in an array.”

● Best Case: The value is the first element → O(1)


● Worst Case: The value is not found or is the last element → O(n).
● Average Case: On average, we check half of the elements → O(n/2).

Dr. Pham Thi Kim Dung © 34


1.4.3. Computational Complexity of
Algorithms
Time Complexity & Space Complexity => Optimize

=> a function of the input size n.

Dr. Pham Thi Kim Dung © 35


Fibonacci Sequence
The Fibonacci sequence is defined as:

with base cases:

Dr. Pham Thi Kim Dung ©


Fibonacci Sequence (Recursive Algorithm)

Dr. Pham Thi Kim Dung ©


Fibonacci Sequence (Dynamic Programming (DP))

Dr. Pham Thi Kim Dung ©


Fibonacci Sequence (Comparison)

Dr. Pham Thi Kim Dung ©



"Nếu không tối ưu cấu trúc dữ liệu, chương trình sẽ
gặp phải vấn đề gì?"

"Có những tình huống nào mà thời gian thực hiện của
giải thuật là yếu tố quyết định?"

Dr. Pham Thi Kim Dung © 40


Coding Task: Write a program to calculate the
sum of numbers in an array using the following
methods:
Iterative method: Using a loop.
Homework
1.
2. Recursive method: Using recursion.

Analysis Task: Compare the computational


complexity of the two methods and explain their
differences in terms of time and space efficiency.

Dr. Pham Thi Kim Dung © 41

You might also like