Scarica IT CODING, TOOLS AND SECURITY - TESSERA, BARBIERATO, ACCETTA e più Appunti in PDF di Programmazione e controllo solo su Docsity!
PRECOURSE (no exam) LECTURE 1 Introduction to Computer Science Computer Science is the study of computational systems. Computer scientists design and analyze algorithms (automatic procedure) to solve programs and study the performance of computer hardware (machine) and software (program). Abstract problems:
- determining what problems can be solved with computers and the complexity of the algorithms that solve them Tangible problems:
- designing applications that perform well on handheld devices, that are easy to use, and that uphold security measures. Principal areas of study within Computer Science include:
- artificial intelligence,
- computer systems and networks, security,
- database systems,
- human computer interaction,
- vision and graphics,
- numerical analysis, programming languages,
- software engineering,
- bioinformatics and theory of computing what can be compute and what not by the machines History of Automatic Calculus The history can be divided into generation, together with an invention:
- Generation 0: Mechanic calculator (1642-1945)
- I Generation : vacuum tubes (1945-1955)
- von Neumann Architecture;
- II Generation : transistor (1955-1965)
- III Generation : Integrated circuits (1965-1980)
- IV Generation : PC and VLSI
- V Generation: Artificial Intelligence Blaise Pascal (1623-1662), French mathematician, physicist, philosopher and theologist. Pascaline : He designed a machine to add and subtract two numbers directly and to perform multiplication and division through repeated addition or subtraction. The machine was able to take in account a carry mechanism. Carry: riporto Gottfried von Leibniz (1646-1716): German mathematician, physicist, theologist and philosopher. Leibniz discovered together with Newton (but independently) calculus (derivatives and integrals). He proposed a mechanic process to assess the correctness of an argument in case of a dispute (Calculemus!) defining who’s right and who’s wrong. The Victorian Age Charles Babbage designed (he never built it) the architecture of Analytical Engine (1834). The machine consisted of four compontents:
- The input section, to introduce the commands to the engine (on punched cards);
- The store (a memory of a capacity equal to 1000 numbers with 40 digit);
- The Mill (calculus unit);
- The output section to show the results of the execution of the commands (on a printer).
Babbage was never able to build the machine because of financial issues. However, its project was realized in more recent times and you can admire it at the Science Museum of London! Ada Lovelace was a mathematician and the daughter of the English romantic poet Lord Byron. Working with Babbage, she understood the importance of writing programs by using a programming language in order to use the Analytical Engine efficiently: a modern programming language has been called Ada after Ada Lovelace. 1800 - 1945 (USA)
- IBM H. Hollerith (1869 - 1929) built an electromechanic system to process the data from the census (censimento) in 1890. Founder of International Business Machines, or IBM).
- The relay Konrad Zuse : built a computer working using an electric operated switch (relay) at the end of the ‘30s, though it was destroyed during a bombing in WWII;
- The binary System : J. Atanasoff e G. Stibbitz understood that a computer should have been representing the information by using the binary system. The II World War: Enigma, Colossus and Alan Turing Winning the war against the Axis motivated the realization of more powerful computers. A. Turing (1912 -1954) was an English mathematician and one of the founder of Computer Science (and Artificial Intelligence, we might add!). He build a machine able to decipher the secret code produced by a device called Enigma used by German submarines to find out the location of British ships. Turing partecipated to the realization of a computer called Colossus (1943) to be deployed in the war. After the war, Turing workend on computability problems and the possibility to build a thinking machine (on a seminal article published on the «Mind» journal in 1950), considered the foundation of artificial intelligence. I Generation (vacuum tube) : a device that controls electric current flow or not making possible to have a binary system. A computer based on this new technology called UNIVAC. J. von Neumann: Hungarian mathematician, enfant prodige (he could speak ancient Greek at the age of 6), left Europe before WW2 to work in USA at the atomic bomb. He developed a new conception of computer essentially still in use today (von Neumann’s machine). The computer we have now is still based on the von Neumann architecture:
- a memory that stores data and instructions
- a control unit that contains an instruction register and program counter
- a processing unit that contains an arithmetic logic unit and an accumulator (a small memory faster than the bigger Memory)
- Input and output mechanisms
- External mass storage II Generation : The transistor : invented in the Bell laboratories. A transistor is a semiconductor device used to amplify or switch electronic signals and electrical power (so it can interrupt or letting the flow of electricity but also amplify it). IBM produced new machines transistors-based called 7090 and 7094. Smaller computers were produced later (1401) and sold to commercial companies (so, not just the Army!). III Generation: The integrated circuits : IBM built a new family of computers called System 360 , providing the possibility to execute more than one program at time. DEC built PDP-11 system targeting universities and research centres, becoming a competitor of IBM.
in the position of the unit (30+7). This is the same in the binary because the number 11001101, we can say that the first digit from the left (1) has position 7, the second (1) has position 6, the third (0) has position 5, and so on, until the last that has position 0. In binary we always start counting from 0! In a binary number of 8 digits, the first digit on the left is the most significant (it has the highest weight ), in opposition to the last one (1), which is the least significant (it has the lowest weight ). How can we convert a number in base 2, such as 1001012 into a number in base 10? The idea is to multiply each digit, from left to right, by 2 at the power of the position of the digit: For example, let’s convert 100101 2 in base 10:
- N 10 = 1 x 2^5 + 0 x 2^4 + 0 x 2^3 +1 x 2^2 + 0 x 2^1 + 1 x 2^0 = 37 10 So far, we have to alphabet, A and C. We want to represent words or numbers, written in A, using simbols of C. Let’s start saying that ‘A’ corresponds to digit 0, that ‘B’ corresponds to digit 1… And then? The problem is the cardinality of C, which has ONLY two symbols. IDEA: instead of using only one bit, shall we try with two bits? We started with 1 bit , so C ={0, 1} then to 2 bits , so C = {00, 01, 10, 11}, then to 3 bits with C = {000, 001, 010, 011, 100, 101, 110, 111}; That’s better, since with 3 bits we can code up to 8 letters of A!, but it’s not enough! We need to add more bits, but not too many… How many do we need? Every time we add a bit, we double the size of the letters we can code:
- with 1 bit, we code 2^1 = 2 symbols of A;
- with 2 bits, we code 2^2 = 4;
- with 3 bits, we code 2^3 = 8 and so on. How about reviewing some basic Math notions?
Given two numbers a, b , by writing a b we mean « a x a x … x a » b times.
For example: 4^3 = 4 x 4 x 4 = 64 Properties: If a, b, c are numbers, then ab^ x ac^ = a (b +c)^ and ab^ / a c^ = a(b-c). Special cases : If b = 1, then a b^ = a. Proof : in this case, a is multiplied to itself once so a 1 = a. If b = 0, then a b^ = 1. Proof : let it be x any number, then a 0 = a(x - x) = a x^ / a x^ = 1 Historically, the idea was to consider a set of 7 bits , which allows to represent up to 2^7 = 128 characters (allow coding all the numbers, letters, capital letters and symbols). After some thoughts, the final decision was to consider a set of 8 bits, which allows to represent up to 28 = 256 characters. These decisions have been taken in 1960 by the American Standards Association that proposed to associate to each of the 256 combinations of bits, one unique symbol (upper/lower letters, digits, punctuation signs, …). This coding convention is called ASCII – ASKI in English (American Standard Code for Information Interchange) and it is a standard! How this standard is composed of:
- The first column (decimal) is just a progressive digit counting all the configurations, numbered from 0 to 127;
- The second column (binary) is the actual bits combination and – at the same time - a coding in base two of the digit in the first column; The values in this column are called bytes ; • By convention, 1 Byte = 8 bits ;
- The third and fourth columns concern representations in base 8 and 16 of the first column ;
- The last column ( ASCII ) represent the character of the alphabet (you may ignore the first 31, as they are not relevant in this class). This codification is embeded in our computer because it refers to the ASCII table, what is important is the binary coloumn and the ASCII. All the letters are presented alphabetically because this is the only way computer knows the alphabetic ordered. Look at the 33rd row showing the! character: the ASCII table tells us that the esclamation point is associated to the sequence of bits 00100001. This means that if you hit the! Symbol on your keyboard, then only the 3rd and the 8th wires of a circuit connected to the the PC will be activated (all the remaining wires are off). In this way, the computer knows you hit the! character. Be careful to not confuse the necessity to convert the decimal numbers into binary numbers and the necessity to convert a number as a character! For example, the numbers in the arithmetical expression «2 + 2» will be coded by collecting the remainders originated by the repeated divisions by 2. On the other hand, the number 24 in «Via Roma 24» (that is, an address) has not the same meaning of the previous example: from the point of view of the computer, 24 is not a number – strictly speaking – but just a combination of two characters (‘2’ and ‘4’) that are coded by using the ASCII table. Overflow and underflow A sequence of 8 bits is called byte. Let’s imagine we have been provided a peculiar computer called Mike that provides cells of memory that can contain 1 byte , that means we can represent 2^8 = 256 values that is any positive integer number between 0 and 255, right? Let’s suppose that we have stored in a memory cell the number 255 (in binary, this corresponds to 11111111). Mike runs every 10 seconds a program called sum(1) that increments of 1 the content of a memory cell: what happens when the number 255 (11111111) is increased upto 256? BOOM!!!!!! No memory left because there was only one byte from 255 to 256. The overflow is the impossibility of the computer of representing the number because the memory is insufficient. The opposite problem is the underflow : the number is too small to be represented. LECTURE 2 Von Neumann machine This machine model was proposed by the computer scientist and mathematician John von Neumann whose developement was possible thanks to the defend founding. It consists of:
- Memory : Storage of information (data/program)
- Processing Unit : Computation/Processing of Information and logic operation (i.e. 10>5?, C preceed A?)
- Input : Means of getting information into the computer. e.g. keyboard, mouse
- Output : Means of getting information out of the computer. e.g. printer, monitor
- Control Unit : Makes sure that all the other parts perform their tasks correctly and at the correct time. CPU, ALU and main memory Central Processing Units (CPU) are built by placing billions of microscopic transistors onto a single computer chip. Those transistors allow to take the actions it needs to run programs that are stored on your system’s memory. CPU is responsible for processing and executing instructions of programs. This process breaks down into three key stages: Fetch, Decode, and Execute. A CPU fetches the instruction from main memory (called RAM – Random Access Memory), decodes what the instruction actually is, and then executes the instruction. There is a continuous relationship between process and memory.
The performance of cache memory is frequently measured in terms of a quantity called Hit ratio , so how many time I found in the cache what I’m looking for. Secondary memory: magnetic disks Disks are magnetically recorded and can be re-recorded over and over. Disks contain rotating platters with a mechanical arm that moves a read/write head across their surface. A disk's surface is divided into concentric tracks (circles within circles), and the thinner the tracks, the more storage is available. The data bits are recorded as magnetic spots on the tracks, and the smaller the spot, the greater the storage. Tracks are further divided into sectors , which hold a block of data that is read or written at one time. For example, READ SECTOR 388, WRITE SECTOR 3214. In order to update the disk, one or more sectors are read into the computer, changed and written back to disk. We have a cylinder that is the set of tracks (on each platter) of equal diameter. The access time is a way to measure how fust our hardisk is, calculated dividing it by:
- Seek time (0-10 milliseconds), find the block where the photo are putted into the computer;
- Rotational time (0-3 milliseconds)
- Transfer time (0.02 millisecond per 8 Kbytes), the time needed to code and to transfer to the memory the code. Secondary memory: optical disks There are three main types of optical media:
- CD, DVD, and Blu-ray disc. CDs can store up to 700 megabytes (MB) of data, and DVDs can store up to 8.4 GB of data. Blu-ray discs, which are the newest type of optical media, can store up to 50 GB of data. Each of these discs has at least 3 distinct layers: plastic, reflective aluminum, and protective polycarbonate. The tiny marks , so the informations, made in the disc’s plastic layer correspond to the 0s and 1s present in binary computer code. The next layer surrounding the plastic layer is reflective aluminum. The major function of this layer is allowing a laser’s light to reflect off of the disc’s surface and then back toward a detector. The data encoded in the plastic layer is eventually read and interpreted as the disc reflects backs the laser beam. The final outer polycarbonate layer’s function is to protect the data stored within the optical disc. I/O device Inputs Keyboard: A keyboard is a series of switches (on/off), connected to a microprocessor (CPU). The CPU will recieve the information about the key we want to execute, using the ASCII table and converting it to the configuration of bites (1 or 0). The processor in a keyboard has to understand several things that are important to the utility of the keyboard, such as:
- Position of the key in the key matrix.
- The amount of bounce and how to filter it.
- The speed at which to transmit, all the keys are stored in a temporary memory to be executed. Mouse: As you move it across your desk, the ball rolls under its own weight and pushes against two plastic rollers linked to thin wheels (rotella). One of the wheels detects movements in an up-and-down direction (like the y-axis on graph/chart paper); the other detects side-to-side movements (like the x-axis on graph paper). How do the wheels measure your hand movements? As you move the mouse, the ball moves the rollers that turn one or both of the wheels.
If you move the mouse straight up, only the y-axis wheel turns; if you move to the right, only the x-axis wheel turns. And if you move the mouse at an angle, the ball turns both wheels at once. There are various problems with mice like this. They don't work on all surfaces. Ideally, you need a special mouse mat but, even if you have one, the rubber ball and its rollers gradually pick up dirt, so the x- and y- axis wheels turn erratically and make the pointer stutter across your screen. Nowadays, typically we use an optical mouse. If you need to clear the space on your desk, best is using a cordless mouse. Outputs Laser print: We do not use them a much because it is expensive but also in terms of ecology for the paper used. Laser printers include not only laser but also contain rotating cylinder coated with photosensitive electrically charged material and series of mirrors; • When you are sending a document to a laser printer, laser light is reflected by the mirrors on to the cylinder which neutralizes electric charges in specific areas. Monitor: A computer monitor is an output device that displays information in visual form. A monitor usually comprises the display device, circuitry in addition power supply and case. While older monitors used a cathode ray tube (CRT), the display device in modern monitors is usually a thin film transistor liquid crystal display (TFT-LCD) with LED backlighting. Monitors are connected to the computer via VGA , Digital Visual Interface ( DVI ), HDMI, Display Port, Thunderbolt , or other proprietary connectors and signals. Resolution refers to the number of individual dots of color, known as pixels , contained on a display. Resolution is expressed by identifying the number of pixels on the horizontal axis (rows) and the number on the vertical axis (columns), such as 800x600. Common Display Standards and Resolutions
- XGA (Extended Graphics Array) = 1024x
- SXGA (Super XGA) = 1280x
- UXGA (Ultra XGA) = 1600x
- QXGA (Quad XGA) = 2048x An image is broken down into tiny elements called pixels. A pixel (short for picture element) represents one color. An image with a resolution of 1024 by 798 pixels has 1024 x 798 pixels (817,152 pixels). In order for the computer to store the image, each pixel is represented by a binary value. This representation of colours is called a “ bit-plane ”. Each bit (the amount of bit dedicated to colours is called bit depth ) doubles the number of available colours:
- **1-bit (can be 0 or 1) would give us 2 colours,
- 2-bits would give us 4 colours and
- 3-bits would give us 8 colours etc.** In a monochrome (two colour) image, like the example below, just 1 bit is needed to represent each pixel e.g. 0 for white and 1 for black. LECTURE 3 The Operative system An operative system, or "OS," is software (collection of programs) that communicates with the hardware, reading the ROM memory, and allows other programs to run. It is composed of system software that cannot be modify, that is the fundamental files your computer needs to boot up and function. Every desktop computer, tablet, and smartphone includes an operative system that provides basic functionality for the device. Common desktop operative systems include Windows , OS X , and Linux. While Windows and OS are not free (the prize of the licence is part of the global cost of the computer) and we can’t see what’s inside these close operative systems, Linux is free of charge (gratis) and it can also coexist with Windows, here I can look into a program and also I can modify it. While each OS is different, most provide a graphical user interface , or GUI , that includes a desktop and the
How do we run this programms? In a multi programming environment , the OS decides the order in which processes have access to the processor, and how much processing time each process has. This function of OS is called process scheduling. An operative System performs the following activities for processor management:
- Keeps tracks of the status of processes.
- The program which perform this task is known as traffic controller.
- Allocates the CPU that is processor to a process.
- De-allocates processor when a process is no more required. The File System When you buy a new Hard Disk, you simply install it on your computer (or you ask to a specialist to do it). Once it has been installed, it is necessary to format it, that is to (re)organize it into new tracks and sectors. The format operation can be done more than once, but remember that the effect is to wipe out your hard disk! This is the reason why usually it is run only once. In some OS, this can be irreversibl e (EXAM). It is strongly suggested to partition your hard disk, i.e. to subdivide the disk into smaller logical disk. For example, if the main disk has a capacity of 1Tb , you might create, for example, 4 partitions :
- One for the OS;
- One for your data (you put here your work);
- One to store music (mp3, flac, waw files);
- One to store photos (png, jpeg, gif files); The advantage in doing so is to protect yourself if the disk gets corrupted: without partition ALL your data are in danger!. With partitions: in case of minor fault, likely only one partition is compromised. You can still backup the remain partitions and replace your disk. Another advantage is that if – for whatever reason – you need to reinstall the OS, you just reinstall one partition, but you don’t loose any data (or music o photos). Finally, the partitions allow to better organize your data. You might even install two OS on your PC! You might have both Windows and Linux on your PC (on different partition and using one at the time depending on your needs). Typically, Windows associate a letter of the alphabet to each partitions (called usually drives):
- First partition Drive C: \ (where the OS is installed);
- Second partition Drive D: \ where – for example – you want to store your data;
- Third partition Drive E:/ etc. Remember that Windows partitioning is not mandatory but only suggested. MacOs and Linux partition the disk when they are installed. A file system organizes the files by using a hierarchical system : a Tree! This mean that there is a root (under Windows, it would be a letter identifying the disk, such as “c:/”) under which all the files are leaves and the folders (containers) are descendants of the root. Networks and the Internet A network consists of two or more computers that are linked in order to share resources (such as printers and CDs), exchange files, or allow electronic communications. The computers on a network may be linked through cables, telephone lines, radio waves, satellites, or infrared light beams. Two very common types of networks include:
- Local Area Network ( LAN ): a network that is confined to a relatively small area. It is generally limited to a geographic area such as a writing lab, school, or building. Computers connected to a network are broadly categorized as servers or workstations.
- Servers are generally not used by humans directly, but rather run continuously to provide "services" to the other computers (and their human users) on the network. Services provided can include printing and faxing , software hosting, file storage and sharing, messaging, data storage and retrieval , complete access control (security) for the network's resources, and many others.
- Workstations are called such because they typically do have a human user which interacts with the
network through them.
- Wide Area Network (WAN): networks in larger geographic areas, such as Lombardy, Italy, or the world. Dedicated transoceanic cabling or satellite uplinks may be used to connect this type of global network. Using a WAN, schools in Rome can communicate with places like Berlin in a matter of seconds, without paying enormous phone bills. Two users a half-world apart with workstations equipped with microphones and a webcams might teleconference in real time. A WAN is complicated. It uses multiplexers , bridges, and routers (outside the scope of this class) to connect local and metropolitan networks to global communications networks like the Internet. All this talk to each other so that we need a protocol : set of rules that governs the communications between computers on a network. In order for two computers to talk to each other, they must be speaking the same idiom s. Many different types of network protocols and standards are required to ensure that your computer (no matter which operative system, network card, or application you are using) can communicate with another computer located on the next desk or half-way around the world. The OSI (Open Systems Interconnection) Reference Model defines seven layers of networking protocols. The network interface card ( NIC ) provides the physical connection between the network and the computer workstation. Most NICs are internal, and they are included in the purchase of most computers. • The most common network interface connections are Ethernet cards and wireless adapters.
- Ethernet cards are usually included with a computer, although additional ethernet cards can be purchased and installed on most computers top speed
- Wireless adapters are found in most portable devices, such as laptops, smart phones, and tablet devices.
- Switches : an ethernet switch is a device that provides a central connection for different devices or different PCs. Every time you connect to the Internet from a PC (tablet or smartphone), your provider (TIM, Fastweb, etc.) assigns to your router an Internet Protocol (IP) address : YOU ARE NOT ANONYMOUS!. The IP consist of 4 triplets that will identify you in the whole world. Each computer on a network is connected to a special tiny device called a router. It allows all the PCs to be connected to a network. It makes sure that a message sent from a given computer arrives at the right (destination) computer. To send a message to computer B, computer A must send the message to the router, which in turn forwards the message to computer B (and makes sure the message is not delivered to computer C). What happens if we need to connect hundreds, thousands, billions of computers? A single router can't scale that far, but we can connect two (or more) routers together. The telephone infrastructure already connects your house with anyone in the world. To connect our network to the telephone infrastructure, we need a special component called a modem. This modem turns the information from our network into information manageable by the telephone infrastructure and vice versa. The next step is to send the messages from our network to the network we want to reach. To do that, we will connect our network to an Internet Service Provider (ISP), a company (Telecom Italia, Fastweb, Vodaphone, etc.) that manages some special routers that are all linked together and can also access other ISPs' routers. So the message from our network is carried through the network of ISP networks to the destination network. The Internet consists of this whole infrastructure of networks. Tutorial Windows key and X key to launch cmd a native application of the Windows operative system and gives the user an option to perform operations using commands. When we open it we see a prompt that show us where we are and that the computer is waiting for us to write the command. IMPORTANT: if you run a command from the prompt and for whatever reason you want to stop its execution, hold the keys Ctrl C together! View rhe cmd commands.
- Milan is in Europe therefore, Milan is not in China If the assumptions of the argument are true, the conclusion of the argument must also be true. Inferences Inference is a kind of judgement you may take. You make inferences everyday! Consider this example:
- If you have a current password, then you can log on to the network (MAJOR PREMISE);
- You have a current password (MINOR PREMISE)
- Therefore: You can log on to the network (CONCLUSION). If P is true, then Q is true P is true therefore, Q is true. This inference (syllogism, if you prefer) is called Modus ponens. There is a second kind of syllogism. Consider this examples:
- If there is smoke, there is fire there is not fire, so there is no smoke.
- If I am happy, then I smile I am not smiling, therefore I am not happy. This type of inference is called modus tollens :
- If P is true, then Q is true (MAJOR PREMISE)
- Q is false. (MINOR PREMISE)
- Therefore P is false. (CONCLUSION) Prositional logic A proposition is a logical expression , not a linguistic expression: it is a statement describing a (proposed) state of affairs in the world , that may be true of the world or false. (E.g. Paris is the capital of Italy FALSE). Propositional Logic enables us to systematically determine the content of a compound sentence if we know the content of the simple sentences from which it is formed. Of course, Boolean algebra applies to propositions. For example, we can obtain the meaning of (a) John is upset and Mary is happy if we know the meaning of (b) and (c). a. John is upset and Mary is happy b. John is upset c. Mary is happy. The previous process can be made automatic and potentially solved by a computer, but before doing so, it is necessary to translate proposition from English language into a more convenient form. Consider the following:
- p = It is raining
- q = Mary is sick
- t = Bob stayed up late last night
- r = Paris is the capital of France
- s = John is a loud-mouth Suppose that what we indicate as negation (¬) corresponds to the use of “not”. As per what we said earlier, we can translate the following compound sentences:
- It isn’t raining ¬p
- It is not the case that Mary isn’t sick ¬¬q (double negation that is equal to q)
- Paris is not the capital of France ¬r.
- John is in no way a loud-mouth ¬s
- Bob did not stay up late last night ¬t Now suppose what we understand informally as conjunction ( ∧ ) corresponds to the use of “and” and related terms in natural language, like “but”.
- It is raining and Mary is sick (p ∧ q )
- Bob stayed up late last night and John is a loud mouth (t ∧ s )
- Paris isn’t the capital of France and It isn’t raining (¬r ∧ ¬p )
- John is a loud mouth but Mary isn’t sick ( s ∧ ¬q ). But here is traslated such as and.
- It is not the case that it is raining and Mary is sick It is not the case that both it is raining and Mary is sick ¬(p ∧ q) A tautology is a proposition that is always true: it rains or it doesn’t rain (one of this logical expression two must be true), I am happy or I am not happy, I will pass the exam or I will not pass the exam. A contradiction is a proposition that is always false: it rains and it doesn’t rain, I am happy and I am not happy, I will pass the exam and I will not pass the exam. LECTURE 5 Algorithms and flow-charts The word algorithm derives from the name of the mathematician, Mohammed ibn-Musa al-Khwarizmi , who was part of the royal court in Baghdad and who lived from about 780 to 850. Al-Khwarizmi's work is the likely source for the word algebra as well. Algorithms are widely used throughout all areas of IT ( information technology ). A search engine algorithm, for example, takes search strings of keywords and operators as input, searches its associated database for relevant web pages, and returns results. An algorithm is a set of well-defined instructions (can be interpreted in only one way) in sequence to solve a problem. Qualities of a good algorithm :
- Input and output should be defined precisely.
- Each step in the algorithm should be clear and unambiguous.
- Algorithms should be most effective among many different ways to solve a problem (generalization).
- An algorithm shouldn't include computer code. • Instead, the algorithm should be written in such a way that it can be used in different programming languages. Example: Algorithm to add two numbers Step 1: Start Step 2: Declare variables num1, num2 and sum ask the operative system to allocate enough memory for 3 variables Step 3: Read values num1 and num2. input, specifing their value Step 4: Add num1 and num2 and assign the result to sum. sum←num1+num Step 5: Display sum Step 6: Stop There is a disadvantage in discussing an algorithm in this fashion: it is too verbose! This approach is fine for simple problems, but it might be more complex to understand if you overdetail the steps. In general is better using flowcharts , a graphical representations of steps. It was originated from computer science as a tool for representing algorithms and programming logic but had extended to use in all other kinds of processes. Flowcharts help us visualize complex processes, or make explicit the structure of problems and tasks. A flowchart can also be used to define a process or project to be implemented. Example: find the largest among three different numbers entered by the user.
The loops can be one of: For, While, Repeat…Until. Since we are essentially counting a set of items one by one, the way the for loop works ( semantics ) is the following:
The for loop is executed counter times : it must have an end! The way the while loop works (semantics) is the following: Here we are not counting how many time we are looping. Note: the initial condition – for any reason – may be false from the beginning: in this case, the while loop is not executed. The way the repeat…until loop works (semantics) is the following: The repeat loop is executed at least once Example: print “Hello world” ten times we want to count so that we use the for loop We can also put count = 1 but in this case we will write count <= 10. Data structures All the algorithms work on an imaginary unlimited memory (the memory becomes real only when you code an algorithm into a program). This memory is made of abstract data structures , ranging from variables (the easiest one) to lists and more (we will see some of them today and others during the course). We use the term abstract because when we come to the real memory , we can only deal with bits and bytes (0 and 1): variables, trees, lists, etc. are just a convenient way to regroup bytes! From the point of view of an algorithm (that is an abstract process or a rational reasoning), the variable is nothing else that a container located in an abstract memory. There is not concern about overflow, because we are in an ideal kind of world. In an algorithm, a variable is denoted by a name of our choice: choose a name that makes sense! Being an area of memory, a variable will include a value. The value will be also characterized by a type (numeric, letters, etc.).
State the problem Make yourself sure that problem is clearly stated: no ambiguities or multiple-interpretations are allowed. The problem may be stated either in a textual description or equivalently in a diagram. Example: Build an algorithm that says if a number N given in input is odd (dispari) or even (pari). Is the problem well-defined? Do we know when a number is odd and when instead is even? We can make few examples: 2 is even, 189 is odd, 24234 is even; 7 is odd. Understand the problem How do we say that a number is “even”? A number is even if is a multiple of 2: e.g. 2, 4, 6, 8, 10, 12 and so forth. Therefore, if a number is not a multiple of 2, it is odd: e.g. 1, 3, 5, 7, 9, 11 and so forth. State the solution by using an algorithm We know that a number is even when is a multiple of two. We may turn this definition is something more subtle and convenient: a number is even if it can be divided by 2. Examples: 18, 40, 54, 12, 2, 6 are even because we can divided them by two; 3, 9, 55, 13, 99 are odd because we cannot divide them by 2. But what does it mean to divide a number by two? This statement is ambiguous. In order to find a sensible answer, we can keep building more examples. Let consider a number, for example, 17. 17 divided by 2 gives 8 as integer quotient and 1 as remainder: 17 is a odd number! 8 divided by 2 gives an integer quotient of 4 and a remainder of 0: 8 is an even number. In conclusion, the rule we will apply to a generic number N is:
- Calculate the quotient Q = int(N/2);
- Calculate the remainder R = N – Q * 2;
- If R is equal to zero, then N is even;
- Otherwise, N is odd. Translate the algorithm into a pseudo-code Pseudo-code is an artificial and informal language that helps programmers develop algorithms. Pseudocode is a "text-based" detail (algorithmic) design tool. The rules of Pseudocode are reasonably straightforward. All statements showing "dependency" are to be indented. These include while, do, for, if, switch. Examples below will illustrate this notion. Examples: If student's grade is greater than or equal to 60 Print "passed" else Print "failed". Example: Set total to zero Set grade counter to one While grade counter is less than or equal to ten Input the next grade Add the grade into the total Set the class average to the total divided by ten Print the class average Convert the pseudo-code to a program Programming languages are the tools developers (artificial languages) use to write instructions for computers to execute. Typically, a programming language consists of statements written in English language (or a form of slang) that translate either a pseudocode or an algorithm. Let be PL a programming language: in order for a program p , written in PL , to be understood by a machine, an interpreter I converts each statement si of p into a corresponding binary string (a sequence of 0s and 1s) that the machine can understand and execute. I is a program as well. How many programming languages do exist? Many. Since a programming language has been typically designed to work in a specific domain (business, graphics, networking, web design, scientific research, biology, etc.), all a developer needs to do is to know the best tool for the job.
Learning a programming language (C, Java, Python, …) is a task that is similar, in many ways, in learning a new language (Portoguese, Chinese, French, …): in order to speak fluent English (for example), you must learn the grammar rules , a dictionary (name, verbs, …) and the semantics (i.e. the meaning of what you hear and say). Most important, you need to practice! The final frontier: Data Science and AI Data Science and Artificial Intelligence are emerging as the two leading research streams in Computer Science. The both share the characteristics to be strongly inter-disciplinary:
- Data Science involves Computer Science, Statistics, Operational Research, Mathematics, Machine Learning (a branch of AI), …
- AI involves cognitive science, logic, philosophy, computer science, linguists, mathematics ethics, … Data Science “The ability to take data — to be able to understand it, to process it, to extract value from it, to visualize it, to communicate it — that’s going to be a hugely important skill in the next decades .” Data scientists need to be curious and result-oriented , with industry-specific knowledge and communication skills that allow them to explain highly technical results to their non-technical counterparts. They possess a strong quantitative background in statistics and linear algebra as well as programming knowledge with focuses in data warehousing, mining, and modeling to build and analyze algorithms. What Data Science is used for?
- Recommender Systems. For example, big vendors such as Amazon or Netflix and social networks such as LinkedIn or Facebook can recommend items to buy or people to connect to because what you bought or the people you connected in the past;
- Fraud Detection. Some companies have to pay very high bill that may be a fraud. Data Science can detect unusual or abnormal patterns that can be originated by frauds (this is applicable also to small customers who pay odd, high bills in the utilities market: gas, electricity, telephone, …)
- Human Resources : Data Science can examine thousands of CVs in order to support the HR organization and hire the best candidate;
- Court of Law : • Data Science can predict if a convicted person will commit a crime in the future (recidivism); The last two problems involve strong ethical issues, since algorithms might discriminate a human being for the color of the skin. Artificial Intelligence Artificial intelligence (AI) is wide-ranging branch of computer science concerned with building smart machines capable of performing tasks that typically require human intelligence. AI is an interdisciplinary science with multiple approaches, but advancements in machine learning and deep learning are creating a paradigm shift in virtually every sector of the tech industry. The current applications are:
- Siri (Alexa, …): one of the most popular personal assistant offered by Apple in iPhone and iPad. The friendly female voice-activated assistant interacts with the user on a daily routine. It uses technology in order to get smarter and capable-to-understand natural language questions and requests.
- Tesla : a type of (expensive) car featuring self-driving, predictive capabilities, and absolute technological innovation;
- Flying Drones : the idea is to ship products to customers at home. They embed a powerful machine learning system that can translate the environment into a 3D model through sensors and video cameras. Trajectory generation algorithm guides the drone on how and where to move