SlideShare a Scribd company logo
College of Computing & Information Technology
King Abdulaziz University
Data Structures I
CPCS-204 – Introduction
CPCS-204 – Data Structures I
Data Structures I: An Introduction page 2
© Dr. Jonathan (Yahya) Cazalas
Data Structures 1: Introduction
 Course Learning Outcomes
 Upon completion of this course, you will be able to:
1. Describe the needs of data structures in problem solving.
2. Design and implement abstract data types.
3. Represent and implement linear/non-linear data
structures.
4. Build stacks and queues using arrays and linked lists.
5. Select and apply appropriate data structures in problem
solving.
6. Select and apply basic searching and sorting algorithms
in problem solving.
7. Apply recursion to solve simple problems.
8. Trace the output of a given piece of code or algorithm.
Data Structures I: An Introduction page 3
© Dr. Jonathan (Yahya) Cazalas
Data Structures 1: Introduction
 How is CPCS-204 different than CPCS-203?
 CPCS-203 teaches how to program in Java
 Language basics, variable declarations, conditional
expressions, if statements, loops, functions, arrays,
structures, and even Object-Oriented programming
 This will not be covered in this class
 You will need to freshen up on your Java very quickly
 If you need help, but a good Java-language book or find a
quality reference online
 With respect to Java, we will cover:
 Arrays and array processing (although already taught)
 Some of the Java Collections as needed
Data Structures I: An Introduction page 4
© Dr. Jonathan (Yahya) Cazalas
Data Structures 1: Introduction
 The goals of Data Structures I:
 Improve knowledge of standard data structures
and abstract data types
 Improve knowledge of standard algorithms used
to solve several classical problems
 Cover some mathematical concepts that are
useful for the analysis of algorithms
 Analyze the efficiency of solutions to problems
 Learn about and be comfortable with recursion
Data Structures I: An Introduction page 5
© Dr. Jonathan (Yahya) Cazalas
Data Structures 1: Introduction
 The goals of Computer Science I:
 In CPCS-202 and 203, we only cared if we
found a solution to the problem at hand
 Didn’t really pay attention to the efficiency of the
answer
 For this class:
 We learn standard ways to solve problems
 And how to analyze the efficiency of those solutions
 Finally, we simply expand upon our knowledge of our
use of the JAVA programming language
Data Structures I: An Introduction page 6
© Dr. Jonathan (Yahya) Cazalas
Data Structures 1: Introduction
 Teaching Method:
 This class is NOT used to teach you JAVA
 The focus of CPCS-202 and 203 (not this class) is to
teach you JAVA
 You should know JAVA already
 In CPCS-202 and 203, majority of time was spent going
of syntax of JAVA
 Programs were often shown in class
 Programs were even written during class
 Essentially a requirement for any course teaching a
programming language
Data Structures I: An Introduction page 7
© Dr. Jonathan (Yahya) Cazalas
Data Structures 1: Introduction
 Teaching Method:
 Majority of this class is used covering new data
structures, abstract data types, and algorithm
analysis
 The teaching of these concepts dictate more
explanation and less of a focus on “code”
 Some code will be shown on the PowerPoint slides
 Such as after we explain a new abstract data type
 We’ll show the code of how you would implement it
 However, writing of actual code will most likely
never be done in class
 Again, that is not the purpose of this class
Data Structures I: An Introduction page 8
© Dr. Jonathan (Yahya) Cazalas
Data Structures 1: Introduction
 Example Problem:
 We will now go over two solutions to a problem
 The first is a straightforward solution that a CPCS-203
student should be able to come up with
 Doesn’t’ care about efficiency
 The second solution is one that a CPCS-204 student
should be able to come up with after some thought
 Cares about efficiency
 Hopefully this example will illustrate part of the
goal of this course
Data Structures I: An Introduction page 9
© Dr. Jonathan (Yahya) Cazalas
Data Structures 1: Introduction
 Max Number of 1’s:
 You are given an nxn integer array
 Say, for example, a 100x100 sized array
 Each row is filled with several 1’s followed by all
0’s
 Example:
 Row 1 may have 38 1’s followed by 62 0’s
 Row 2 may have 73 1’s followed by 27 0’s
 Row 3 may have 12 1’s followed by 82 0’s
 You get the idea
 The goal of the problem is to identify the row
