SlideShare ist ein Scribd-Unternehmen logo
1 von 55
Downloaden Sie, um offline zu lesen
Lecture : 1Introduction to Algorithm and Data-Structures 
Instructor: Anum Masood 
1
• 
Course: 
CSC112 : Algorithms and Data Structures 
• 
Instructor: 
Anum Masood(Lecturer) : CS, CIIT/Wah 
• 
Office Hours: 
Thurs, 09:00 am -11:00 am, CS Faculty Hall 
2
Course Books 
• 
Data Structures Through C in depth, by S.K. Srivastavaand DeepaliSrivastava 
• 
Reference Books: 
1. 
Introduction to Algorithms, by Thomas H.Cormen(3rdEdition) 
2. 
Data Structures and Algorithm Analysis in C++, by Mark Allen Weiss (3rdEdition) 3
Evaluation Criteria 
S. No. 
Assessment Basis 
%age 
1 
Assignments 
5 
2 
Quizzes/ClassParticipation 
10 
3 
GroupProjectPresentationandReport 
10 
4 
1stSessional(after4weeks) 
10 
5 
2ndSessional(after9weeks) 
15 
6 
Finalexamination(after15weeks) 
50 
Total 
100 
4
Group Project 
• 
Total Weightage: 
10 % of Total Marks 
• 
Group Members: 
Max.3members(eachindividualonseparatemodule) 
After2ndSessional-WeeklyProgressReport 
ProjectDemonstrationalongwithProjectReport 
5
Course Objectives 
• 
Theobjectivesofthiscoursearetomakethestudentsableto: 
1. 
Introducedifferenttechniquesforanalyzingtheefficiencyofalgorithms 
2. 
Practicedevelopingefficientprogramswiththeusageofappropriatealgorithmsandsuitabledatastructures 
3. 
Gainfamiliaritywithanumberofclassicalproblemsthatoccurfrequentlyinreal-worldapplications. 
6
Course Outcomes 
• 
Attheendofthecourse,thestudentshouldbeableto: 
1. 
Understandtheproblemandaccordinglyproposespecificdatastructure. 
2. 
Analyzetheproposeddatastructureintermsofspacecomplexityandtimeefficiency. 
3. 
Modifyproposeddatastructureandalgorithmtobestfitthegivenproblem 
7
Tentative Lecture Topics 
8 
Lecture No. 
LectureTopic 
Sub-Topics 
1. 
Introduction 
Abstract Data Type 
String Operations 
Complexity of Algorithm 
O Notation 
Data Structure Operations 
2. 
Arrays 
What is Array? 
ArrayDeclaration 
Linear Arrays & Multi-dimensional Arrays representation in memory 
Traversing, Insertion, Deletion and Searching in Linear Arrays
Tentative Lecture Topics (Cont…) 
9 
Lecture No. 
LectureTopic 
Sub-Topics 
3. 
Matrices & Recursion 
Matrices Operations 
Recursion operation with examples 
4. 
Pointers 
What is pointer? 
PointerDeclaration 
Pointer to Pointer 
Pointer and Functions 
Pointers and Array 
Pointer with Multi-dimensional Arrays
Tentative Lecture Topics (Cont…) 
10 
Lecture No. 
LectureTopic 
Sub-Topics 
5. 
Structure 
What is Structure? 
StructureDeclaration 
Array of Structures 
Passing Structure to Function 
Pointer of Structures 
6. 
Linked List 
What is List? 
Representation of Linked Lists in Memory 
Traversing, Searching, Insertion and Deletionin Linked List 
1stSessional 
7. 
DoubleLinked List 
Reverse, Circular and Double Linked List
Tentative Lecture Topics (Cont…) 
11 
Lecture No. 
LectureTopic 
Sub-Topics 
8. 
Stacks 
StacksIntroduction 
Array Implementation of Stack (PUSH and POP operations) 
9. 
Queue 
QueueIntroduction 
Arrays Representation of Queue 
Insertion and Deletion in Queues 
Deques 
Priority Queue (Array Representation) 
10. 
Trees 
TreesIntroduction 
Binary Tree and Linked Representation of Binary Trees
Tentative Lecture Topics (Cont…) 
12 
Lecture No. 
LectureTopic 
Sub-Topics 
11. 
Binary Trees 
Traversing Binary Trees (Pre-Order, In- Order and Post-Order) 
Strictly, Complete and Extended Binary Tree 
12. 
Binary Search Tree 
Binary Search Tree Introduction 
Insertion and Deletion in Binary Search Trees 
13. 
Graph 
GraphIntroduction 
Representation of Graph 
Traversal in Graph 
Breadth First search and Depth First Search 
2ndSessional
Tentative Lecture Topics (Cont…) 
13 
Lecture No. 
LectureTopic 
Sub-Topics 
14. 
Huffman’s Algorithm 
Huffman’s Algorithm (Encoding and Decoding) 
15. 
Sorting 
SortingIntroduction 
Heap Sort, Bubble Sort, Selection Sort, Insertion Sort, Shell Sort, Quick Sorting 
16. 
Searching 
Sequential Searching 
Binary Searching 
17. 
Projects 
Project Demonstrations 
Report Submission 
Final Exams
Lecture Layout 
• 
Introduction to Algorithms 
• 
Introduction to Data Structures 
• 
Abstract Data Types (ADT) 
• 
String Operations 
14
Algorithms 
• 
Analgorithmisanywell-definedcomputationalprocedurethattakessomevaluesorsetofvaluesasinputandproducessomevalueorsetofvaluesasoutput. 
– 
An algorithm is thus a sequence of computational steps that transforms the input into output. 
15
Definition: Algorithm 
• 
Acomputeralgorithmisadetailedstepbystepmethodforsolvingtheproblem 
• 
AnalgorithmisasequenceofClear-cutinstructionsforsolvingtheprobleminafiniteamountoftime. 
• 
MoreGenerallyanalgorithmisanywelldefinedcomputationalprocedurethattakescollectionofelementsasinputandprocedurescollectionofelementsasoutput. 
• 
Analgorithmisasetofrulesforcarryingoutcalculationeitherbyhandoronamachine. 
• 
Analgorithmisanabstractionofaprogramtobeexecutedonaphysicalmachine(modelofComputation). 
16
Design and Analysis of Algorithms 
• 
The “design” pertain to: 
– 
the description of algorithm at an abstract level by means of a pseudo language, and 
– 
Proof of correctness that is, the algorithm solves the given problem in all cases 
• 
The “analysis” deals with performance evaluation (complexity analysis) 
17
Algorithm 
• 
An algorithm is a sequence of computational steps that transform the input into the output. 
• 
Example: 
– 
Sorting Problem 
– 
Input: A sequence of n numbers 
– 
Output: A permutation (reordering) of the input sequence 
18
Algorithm 
Problem 
Algorithm 
Input 
Output 
19
What's an algorithm? 
• 
An algorithm is a mathematical model for solving a problem. 
20
How do you learn algorithmsto cook? 
• 
To become good chef, you don’t learn lots of recipes by heart. 
• 
study existing recipes. 
• 
practice making them. 
• 
experiment with one’s own variations. 
• 
Until one can invent their own recipes. 
21
How do you learn about algorithms? 
• 
A good computer scientist doesn’t need to memorize a big book of algorithms: 
• 
We will begin studying famous algorithms. 
• 
You can practice executing them by hand. 
• 
Once you recognize more general patterns, you can experiment with your own variations. 
• 
At the end of the course, invent algorithms yourself. 
22
Analysis of Algorithms 
The theoretical study of computer-program performance and resource usage. What’s more important than performance? 
– 
modularity 
– 
correctness 
– 
maintainability 
– 
functionality 
– 
robustness 
– 
user-friendliness 
– 
simplicity 
– 
extensibility 
– 
reliability 
23
24 
Algorithms, Programming 
• 
Agoodunderstandingofalgorithmsisessentialforagoodunderstandingofthemostbasicelementofcomputersciencei.e.programming. 
• 
Unlikeaprogram,analgorithmisamathematicalentity, whichisindependentofaspecificprogramminglanguage, machine,orcompiler. 
• 
Thus,insomesense,algorithmdesignisallaboutthemathematicaltheorybehindthedesignofgoodprograms.
Why study algorithms and performance? 
• 
Algorithms help us to understand scalability. 
• 
Performance often draws the line between what is feasible and what is impossible. 
• 
Algorithmic mathematics provides a language for talking about program behavior. 
• 
Performance is the currency of computing. 
25
26 
Why Study Algorithms? 
• 
Necessary in any computer programming problem 
– 
Improve algorithm efficiency: run faster, process more data, do something that would otherwise be impossible 
– 
Solve problems of significantly large sizes 
• 
Compare algorithms 
• 
Algorithms as a field of study 
– 
Learn about a standard set of algorithms 
– 
New discoveries arise 
– 
Numerous application areas 
• 
Learn techniques of algorithm designand analysis 
• 
how does one coordinate the efforts of many programmers working on a single piece of software
27 
Applications 
• 
Multimedia 
– 
CD player, DVD, MP3, JPEG, DivX 
• 
Internet 
– 
Packet routing, data retrieval (Google) 
• 
Communication 
– 
Cell-phones, e-commerce 
• 
Computers 
– 
Circuit layout, file systems 
• 
Science 
– 
Human genome 
• 
Transportation 
– 
Airline crew scheduling, UPS deliveries
28 
Roadmap 
• 
Different problems 
– 
Sorting 
– 
Searching 
– 
String processing 
– 
Graph problems 
– 
Geometric problems 
– 
Numerical problems 
• 
Different design paradigms 
– 
Divide-and-conquer 
– 
Incremental 
– 
Dynamic programming 
– 
Greedy algorithms 
– 
Randomized/probabilistic 
– 
NP Completeness
29 
Analyzing Algorithms 
• 
Predict the amount of resources required: 
• 
memory: how much space is needed? 
• 
computational time: how fast the algorithm runs? 
• 
FACT: running time grows with the size of the input 
• 
Input size (number of elements in the input) 
– 
Size of an array, polynomial degree, # of elements in a matrix, # of bits in the binary representation of the input, vertices and edges in a graph 
Def: Running time = the number of primitive operations (steps) executed before termination 
– 
Arithmetic operations (+, -, *), data movement, control, decision making (if, while), comparison
Combinatorial Problems 
• 
These are problems that ask (explicitly or implicitly) to find a combinatorial object such as: 
– 
A permutation 
– 
A combination 
• 
That satisfies certain constraints and has some desired property. 
• 
E.g. 
– 
Traveling Salesperson problem 
30
Traveling Salesperson Problem 
31
Algorithms Analysis 
• 
How to analyze an algorithm? 
• 
Predicting the resourcesthat the algorithm requires: 
– 
Memory 
– 
Communications Bandwidth 
– 
Computational time 
32
Algorithms Analysis (Cont...) 
• 
Two parameters of measuring algorithm’s efficiency 
1. 
Time Efficiency 
 
