Lecture 1 - Introduction

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 27

In this lecture:

COS3711
Lecture 1 ❖ Introduction
Based on chapter 1
of our recommended ❖ Overview of Data Structures
book
❖ Overview of Algorithms

❖ Lecture Summary
Important attributes of a computer program

[correctness] • performs the task it was designed to do

[documentation] • easy to read the program and understand the code

[maintainability] • easy to modify the program

[portability] • code can be compiled and run on a variety of computers

[Finiteness] • always terminates after a finite number of steps

[Definiteness] • all actions can be carried out precisely and unambiguously

[space usage] • How much memory is needed to run the program?

[running time] • How long does it take to run the program?

[scalability] • Is the code able to solve problems for a large range of inputs?
Introduction

• in OOP, you built applications for a particular use case • add contacts to an
existing list
• each application required data storage • search a contact
• edit contact
• you created classes to store data information
• delete a contact
• You created objects based on those classes
• iterate over all
contacts in the
phone book
• Real applications require more than two or 3 objects
• perform some
kind of
• e.g. an application to stores contacts in a mobile phone transformation on
the data
Introduction
• programming all about:

• data representation (storage) and


• performing operations on data (algorithms)

• core programming problems easy to solve with correct data structure and algorithms

• Often you want an application that:

• represent the complexity of data


• express interrelationships between data in an intuitive way
• provide ability to perform required operations
• occupy less storage space
• allows operation to run very quickly

• often achieving these is a trade-off


What is a data structure?

• an arrangement of data in memory (or disk)

• a way to organize information


T
• provides:
o H • well-defined format to store information

L R T M • well-defined operations (APIs) to manipulate (access) stored information

A S • efficiency in performing operations


G
• understanding data structures enables:

• knowledge of trade-offs for selecting a particular data structure

• knowledge of what operations can be performed efficiently


What is an algorithm?

• a sequence of computational steps that transform


the input into the output when solving a well-
specified computational problem.

• they manipulate the data structures in various ways:

• searching for a particular data item

• performing some calculation

• rearranging (sorting) the data


Overview of Data Structures
Why study Data Structures?

• a vast number of possibilities to store and manipulate data


• common techniques to store data are helpful in many situations
• no single data structure works well for all purposes
• Each has strengths and limitations associated with them

• Questions to ask:
• How well it stores the data in computer memory?

• How it works/ handles different amounts of data: a hundred? a thousand? a million?

• Whether it permits quick insertion of new and deletion of existing data?

• How it allows for fast searching (retrieval) of specified data?

• How it allow rearrangement of data (sorting) in a specified order?


Creating Data Structures

• specify how data is represented and stored, along with specific operations that can be
performed on that data

• Data Structures in a physical world:


• Maps

• Dictionaries

• Filing cabinets

• Library Cabinets

• In this course
• Concrete implementation of
Abstract Data Types – defining the
behavior and characteristics of our model
Data Structures in code

Data Structures are used for:

• Real-world data storage

• Programmer’s tools

• Modeling
Real-world data storage

• Data structures are used to describe physical


entities external to the computer

financial
personnel record inventory record
transaction record
• to describes an • to describe an • to describe an
actual human existing grocery actual check
being item written to pay
the electric bill
Programmer’s tools

• uses a stack • stores files • use B-tree • uses hash • a number of

compiler implementations
Java Virtual Machine

File system

General Programming
relational databases
to remember and folders indexes for tables to look collections
all of a using a tree data retrieval up identifiers (set,
program's structure. multi_set,
methods that map,
have been multi_map)
called but are are normally
not yet implemented
finished as red-black
trees
Modeling

directly model real-world situations

Queues Priority Queue Graphs Trees

• students waiting in • model an emergency • connections/relations • some game engines


line to register or cars room at a hospital, in social networks to accurately simulate
waiting at a car wash where patients are • airline routes between human movement
seen in order of cities or tasks in a
severity of their project
medical needs.
Benefits of creating data structures

Reduces programming Promotes interoperability


Promotes software reuse
effort APIs to interoperate
interfaces are by nature
facilitates interoperability seamlessly, even though they
reusable
among unrelated APIs were written independently.

Increases speed and quality


Easy to learn and use new
Reduces design efforts provides high-performance
APIs
no need to reinvent the wheel and implementations, leaving
provides consistency among
by creating new APIs more time to improving
ad hoc collection sub-APIs
quality and performance
How we will create data structures in code