that has the maximum number of 1’s.
Data Structures I: An Introduction page 10
© Dr. Jonathan (Yahya) Cazalas
Data Structures 1: Introduction
 Max Number of 1’s:
 Straightforward CPCS-203 style solution:
 Make a variable called MaxOnes and set equal
to 0
 For each row do the following:
 Start from the beginning of the row on the left side
 Scan left to right, counting the number of 1’s until the first zero
is encountered
 If the number of 1’s is greater than the value stored in
MaxOnes, update MaxOnes with the number of 1’s seen on
this row
 Clearly, this works
 But let’s see how long this algorithm will take
Data Structures I: An Introduction page 11
© Dr. Jonathan (Yahya) Cazalas
Data Structures 1: Introduction
 Max Number of 1’s:
 Analysis of Straightforward Solution:
 Basically we iterate through each square that contains
a 1, as well as the first 0 in each row
 If all cells were 0, we would only “visit” one cell per
row, resulting in n visited cells
 However, if all cells were 1’s, we would “visit” all of the
cells (n2 total)
 So in the worst case, the number of simple steps the
algorithm takes would be approximately n2
 This makes the running time of this algorithm O(n2)
 The meaning of this Big-O will be discussed later in the
semester
Data Structures I: An Introduction page 12
© Dr. Jonathan (Yahya) Cazalas
Data Structures 1: Introduction
 Max Number of 1’s:
 Analysis of Straightforward Solution:
 There seems to be extra work done here
 Once we know that a row has 12 1’s, for example, it
seems pointless to start checking at the beginning of
the next row
 Why not just start at column 12
 If it’s a 0, then that row can’t be the winner
 If it is a 1, then clearly there is no point in going back, on
that row, and checking the previous 11 squares
 This idea leads to a more efficient algorithm
Data Structures I: An Introduction page 13
© Dr. Jonathan (Yahya) Cazalas
Data Structures 1: Introduction
 Max Number of 1’s:
 More Efficient CPCS-204 style algorithm:
1. Initialize the current row and current column to 0
2. While the current row is less than n (or before the
last row)
a. While the cell at the current row and column is 1
 Increment the current column
b. Increment the current row
3. The current column index represents the
maximum number of 1’s seen
4. Now let’s trace through a couple of examples
Data Structures I: An Introduction page 14
© Dr. Jonathan (Yahya) Cazalas
Data Structures 1: Introduction
 Max Number of 1’s:
 Example 1:
1 1 0 0 0 0
0 0 0 0 0 0
1 1 1 0 0 0
1 1 1 0 0 0
1 1 1 1 1 0
1 1 1 1 0 0
Data Structures I: An Introduction page 15
© Dr. Jonathan (Yahya) Cazalas
Data Structures 1: Introduction
 Max Number of 1’s:
 Example 2:
0 0 0 0 0 0 0 0
1 1 0 0 0 0 0 0
1 0 0 0 0 0 0 0
1 1 1 0 0 0 0 0
1 1 1 1 0 0 0 0
1 1 1 1 1 1 0 0
1 1 1 1 1 1 1 0
1 1 1 1 1 1 1 1
Data Structures I: An Introduction page 16
© Dr. Jonathan (Yahya) Cazalas
Data Structures 1: Introduction
 Max Number of 1’s:
 Analysis of Better Solution:
 How many steps will this algorithm take, in terms of n?
 Each “step” taken by the algorithm either goes to the right
or down in the table.
 There are a maximum of n-1 steps to the right
 And a maximum of n-1 steps down that could be taken
 Thus the maximum number of “steps” that can be done
during this algorithm is approximately 2n
 And this is the worst case
 So the running time of this algorithm is O(n)
 An improvement of the previous algorithm
 Input size of 100 for n
 n2 would be 10,000 steps and 2n would be 200 steps
Data Structures I: An Introduction page 17
© Dr. Jonathan (Yahya) Cazalas
Data Structures 1: Introduction
 Implementing an Algorithm in JAVA:
 In this class, you will have an opportunity to
improve upon your ability to write programs that
implement an algorithm you have learned
 You must know the syntax of JAVA in order to
properly and effective do this
 There’s no set way to create code to implement
an algorithm
 But this example shows some steps you can take in
doing so
Data Structures I: An Introduction page 18
© Dr. Jonathan (Yahya) Cazalas
Data Structures 1: Introduction
 Implementing an Algorithm in JAVA:
 Here are some issues to think about:
1. What data structures are going to be used?
2. What functions are going to be used?
3. What run-time errors should we protect
against?
4. What atypical cases may we have to deal with?
5. What is an efficient way to execute the steps in
the algorithm?
Data Structures I: An Introduction page 19
© Dr. Jonathan (Yahya) Cazalas
Data Structures 1: Introduction
 Maximum Number of 1’s
 This was a creative exercise
 Much of what you learn in class will not be
 We have many set algorithms and data
structures that you will study
 Occasionally you will have to come up with new
ideas like this one
 Mostly, however, you will simply have to apply
the data structures and algorithms shown in
class fairly directly to solve the given problems
Data Structures I: An Introduction page 20
© Dr. Jonathan (Yahya) Cazalas
Data Structures 1: Introduction
Are
You
Excited?
Data Structures I: An Introduction page 21
© Dr. Jonathan (Yahya) Cazalas
Daily FAIL Picture
 Fail pictures
 During the slides (or at the end), we will often
give a funny “fail” picture or a “human stupidity”
picture
 Helps to “lighten things up”
 So here you go:
Data Structures I: An Introduction page 22
© Dr. Jonathan (Yahya) Cazalas
Human Stupidity
College of Computing & Information Technology
King Abdulaziz University
Data Structures I
CPCS-204 – Introduction
CPCS-204 – Data Structures I

More Related Content

PDF
arrays
Mohamed Elsayed
 
PDF
Data Structures and Algorithms Made Easy in Java ( PDFDrive ).pdf
AbdurraufSharifaiGar
 
PPTX
Doubly & Circular Linked Lists
Afaq Mansoor Khan
 
PPTX
AVL Tree Data Structure
Afaq Mansoor Khan
 
PDF
Statistics lecture 8 (chapter 7)
jillmitchell8778
 
PDF
Data visualization introduction
ManokamnaKochar1
 
PDF
SQL
Ravi Bansal
 
PPT
stack and queue array implementation in java.
CIIT Atd.
 
Data Structures and Algorithms Made Easy in Java ( PDFDrive ).pdf
AbdurraufSharifaiGar
 
Doubly & Circular Linked Lists
Afaq Mansoor Khan
 
AVL Tree Data Structure
Afaq Mansoor Khan
 
Statistics lecture 8 (chapter 7)
jillmitchell8778
 
Data visualization introduction
ManokamnaKochar1
 
stack and queue array implementation in java.
CIIT Atd.
 

What's hot (20)

PDF
binary_search
Mohamed Elsayed
 
PDF
linked_lists
Mohamed Elsayed
 
PDF
hash_tables
Mohamed Elsayed
 
PPTX
Tree in data structure
ghhgj jhgh
 
PPTX
Excel Pivot Tables
Matt Saragusa
 
PPTX
Trees
Burhan Ahmed
 
PDF
stacks1
Mohamed Elsayed
 
PPT
Binary tree
Vanitha Chandru
 
PPTX
Avl trees final
PRAKASH RANJAN SINGH
 
PPT
Binary Trees.ppt
Riannel Tecson
 
PPT
Data Structure and Algorithms Binary Search Tree
ManishPrajapati78
 
PPTX
Queue_Data_Structure.pptx
sandeep54552
 
PPTX
Linked list
Arbind Mandal
 
PPTX
Heap tree
JananiJ19
 
PPTX
Arboles multicamino
Luis Andres Garcia
 
DOCX
Data Structure Question Bank(2 marks)
pushpalathakrishnan
 
PPTX
Heap sort
Ayesha Tahir
 
PPTX
DPLYR package in R
Bimba Pawar
 
PPTX
Data Structures - Lecture 7 [Linked List]
Muhammad Hammad Waseem
 
binary_search
Mohamed Elsayed
 
linked_lists
Mohamed Elsayed
 
hash_tables
Mohamed Elsayed
 
Tree in data structure
ghhgj jhgh
 
Excel Pivot Tables
Matt Saragusa
 
Binary tree
Vanitha Chandru
 
Avl trees final
PRAKASH RANJAN SINGH
 
Binary Trees.ppt
Riannel Tecson
 
Data Structure and Algorithms Binary Search Tree
ManishPrajapati78
 
Queue_Data_Structure.pptx
sandeep54552
 
Linked list
Arbind Mandal
 
