03 Algorithm Design Elements Uml

You might also like

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

Data Structures and

Algorithms (DSA)
Dr Suryaprakash Kompalli
Agenda
• Introduction to Algorithm Design
• Practical representations
– Pseudo code
– Flowcharts
– Finite state machines
– Unified Modeling Language (UML)
Introduction to Algorithm Design


Application / Real life problem: Description of an objective that
needs to be achieved by processing data

Instance: A replication of the problem using specific data-set

Algorithm: Well-defined logic that processes the data in a finite
time to meet the objective correctly

Implementation: Programs or codes that can run on a computing
device and simulate the algorithm

Tests: Sets of instances that check whether an implementation is
meeting the objective or not
Introduction to Algorithm Design


The underlying data structure is central to algorithm design, ex:
– Can you design insertion sort for integers if the numbers are stored in a singly
linked list?
– If you need to sort 10 Million integers, is array the best representation?

Other aspects:
– Formally, an algorithm should end in finite time.
– What will you prefer, an algorithm that sorts 1 Million integers in 1 hour, or an
algorithm that sorts 1 Million integers in 1 minute?
– An implementation should be able to run on the hardware that the problems needs
it to run on. What is better: an implementation that uses 4GB of RAM or one that
uses 0.2 GB of RAM? The amount of memory consumed is also dependent on the
algorithm used.
Introduction to Algorithm Design

State-dependent algorithms behave differently based on certain parameters
or conditions. Generally these systems may have two or more sources of data.
One data source will determine the output; one or mode data sources can
determine the state. Examples:
– Keep water temperature at or above 60 degree Celsius. A control system for this would have to
monitor two states – water temperature less than 60 degrees, and water temperature more
than 60 degrees. Data: Quantity of water, temperature of water
– Online ad delivery: Which page is the user looking at? How many minutes was the user reading
this page? What kind of device does the user have?

State-less algorithms depend on a fixed quantum of data to meet an
objective. Processing is not changed midway due to other inputs.
– Sorting algorithms
– Industrial systems that take only one input; example: hydraulic jacks.
– There may be some conditions regarding inputs/outputs: example: inputs should be +ve
numbers, output should be between 0 to 1 etc
Introduction to Algorithm Design

Start Input
Step 1 Step 2 Step 3 Output
End

State less system

Step 3.A
Step 1.A
Start Input
Step 2 Step 3.B Output
End
Step 1.B
Step 3.C
Intermediate
Input Intermediate
Input
State based system

It is not always necessary that a system will change state based on intermediate input. States can change based
on input data as well. Example: If number of user requests > 100K, move those requests to critical systems
Agenda
• Introduction to Algorithm Design
• Practical representations
– Pseudo code
– Flowcharts
– Finite state machines
– Unified Modeling Language (UML)
Practical Representations: Pseudo code

Pseudo code represents the steps involved in an algorithm
– Pseudo code may or may not represent all the corner cases that need to be checked when an
algorithm is implemented; e.x: Check that the raw material vat is not empty, check that input
array contains valid data
– Has a human-readable structure
– Looks like a computer program but cannot be compiled to run on a machine
– Accurately represents all steps of an algorithm needed to get results for input data
– Should allow one to completely understand the algorithm and infer aspects like run-time
complexity, memory usage etc

There is currently no universal standard or syntax for writing pseudo code
– Many constructs like mathematical functions, variable initialization, modification of variables,
comments are allowed as part of pseudo code
Practical Representations: Pseudo code

Example control structures used in pseudo code:
– If then else
– Numerical loops / iterations: Do following steps “n” times
– Conditional loops / iterations: While <Condition> do following steps

Example condition: A < B, File end is not reached, temperature < 60

A set of steps that perform a specific task can be defined using functions /
procedures / processes
– Example: Process cleanCarburetor(), Function Partition(A, p, r)
Practical Representations: Pseudo code

Example pseudo code:

Quicksort(A, startIndex, endIndex)


1. If (startIndex < endIndex)
a. partitionIndex = Partition(A, startIndex,
endIndex)
b. Quicksort(A, startIndex, partitionIndex)
c. Quicksort(A, partitionIndex+1, endIndex)

Partition(A, startIndex, endIndex)


1. lastValue = A[ endIndex ]
2. i = startIndex
a. For j = startIndex to endIndex – 1
i. If A[ j ] >= lastValue
A. Swap A[ i ] with A[ j ]
B. i = i + 1
3. Swap A[ i ] with A[ endIndex ]
Source: Simulating revenue management in an airline market with demand
4. Return i
segmentation and strategic interaction, Alessandro V. M. Oliveira, January 2003
Agenda
• Introduction to Algorithm Design
• Practical representations
– Pseudo code
– Flowcharts
– Finite state machines
– Unified Modeling Language (UML)
Practical Representations: Flowcharts

Invented by John Von Neumann in the early 20th century at IBM

Represents logical elements in the form of geometric shapes

Control or program flow is shown using arrows

Used in several domains as a way to represent structured processes, control
systems, computer programs etc.

Flowcharts are easy to interpret if the algorithms are small, but large
flowcharts become difficult to interpret

Several flowchart notations and standards exist
– These notations are more or less established in the computing domain
Practical Representations: Flowcharts

Several flowchart notations and standards exist
– These notations are more or less established in the computing domain

Program flow
Terminal Process Decision Input / Output Comment Predefined
Process (functions)

