Title: EEM 480 Algorithms and Complexity
1EEM 480 Algorithms and Complexity
- by
- Assist. Prof. Dr. Emin Germen
2What is this Course About
- Of Course it is about
- Algorithms
- Complexity
3Any More?
- Algorithms Complexity Data Structure
- Data Structure Concerns with the representation
and manipulation of data. - All the programs use and manipulate data
- IS (Computer Programming Data Structure)
- YES OR NO
4What is Data Structure
- Designing the program which modifies data
- The Key Point
Algorithm
5What is this Course About
- Learning and designing algorithms to manipulate
the data - Comparing criteria of different algorithms
- Understand what a good program is
- Learning effective utilizations of computer
sources
6Syllabus
- Review of C
- Classes
- Encapsulation, Overloading, Inheritance,
Overriding - Pointers to objects
- Templates
- Arrays and Linked Lists
- Linear lists
- Formula based representation
- Linked representations
- Indirect addressing and pointers
- Arrays and Matrices
- Arrays, Matrices, Special matrices, Sparse
matrices - Stacks
- Queues
- Hash Tables
- Trees
- Binary trees
- Tree traversals
- Priority Queues
7Reference and Evaluation
- Data Structures, algorithms, and Applications
in C, Sartaj Sahni - Evaluation
- Mt 1 10
- Mt 2 20
- Projects 30
- Final Exam 40
8Object Oriented Programming
- Object
- Class
- Encapsulation
- Inheritence
- Polymormhism
9Object
- An object is a bundle of variables and related
methods. - When an object is mapped into software
representation, it consists of 2 parts - DATA STRUCTURE characteristics of data structure
are refered to as ATTRIBUTES - PROCESSES that may correctly change the data
structureprocesses are refered to as OPERATIONS
or METHODS
10Class
- A class is a blueprint for an object.
- Objects with the same data structure (Attributes)
and behavior (Methods or Operations) are grouped
together (called a class ).
11Encapsulation
- Encapsulation is the procedure of covering up of
data and functions into a single unit - Class
- Data1
- Data2
- Procedure1(Data1)
- Data2 Procedure()
- Encapsulation hides the implementation away from
the user
12Inheritence
- As objects do not exist by themselves but are
instances of a CLASS, a class can inherit the
features of another class and add its own
modifications. (This could mean restrictions or
additions to its functionality). Inheritance aids
in the reuse of code.
13Polymorphism
- Polymorphism means the ability to request that
the same Operations be performed by a wide range
of different types of things.
14So What???????
- What are those things????
- How can I use them???????
- I have searched OOP in the Internet and find
ABSTRACTION. What does it mean???? - I can write good programs using structural
proggramming techniques? Why should I use OOP?
15Intoduction to OOP with C
- Object Oriented Programming
- Class
- The structure which keeps data and function
- Interitance
- Inheritance is also called derivation. The new
class inherits the functionality of an existing
class. The existing class is called the base
class, and the new class is called the derived
class. A similar inheritance can be derived for
animals, mammals, and dogs. - Polymorphisim Overloading
- Ability to have more than one function with the
same name that differ in their parameter lists.
16Program 1
Header for the basic C procedures
- include ltstdio.hgt
- include ltiostream.hgt
- void main(void)
-
- cout ltlt "This is not my first program"
- cout ltlt "\n"
Streaming functions
17Program 2
- // Workspace Triangle
- // Program name Area.cpp
- // The area of a triangle is half its base times
height - // Area of triangle (Base length of triangle
Height of triangle)/2 - include ltiostream.hgt // Precompiled header
- double base,height,area // Declaring the
variables - double Area(double,double) // Function
Prototype/declaration - int main()
-
- cout ltlt "\nEnter Height of Triangle " // Enter
a number - cin gtgt height // Store the input in variable
- cout ltlt "\nEnter Base of Triangle " // Enter a
number - cin gtgt base // Store the input in variable
- area Area(base,height) // Store the result
from the Area function - // in the variable area
- cout ltlt "The Area of the Triangle is "ltlt area
ltlt endl
The Output
18Program 3 Polymorphism
- include ltiostream.hgt
- double base,height,radius // Global variables
- double Area_of_triangle,Area_of_circle // Global
variables - int choice // Global variable
- double Area (double,double) // Function
prototype - double Area (double) // Function prototype
- const double pi 3.14 // Constant variable
- void main() // main function
-
- cout ltlt "To find the area of a Triangle, input 1
\n" - cout ltlt "To find the area of a Circle, input 2
\n" - cin gtgt choice
- if (choice 1)
-
- if (choice 2)
-
- cout ltlt "Enter radius of the Circle "
- cin gtgt radius
- Area_of_circle Area(radius)
- cout ltlt "The area of the Circle is " ltlt
Area_of_circleltltendl -
- if (choice ! 1 choice ! 2)
-
- cout ltlt "Sorry! You must enter either 1 or 2
\n" -
-
- double Area (double base, double height)
-
- return (0.5baseheight)
-
- double Area(double radius)
The Output
19Defining Class in C
- Almost same as defining Struct
- Insert Functions into the Struct
- Some important functions
- Constructor
- Destructor
20Object Oriented C Class
CCircleCCircle(int r) m_radius
r CCircleCCircle() void
CCircleDisplayArea(void) float
fArea fArea CalculateArea() cout ltlt "The
Arae of the circle " ltlt fArea ltlt "\n" float
CCircleCalculateArea(void) float f f
(float) (3.14 m_radius m_radius) return
f
include ltiostream.hgt class CCircle public CCi
rcle(int r) void SetRadius(int r) void
DisplayArea(void) CCircle() private float
CalculateArea(void) int m_radius int
m_color
CONSTRUCTOR
DESTRUCTOR
21Dynamic Memory Allocation
- new
- int y //pointer
- y new int // creates place
- y 10 // OK
- Or
- int y new int(10)
22OOP in C
- Simple example of defining object and class
- Programming Example
- Overloading
- Programming Example
- Inheritance
- Programming Example
- Templates
- Programming Example
23One Dimensional Array and Exception Handling
- float x new float n
- Or better
- float x
- try x new float n
- catch (xalloc)
- cerr ltlt Out of memory ltlt endl
- exit(1)
24Delete
- delete y //free the memory allocated by y
- delete x // free the one dimensional array