How fast an algorithm in question runs 
2. 
Space Efficiency 
 
It deals with the extra space the algorithm requires 
33
Algorithms Time Complexity Analysis 
• 
We do not 
 
Determine the actual number of CPU cycles 
 
Want to count every instruction executed 
• 
We want a measure that is independent of the 
 
Computer 
 
Programming language 
 
Programmer 
 
Compiler used for coding 
34
Definition: Data Structures 
• 
Adatastructureissystematicwayoforganizingandaccessingdatawithaspecificrelationshipbetweentheelements. 
• 
Whydatastructures? 
Mainpurposeistowriteefficientprograms.Inordertodothat,oneneedstoorganizethedatainsuchawaythatitcanbeaccessedandmanipulatedefficiently. 
35
Data Structures (Cont…) 
• 
Adatastructureisanaggregationofdatacomponentsthattogetherconstituteameaningfulwhole. 
• 
Adatastructureisawayofarrangingdatainacomputer'smemoryorotherdiskstorage. 
• 
Adatastructureisacollectionofdata,organizedsothatitemscanbestoredandretrievedbysomefixedtechniques. 
• 
Example:Supposeyouarehiredtocreateadatabaseofnameswithallcompany'smanagementandemployees. 
36
Example of Data Structures 
• 
Youcanmakealist.Youcanalsomakeatree. 
37
Example of Data Structures (Cont…) 
• 
Youcanusestackdata-structureasHanoitower. 
38
Data Structures Types 
• 
Thereareseveralcommondatastructures:arrays, linkedlists,queues,stacks,binarytrees,hashtables, etc. 
• 
Thesedatastructurescanbeclassifiedaseitherlinearornonlineardatastructures,basedonhowthedataisconceptuallyorganizedoraggregated. 
39
Data Structures Classification 
Data Structures 
Linear 
Direct Access 
Array 
Sequential Access 
List 
Stack (LIFO) 
Queue (FIFO) 
Non-Linear 
Trees 
Hash Table 
Graphs 
40
Linear Vs Non-Linear Data Structures 
41 
LinearStructures 
Non-LinearStructures 
Array,list,queue,andstackbelongtothiscategory. 
Eachofthemisacollectionthatstoresitsentriesinalinearsequence. 
Theydifferintherestrictionstheyplaceonhowtheseentriesmaybeadded,removed,oraccessed. 
CommonrestrictionsincludeFIFOandLIFO. 
Treesandgraphsareclassicalnon- linearstructures. 
Dataentriesarenotarrangedinasequence,butwithdifferentrules.
Common Examples Stack 
42
Common Examples Queue 
43
Algorithms and Data Structures 
44
AbstractDataTypes(ADT) 
• 
Firstthingwhilewritingprogramsistoconsidertheproblem.Problemsinreallifearecomplicated. Descriptionoftheseproblemsinasimple,preciselanguageisrequired. 
• 
Example:consideraprogramthatmanagesthestudentrecords.AUniversityAdministratorasksyoutocreateaprogramwhichallowstoadministerthestudentsinaclass. 
• 
Vagueproblem->Simplifyit.Whatstudentinformationisneededfortherecord?Whatoperationsshouldbeallowed? 
45
Example of Abstraction 
• 
The abstract view of a Cell-Phone: 
Abstraction is hiding all the details 
User concern only with display icons and Applications. 
Unconcerned: About what are the inputs of the applications , or how circuit inside was constructed. 
46
Abstract Data Type 
• 
AnADTisamathematicalmodelofadatastructurethatspecifiesthetypeofdatastored, theoperationssupportedonthem,andthetypesofparametersoftheoperations. 
• 
AnADTspecifieswhateachoperationdoes,butnothowitdoesit. 
• 
Typically,anADTcanbeimplementedusingoneofmanydifferentdatastructures. 
• 
UsefulfirststepindecidingwhatdatastructuretouseinaprogramistospecifyanADTfortheprogram. 
47
List ADT 
• 
Component: 
Item 
• 
Operations: 
Insertion 
Deletion 
Search 
Display 
48
String 
• 
A stringis a sequence of characters from an alphabet. 
• 
Strings of particular interest are: 
– 
Text strings 
– 
Bit strings 
– 
Gene sequences 49
String Operations 
• 
String is a sequence of consecutive characters which ends with ‘0’. 
• 
String Operations along with the syntax can be: 
50 
Operations 
Traditional Syntax 
Length 
Length(string); 
Concatenation 
Concate(string1,string2); 
Comparison 
Compare(string1,string2); 
Copy 
CopyString(string1,string2); 
Substring 
Substring(string, strt_position_int, end_position_int);
String Operations (Cont…) 
Operations 
Description 
Example 
Length 
Thenumberofcharactersinthestring 
LENGTH(‘COMPUTER’)=8, 
LENGTH(‘ ’)=? 
Concatenation 
Tocombinetwostringsintoone. 
S1=‘Hello’, S2=‘Pakistan’ then 
S1+ S2= ‘HelloPakistan’ 
But 
S1+ ‘ ’ + S2= ‘Hello Pakistan’ 
Comparison 
Tocompareanytwostrings 
Returns<0,if(String1<String2) 
Returns=0,if(String1==String2) 
Returns>0,if(String1>String2) 
S1=‘Hello’, S2=‘Pakistan’ then ? 
COMPARE(S1, S2)=? 
S1=‘Welcome to Java’, S2= ‘Welcome to C’ then 
COMPARE(S1, S2)=? 
Copy 
Tocopythecharactersofonestringtotheanother 
S1=‘Data Structures’, S2=‘Algorithms’ 
Then Copy_String=(S1,S2) = ‘Algorithms’ 
Substring 
Displaythepartofstring 
SUBSTRING(‘The End’, 4, 4)=? 
51
Length of String 
Length(char*str) 
{ 
intcount=0; 
while(*str!=‘0’) 
{ 
count++; 
str++; 
} 
return(count); 
} 
52 
H 
E 
L 
L 
O 
‘0’ 
C=1 
C=2 
C=3 
C=4 
C=5 
Str=1 
Str=2 
Str=3 
Str=4 
Str=5 
Str=6 
Note: *strrefers to the value place at str 
While stris the position
Concatenation of String 
Concat(char*str1, char*str2) 
{ 
while(*str1!=‘0’) 
str1++; 
while(*str2!=‘0’) 
{ 
*str1=*str2; 
str1++; 
str2++; 
} 
str1=‘0’; 
} 
53 
H 
E 
L 
L 
O 
‘0’ 
Str1=1 
Str1=2 
Str1=3 
Str1=4 
Str1=5 
Str1=6 
Note: *strrefers to the value place at str 
While stris the position 
Str2=1 
Str2=2 
Str2=3 
Str2=4 
Str2=5 
Str2=6 
W 
O 
R 
L 
D 
‘0’ 
H 
E 
L 
L 
O 
W 
O 
R 
L 
D 
‘0’ 
Str1=1 
Str1=2 
Str1=3 
Str1=4 
Str1=5 
Str1=6 
Str1=7 
Str1=8 
Str1=9 
Str1=10 
Str1=11
String Comparison 
Value 
RelationbetweenComparedStringandComparingString 
0 
Theycompareequal 
< 0 
Eitherthevalueofthefirstcharacterthatdoesnotmatchislowerinthecomparedstring,orallcomparedcharactersmatchbutthecomparedstringisshorter. 
> 0 
Eitherthevalueofthefirstcharacterthatdoesnotmatchisgreaterinthecomparedstring,orallcomparedcharactersmatchbutthecomparedstringislonger. 
54
Questions 
55