Connectors Preparation or
Left: On-page, Database Document Multiple Manual operation Manual input
program initialization
Right: Off page documents
Practical Representations: Flowcharts An example flow chart of a pushbutton switch control
system for wireless emergency start and stop for robots.

The goal of switch control is: (i) Detect if robots are


communicating with it, (ii) Show LEDs to indicate status of
robots, (iii) Send start/stop instructions to robot if any
buttons on the switch control are pressed. The system
also has to monitor its own battery status to ensure that it
has enough power to transmit and operate correctly.

The switch control system first initializes its internal


controls.

Then it waits to read a pacakge from the robots around it.

If any signals are received, suitable LEDS are blinked to


show connection to a robot.

If no signal is received, the system assumes that robots


are not present.

If buttons are pressed, this information is sent to robots.


Design and Development of a Wireless Emergency Start and Stop System for Robots, D. Garcia Ramon Barber Miguel
Angel Salichs, Proceedings of the 11th International Conference on Informatics in Control, Automation and Robotics, Sep
2014
Practical Representations: Flowcharts
Start Start Flowcharts showing
different methods to
swap two numbers
Input A, B Input A, B

temp = A A=A+B

A=B B=A-B

B = temp A=A-B

Output A, B Output A, B

Stop Stop
Practical Representations: Flowcharts

Flowchart showing operation of an automated boiler


plant

Source: Developing a Human Machine Interface (HMI) for Industrial Automated Systems using Siemens
Simatic WinCC FlexibleAdvanced Software Erwin Normanyo, Francis Husinu, Ofosu Robert Agyare,
Journal of Emerging Trends in Computing and Information Sciences, Feb 2014
Agenda
• Introduction to Algorithm Design
• Practical representations
– Pseudo code
– Flowcharts
– Finite state machines
– Unified Modeling Language (UML)
Practical Representations: Finite State Machines

A Finite State Machine or Automata (FSM or FSA) is represented by the
system: FSM , F=(S , s0 , E , s e )
S : States in which a process can exist
s 0 ∈S : set of special states which signify the start of a process
E : Edges or transition between states, generally shown as e(s1, s 2 )
It is not necessary that all pairs of nodes should have edges.
s e : set of special states which signify the end of a process
ε/start

Finite state automata modeling a


diesel generator

No oil/
overheated
Engine is Engine is
generating generating
power normally low/no power

Normal oil/
temperature
Practical Representations: Finite State Machines
Finite state machine to model a
few words in Hindi

Finite state machine to model a


few english words

Source: Slides on Morphology and Finite State Transducers, Intro to NLP, CS585, Fall 2014, Brendan O’Connor, https://tinyurl.com/2ch5drb3
Slides on Design and comparison of segmentation driven and recognition driven Devanagari OCR, Document Image Analysis for Libraries,
2006, https://tinyurl.com/qh83rr1k
Practical Representations: Finite State Machines

Finite State Machine to model a traffic


light system

Source: Dialog Semiconductor AN-CM-231 Spec Sheet, V 0.1, Mar 2018, https://tinyurl.com/zsrvmbvu
Practical Representations: Finite State Machines

FSMs are very efficient at representing state of specific parts of a system

FSMs can become very large and difficult to interpret

The actual design and flow of an algorithm may not always be represented by
FSMs
Agenda
• Introduction to Algorithm Design
• Practical representations
– Pseudo code
– Flowcharts
– Finite state machines
– Unified Modeling Language (UML)
Practical Representations: Unified Modeling Language

Designed to provide a standard for visualizing different
components of a software system

Approved as an ISO standard in 2005

Has several standards that specify how to represent:
– System activities / processes
– Communication systems / protocols
– State dependent systems
– System interactions
– Time based systems
– Use cases

Combines the features of flowcharts, Finite State Machines
and many more representation systems
Practical Representations: Unified Modeling Language

Left: Example of Sequence


diagram for withdrawing amount
from ATM

Top: Example of a state diagram for


the ATM session

Source: Sample of UML Diagrams for ATM System, CIS 375. Introduction to Software
Engineering, Univ of Michigan, https://tinyurl.com/swiwko2p
Practical Representations: Unified Modeling Language
Vending machine activites

Fill items
Owner Worker

Audit

Move / repair

Contractor
Purchase items

A simplified use-case diagram for a


Customer vending machine
Practical Representations: Unified Modeling Language

Code
char
/ Char
Code Slash
* /
Star /
newline Char
newline
Char Single line /
Char Comment
Slash
Comment Single line comment
/
* /
* Star
*
Comment
Char

Char
Representation of how to parse comments in C/C++ code

Left: A Finite State Machine Right: A State Diagram using UML


Practical Representations: Unified Modeling Language

Additional resources to learn UML:
– Martina Seidl, Marion Scholz, Christian Huemer, Gerti Kappel.
UML@Classroom, Springer Verlag, 2015.
http://www.uml.ac.at/en/
– Applying UML and Patterns: An Introduction to Object-Oriented
Analysis and Design and Iterative Development, Craig Larman,
978-0131489066, URL:
https://personal.utdallas.edu/~chung/SP/applying-uml-and-
patterns.pdf
Thank You !!!
Pattern to be accepted: [abba]*

UML (2)
Any other Any other
value Any other
value
Value
State: Start State: Enter b b State: b
a
Null/End
of input a State: Invalid string
b

State: Valid string State: a

Any other
Value
UML (1)

Any other Any other


value Any other
value
Value
State: Start State: Enter b b State: b
a
Null/End
of input a State: Invalid string
b

State: Valid string State: a

Any other
Value

You might also like