15- Algorithms and Complexity 15- Time Complexity of Algorithms

Document Sample
15- Algorithms and Complexity 15- Time Complexity of Algorithms Powered By Docstoc
					Algorithms and Complexity


Time Complexity of Algorithms
• The Time Complexity of an Algorithm is a measure of how the amount of “time” required to execute an algorithm grows as a function (in relation to) the problem size.
execution time execution time problem size

problem size

Time complexity Classes Exponential
• Exponential Time Complexity:
– The algorithm takes an amount of time to execute that grows much, much, faster than the size of the problem. • e.g., when the problem size is 10 times the original size, the time taken is 1000 times more! – This class of algorithms is impractical for large problem sizes • Examples include 2^n, 10^n, n^n, etc.

execution time

problem size


Time complexity Classes Exponential
• Just how fast does an exponential function grow?
– How much would you save in one month if every day you deposit into your savings account an amount equal to what is already there on that day, starting with 1 cent on day one?

1 + 1 + 2 + 4 + 8 + 16 + 32 + 64 + … cents


Time complexity Classes Exponential
• Just how fast does an exponential function grow?
– How much would you save in one month if every day you deposit into your savings account an amount equal to what is already there on that day, starting with 1 cent on day one?

This is called an exponential explosion


Time complexity Classes Exponential
• Algorithms with exponential time complexity often require the search for a solution in an “exponentially large” solution space.
– Travelling Salesman Problem – Chess and other similar strategy games

– For such problems we look for a “good enough” approximation to the solution. Typically, this works, but . . . • For some problems finding a good approximation is itself impractical (e.g., may take an exponential amount of time!)

Operating Systems


“Improving” the computer
• Consider our view of the computer at this point:
– Our resources (memory, CPU, I/O) are controlled by programs (really just one program) • Our computer is programmable, but how are we getting these programs into our computer? How will they run? Run one, reboot, repeat? • we don't even know how a single program is loaded and run... – what about more than one program? – the Von Neumann bottleneck only lets us run “one program” at a time • there would be a lot of issues in running multiple programs simultaneously

• sharing devices, if possible, would be dangerous!
– communicating with all the peripheral devices will require a lot of program code for each device – that’s a lot of devices to write program code for – And we haven’t talked about how humans will use the computer.

• Enter the Operating System


What is an operating system?
• What does an operating system do?
– Control how programs are run on the computer – Provide an abstraction of the specific details of the hardware of the computer – That abstraction provides more than just the hardware itself • Provide a mechanism for hardware components to be shared among different programs – Including the CPU! – Provide the user an interface to the programs and files on the computer

• The operating system (OS):
– Collection of programs that form a “virtual hardware” – The OS is the foundation for other programs • All programs run on-top of the operating system rather than the real hardware • The OS is (essentially) the first program to run when you turn on the computer


What is an operating system?
• What does an operating system do?

some program run by the OS on the real hardware on your behalf

Operating System programs that provide an interface to the software hardware Real Physical Hardware hardware CPU/Memory/Input/Output/Storage/Periphera ls
• Programs (even the OS) run from memory • Remember that RAM is empty when we first turn on our computer – How does the OS get into memory to run in our computer?


What really runs first?
• Remember ROM?
– Small non-volatile “memory” that will be there when we turn on the computer – We place a small “hand-off” program into ROM and it will be run first when we turn on the computer • BIOS (Basic Input Output System) – Small “unchangeable” software stored in ROM – BIOS software is embedded (built into) in the motherboard » BIOS can be changed: » Flash update – it’s just rarely done – BIOS: • A collection of programs that have the capability of communicating with peripheral devices (hardware) – Keyboards, Disk drives, printers, display/monitors, and other devices. • BIOS is what let's us run our operating system

Booting the Computer
• Booting (Starting) the computer:
– The computer invokes the BIOS initialization program found in ROM • Makes the computer recognize the keyboard, floppy and the hard disk drives. – 1. Diagnostics are run on the hardware devices in the computer – 2. The program in ROM instructs the computer to look for an operating system • depending on your settings, the computer will look in different places at different orders – usually: – 1st try the Floppy disk – Then try a CD-ROM – Then a hard disk (sometimes there are multiple hard disks) – 3. The operating system (wherever found) must be loaded into RAM so it can 15-12 run