Weitere ähnliche Inhalte

Was ist angesagt?

Computability and Complexity
Computability and ComplexityComputability and Complexity
Computability and ComplexityEdward Blurock
 
Pybcn machine learning for dummies with python
Pybcn machine learning for dummies with pythonPybcn machine learning for dummies with python
Pybcn machine learning for dummies with pythonJavier Arias Losada
 
Design & Analysis of Algorithms Lecture Notes
Design & Analysis of Algorithms Lecture NotesDesign & Analysis of Algorithms Lecture Notes
Design & Analysis of Algorithms Lecture NotesFellowBuddy.com
 
Text Classification with Lucene/Solr, Apache Hadoop and LibSVM
Text Classification with Lucene/Solr, Apache Hadoop and LibSVMText Classification with Lucene/Solr, Apache Hadoop and LibSVM
Text Classification with Lucene/Solr, Apache Hadoop and LibSVMlucenerevolution
 
Europython - Machine Learning for dummies with Python
Europython - Machine Learning for dummies with PythonEuropython - Machine Learning for dummies with Python
Europython - Machine Learning for dummies with PythonJavier Arias Losada
 
Aad introduction
Aad introductionAad introduction
Aad introductionMr SMAK
 
Introduction to Tree-LSTMs
Introduction to Tree-LSTMsIntroduction to Tree-LSTMs
Introduction to Tree-LSTMsDaniel Perez
 