The data structure contains the following:

Interfaces
Implementations:
abstract data types
concrete implementations
representing the data
of the interfaces
collections
• allows data collections to • enables reusable
be manipulated functionality
independent of the
representation details
Data Structures covered in our course

• Arrays
• Ordered Arrays

• Stacks
Standard data Structures already implemented
• Queues
• Java Collections Framework
• Linked Lists • C++ Standard Template Library (STL)
• Trees (binary, red black, 2-3-4) • Smalltalk's collection hierarchy
• Sets, Maps • Microsoft .NET framework

• Hash Tables

• Heaps

• Graphs
Overview of Algorithms
Overview of Algorithms

• sequence of steps that transform the input into the output when
solving a computational problem.

• A computational problem consists of:


input Process output
• a set of instances or cases (input), together with (data) (information)

• a set of solutions (output) for every instance/case.

• Problem solved by describing:


• breaking down the problem into smaller subproblems

• specifying instructions for each subproblem, and

• combining the solutions to arrive at an overall solution


Examples of computational problems

Decision problem • Given a positive integer n, determine if n is prime.

• Given representations of positive integers, determine collections


Search problem
of primes

• Given a positive integer n, count the number of nontrivial prime


Counting problem
factors of n

• find a "best possible" solution among the set of all possible


Optimization problem
solutions to a search problem.

• "Given a list of cities and the distances between each pair of


Function problem cities, find the shortest possible route that visits each city
exactly once and returns to the origin city."

• Given a graph G, determine if every independent set in G has


Promise problem
size at most 5, or G has an independent set of size at least 10."
Algorithms in real life

Every single branch of computer science uses algorithms:

producing
predicting traffic
automatic ranking internet analyzing a
jams and road
movie/book search results genome
accidents
recommendations
Algorithms in real life
• It's important that the algorithms we use are efficient:
• users want to see the search results in a blink of an eye

• Users want correct results

• achieve excellent search quality

• high prediction accuracy, and

• make relevant recommendations


Connection between Algorithm and Data Structure
defines how data is represented
allows algorithms to access and manipulate data
Data structures - ways to organize and store
quickly and efficiently
data in memory.
can be optimized for different types of
operations

can be executed on various data structures


algorithm - a set of instructions or procedures
performs specific operations, such as searching,
that takes some input and produce an output.
sorting, inserting, deleting, and modifying data

algorithms are the logic that tells a computer what to do


data structures are the tools that allow algorithms to do it efficiently
they work together to solve problems and process data efficiently
How we will study algorithms in this course:
• Our algorithms apply directly to specific data structures
• We would typically want to know how to:
• Insert a new data item
• Edit data item already stored in a data structure
• Search for a specified item
• Delete a specified item
• Traverse the data collection
• Re-arrange data according to some specified order

• For each algorithm we may want to:


• Describe the algorithm
• Prove algorithm correctness
• Evaluate its efficiency in terms of resources (time and space)
• Implement it in a programming language of choice
Example: Describing algorithms

We will use pseudocode to describe algorithms


Example: in an array A storing n integers, find the largest stored integer

Algorithm arrayMax(A,n):
Input: An array A storing 𝑛 ≥ 1 integers

Output: The maximum element in A

currentMax ← 𝐴 0

for i ← 1 to 𝑛 − 1 do
if currentMax < 𝐴[𝑖] then

currentMax ← 𝐴[𝑖]

return currentMax
Determining the efficiency

• We often have several algorithms to the same problem


• Which algorithm is the best?
• How can we choose between two algorithms for the same task?
• In practice, we don’t just want algorithms
we want good algorithms
• Criterion for goodness?
• Time taken
• Space taken
• Required network resources
Lecture Summary
• data structures and algorithms are essential to computer programming

• data structures specifies how data should be organized in computer’s memory

• algorithms are the methods/procedures for carrying out a particular task

• several algorithms may be available to carry out a certain task

• In this course we will:

• Use data structure to organize data in our programs

• Use pseudocode to describe algorithms for manipulating data structures

• evaluate relative efficiency of any two algorithms

• Implement algorithms using the Java programming language

• the correct choice of data structure + algorithms = major improvements in program efficiency

You might also like