Heap tree
JananiJ19
 
Arboles multicamino
Luis Andres Garcia
 
Data Structure Question Bank(2 marks)
pushpalathakrishnan
 
Heap sort
Ayesha Tahir
 
DPLYR package in R
Bimba Pawar
 
Data Structures - Lecture 7 [Linked List]
Muhammad Hammad Waseem
 
Ad

Similar to introduction (20)

PPT
algo 1.ppt
example43
 
PPTX
1-Introduction to Data Structures beginner.pptx
231b209
 
PPTX
EE-232-LEC-01 Data_structures.pptx
iamultapromax
 
PPTX
Lecture 1.pptx
SisayNegash4
 
PPTX
End-to-End Machine Learning Project
Eng Teong Cheah
 
PDF
Exploratory data analysis v1.0
Vishy Chandra
 
DOCX
dsa 12217554 AdiMunot 4444444444(1).docx
AbhinavTiwari856546
 
PDF
A tour of the top 10 algorithms for machine learning newbies
Vimal Gupta
 
PPT
Data Structures- Part1 overview and review
Abdullah Al-hazmy
 
PPT
Intro_2.ppt
MumitAhmed1
 
PPT
Intro.ppt
SharabiNaif
 
PPT
Intro.ppt
Anonymous9etQKwW
 
PPTX
Lect1.pptx
muhammadRamzan816406
 
PDF
Troubleshooting Deep Neural Networks - Full Stack Deep Learning
Sergey Karayev
 
PDF
9th Comp Ch 1 LQ.pdf
Naeem Mughal
 
DOCX
Algorithm
nivlayalat
 
PPTX
Algorithm and flowchart with pseudo code
hamza javed
 
PDF
Andrew NG machine learning
ShareDocView.com
 
PDF
Top 50+ Data Science Interview Questions and Answers for 2025 (1).pdf
khushnuma khan
 
PPT
Lecture#1(Algorithmic Notations).ppt
MuhammadTalhaAwan1
 
algo 1.ppt
example43
 
1-Introduction to Data Structures beginner.pptx
231b209
 
EE-232-LEC-01 Data_structures.pptx
iamultapromax
 
Lecture 1.pptx
SisayNegash4
 
End-to-End Machine Learning Project
Eng Teong Cheah
 
Exploratory data analysis v1.0
Vishy Chandra
 
dsa 12217554 AdiMunot 4444444444(1).docx
AbhinavTiwari856546
 
A tour of the top 10 algorithms for machine learning newbies
Vimal Gupta
 
Data Structures- Part1 overview and review
Abdullah Al-hazmy
 
Intro_2.ppt
MumitAhmed1
 
Intro.ppt
SharabiNaif
 
Intro.ppt
Anonymous9etQKwW
 
Troubleshooting Deep Neural Networks - Full Stack Deep Learning
Sergey Karayev
 
9th Comp Ch 1 LQ.pdf
Naeem Mughal
 
Algorithm
nivlayalat
 
Algorithm and flowchart with pseudo code
hamza javed
 
Andrew NG machine learning
ShareDocView.com
 
Top 50+ Data Science Interview Questions and Answers for 2025 (1).pdf
khushnuma khan
 
Lecture#1(Algorithmic Notations).ppt
MuhammadTalhaAwan1
 
Ad

More from Mohamed Elsayed (20)

PDF
binary_trees4
Mohamed Elsayed
 
PDF
binary_trees3
Mohamed Elsayed
 
PDF
binary_trees2
Mohamed Elsayed
 
PDF
binary_trees1
Mohamed Elsayed
 
PDF
queues
Mohamed Elsayed
 
PDF
stacks2
Mohamed Elsayed
 
PDF
algorithm_analysis2
Mohamed Elsayed
 
PDF
algorithm_analysis1
Mohamed Elsayed
 
PDF
recursion3
Mohamed Elsayed
 
PDF
recursion2
Mohamed Elsayed
 
PDF
recursion1
Mohamed Elsayed
 
PDF
linked_lists4
Mohamed Elsayed
 
PDF
linked_lists3
Mohamed Elsayed
 
PDF
Linked_lists2
Mohamed Elsayed
 
PDF
sorted_listmatch
Mohamed Elsayed
 
PDF
heaps2
Mohamed Elsayed
 
PDF
heaps
Mohamed Elsayed
 