Overview of machine learning
Overview of machine learning Overview of machine learning
Overview of machine learning SolivarLabs
 
Introduction to Data Analytics with R
Introduction to Data Analytics with RIntroduction to Data Analytics with R
Introduction to Data Analytics with RWei Zhong Toh
 
Important Concepts for Machine Learning
Important Concepts for Machine LearningImportant Concepts for Machine Learning
Important Concepts for Machine LearningSolivarLabs
 
Design and analysis of algorithms - Abstract View
Design and analysis of algorithms - Abstract ViewDesign and analysis of algorithms - Abstract View
Design and analysis of algorithms - Abstract ViewWaqas Nawaz
 
Towards typesafe deep learning in scala
Towards typesafe deep learning in scalaTowards typesafe deep learning in scala
Towards typesafe deep learning in scalaTongfei Chen
 
3Sem-Logic Design Notes-Unit4-Design and Analysis of Combinational Logic
 3Sem-Logic Design Notes-Unit4-Design and Analysis of Combinational Logic 3Sem-Logic Design Notes-Unit4-Design and Analysis of Combinational Logic
3Sem-Logic Design Notes-Unit4-Design and Analysis of Combinational LogicDr. Shivananda Koteshwar
 

Was ist angesagt? (20)

Computability and Complexity
Computability and ComplexityComputability and Complexity
Computability and Complexity
 
Pybcn machine learning for dummies with python
Pybcn machine learning for dummies with pythonPybcn machine learning for dummies with python
Pybcn machine learning for dummies with python
 
Design & Analysis of Algorithms Lecture Notes
Design & Analysis of Algorithms Lecture NotesDesign & Analysis of Algorithms Lecture Notes
Design & Analysis of Algorithms Lecture Notes
 
Text Classification with Lucene/Solr, Apache Hadoop and LibSVM
Text Classification with Lucene/Solr, Apache Hadoop and LibSVMText Classification with Lucene/Solr, Apache Hadoop and LibSVM
Text Classification with Lucene/Solr, Apache Hadoop and LibSVM
 
ADA complete notes
ADA complete notesADA complete notes
ADA complete notes
 
Europython - Machine Learning for dummies with Python
Europython - Machine Learning for dummies with PythonEuropython - Machine Learning for dummies with Python
Europython - Machine Learning for dummies with Python
 
Aad introduction
Aad introductionAad introduction
Aad introduction
 
Intro to automata theory
Intro to automata theoryIntro to automata theory
Intro to automata theory
 
Introduction to Tree-LSTMs
Introduction to Tree-LSTMsIntroduction to Tree-LSTMs
Introduction to Tree-LSTMs
 
Analysis of algorithm
Analysis of algorithmAnalysis of algorithm
Analysis of algorithm
 
Overview of machine learning
Overview of machine learning Overview of machine learning
Overview of machine learning
 
Unit 5
Unit 5Unit 5
Unit 5
 
Introduction to Data Analytics with R
Introduction to Data Analytics with RIntroduction to Data Analytics with R
Introduction to Data Analytics with R
 
QBIC
QBICQBIC
QBIC
 
Important Concepts for Machine Learning
Important Concepts for Machine LearningImportant Concepts for Machine Learning
Important Concepts for Machine Learning
 
Daa unit 1
Daa unit 1Daa unit 1
Daa unit 1
 
Design and analysis of algorithms - Abstract View
Design and analysis of algorithms - Abstract ViewDesign and analysis of algorithms - Abstract View
Design and analysis of algorithms - Abstract View
 
Towards typesafe deep learning in scala
Towards typesafe deep learning in scalaTowards typesafe deep learning in scala
Towards typesafe deep learning in scala
 
Unit7
Unit7Unit7
Unit7
 
3Sem-Logic Design Notes-Unit4-Design and Analysis of Combinational Logic
 3Sem-Logic Design Notes-Unit4-Design and Analysis of Combinational Logic 3Sem-Logic Design Notes-Unit4-Design and Analysis of Combinational Logic
