Data Structures and Algorithms - Sharif
Transcript of Data Structures and Algorithms - Sharif
Data Structures
Data Structures and Algorithms
Instructor InformationArash Vaezi
Lab: 712Telegram group: datastructure40254
Telegram Channel: @ DS_40254Email: [email protected]
https://vc.sharif.edu/ch/avaezi
Course InformationCS 40254 Data Structure
Times: Sunday - Tuesday 1.5 p.m. - 3 p.m.Online
Overview
Concept (Use Efficiently, Novelty)
Course objectives
Be familiar with problem solving
Be able to develop (and implement) algorithms
Be able to trace algorithms
Be able to select appropriate data structures and algorithms for given problems
Algorithmic Thinking
Be able to provide novel data structures
Previous Knowledge:
Be familiar with: Java, C++, or C#.
References
• Lecture Notes / Slides
Grading/Evaluation
Course Assessment Tools Number Grade
1 Mid term 1 6
2 Final 1 8
3 Homework 3 3
4 Research Project 1 +1
5 Implementation 3 3
6 Quizzes/Class Works ! +1-2
Total 22
What is data?o Data
A collection of raw facts from which conclusion may be drawn
e.g. Data: Temperature 35°C; Conclusion: It is hot.
o Types of data
Textual: For example, your name (Arash)
Numeric: For example, your ID (40254)
Audio: For example, your voice
Video: For example, your voice and picture
(...)
• DS: is representation of the logical relationship existing between individual elements of data
• DS: is a specialized format for organizing and storing data in memory that considers not only
the elements stored but also their relationship to each other.
• A particular way of storing and organizing data in a computer so that it can be used efficiently
and effectively.
• Data structure is the logical or mathematical model of a particular organization of data.
• A group of data elements grouped together under one name. For example, an array of integers
What is data structure?
The Need for Data Structures
o Goal: to organize data
o Criteria: to facilitate efficient storage of data
retrieval of data
manipulation of data
o Design Issue: select and design appropriate data types
(This is the main motivation to learn and understand data structures)
Data Structure Operations
Traversing
Accessing each data element exactly once so that
certain items in the data may be processed
Searching
Finding the location of the data element (key) in the
structure
Insertion
Adding a new data element to the structure
Data Structure Operations
Deletion
Removing a data element from the structure
Sorting
Arrange the data elements in a logical order
(ascending/descending)
Merging
Combining data elements from two or more data
structures into one
Array
Linked ListTree
Queue
Stack
Types of data structures
In detail, when we talk about data structures and algorithms, we are talking queues, stacks, heaps, binary
search, dynamic programming and so on.
These are patterns for abstraction and solving problems, which invented by computer pioneers. We can
effectively learn them and use them to solve many practical development problems.
There are many, but we named a few. We’ll learn these data structures in great detail!
What is algorithm?
• A finite set of instructions which accomplish a particular task
• A method or process to solve a problem
• Transforms input of a problem to output
• The definition of an algorithm is: The methods for solving problems, which could
be implemented in programming.
Algorithm development is an art and it needs practice!
• It must be correct
• It must be finite (in terms of time and size)
• It must terminate
• It must be unambiguous Which step is next?
• It must be space and time efficient
A problem can be solved in many
different ways, which solution,
amongst the different possible
solutions is optimal?
Notes:
*Efficiency is always a concern and as the inout gets larger it becomes more of a concern.
*Scalability
*Efficient procedures for solving large-scale problems
Concept of Abstraction
We use abstraction everyday!!!
Class iPhone: Phone
{
}
Class Samsung: Phone
{
}
Class Huawei: Phone
{
}
.
.
.
Class Repair-Phone
{
public void repair (iPhone ip)
{
\\code
}
public void repair (Samsung s)
{
\\code
}
public void repair (Huawei h)
{
\\code
}
}
Concept of Abstraction
We use abstraction everyday!!!
Abstract class Phone
{
}
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
Class iPhone: Phone
{
}
Class Samsung: Phone
{
}
Class Huawei: Phone
{
}
.
.
.
Class Repair-Phone
{
public void repair (Phone p)
{
\\code
}
}
Peak FindingOne-Dimensional
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9]
1 2 . n/2 n-1
Peak: Position i is a peak if and only if A[i-1] <= A[i] >= A[i+1].
A[0] >= A[1]
A[n-1] >= A[n-2]
Problem: Find a Peak if it exists.
. n
A[i] >= 0
Peak FindingOne-Dimensional
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9]
1 2 . n/2 n-1
Peak: Position i is a peak if and only if A[i-1] <= A[i] >= A[i+1].
A[0] >= A[1]
A[n-1] >= A[n-2]
Problem: Find a Peak (if it exists).
. n
A[i] >= 0
Straight-Forward-Algorithm
Might be Peak
Algorithm: Start from one side, say left, and search the array for a peak.
Worst-case complexity: in the worst-case we have to look at all the elements.
*This is a linear time algorithm.
Analysis of Algorithms
تحلیل برنامه ها
حافظه, زمان اجرا: پارامترهای مهم در تحلیل برنامه ها و الگوریتم ها
, (اری میانیسیستم نرم افز)سیستم عامل , کامپایلر, پیچیدگی سخت افزار, نوع پردازنده...اندازه یا پیچیدگی ورودی و , پیچیدگی الگوریتم
عت در یک دوره ی زمانی طراحی سخت افزار طوری پیشرفت می کند که سر: قانون مور.در واقع سرعت سخت افزار خطی است. آن نسبت به قبل دو برابرمی شود
.یک الگوریتم برحسب اندازه می تواند نمائی باشد( رشد)در حالی که رفتار*
Peak FindingOne-Dimensional
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9]
1 2 . n/2 n-1
Peak: Position i is a peak if and only if A[i-1] <= A[i] >= A[i+1].
A[0] >= A[1]
A[n-1] >= A[n-2]
Problem: Find a Peak if it exists.
. n
A[i] >= 0
Peak FindingOne-Dimensional
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9]
1 2 . n/2 n-1
Peak: Position i is a peak if and only if A[i-1] <= A[i] >= A[i+1].
A[0] >= A[1]
A[n-1] >= A[n-2]
Problem: Find a Peak if it exists.
. n
A[i] >= 0
A Better Algorithm
Look at the n/2 position
Algorithm: (Recursive)
1- If A[n/2] < A[n/2 -1] then only look at the left half [1 .. n/2] to look for a peak.
2- Else if A[n/2] < A[n/2 + 1] then only look at [n/2 .. n] for a peak.
3- Else n/2 position is a peak.
Peak FindingOne-Dimensional
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9]
1 2 . n/2 n-1
Peak: Position i is a peak if and only if A[i-1] <= A[i] >= A[i+1].
A[0] >= A[1]
A[n-1] >= A[n-2]
Problem: Find a Peak if it exists.
. n
A[i] >= 0
Look at the n/2 position
Algorithm: (Recusive)
1- If A[n/2] < A[n/2 -1] then only look at the left half [1 .. n/2] to look for a peak.
2- Else if A[n/2] < A[n/2 + 1] then only look at [n/2 .. n] for a peak.
3- Else n/2 position is a peak.
Complexity of the Algorithm:
T(n) = “work” algorithm does on input of size n
T(n) = T(n/2) + 2
We can prove that this version takes only log n time complexity.
?