PDF
graphs
Mohamed Elsayed
 
PDF
quick_sort
Mohamed Elsayed
 
PDF
merge_sort
Mohamed Elsayed
 
binary_trees4
Mohamed Elsayed
 
binary_trees3
Mohamed Elsayed
 
binary_trees2
Mohamed Elsayed
 
binary_trees1
Mohamed Elsayed
 
algorithm_analysis2
Mohamed Elsayed
 
algorithm_analysis1
Mohamed Elsayed
 
recursion3
Mohamed Elsayed
 
recursion2
Mohamed Elsayed
 
recursion1
Mohamed Elsayed
 
linked_lists4
Mohamed Elsayed
 
linked_lists3
Mohamed Elsayed
 
Linked_lists2
Mohamed Elsayed
 
sorted_listmatch
Mohamed Elsayed
 
quick_sort
Mohamed Elsayed
 
merge_sort
Mohamed Elsayed
 

Recently uploaded (20)

PPTX
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
DOCX
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
PPTX
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
PPTX
Five Point Someone – Chetan Bhagat | Book Summary & Analysis by Bhupesh Kushwaha
Bhupesh Kushwaha
 
PPTX
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
PPTX
Continental Accounting in Odoo 18 - Odoo Slides
Celine George
 
PPTX
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
PPTX
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
PPTX
INTESTINALPARASITES OR WORM INFESTATIONS.pptx
PRADEEP ABOTHU
 
PPTX
Kanban Cards _ Mass Action in Odoo 18.2 - Odoo Slides
Celine George
 
PDF
The-Invisible-Living-World-Beyond-Our-Naked-Eye chapter 2.pdf/8th science cur...
Sandeep Swamy
 
PPTX
Cleaning Validation Ppt Pharmaceutical validation
Ms. Ashatai Patil
 
PPTX
Gupta Art & Architecture Temple and Sculptures.pptx
Virag Sontakke
 
PDF
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
PPTX
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
PDF
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
PPTX
Artificial-Intelligence-in-Drug-Discovery by R D Jawarkar.pptx
Rahul Jawarkar
 
PPTX
Sonnet 130_ My Mistress’ Eyes Are Nothing Like the Sun By William Shakespear...
DhatriParmar
 
PPTX
Command Palatte in Odoo 18.1 Spreadsheet - Odoo Slides
Celine George
 
PDF
Virat Kohli- the Pride of Indian cricket
kushpar147
 
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
Five Point Someone – Chetan Bhagat | Book Summary & Analysis by Bhupesh Kushwaha
Bhupesh Kushwaha
 
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
Continental Accounting in Odoo 18 - Odoo Slides
Celine George
 
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
INTESTINALPARASITES OR WORM INFESTATIONS.pptx
PRADEEP ABOTHU
 
Kanban Cards _ Mass Action in Odoo 18.2 - Odoo Slides
Celine George
 
The-Invisible-Living-World-Beyond-Our-Naked-Eye chapter 2.pdf/8th science cur...
Sandeep Swamy
 
Cleaning Validation Ppt Pharmaceutical validation
Ms. Ashatai Patil
 
Gupta Art & Architecture Temple and Sculptures.pptx
Virag Sontakke
 
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
Artificial-Intelligence-in-Drug-Discovery by R D Jawarkar.pptx
Rahul Jawarkar
 
Sonnet 130_ My Mistress’ Eyes Are Nothing Like the Sun By William Shakespear...
DhatriParmar
 
Command Palatte in Odoo 18.1 Spreadsheet - Odoo Slides
Celine George
 
Virat Kohli- the Pride of Indian cricket
kushpar147
 