3Sem-Logic Design Notes-Unit4-Design and Analysis of Combinational Logic
 

Andere mochten auch

Basic terminologies
Basic terminologiesBasic terminologies
Basic terminologiesRajendran
 
Elementary data organisation
Elementary data organisationElementary data organisation
Elementary data organisationMuzamil Hussain
 
Lecture 3 data structures and algorithms
Lecture 3 data structures and algorithmsLecture 3 data structures and algorithms
Lecture 3 data structures and algorithmsAakash deep Singhal
 
Lecture 1 data structures and algorithms
Lecture 1 data structures and algorithmsLecture 1 data structures and algorithms
Lecture 1 data structures and algorithmsAakash deep Singhal
 
DATA STRUCTURES
DATA STRUCTURESDATA STRUCTURES
DATA STRUCTURESbca2010
 

Andere mochten auch (6)

Basic terminologies
Basic terminologiesBasic terminologies
Basic terminologies
 
Elementary data organisation
Elementary data organisationElementary data organisation
Elementary data organisation
 
Lecture 3 data structures and algorithms
Lecture 3 data structures and algorithmsLecture 3 data structures and algorithms
Lecture 3 data structures and algorithms
 
Tree
TreeTree
Tree
 
Lecture 1 data structures and algorithms
Lecture 1 data structures and algorithmsLecture 1 data structures and algorithms
Lecture 1 data structures and algorithms
 
DATA STRUCTURES
DATA STRUCTURESDATA STRUCTURES
DATA STRUCTURES
 

Ähnlich wie Introduction to Algorithms and Data Structures Lecture

Algorithms and Data Structures
Algorithms and Data StructuresAlgorithms and Data Structures
Algorithms and Data Structuressonykhan3
 
Lec01-Algorithems - Introduction and Overview.pdf
Lec01-Algorithems - Introduction and Overview.pdfLec01-Algorithems - Introduction and Overview.pdf
Lec01-Algorithems - Introduction and Overview.pdfMAJDABDALLAH3
 
Chapter1.1 Introduction.ppt
Chapter1.1 Introduction.pptChapter1.1 Introduction.ppt
Chapter1.1 Introduction.pptTekle12
 
Chapter1.1 Introduction to design and analysis of algorithm.ppt
Chapter1.1 Introduction to design and analysis of algorithm.pptChapter1.1 Introduction to design and analysis of algorithm.ppt
Chapter1.1 Introduction to design and analysis of algorithm.pptTekle12
 
Design and Analysis Algorithms.pdf
Design and Analysis Algorithms.pdfDesign and Analysis Algorithms.pdf
Design and Analysis Algorithms.pdfHarshNagda5
 
Data Structure and Algorithms
Data Structure and AlgorithmsData Structure and Algorithms
Data Structure and Algorithmsiqbalphy1
 
Unit I- Data structures Introduction, Evaluation of Algorithms, Arrays, Spars...
Unit I- Data structures Introduction, Evaluation of Algorithms, Arrays, Spars...Unit I- Data structures Introduction, Evaluation of Algorithms, Arrays, Spars...
Unit I- Data structures Introduction, Evaluation of Algorithms, Arrays, Spars...DrkhanchanaR
 
Data structures and algorithms Module-1.pdf
Data structures and algorithms Module-1.pdfData structures and algorithms Module-1.pdf
Data structures and algorithms Module-1.pdfDukeCalvin
 
Algo_Lecture01.pptx
Algo_Lecture01.pptxAlgo_Lecture01.pptx
Algo_Lecture01.pptxShaistaRiaz4
 
a581a6a2cb5778045788f0b1d7da1c0236f.pptx
a581a6a2cb5778045788f0b1d7da1c0236f.pptxa581a6a2cb5778045788f0b1d7da1c0236f.pptx
a581a6a2cb5778045788f0b1d7da1c0236f.pptxchristinamary2620
 
Csci101 lect00 introduction
Csci101 lect00 introductionCsci101 lect00 introduction
Csci101 lect00 introductionElsayed Hemayed
 
clean architecture uncle bob AnalysisAndDesign.el.en.pptx
clean architecture uncle bob AnalysisAndDesign.el.en.pptxclean architecture uncle bob AnalysisAndDesign.el.en.pptx
clean architecture uncle bob AnalysisAndDesign.el.en.pptxsaber tabatabaee
 
Data structure and algorithm.
Data structure and algorithm. Data structure and algorithm.
Data structure and algorithm. Abdul salam
 

Ähnlich wie Introduction to Algorithms and Data Structures Lecture (20)

Algorithms and Data Structures
Algorithms and Data StructuresAlgorithms and Data Structures
Algorithms and Data Structures
 
Intro_2.ppt
Intro_2.pptIntro_2.ppt
Intro_2.ppt
 
Intro.ppt
Intro.pptIntro.ppt
Intro.ppt
 
Intro.ppt
Intro.pptIntro.ppt
Intro.ppt
 
Lec01-Algorithems - Introduction and Overview.pdf
Lec01-Algorithems - Introduction and Overview.pdfLec01-Algorithems - Introduction and Overview.pdf
Lec01-Algorithems - Introduction and Overview.pdf
 
Chapter1.1 Introduction.ppt
Chapter1.1 Introduction.pptChapter1.1 Introduction.ppt
Chapter1.1 Introduction.ppt
 
Chapter1.1 Introduction to design and analysis of algorithm.ppt
Chapter1.1 Introduction to design and analysis of algorithm.pptChapter1.1 Introduction to design and analysis of algorithm.ppt
Chapter1.1 Introduction to design and analysis of algorithm.ppt
 
Design and Analysis Algorithms.pdf
Design and Analysis Algorithms.pdfDesign and Analysis Algorithms.pdf
Design and Analysis Algorithms.pdf
 
Data Structure and Algorithms
Data Structure and AlgorithmsData Structure and Algorithms
Data Structure and Algorithms
 
