Ecs 1
Ecs 1
computer science
Overview
Programming Paradigm Virtualization
Computer Language
Comparatively easier
Debugging and
Not easy Easy
Maintenance
HLL vs LLL
Low Level Language (LLL) High Level Language (HLL)
Direct memory Management Interpreted
It provides a set of commands by segregating the program into variables, functions,
statements & conditional operators.
Various Programming editors or IDEs help users develop programming code using
one or more programming languages.
Some of them are Adobe Dreamweaver, Eclipse or Microsoft visual studio, BASIC, C,
Java, PASCAL, FORTRAN are examples of Procedural Programming Language.
Procedural Language (Sample)
#include <stdio.h>
int main() printf("G.C.D of %d and %d is %d", n1, n2,
gcd);
{
int n1, n2, i, gcd;
return 0;
printf("Enter two integers: ");
}
scanf("%d %d", &n1, &n2);
for(i=1; i <= n1 && i <= n2; ++i)
{
// Checks if i is factor of both integers
if(n1%i==0 && n2%i==0)
gcd = i;
}
Functional Programming Language
Declarative programming paradigm constructed by applying and composing
functions.
Does not support iteration like loop statements & conditional statements like if-else.
Some of the most prominent functional programming languages are Haskell, SML,
Scala, F#, ML, Scheme, and More.
Functional Programming Language
(Sample)
#include<iostream.h> void main()
#include<conio.h> {
int x=400, y=600;
void swap(int x, int y) clrscr();
{ swap(x, y); // arguments
passed to the function
int temp;
cout<<"Value of x"<<x;
temp=x;
cout<<"Value of y"<<y;
x=y;
getch();
y=temp;
}
}
Object-oriented programming Language
Based on the “objects” i.e. it contains data in the form of fields and the code in the
form of procedures.
Most multi-paradigm languages are OOPs languages such as Java, C++, C#, Python,
Javascript, and more.
Object-oriented programming
Language (Sample)
// Main method
public class Main {
public static void main(String[] args) {
// Static method
myStaticMethod(); // Call the static method
static void myStaticMethod() {
// myPublicMethod(); This would compile an
System.out.println("Static methods can be called error
without creating objects");
}
Main myObj = new Main(); // Create an object of
// Public method Main
public void myPublicMethod() { myObj.myPublicMethod(); // Call the public
method on the object
System.out.println("Public methods must be
called by creating objects"); }
} }
Scripting Programming Languages
programming languages that are not compiled but are rather interpreted.
The instructions are written for a run time environment.
majorly used in web applications, System administration, games and multimedia.
It is used to create plugins and extensions for existing applications.
Server Side Scripting Languages: Javascript, PHP, and PERL.
Client-Side Scripting Languages: Javascript, AJAX, Jquery
System Administration: Shell, PERL, Python
Linux Interface: BASH
Web Development: Ruby
Scripting programming Language
(Sample)
$('#loadTable').click(function() {
$.get('example/tableData.xml', function(data) {
$('div#tableWrapper').append('<table id="ajaxTable"></table>');
$(document).ready(function() {
$(data).children('tabledata').children('row').each(function() {
var thisRow = this;
$('table#ajaxTable').append('<tr></tr>');
$(thisRow).children('column').each(function() {
var thisColumn = $(this).text();
$('table#ajaxTable').children('tbody').children('tr').last().append('<td
style="border: 1px solid black">' + thisColumn + '</td>');
});
});
});
});
});
Logic Programming
The programming paradigm is largely based on formal logic.
The language does not tell the machine how to do something but employs
restrictions on what it must consider doing.
Assembler
abstract interface
Computer
Machine Architecture
abstract interface
Language
Hardware Gate Logic
abstract interface
Platform
Electrical
Chips & Engineering
Hardware Physics
Logic Gates
hierarchy
Machine language
Abstraction – implementation duality:
Symbolic version
Memory
state
reset Screen
Computer
Keyboard
Basic computer
A 16-bit machine consisting of the following elements:
inM
writeM
Instruction outM Data
instruction
CPU
Memory Memory
addressM
(ROM32K) (Memory)
pc
reset
Both memory chips are 16-bit wide and have 15-bit address space.
Basic computer (CPU)
A 16-bit machine consisting of the following elements:
ALU output
C C
C
D
D
C C
decode
outM
ALU
C
A
Mux
A
C
instruction A/M
Mux
M
inM
C writeM
A
addressM
C
reset
A
PC pc
Basic computer
A 16-bit machine consisting of the following elements:
Data memory: RAM – an addressable sequence of registers
Instruction memory: ROM – an addressable sequence of registers
Registers: D, A, M, where M stands for RAM[A]
Processing: ALU, capable of computing various functions
Program counter: PC, holding an address
Control: The ROM is loaded with a sequence of 16-bit instructions, one per
memory location, beginning at address 0. Fetch-execute cycle: later
Instruction set: Two instructions: A-instruction, C-instruction.
The A-instruction
Where value is either a number or a symbol referring to some number.
Why A-instruction?
Effect:
@17 // A = 17
D = A // D = 17
Selecting a RAM location
( register = RAM[A]) @17 // A = 17
D = M // D = RAM[17]
M = -1 // RAM[17]=-1
0, 1, -1, D, A, !D, !A, -D, -A, D+1, A+1, D-1, A-1, D+A, D-A, A-D, D&A, D|A
M, !M, -M, M+1, M-1, D+M, D-M, M-D, D&M, D|M
symbolic binary
@17 0000 0000 0001 0001
translate
D+1; JLE 1110 0111 1100 0110
execute
hardware
The A-instruction
symbolic binary
@value 0value
value is a non-negative decimal
number <= 215-1 or value is a 15-bit binary number
Example
@21 0000 0000 0001 0101
The C-instruction
binary
symbolic
111A C1C2C3C4 C5C6 D1D2 D3J1J2J3
dest = comp ; jump
]
comp dest jump
not used
opcode
111A C1C2C3C4 C5C6 D1D2 D3J1J2J3
The C-instruction
A D M
111A C1C2C3C4 C5C6 D1D2 D3J1J2J3
The C-instruction
Hack assembly/machine language
Source code (example) Target code
// Computes 1+...+RAM[0] 0000000000010000
// And stored the sum in RAM[1] 1110111111001000
@i 0000000000010001
M=1 // i = 1 1110101010001000
@sum 0000000000010000
M=0 // sum = 0 1111110000010000
(LOOP) 0000000000000000
@i // if i>RAM[0] goto WRITE 1111010011010000
D=M 0000000000010010
@R0 1110001100000001
D=D-M 0000000000010000
@WRITE 1111110000010000
D;JGT assemble 0000000000010001
@i // sum += i 1111000010001000
D=M 0000000000010000
@sum assembler 1111110111001000
M=D+M 0000000000000100
@i // i++ or CPU emulator 1110101010000111
M=M+1 0000000000010001
@LOOP // goto LOOP 1111110000010000
0;JMP 0000000000000001
(WRITE) 1110001100001000
@sum 0000000000010110
D=M 1110101010000111
@R1
M=D // RAM[1] = the sum
(END)
@END We will focus on writing the assembly code.
0;JMP
Working with registers and memory
D: data register
A: address/data register
M: the currently selected memory cell, M=RAM[A]
Programming exercises
Exercise: Implement the following tasks
using commands:
1. Set D to A-1
2. Set both A and D to A + 1
3. Set D to 19
4. D++
5. D=RAM[17]
6. Set RAM[5034] to D - 1
7. Set RAM[53] to 171
8. Add 1 to RAM[7],
and store the result in D.
Programming exercises
Exercise: Implement the following tasks
1. D = A-1
using Hack commands:
2. AD=A+1
1. Set D to A-1
3. @19
2. Set both A and D to A + 1
D=A
3. Set D to 19
4. D=D+1
4. D++
5. @17
5. D=RAM[17]
D=M
6. Set RAM[5034] to D - 1 6. @5034
7. Set RAM[53] to 171 M=D-1
8. Add 1 to RAM[7], 7. @171
and store the result in D.
D=A
@53
M=D
8. @7
D=M+1
A simple program: add two numbers (demo)
PROGRAM DEVELOPMENT
C++ program
// Your First C++ Program
#include <iostream>
int main() {
return 0;
}
Java Program
/* This is a simple Java program. public static void main(String
args[])
FileName : "HelloWorld.java". */
{
class HelloWorld
System.out.println("Hello,
{ World");
// Your program begins with a call to }
main().
}
// Prints "Hello, World" to the
terminal window.
Development cycle (PDLC)
Define
◦ Scope, objective
Analyze
◦ Input – process - output
Assumptions, deliverables, exceptions
Algorithm development
Coding and Documentation
Testing and Debugging
Maintenance
Algorithm
An algorithm is a sequence of unambiguous instructions for solving a
problem, i.e., for obtaining a required output for any legitimate input
in a finite amount of time
Algorithmic Analysis
Space complexity
Time complexity
Further, dependency on
◦ amount of input
◦ Sequence of input
What does “size of the input” mean?
If we are searching an array, the “size” of the input could be the size of the array
If we are merging two arrays, the “size” could be the sum of the two array sizes
If we are computing the nth Fibonacci number, or the nth factorial, the “size” is
n
We choose the “size” to be the parameter that most influences the actual
time/space required
83
General Analysis
Complexity is measured as a function of size of input
2n, nn,n!
Better execution time is through algorithm with linear growth for large
amount of data
Time Analysis
Active or Characteristic Operations alone to be considered
Ignore the constants and arrive at a common notation with respect to ‘n’
with the highest value.
Is it possible to find exact values?
It is sometimes possible, in assembly language, to compute exact time and
space requirements
We know exactly how many bytes and how many cycles each machine
instruction takes
The steps required to sort an array depend on the actual numbers in the array
(which we do not know in advance)
High level Languages
In a higher-level language (such as Java), we do not know how long each operation takes
We don’t know exactly what the compiler does with this
The compiler probably optimizes the test anyway (replacing the slower version with
the faster one)
It does not include calling a method whose time is unknown or is not a constant
If a statement involves a choice (if or switch) among operations, each of which takes
constant time, we consider the statement to take constant time
n : The number of times through the loop (we can use this as the “size” of the
problem)
Which is better?
◦ Clearly, algorithm B is better if our problem size is small, that is, if n < 50
We usually care most about very large problems - But not always!
The array subset problem
Suppose you have two sets, represented as unsorted arrays:
int[] sub = { 7, 1, 3, 2, 5 };
int[] super = { 8, 4, 7, 1, 2, 3, 9 };
and you want to test whether every element of the first set (sub) also occurs in the second set
(super):
System.out.println(subset(sub, super));
(The answer in this case should be false, because sub contains the integer 5, and super
doesn’t)
We are going to write method subset and compute its time complexity (how fast it is)
Let’s start with a helper function, member, to test whether one number is in an array
92
member
static boolean member(int x, int[] a) {
int n = a.length;
for (int i = 0; i < n; i++) {
if (x == a[i]) return true;
}
return false;
}
If x is not in a, the loop executes n times, where n = a.length
◦ This is the worst case
If x is in a, the loop executes n/2 times on average
Either way, linear time is required: k*n+c
subset
static boolean subset(int[] sub, int[] super) {
int m = sub.length;
for (int i = 0; i < m; i++)
if (!member(sub[i], super) return false;
return true;
}
The loop (and the call to member) will execute:
◦ m = sub.length times, if sub is a subset of super
◦ This is the worst case, and therefore the one we are most interested in
◦ Fewer than sub.length times (but we don’t know how few)
◦ We would need to figure this out in order to compute average time complexity
The worst case is a linear number of times through the loop
But the loop body doesn’t take constant time, since it calls member, which takes linear
time
Analysis of array subset algorithm
We’ve seen that the loop in subset executes m = sub.length times (in the
worst case)
Also, the loop in subset calls member, which executes in time linear in n =
super.length
Hence, the execution time of the array subset method is m*n, along with
assorted constants
◦ We go through the loop in subset m times, calling member each time
◦ We go through the loop in member n times
◦ If m and n are similar, this is roughly quadratic
95
What about the constants?
Forget the constants!
An added constant, f(n)+c, becomes less and less important as n gets larger
◦ Improving k gives a linear speedup (cutting k in half cuts the time required in half)
Our timing formula is a polynomial, and may have terms of various orders
(constant, linear, quadratic, cubic, etc.)
◦ We simplify n2 + 3n + 5 to just n2
Big O notation
When we have a polynomial that describes the time requirements of an
algorithm, we simplify it by:
◦ Throwing out all but the highest-order term
◦ Throwing out all the constants
Consider R = x2 + 3x + 5 as x varies:
◦ x=0 x2 = 0 3x = 0 5=5 R=5
◦ x = 10 x2 = 100 3x = 30 5=5 R = 135
◦ x = 100 x2 = 10000 3x = 300 5=5 R = 10,305
◦ x = 1000 x2 = 1000000 3x = 3000 5=5 R = 1,003,005
◦ x = 10,000 x2 = 108 3x = 3*104 5=5 R = 100,030,005
◦ x = 100,000 x2 = 1010 3x = 3*105 5=5 R = 10,000,300,005
100
y = x2 + 3x + 5, for x=1..10
101
y = x2 + 3x + 5, for x=1..20
102
Common time complexities
BETTER O(1) constant time
O(log n) log time
O(n) linear time
O(n log n) log linear time
O(n2) quadratic time
O(n3) cubic time
O(2n) exponential time
WORSE
103
Algorithm
Repeat for I = 1 to N
Repeat for J = 1 to N
SUM 0
Repeat for K = 1 to N
SUM SUM + A[I, K] * B[K, J]
End Repeat K
C[I,J] SUM
End Repeat J
End Repeat I
Order Notation
The Big-O notation:
◦ asymptotic behavior
Example: 2n + 10 is O(n)
◦ 2n + 10 cn
◦ (c 2) n 10
◦ n 10/(c 2)
◦ Pick c = 3 and n0 = 10
More Big-Oh Examples
7n-2
7n-2 is O(n)
need c > 0 and n0 1 such that 7n-2 c•n for n n0
this is true for c = 7 and n0 = 1
3n3 + 20n2 + 5
3n3 + 20n2 + 5 is O(n3)
need c > 0 and n0 1 such that 3n3 + 20n2 + 5 c•n3 for n n0
this is true for c = 4 and n0 = 21
3 log n + 5
3 log n + 5 is O(log n)
need c > 0 and n0 1 such that 3 log n + 5 c•log n for n n0
this is true for c = 8 and n0 = 2
Big-Oh and Growth Rate
The big-Oh notation gives an upper bound on the growth rate of a function
The statement “f(n) is O(g(n))” means that the growth rate of f(n) is no more
than the growth rate of g(n)
We can use the big-Oh notation to rank functions according to their growth
rate
f(n) is O(g(n)) g(n) is O(f(n))
g(n) grows more Yes No
f(n) grows more No Yes
Same growth Yes Yes
Big-Oh Rules
f n a0 a1n a2 n2 ... ad nd
If is f(n) a polynomial of degree d, then f(n) is O(nd), i.e.,
Example:
◦ We determine that algorithm arrayMax executes at most 7n 1 primitive
operations
◦ We say that algorithm arrayMax “runs in O(n) time”
Since constant factors and lower-order terms are eventually dropped anyhow, we can
disregard them when counting primitive operations
Important Functions Growth Rates
n log(n) n nlog(n) n2 n3 2n
8 3 8 24 64 512 256
◦ Easier to analyze
Some algorithms may be more efficient if data completely loaded into memory
version of instructions.
variables are pushed onto the system stack, where they wait for further
variables of the function A()will get stored on the system stack temporarily,
while the function B() is called and executed inside the funciton A().
Example: }
Linear: int sum(int A[], int n) { int sum = 0, i; for(i = 0; i < n; i++) sum
= sum + A[i]; return sum; }
Relatives of Big-Oh
big-Omega
big-Theta
◦ f(n) is (g(n)) if there are constants c’ > 0 and c’’ > 0 and an
integer constant n0 1 such that c’•g(n) f(n) c’’•g(n) for
n n0
Examples
You go and ask the first person of the class, if he has the pen. Also, you ask
this person about other 99 people in the classroom if they have that pen &
So on.
O(log n): Now I divide the class in two groups, then ask: “Is it on the left
side, or the right side of the classroom?” Then I take that group and divide
it into two and ask again, and so on. Repeat the process till you are left with
one student who has your pen. This is what you mean by O(log n).
Problem
int i, j, k = 0;
for (i = n / 2; i <= n; i++) {
for (j = 2; j <= n; j = j * 2) {
k = k + n / 2;
}
}
Output:
O(nLogn)
Problem
int a = 0, i = N;
while (i > 0) {
a += i;
i /= 2;
}
PROGRAM
EXECUTION
program
Plain text
Parsing
Text to command
Resource Manager
Bus
I / O Devices
Central processing unit
Central Processing Unit
A bus is a group of parallel wires that carry control signals and data
between components.
Main memory
Main memory holds information such as computer programs, numeric
data, or documents created by a word processor.
If a capacitor does not have a charge, then its state is said to be 0, or OFF.
150
Secondary Storage Media
Disks -- floppy, hard, removable (random access)
• Tapes (sequential access)
• CDs (random access)
• DVDs (random access)
Secondary storage media store files that contain
• computer programs
• data
• other types of information
• Called persistent (permanent) storage because it is non-volatile.
151
I/O (Input/Output) Devices
Information input and output is handled by I/O (input/output) devices.
More generally, these devices are known as peripheral devices.
Examples:
◦ monitor
◦ keyboard
◦ mouse
◦ disk drive (floppy, hard, removable)
◦ CD or DVD drive
◦ printer
◦ scanner
152
Computer level hierarchy