DSA\300 – Data Structures and
Algorithms with C#
Data Structures & Algorithms
with C# – Lecture #1!
Ahmed Mohyeldin
Lecture #1
INTRODUCTION
Problem Algorithm Flowchart/Pseudocode
Program Solution
MOHY Mindworks Data Structures and Algorithms with C#, Copyright © 2017-2018 Ahmed Mohyeldin 2
Computers – What is Computer?
A Computer is a tool for solving problems with
data!
Raw Data Information
Processing
MOHY Mindworks Data Structures and Algorithms with C#, Copyright © 2017-2018 Ahmed Mohyeldin 3
Computers – How does it Work?
Computers are machines, and at the most basic
level, they are a collection of switches—where 1
represents "on" and 0 represents "off".
Everything that a computer does is implemented in
this most basic of all numbering systems—binary.
Each Computer Processor has its own binary
machine language—Instruction set!
MOHY Mindworks Data Structures and Algorithms with C#, Copyright © 2017-2018 Ahmed Mohyeldin 4
Computers – How does it Solve Problems?
A Computer is not capable of performing
calculations or manipulating data without exact
step-by-step instructions in the form of a
Computer Program.
When you write a program, you are in effect Solving a
Problem (i.e., Writing a Program = Solving a Problem).
Deriving a solution to a problem requires your powers of
logic, together with a systematic methodology.
MOHY Mindworks Data Structures and Algorithms with C#, Copyright © 2017-2018 Ahmed Mohyeldin 5
Programs – What is a Program?
A Program is a sequence of instructions that tell
a computer how to do a task. When a computer
follows the instructions in a program, we say it
executes the program.
MOHY Mindworks Data Structures and Algorithms with C#, Copyright © 2017-2018 Ahmed Mohyeldin 6
Programs – How to Write a Program?
If you really wanted to tell a computer what to do
directly, you'd have to talk to it in binary, giving it
coded sequences of 1s and 0s that tell it which
machine instructions to execute.
MOHY Mindworks Data Structures and Algorithms with C#, Copyright © 2017-2018 Ahmed Mohyeldin 7
Programs – How to Write a Program? (…cont’d)
However, this is nearly impossible. In practice,
we use a programming language with algorithm
and flowchart for solving problem.
MOHY Mindworks Data Structures and Algorithms with C#, Copyright © 2017-2018 Ahmed Mohyeldin 8
Solving Problems with Computers
ALGORITHMS
MOHY Mindworks Data Structures and Algorithms with C#, Copyright © 2017-2018 Ahmed Mohyeldin 9
Algorithms – The word Algorithm?
As a word “algorithm” has a long history; its
origin can be traced to the Arabic mathematician
El-Khawarizmi.
In general, common usage of the word would suggest
the meaning of “rule”, “procedure”, “method”,
“technique”, or “strategy”.
In computer programming, an algorithm is the set of
man-generated instructions, or program consists of
the necessary steps needed to derive a solution to a
given problem.
That is a computer program is a specific implementation of
an “algorithm”; a representation of a method for solving a
problem.
MOHY Mindworks Data Structures and Algorithms with C#, Copyright © 2017-2018 Ahmed Mohyeldin 10
Algorithms – Simple Algorithm Example?
Describe the way to find words in a dictionary?
11. Determine the first letter of the word.
22. Find the page of the dictionary where this letter first
appears.
33. Determine the next letter of the word.
44. Find the page where these letters first occur in
sequence.
55. Continue step 3 and 4 for succeeding letters of the
world until correspondence is obtained between the
desired word and the word listed in the dictionary.
MOHY Mindworks Data Structures and Algorithms with C#, Copyright © 2017-2018 Ahmed Mohyeldin 11
Algorithms – What is an Algorithm?
An Algorithm is a finite set of unambiguous
instructions which provides a solution to a
particular class of problems.
The creation of an algorithm may be viewed as an
intermediate step in writing a computer program for solving a
problem.
MOHY Mindworks Data Structures and Algorithms with C#, Copyright © 2017-2018 Ahmed Mohyeldin 12
Algorithms – The Properties of Algorithms?
The five characteristics of a good algorithm in a
computing environment:
1 Precision – The instructions used must be precise.
It must use well-defined instructions set to perform.
2 Finiteness – It must reach a solution in finite time.
It must occupy limited space and take limited time to perform.
3 Generality – It must solve a whole class of problems.
It must not be designed to solve only a single specific
problem.
4 Effectiveness – It must perform efficiently.
It must solve the problem for which it is proposed in an
efficient manner measured in the number of steps to perform.
5 Consistency – It must be uniquely-defined.
There must not be two or more conflicting behaviors.
MOHY Mindworks Data Structures and Algorithms with C#, Copyright © 2017-2018 Ahmed Mohyeldin 13
Algorithms – The Problem-Solving Process?
Generally, the evolution from problem to
program solution includes:
Analyzing the problem.
Developing the algorithm.
Coding the program.
Executing the program.
Testing & Debugging the
Program.
Documenting the program.
MOHY Mindworks Data Structures and Algorithms with C#, Copyright © 2017-2018 Ahmed Mohyeldin 14
Algorithms – Representation of Algorithms?
Algorithms can be represented in several
different ways.
Commonly-used Techniques:
Flowcharts – is basically a pictorial view for illustrating
algorithms.
Pseudo Code – is a kind of structured English for
describing algorithms.
Other techniques:
Decision Tables
Data Flow Diagrams (DFDs).
Hierarchy Plus Input Output (HIPO).
MOHY Mindworks Data Structures and Algorithms with C#, Copyright © 2017-2018 Ahmed Mohyeldin 15
Designing Algorithms using
FLOWCHARTS
MOHY Mindworks Data Structures and Algorithms with C#, Copyright © 2017-2018 Ahmed Mohyeldin 16
Flowcharts – What is a Flowchart?
A Flowchart is a way of describing and
expressing an algorithm in a pictorial
representation.
It illustrates the actions to be carried out for solving
the problem through the interconnection of
specialized symbols with flow lines.
Flowchart Standards:
There are a standard set of symbols established by
the ANSI.
Also, other commonly-used conventions are there
such as: Gaddis, IBM, SDL, etc.
MOHY Mindworks Data Structures and Algorithms with C#, Copyright © 2017-2018 Ahmed Mohyeldin 17
Flowcharts – Flowgorithm Flowcharting Tool
For demonstrating the use flowcharts to design
algorithms, a simple but powerful flowcharting
tool called Flowgorithm will be used here.
What does Flowgorithm can do for me?
Flowgorithm is a free application that helps you create
programs using simple flowcharts.
Flowgorithm supports all commonly used flowchart
standards.
Flowgorithm allows you to run and test your algorithm to
verify its correctness.
Flowgorithm can convert flowcharts to C++, C#, Java,
Python, Visual [Link] code as well as Gaddis
Pseudocode - used in the book “Starting out with
Programming Logic and Design" by Tony Gaddis.
Download URL: [Link]
MOHY Mindworks Data Structures and Algorithms with C#, Copyright © 2017-2018 Ahmed Mohyeldin 18
Flowcharts – Flowgorithm Flowchart Symbols
Everything in a flowchart is represented by a
specific shape as follows:
MOHY Mindworks Data Structures and Algorithms with C#, Copyright © 2017-2018 Ahmed Mohyeldin 19
Flowcharts – Flowgorithm Live Demo
Think Design Draw Test
MOHY Mindworks Data Structures and Algorithms with C#, Copyright © 2017-2018 Ahmed Mohyeldin 20
Designing Algorithms using
PSEUDOCODE
MOHY Mindworks Data Structures and Algorithms with C#, Copyright © 2017-2018 Ahmed Mohyeldin 21
Pseudocode – What is a Pseudocode?
A Pseudocode is an informal high-level
description of the logic of an algorithm in a
program-like statements written in plain English.
Advantages: Pseudocode allows the designer to
focus on the logic of the algorithm without being
distracted by details of language syntax.
Disadvantages: Pseudocode does not have any
unified standard format (i.e., rules for formulating
instruction). However, several conventions do exist,
e.g.,:
Gaddis-Style Pseudocode, C-Style Pseudocode, Fortran-
Style Pseudocode, etc.
MOHY Mindworks Data Structures and Algorithms with C#, Copyright © 2017-2018 Ahmed Mohyeldin 22
Pseudocode – Pseudocode Example
C-Style Pseudo Code Example:
1 void function ShowOddEven
2 for (i = 1; i <= 100; i++) {
3 If i is divisible by 2
4 print i & " is EVEN NUMBER" & a newline;
5 else
6 print i & " is ODD NUMBER" & a newline;
7 }
Fortran-Style Pseudo Code Example:
1 Program function ShowOddEven
2 do i = 1 to 100
3 If i is divisible by 2
4 print i & " is EVEN NUMBER" & a newline;
5 else
6 print i & " is ODD NUMBER" & a newline;
7 continue
MOHY Mindworks Data Structures and Algorithms with C#, Copyright © 2017-2018 Ahmed Mohyeldin 23
Pseudocode – Flowgorithm Live Demo
Think Design Draw Test
MOHY Mindworks Data Structures and Algorithms with C#, Copyright © 2017-2018 Ahmed Mohyeldin 24
Implementing Algorithms using
PROGRAMMING LANGUAGES
MOHY Mindworks Data Structures and Algorithms with C#, Copyright © 2017-2018 Ahmed Mohyeldin 25
Computer Languages – What is a Language?
A Programming Language is a language
developed to express programs.
Machine Language: Native code, No compilation is needed!
All computers have a native programming language that they
understand, commonly referred to as machine code. However,
machine code is a difficult language for us to follow:
It is typically expressed in the binary number system, and
It is unique to a particular computer architecture (thus two
different computers could potentially use two different versions
of machine code).
Other Programming Languages Compilation is needed!
Other Programming Languages, Languages such as Assembly,
BASIC, Java and C/C++ exist to provide a better interface between
us, as the programmers, and the computer, by allowing programs to
be expressed in a language that is easier for us to understand and
potentially common to a number of computer architectures, but
which can still be translated into machine code.
MOHY Mindworks Data Structures and Algorithms with C#, Copyright © 2017-2018 Ahmed Mohyeldin 26
Translation Schemes – Why it is needed?
010101010101101101
011000111101010101
011011101010110100
011011101010110100
Human beings speak in Each family of computer
different languages such as processors speaks in their one
English, French, German, Machine Language!
Arabic, etc. A Machine Language
A Human language vocabulary vocabulary is normally
would be composed of millions composed of few binary
of words, where each word is a instructions called the
specific sequence of the instruction set. Each instruction
language alphabet (i.e., set of is a unique binary number (i.e.,
letter letters). sequence of 0s and 1s).
e.g., for the 8086 family Translation 01D8 (Hexadecimal)
ADD EAX, EBX =
Assembly Instruction 0000 0001 1101 1101(Binary)
MOHY Mindworks Data Structures and Algorithms with C#, Copyright © 2017-2018 Ahmed Mohyeldin 27
Translation Schemes – Compiled Scheme
Compilation: is the process of translating
program source code file, written in high-level
language, into platform-specific, machine-
dependent language before it can run.
// HelloWorld.c 0100: 101101111
#include <stdio.h> 1002: 010111001
C:\>[Link]
1004: 001011110
1006: 011011110 Hello, C Programming
int main(void)
{ 1008: 101000010 World!
printf("Hello, C 100A: 111111000 C:\>_
Programming 100B: 010110101
World!\n"); 100C: 101111110
return 0; 100D: 000110100
} 100E: 000110100
Source Code File Executable File
Compile-Time Run-Time
Compiler is a program that translates the source code
of a program, written in a high-level language, to
machine language.
MOHY Mindworks Data Structures and Algorithms with C#, Copyright © 2017-2018 Ahmed Mohyeldin 28
Translation Schemes – Interpreted Scheme
Interpretation: is the process of interpreting a
program's source code line-by-line by a program
called Interpreter, when there is a control
structure .
// HelloWorld.c
#include <stdio.h> C:\>[Link]
Hello, C Programming
int main(void)
{
World!
printf("Hello, C C:\>_
Programming
World!\n"); 0100: 101101111
return 0; 1002: 010111001
} 1004: 001011110
Source Code File Machine Code
Instructions Stream
Compile-Time Run-Time
Interpreter is a program that translates the source
code of a program, written in a high-level/intermediate
language, line-by-line to machine language at run-
time.
MOHY Mindworks Data Structures and Algorithms with C#, Copyright © 2017-2018 Ahmed Mohyeldin 29
Question!
What do you think?
Compilation is a “compile once and run many”
scheme.
Interpretation is an “interpret at every run line-
by-line” scheme.
Both schemes have their advantages and
disadvantages.
Is there any thing in-between
MOHY Mindworks Data Structures and Algorithms with C#, Copyright © 2017-2018 Ahmed Mohyeldin 30
Translation Schemes – Hybrid Scheme
Hybrid (Mixed) Translation: is the combination of
both Interpreted and Compiled schemes.
// [Link] 0100: 101101111
using System; 1002: 010111001
class Program { 1004: 001011110
static int main(void) 1006: 011011110
{ [Link]( 1008: 101000010
"Hello, C# Prog. 100A: 111111000
…… World!\n"); It to intermediate Code 100B: 010110101
return 0; 100C: 101111110
} 100D: 000110100
} 100E: 000110100
Source Code File P-Code File
Compile-Time
0100: 101101111 0100: 101101111
1002: 010111001 1002: 010111001
C:\>[Link]
1004: 001011110 1004: 001011110
1006: 011011110 1006: 011011110
Hello, C# Programming
1008: 101000010 1008: 101000010 World!
100A: 111111000 100A: 111111000 C:\>_
100B: 010110101 100B: 010110101
100C: 101111110 100C: 101111110
100D: 000110100 100D: 000110100
100E: 000110100 100E: 000110100
P-Code File Executable File
Run-Time
MOHY Mindworks Data Structures and Algorithms with C#, Copyright © 2017-2018 Ahmed Mohyeldin 31
Translation Schemes – Quick Comparison
LARGE
Pure Compiler
FAST
Implementation
Examples: Fortran, Pascal, C, C++
Executable Size
Execution Speed
Hybrid Pseudo-Code
Implementation
Examples: Java, Microsoft C# .NET
Pure Interpreter
SMALL
SLOW
Implementation
Examples: BASIC
MOHY Mindworks Data Structures and Algorithms with C#, Copyright © 2017-2018 Ahmed Mohyeldin 32
Programming Languages – Evolution Process
Classification Levels of Programming Languages:
HUAMN LANGUAGE
HIGH LEVEL
LOW LEVEL
BINARY
MOHY Mindworks Data Structures and Algorithms with C#, Copyright © 2017-2018 Ahmed Mohyeldin 33
Programming Paradigms – Evolution Process
The Program Design Philosophy Evolution
Process:
MOHY Mindworks Data Structures and Algorithms with C#, Copyright © 2017-2018 Ahmed Mohyeldin 34
Programming Paradigms – Linear
Linear
Program
Skelton
Examples:
BASIC, Fortran
Advantages
Suitable for small programs.
Disadvantages
Difficult to debug.
Difficult to maintain.
Difficult to write large complex programs.
Sequential order programming prevents the use of teamwork.
Repetition (Redundancy) of instructions.
MOHY Mindworks Data Structures and Algorithms with C#, Copyright © 2017-2018 Ahmed Mohyeldin 35
Programming Paradigms – Structured
Structured
Program
Skelton
Examples:
Pascal, C
Advantages
Easier to debug.
Easier to maintain.
Made it possible to write moderately complex programs fairly easily.
Modular design concept supports the use of teamwork.
Disadvantages
Global variables and shared data areas.
Difficult to establish very large programs.
Limited capabilities to reuse code in different projects.
MOHY Mindworks Data Structures and Algorithms with C#, Copyright © 2017-2018 Ahmed Mohyeldin 36
Programming Paradigms – Object-Oriented
Object-Orientation (OO) is one of the
computational thinking methodologies which
implies that
Any system is composed of objects (certainly a
system is also an object).
e.g., Computer System, Educational System, Political System,
Economic System, Law System, etc.
The evolution and development of a system is caused
by the interactions among the objects inside or
outside the system.
Object 1
Object 4 Object 2
Object 3
MOHY Mindworks Data Structures and Algorithms with C#, Copyright © 2017-2018 Ahmed Mohyeldin 37
Introduction to DSA – Homework
MOHY Mindworks Data Structures and Algorithms with C#, Copyright © 2017-2018 Ahmed Mohyeldin 38
Introduction to DSA – Homework
Flowchart Representation Assignments:
1. Draw a flowchart to represent the algorithms shown in List
HW1.1 and List HW1.2.
Pseudocode Representation Assignments:
2. Write a pseudocode to represent the algorithms shown in List
HW1.1 and List HW1.2 using the following pseudocode styles:
Gaddis-Style, C-Style and Fortran-Style pseudocodes.
Program Implementation Assignments:
3. Write efficient C# implementations of the previous
pseudocodes shown in List HW1.1 and List HW1.2 using static
class methods that work on matrices of double elements.
Bonus: Write a C# Matrix class of double elements that
supports matrix addition and multiplication operations.
Good Luck
MOHY Mindworks Data Structures and Algorithms with C#, Copyright © 2017-2018 Ahmed Mohyeldin 39
Algorithm Analysis – Homework (HW1.1) (…cont’d)
1 // List HW1.1: Matrix Addition Algorithm – Pseudocode
2 //
3 procedure Add( A : n X m matrix, B: n X m matrix)
4 defined as:
5
6 for each i in 0 to n - 1 inclusive do:
7 for each j in 0 to m - 1 inclusive do:
8 C[i][j] += A[i][j] + B[i][j]
9 end for
10 end for
11
12 return C
13 end procedure
14
15 /* Count the steps that are taken during execution? */
16
17
18
19
MOHY Mindworks Data Structures and Algorithms with C#, Copyright © 2017-2018 Ahmed Mohyeldin 40
Algorithm Analysis – Homework (HW1.2) (…cont’d)
1 // List HW1.2: Schoolbook Matrix Multiplication
2 // Algorithm – Pseudocode #1:
3 procedure Multiply( A : n X m matrix, B: m X l matrix)
4 defined as:
5
6 for each i in 0 to n - 1 inclusive do:
7 for each j in 0 to l - 1 inclusive do:
8 C[i][j] = 0
9 for each k in 0 to m - 1 inclusive do:
10 C[i][j] += A[i][k] * B[k][j]
11 end for
12 end for
13 end for
14 return C
15 end procedure
16
17 /* Count the steps that are taken during execution? */
18
19
MOHY Mindworks Data Structures and Algorithms with C#, Copyright © 2017-2018 Ahmed Mohyeldin 41
Algorithm Analysis – Homework (HW1.2) (…cont’d)
1 // List HW1.2: Schoolbook Matrix Multiplication
2 // Algorithm – Pseudocode #2:
3 procedure Multiply( A : n X m matrix, B: m X l matrix)
4 defined as:
5
6 for each i in 0 to n - 1 inclusive do:
7 for each j in 0 to l - 1 inclusive do:
8 sum = 0
9 for each k in 0 to m - 1 inclusive do:
10 sum += A[i][k] + B[k][j]
11 end for
12 C[i][j] = sum
13 end for
14 end for
15 return C
16 end procedure
17
18 /* Count the steps that are taken during execution? */
19
MOHY Mindworks Data Structures and Algorithms with C#, Copyright © 2017-2018 Ahmed Mohyeldin 42
SUMMARY – Introduction to DSA.
Computers?
Computer is a tool for solving problems with data! That is
“Computers is all about running Algorithms through Programs”.
Algorithms?
An Algorithm is a finite set of unambiguous instructions which
provides a solution to a particular class of problems.
Algorithm Characteristics Precise, Finite, General, Efficient,
and Consistent.
Algorithm Representation Flowcharts, or Pseudocode.
Programming Languages?
A Programming Language is a language developed to
express programs (i.e., implement algorithms).
Classification Levels Low Level (Machine, Assembly) and
High-Level (C, C++, C#, Java, Python, etc.).
Translation Schemes Compiled, Interpreted, or Hybrid.
Programming Paradigms Linear, Structured, or Object-
Oriented.
MOHY Mindworks Data Structures and Algorithms with C#, Copyright © 2017-2018 Ahmed Mohyeldin 43
P Please consider the environment before printing this material.
C:\>[Link]
DSA Programming
with C#!
C:\>_
Now, Let’s go to the DSA programming Lab
Lecture #1: Introduction
Problem Algorithm Flowchart/Pseudocode Program
Solution
SUMMARY – Q & A
Feedbacks: email to ameldin@[Link]. We value your feedback!
(Please include the following prefix in the subject field: [DSA-C#])
MOHY Mindworks Data Structures and Algorithms with C#, Copyright © 2017-2018 Ahmed Mohyeldin 44