Unit I- Data structures Introduction, Evaluation of Algorithms, Arrays, Spars...
Unit I- Data structures Introduction, Evaluation of Algorithms, Arrays, Spars...Unit I- Data structures Introduction, Evaluation of Algorithms, Arrays, Spars...
Unit I- Data structures Introduction, Evaluation of Algorithms, Arrays, Spars...
 
lect 1-ds algo(final)_2.pdf
lect 1-ds  algo(final)_2.pdflect 1-ds  algo(final)_2.pdf
lect 1-ds algo(final)_2.pdf
 
Lec1.ppt
Lec1.pptLec1.ppt
Lec1.ppt
 
Data structures and algorithms Module-1.pdf
Data structures and algorithms Module-1.pdfData structures and algorithms Module-1.pdf
Data structures and algorithms Module-1.pdf
 
Algo_Lecture01.pptx
Algo_Lecture01.pptxAlgo_Lecture01.pptx
Algo_Lecture01.pptx
 
Unit no_1.pptx
Unit no_1.pptxUnit no_1.pptx
Unit no_1.pptx
 
lecture 01.1.ppt
lecture 01.1.pptlecture 01.1.ppt
lecture 01.1.ppt
 
a581a6a2cb5778045788f0b1d7da1c0236f.pptx
a581a6a2cb5778045788f0b1d7da1c0236f.pptxa581a6a2cb5778045788f0b1d7da1c0236f.pptx
a581a6a2cb5778045788f0b1d7da1c0236f.pptx
 
Csci101 lect00 introduction
Csci101 lect00 introductionCsci101 lect00 introduction
Csci101 lect00 introduction
 
clean architecture uncle bob AnalysisAndDesign.el.en.pptx
clean architecture uncle bob AnalysisAndDesign.el.en.pptxclean architecture uncle bob AnalysisAndDesign.el.en.pptx
clean architecture uncle bob AnalysisAndDesign.el.en.pptx
 
Data structure and algorithm.
Data structure and algorithm. Data structure and algorithm.
Data structure and algorithm.
 