Booting the Computer
• Cold boot:
– Starting up the computer by turning the power on (power was off previously). – BIOS is loaded into RAM and BIOS looks for and loads the operating system into RAM.

• Warm boot:
– Kick back to BIOS and reload the operating system into RAM without disrupting the power to the disk drives or power supply. – (aka reboot/restart) – both reload the operating system

– the operating system is a collection of programs . . .


The composition of the OS
• The Operating System is a collection of programs that provide essential services for the computer
– The most important of these programs is known as the kernel – The kernel is the one program that controls (can invoke) all other programs in the Operating System • The kernel is what makes the services of the Operating System available. • when control of the computer is passed from BIOS to the Operating System, it’s really passed to the kernel program, which starts running. • Yet, the kernel is invisible to the user.

What you see of the OS
• User Interface (UI): a program that acts as an interface between the Operating System and the user
– (Provided by the Operating System) – The part of the operating system that the user sees and communicates with. • How does the user communicate with the User Interface program? – Input/Output devices – Two basic user interfaces presented to the user: • Command line – Commands are typed in using the keyboard instructing what program to run. Results are plain text (ASCII) to the display. – Example: >copy c:paper.txt a:*.* – (think text) • Graphical User Interface (GUI) - Graphical representation of commands (icons) are selected using an input device, usually a mouse. – Example: you double click on the Microsoft Word icon 15-15 – (think graphics)

Graphical User Interfaces
• Xerox PARC (Palo Alto Research Center)
– 1981 – Xerox Star Computer features a graphical interface – desktop analogy for computing – WIMP – windows, icons, menus, and pointing devices • In this GUI the cursor of the pointing device (mouse) appears on the screen and can be used to point to icons, work within windows, and select from menus.

• As early as 1984 the Macintosh computer had adopted this interface • 1983 Microsoft announces Windows 1.0 • 1995 Windows 95 released – finally “leaving MSDOS” architecture

Running Programs
• Either kind of User Interface will allow you to run or execute programs on your computer
– As we know, a program must be in memory to run • We often refer to a “running” program as a process – In our discussion we imagine the kernel is running and so is the User Interface? Remember... – von Neumann Bottleneck: Microcomputers having only one program counter can run only one program at a time. • The CPU can only process a single instruction at any one time, no matter how many tasks there are to do – Why? » Because we only have a single program counter

– Well, the “interface” to the CPU provided by the OS can switch CPU resources 15-17 between programs

Running Programs
• Context Switching:
– the CPU can be switched from running one program to another • This does not allow two programs to be running at the same time – It allows you to switch the CPU between two programs • But since our CPU only has one program counter, we need to keep track of the CPU’s instruction decoding state for both programs – current instruction – next instruction • So we can switch the context of the CPU from one program to the next • Context switching “swaps” the program running in the CPU with a different program 15-18

• Multitasking:
– Using context switching we can allow several applications “appear” to be running at the same time – (We just context switch very frequently)

• Scheduling
– A software program in the OS called the scheduler decides which program will be run at what time and for how long • Programs are run for fractions of a second before swapped • The scheduler should be smart about how it decides which programs to run and which to wait… – How can we be smart about this? » Hint: Different programs might need different resources…

• Pre-emptive scheduling allows certain programs that are waiting to be run to take precedence over other programs


Running Programs
• Real-time processing:
– Do this NOW!

– A program has a deadline • Requires quick or timely return of results.
• If it’s not done in time, it’s a failure

– Results are urgent, so should get highest priority. – Referred to in the engineering and manufacturing world as Controlling processes. • ATM: Customer wants cash now, not tomorrow! Bank wants to know how much money you have (even if after hours) before you are able to make a withdrawal from your account. • Airline ticket reservation system: Travel agent needs to know how many seats are still available. (Immediately)

• Running programs need to be in memory (RAM)