introduction

  • 1. College of Computing & Information Technology King Abdulaziz University Data Structures I CPCS-204 – Introduction CPCS-204 – Data Structures I
  • 2. Data Structures I: An Introduction page 2 © Dr. Jonathan (Yahya) Cazalas Data Structures 1: Introduction  Course Learning Outcomes  Upon completion of this course, you will be able to: 1. Describe the needs of data structures in problem solving. 2. Design and implement abstract data types. 3. Represent and implement linear/non-linear data structures. 4. Build stacks and queues using arrays and linked lists. 5. Select and apply appropriate data structures in problem solving. 6. Select and apply basic searching and sorting algorithms in problem solving. 7. Apply recursion to solve simple problems. 8. Trace the output of a given piece of code or algorithm.
  • 3. Data Structures I: An Introduction page 3 © Dr. Jonathan (Yahya) Cazalas Data Structures 1: Introduction  How is CPCS-204 different than CPCS-203?  CPCS-203 teaches how to program in Java  Language basics, variable declarations, conditional expressions, if statements, loops, functions, arrays, structures, and even Object-Oriented programming  This will not be covered in this class  You will need to freshen up on your Java very quickly  If you need help, but a good Java-language book or find a quality reference online  With respect to Java, we will cover:  Arrays and array processing (although already taught)  Some of the Java Collections as needed
  • 4. Data Structures I: An Introduction page 4 © Dr. Jonathan (Yahya) Cazalas Data Structures 1: Introduction  The goals of Data Structures I:  Improve knowledge of standard data structures and abstract data types  Improve knowledge of standard algorithms used to solve several classical problems  Cover some mathematical concepts that are useful for the analysis of algorithms  Analyze the efficiency of solutions to problems  Learn about and be comfortable with recursion
  • 5. Data Structures I: An Introduction page 5 © Dr. Jonathan (Yahya) Cazalas Data Structures 1: Introduction  The goals of Computer Science I:  In CPCS-202 and 203, we only cared if we found a solution to the problem at hand  Didn’t really pay attention to the efficiency of the answer  For this class:  We learn standard ways to solve problems  And how to analyze the efficiency of those solutions  Finally, we simply expand upon our knowledge of our use of the JAVA programming language
  • 6. Data Structures I: An Introduction page 6 © Dr. Jonathan (Yahya) Cazalas Data Structures 1: Introduction  Teaching Method:  This class is NOT used to teach you JAVA  The focus of CPCS-202 and 203 (not this class) is to teach you JAVA  You should know JAVA already  In CPCS-202 and 203, majority of time was spent going of syntax of JAVA  Programs were often shown in class  Programs were even written during class  Essentially a requirement for any course teaching a programming language
  • 7. Data Structures I: An Introduction page 7 © Dr. Jonathan (Yahya) Cazalas Data Structures 1: Introduction  Teaching Method:  Majority of this class is used covering new data structures, abstract data types, and algorithm analysis  The teaching of these concepts dictate more explanation and less of a focus on “code”  Some code will be shown on the PowerPoint slides  Such as after we explain a new abstract data type  We’ll show the code of how you would implement it  However, writing of actual code will most likely never be done in class  Again, that is not the purpose of this class
  • 8. Data Structures I: An Introduction page 8 © Dr. Jonathan (Yahya) Cazalas Data Structures 1: Introduction  Example Problem:  We will now go over two solutions to a problem  The first is a straightforward solution that a CPCS-203 student should be able to come up with  Doesn’t’ care about efficiency  The second solution is one that a CPCS-204 student should be able to come up with after some thought  Cares about efficiency  Hopefully this example will illustrate part of the goal of this course
  • 9. Data Structures I: An Introduction page 9 © Dr. Jonathan (Yahya) Cazalas Data Structures 1: Introduction  Max Number of 1’s:  You are given an nxn integer array  Say, for example, a 100x100 sized array  Each row is filled with several 1’s followed by all 0’s  Example:  Row 1 may have 38 1’s followed by 62 0’s  Row 2 may have 73 1’s followed by 27 0’s  Row 3 may have 12 1’s followed by 82 0’s  You get the idea  The goal of the problem is to identify the row that has the maximum number of 1’s.
  • 10. Data Structures I: An Introduction page 10 © Dr. Jonathan (Yahya) Cazalas Data Structures 1: Introduction  Max Number of 1’s:  Straightforward CPCS-203 style solution:  Make a variable called MaxOnes and set equal to 0  For each row do the following:  Start from the beginning of the row on the left side  Scan left to right, counting the number of 1’s until the first zero is encountered  If the number of 1’s is greater than the value stored in MaxOnes, update MaxOnes with the number of 1’s seen on this row  Clearly, this works  But let’s see how long this algorithm will take
  • 11. Data Structures I: An Introduction page 11 © Dr. Jonathan (Yahya) Cazalas Data Structures 1: Introduction  Max Number of 1’s:  Analysis of Straightforward Solution:  Basically we iterate through each square that contains a 1, as well as the first 0 in each row  If all cells were 0, we would only “visit” one cell per row, resulting in n visited cells  However, if all cells were 1’s, we would “visit” all of the cells (n2 total)  So in the worst case, the number of simple steps the algorithm takes would be approximately n2  This makes the running time of this algorithm O(n2)  The meaning of this Big-O will be discussed later in the semester
  • 12. Data Structures I: An Introduction page 12 © Dr. Jonathan (Yahya) Cazalas Data Structures 1: Introduction  Max Number of 1’s:  Analysis of Straightforward Solution:  There seems to be extra work done here  Once we know that a row has 12 1’s, for example, it seems pointless to start checking at the beginning of the next row  Why not just start at column 12  If it’s a 0, then that row can’t be the winner  If it is a 1, then clearly there is no point in going back, on that row, and checking the previous 11 squares  This idea leads to a more efficient algorithm
  • 13. Data Structures I: An Introduction page 13 © Dr. Jonathan (Yahya) Cazalas Data Structures 1: Introduction  Max Number of 1’s:  More Efficient CPCS-204 style algorithm: 1. Initialize the current row and current column to 0 2. While the current row is less than n (or before the last row) a. While the cell at the current row and column is 1  Increment the current column b. Increment the current row 3. The current column index represents the maximum number of 1’s seen 4. Now let’s trace through a couple of examples
  • 14. Data Structures I: An Introduction page 14 © Dr. Jonathan (Yahya) Cazalas Data Structures 1: Introduction  Max Number of 1’s:  Example 1: 1 1 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 1 1 1 0 0 0 1 1 1 1 1 0 1 1 1 1 0 0
  • 15. Data Structures I: An Introduction page 15 © Dr. Jonathan (Yahya) Cazalas Data Structures 1: Introduction  Max Number of 1’s:  Example 2: 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1
  • 16. Data Structures I: An Introduction page 16 © Dr. Jonathan (Yahya) Cazalas Data Structures 1: Introduction  Max Number of 1’s:  Analysis of Better Solution:  How many steps will this algorithm take, in terms of n?  Each “step” taken by the algorithm either goes to the right or down in the table.  There are a maximum of n-1 steps to the right  And a maximum of n-1 steps down that could be taken  Thus the maximum number of “steps” that can be done during this algorithm is approximately 2n  And this is the worst case  So the running time of this algorithm is O(n)  An improvement of the previous algorithm  Input size of 100 for n  n2 would be 10,000 steps and 2n would be 200 steps
  • 17. Data Structures I: An Introduction page 17 © Dr. Jonathan (Yahya) Cazalas Data Structures 1: Introduction  Implementing an Algorithm in JAVA:  In this class, you will have an opportunity to improve upon your ability to write programs that implement an algorithm you have learned  You must know the syntax of JAVA in order to properly and effective do this  There’s no set way to create code to implement an algorithm  But this example shows some steps you can take in doing so
  • 18. Data Structures I: An Introduction page 18 © Dr. Jonathan (Yahya) Cazalas Data Structures 1: Introduction  Implementing an Algorithm in JAVA:  Here are some issues to think about: 1. What data structures are going to be used? 2. What functions are going to be used? 3. What run-time errors should we protect against? 4. What atypical cases may we have to deal with? 5. What is an efficient way to execute the steps in the algorithm?
  • 19. Data Structures I: An Introduction page 19 © Dr. Jonathan (Yahya) Cazalas Data Structures 1: Introduction  Maximum Number of 1’s  This was a creative exercise  Much of what you learn in class will not be  We have many set algorithms and data structures that you will study  Occasionally you will have to come up with new ideas like this one  Mostly, however, you will simply have to apply the data structures and algorithms shown in class fairly directly to solve the given problems
  • 20. Data Structures I: An Introduction page 20 © Dr. Jonathan (Yahya) Cazalas Data Structures 1: Introduction Are You Excited?
  • 21. Data Structures I: An Introduction page 21 © Dr. Jonathan (Yahya) Cazalas Daily FAIL Picture  Fail pictures  During the slides (or at the end), we will often give a funny “fail” picture or a “human stupidity” picture  Helps to “lighten things up”  So here you go:
  • 22. Data Structures I: An Introduction page 22 © Dr. Jonathan (Yahya) Cazalas Human Stupidity
  • 23. College of Computing & Information Technology King Abdulaziz University Data Structures I CPCS-204 – Introduction CPCS-204 – Data Structures I