Introduction to Algorithms and Data Structures Lecture

  • 1. Lecture : 1Introduction to Algorithm and Data-Structures Instructor: Anum Masood 1
  • 2. • Course: CSC112 : Algorithms and Data Structures • Instructor: Anum Masood(Lecturer) : CS, CIIT/Wah • Office Hours: Thurs, 09:00 am -11:00 am, CS Faculty Hall 2
  • 3. Course Books • Data Structures Through C in depth, by S.K. Srivastavaand DeepaliSrivastava • Reference Books: 1. Introduction to Algorithms, by Thomas H.Cormen(3rdEdition) 2. Data Structures and Algorithm Analysis in C++, by Mark Allen Weiss (3rdEdition) 3
  • 4. Evaluation Criteria S. No. Assessment Basis %age 1 Assignments 5 2 Quizzes/ClassParticipation 10 3 GroupProjectPresentationandReport 10 4 1stSessional(after4weeks) 10 5 2ndSessional(after9weeks) 15 6 Finalexamination(after15weeks) 50 Total 100 4
  • 5. Group Project • Total Weightage: 10 % of Total Marks • Group Members: Max.3members(eachindividualonseparatemodule) After2ndSessional-WeeklyProgressReport ProjectDemonstrationalongwithProjectReport 5
  • 6. Course Objectives • Theobjectivesofthiscoursearetomakethestudentsableto: 1. Introducedifferenttechniquesforanalyzingtheefficiencyofalgorithms 2. Practicedevelopingefficientprogramswiththeusageofappropriatealgorithmsandsuitabledatastructures 3. Gainfamiliaritywithanumberofclassicalproblemsthatoccurfrequentlyinreal-worldapplications. 6
  • 7. Course Outcomes • Attheendofthecourse,thestudentshouldbeableto: 1. Understandtheproblemandaccordinglyproposespecificdatastructure. 2. Analyzetheproposeddatastructureintermsofspacecomplexityandtimeefficiency. 3. Modifyproposeddatastructureandalgorithmtobestfitthegivenproblem 7
  • 8. Tentative Lecture Topics 8 Lecture No. LectureTopic Sub-Topics 1. Introduction Abstract Data Type String Operations Complexity of Algorithm O Notation Data Structure Operations 2. Arrays What is Array? ArrayDeclaration Linear Arrays & Multi-dimensional Arrays representation in memory Traversing, Insertion, Deletion and Searching in Linear Arrays
  • 9. Tentative Lecture Topics (Cont…) 9 Lecture No. LectureTopic Sub-Topics 3. Matrices & Recursion Matrices Operations Recursion operation with examples 4. Pointers What is pointer? PointerDeclaration Pointer to Pointer Pointer and Functions Pointers and Array Pointer with Multi-dimensional Arrays
  • 10. Tentative Lecture Topics (Cont…) 10 Lecture No. LectureTopic Sub-Topics 5. Structure What is Structure? StructureDeclaration Array of Structures Passing Structure to Function Pointer of Structures 6. Linked List What is List? Representation of Linked Lists in Memory Traversing, Searching, Insertion and Deletionin Linked List 1stSessional 7. DoubleLinked List Reverse, Circular and Double Linked List
  • 11. Tentative Lecture Topics (Cont…) 11 Lecture No. LectureTopic Sub-Topics 8. Stacks StacksIntroduction Array Implementation of Stack (PUSH and POP operations) 9. Queue QueueIntroduction Arrays Representation of Queue Insertion and Deletion in Queues Deques Priority Queue (Array Representation) 10. Trees TreesIntroduction Binary Tree and Linked Representation of Binary Trees
  • 12. Tentative Lecture Topics (Cont…) 12 Lecture No. LectureTopic Sub-Topics 11. Binary Trees Traversing Binary Trees (Pre-Order, In- Order and Post-Order) Strictly, Complete and Extended Binary Tree 12. Binary Search Tree Binary Search Tree Introduction Insertion and Deletion in Binary Search Trees 13. Graph GraphIntroduction Representation of Graph Traversal in Graph Breadth First search and Depth First Search 2ndSessional
  • 13. Tentative Lecture Topics (Cont…) 13 Lecture No. LectureTopic Sub-Topics 14. Huffman’s Algorithm Huffman’s Algorithm (Encoding and Decoding) 15. Sorting SortingIntroduction Heap Sort, Bubble Sort, Selection Sort, Insertion Sort, Shell Sort, Quick Sorting 16. Searching Sequential Searching Binary Searching 17. Projects Project Demonstrations Report Submission Final Exams
  • 14. Lecture Layout • Introduction to Algorithms • Introduction to Data Structures • Abstract Data Types (ADT) • String Operations 14
  • 15. Algorithms • Analgorithmisanywell-definedcomputationalprocedurethattakessomevaluesorsetofvaluesasinputandproducessomevalueorsetofvaluesasoutput. – An algorithm is thus a sequence of computational steps that transforms the input into output. 15
  • 16. Definition: Algorithm • Acomputeralgorithmisadetailedstepbystepmethodforsolvingtheproblem • AnalgorithmisasequenceofClear-cutinstructionsforsolvingtheprobleminafiniteamountoftime. • MoreGenerallyanalgorithmisanywelldefinedcomputationalprocedurethattakescollectionofelementsasinputandprocedurescollectionofelementsasoutput. • Analgorithmisasetofrulesforcarryingoutcalculationeitherbyhandoronamachine. • Analgorithmisanabstractionofaprogramtobeexecutedonaphysicalmachine(modelofComputation). 16
  • 17. Design and Analysis of Algorithms • The “design” pertain to: – the description of algorithm at an abstract level by means of a pseudo language, and – Proof of correctness that is, the algorithm solves the given problem in all cases • The “analysis” deals with performance evaluation (complexity analysis) 17
  • 18. Algorithm • An algorithm is a sequence of computational steps that transform the input into the output. • Example: – Sorting Problem – Input: A sequence of n numbers – Output: A permutation (reordering) of the input sequence 18
  • 19. Algorithm Problem Algorithm Input Output 19
  • 20. What's an algorithm? • An algorithm is a mathematical model for solving a problem. 20
  • 21. How do you learn algorithmsto cook? • To become good chef, you don’t learn lots of recipes by heart. • study existing recipes. • practice making them. • experiment with one’s own variations. • Until one can invent their own recipes. 21
  • 22. How do you learn about algorithms? • A good computer scientist doesn’t need to memorize a big book of algorithms: • We will begin studying famous algorithms. • You can practice executing them by hand. • Once you recognize more general patterns, you can experiment with your own variations. • At the end of the course, invent algorithms yourself. 22
  • 23. Analysis of Algorithms The theoretical study of computer-program performance and resource usage. What’s more important than performance? – modularity – correctness – maintainability – functionality – robustness – user-friendliness – simplicity – extensibility – reliability 23
  • 24. 24 Algorithms, Programming • Agoodunderstandingofalgorithmsisessentialforagoodunderstandingofthemostbasicelementofcomputersciencei.e.programming. • Unlikeaprogram,analgorithmisamathematicalentity, whichisindependentofaspecificprogramminglanguage, machine,orcompiler. • Thus,insomesense,algorithmdesignisallaboutthemathematicaltheorybehindthedesignofgoodprograms.
  • 25. Why study algorithms and performance? • Algorithms help us to understand scalability. • Performance often draws the line between what is feasible and what is impossible. • Algorithmic mathematics provides a language for talking about program behavior. • Performance is the currency of computing. 25
  • 26. 26 Why Study Algorithms? • Necessary in any computer programming problem – Improve algorithm efficiency: run faster, process more data, do something that would otherwise be impossible – Solve problems of significantly large sizes • Compare algorithms • Algorithms as a field of study – Learn about a standard set of algorithms – New discoveries arise – Numerous application areas • Learn techniques of algorithm designand analysis • how does one coordinate the efforts of many programmers working on a single piece of software
  • 27. 27 Applications • Multimedia – CD player, DVD, MP3, JPEG, DivX • Internet – Packet routing, data retrieval (Google) • Communication – Cell-phones, e-commerce • Computers – Circuit layout, file systems • Science – Human genome • Transportation – Airline crew scheduling, UPS deliveries
  • 28. 28 Roadmap • Different problems – Sorting – Searching – String processing – Graph problems – Geometric problems – Numerical problems • Different design paradigms – Divide-and-conquer – Incremental – Dynamic programming – Greedy algorithms – Randomized/probabilistic – NP Completeness
  • 29. 29 Analyzing Algorithms • Predict the amount of resources required: • memory: how much space is needed? • computational time: how fast the algorithm runs? • FACT: running time grows with the size of the input • Input size (number of elements in the input) – Size of an array, polynomial degree, # of elements in a matrix, # of bits in the binary representation of the input, vertices and edges in a graph Def: Running time = the number of primitive operations (steps) executed before termination – Arithmetic operations (+, -, *), data movement, control, decision making (if, while), comparison
  • 30. Combinatorial Problems • These are problems that ask (explicitly or implicitly) to find a combinatorial object such as: – A permutation – A combination • That satisfies certain constraints and has some desired property. • E.g. – Traveling Salesperson problem 30
  • 32. Algorithms Analysis • How to analyze an algorithm? • Predicting the resourcesthat the algorithm requires: – Memory – Communications Bandwidth – Computational time 32
  • 33. Algorithms Analysis (Cont...) • Two parameters of measuring algorithm’s efficiency 1. Time Efficiency  How fast an algorithm in question runs 2. Space Efficiency  It deals with the extra space the algorithm requires 33
  • 34. Algorithms Time Complexity Analysis • We do not  Determine the actual number of CPU cycles  Want to count every instruction executed • We want a measure that is independent of the  Computer  Programming language  Programmer  Compiler used for coding 34
  • 35. Definition: Data Structures • Adatastructureissystematicwayoforganizingandaccessingdatawithaspecificrelationshipbetweentheelements. • Whydatastructures? Mainpurposeistowriteefficientprograms.Inordertodothat,oneneedstoorganizethedatainsuchawaythatitcanbeaccessedandmanipulatedefficiently. 35
  • 36. Data Structures (Cont…) • Adatastructureisanaggregationofdatacomponentsthattogetherconstituteameaningfulwhole. • Adatastructureisawayofarrangingdatainacomputer'smemoryorotherdiskstorage. • Adatastructureisacollectionofdata,organizedsothatitemscanbestoredandretrievedbysomefixedtechniques. • Example:Supposeyouarehiredtocreateadatabaseofnameswithallcompany'smanagementandemployees. 36
  • 37. Example of Data Structures • Youcanmakealist.Youcanalsomakeatree. 37
  • 38. Example of Data Structures (Cont…) • Youcanusestackdata-structureasHanoitower. 38
  • 39. Data Structures Types • Thereareseveralcommondatastructures:arrays, linkedlists,queues,stacks,binarytrees,hashtables, etc. • Thesedatastructurescanbeclassifiedaseitherlinearornonlineardatastructures,basedonhowthedataisconceptuallyorganizedoraggregated. 39
  • 40. Data Structures Classification Data Structures Linear Direct Access Array Sequential Access List Stack (LIFO) Queue (FIFO) Non-Linear Trees Hash Table Graphs 40
  • 41. Linear Vs Non-Linear Data Structures 41 LinearStructures Non-LinearStructures Array,list,queue,andstackbelongtothiscategory. Eachofthemisacollectionthatstoresitsentriesinalinearsequence. Theydifferintherestrictionstheyplaceonhowtheseentriesmaybeadded,removed,oraccessed. CommonrestrictionsincludeFIFOandLIFO. Treesandgraphsareclassicalnon- linearstructures. Dataentriesarenotarrangedinasequence,butwithdifferentrules.
  • 44. Algorithms and Data Structures 44
  • 45. AbstractDataTypes(ADT) • Firstthingwhilewritingprogramsistoconsidertheproblem.Problemsinreallifearecomplicated. Descriptionoftheseproblemsinasimple,preciselanguageisrequired. • Example:consideraprogramthatmanagesthestudentrecords.AUniversityAdministratorasksyoutocreateaprogramwhichallowstoadministerthestudentsinaclass. • Vagueproblem->Simplifyit.Whatstudentinformationisneededfortherecord?Whatoperationsshouldbeallowed? 45
  • 46. Example of Abstraction • The abstract view of a Cell-Phone: Abstraction is hiding all the details User concern only with display icons and Applications. Unconcerned: About what are the inputs of the applications , or how circuit inside was constructed. 46
  • 47. Abstract Data Type • AnADTisamathematicalmodelofadatastructurethatspecifiesthetypeofdatastored, theoperationssupportedonthem,andthetypesofparametersoftheoperations. • AnADTspecifieswhateachoperationdoes,butnothowitdoesit. • Typically,anADTcanbeimplementedusingoneofmanydifferentdatastructures. • UsefulfirststepindecidingwhatdatastructuretouseinaprogramistospecifyanADTfortheprogram. 47
  • 48. List ADT • Component: Item • Operations: Insertion Deletion Search Display 48
  • 49. String • A stringis a sequence of characters from an alphabet. • Strings of particular interest are: – Text strings – Bit strings – Gene sequences 49
  • 50. String Operations • String is a sequence of consecutive characters which ends with ‘0’. • String Operations along with the syntax can be: 50 Operations Traditional Syntax Length Length(string); Concatenation Concate(string1,string2); Comparison Compare(string1,string2); Copy CopyString(string1,string2); Substring Substring(string, strt_position_int, end_position_int);
  • 51. String Operations (Cont…) Operations Description Example Length Thenumberofcharactersinthestring LENGTH(‘COMPUTER’)=8, LENGTH(‘ ’)=? Concatenation Tocombinetwostringsintoone. S1=‘Hello’, S2=‘Pakistan’ then S1+ S2= ‘HelloPakistan’ But S1+ ‘ ’ + S2= ‘Hello Pakistan’ Comparison Tocompareanytwostrings Returns<0,if(String1<String2) Returns=0,if(String1==String2) Returns>0,if(String1>String2) S1=‘Hello’, S2=‘Pakistan’ then ? COMPARE(S1, S2)=? S1=‘Welcome to Java’, S2= ‘Welcome to C’ then COMPARE(S1, S2)=? Copy Tocopythecharactersofonestringtotheanother S1=‘Data Structures’, S2=‘Algorithms’ Then Copy_String=(S1,S2) = ‘Algorithms’ Substring Displaythepartofstring SUBSTRING(‘The End’, 4, 4)=? 51
  • 52. Length of String Length(char*str) { intcount=0; while(*str!=‘0’) { count++; str++; } return(count); } 52 H E L L O ‘0’ C=1 C=2 C=3 C=4 C=5 Str=1 Str=2 Str=3 Str=4 Str=5 Str=6 Note: *strrefers to the value place at str While stris the position
  • 53. Concatenation of String Concat(char*str1, char*str2) { while(*str1!=‘0’) str1++; while(*str2!=‘0’) { *str1=*str2; str1++; str2++; } str1=‘0’; } 53 H E L L O ‘0’ Str1=1 Str1=2 Str1=3 Str1=4 Str1=5 Str1=6 Note: *strrefers to the value place at str While stris the position Str2=1 Str2=2 Str2=3 Str2=4 Str2=5 Str2=6 W O R L D ‘0’ H E L L O W O R L D ‘0’ Str1=1 Str1=2 Str1=3 Str1=4 Str1=5 Str1=6 Str1=7 Str1=8 Str1=9 Str1=10 Str1=11
  • 54. String Comparison Value RelationbetweenComparedStringandComparingString 0 Theycompareequal < 0 Eitherthevalueofthefirstcharacterthatdoesnotmatchislowerinthecomparedstring,orallcomparedcharactersmatchbutthecomparedstringisshorter. > 0 Eitherthevalueofthefirstcharacterthatdoesnotmatchisgreaterinthecomparedstring,orallcomparedcharactersmatchbutthecomparedstringislonger. 54