CS 1110 Notes

45
Computer Science Overview Types of Programs Program: a sequence of instructions that specifies how to perform a computation Algorithm: a general process for solving a category of problems Declarative Programming: a programming paradigm where the program describes what should be accomplished without describing the control-flow of how to actually accomplish it, in stark contrast with imperative programming Imperative Programming: a programming paradigm that describes computation in terms of statements that change a program state Procedural Programming: imperative programming sub-paradigm where the program uses functions or subrou- tines Structured Programming: a programming paradigm where the program uses lots of subroutines, loops, condi- tionals, or other methods of imposing structure on the program, as opposed to a giant list of statements or the use of the notorious GOTO statement Object-Oriented Programming (OOP): a programming paradigm where the program consists of a set of objects, each with a set of data fields and methods, interacting with each other Functional Programming: a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data; a subtype of declarative programming Program Use Portability: a programming language whose programs can be run on different kinds of computers with little or no modification Interpreter: a program that Compiler: a program that Source Code: a high-level program Object Code (Executable): the translated source code that the computer can execute Prompt: a set of symbols such as >>> to indicate that the interpreter is ready for user input Script: a file containing code for an interpreter to execute Interactive Mode: Script Mode: 1

description

aoeu

Transcript of CS 1110 Notes

Page 1: CS 1110 Notes

Computer Science Overview

Types of ProgramsProgram: a sequence of instructions that specifies how to perform a computation

Algorithm: a general process for solving a category of problems

Declarative Programming: a programming paradigm where the program describes what should be accomplishedwithout describing the control-flow of how to actually accomplish it, in stark contrast with imperative programming

Imperative Programming: a programming paradigm that describes computation in terms of statements thatchange a program state

Procedural Programming: imperative programming sub-paradigm where the program uses functions or subrou-tines

Structured Programming: a programming paradigm where the program uses lots of subroutines, loops, condi-tionals, or other methods of imposing structure on the program, as opposed to a giant list of statements or the useof the notorious GOTO statement

Object-Oriented Programming (OOP): a programming paradigm where the program consists of a set of objects,each with a set of data fields and methods, interacting with each other

Functional Programming: a programming paradigm that treats computation as the evaluation of mathematicalfunctions and avoids state and mutable data; a subtype of declarative programming

Program UsePortability: a programming language whose programs can be run on different kinds of computers with little or nomodification

Interpreter: a program that

Compiler: a program that

Source Code: a high-level program

Object Code (Executable): the translated source code that the computer can execute

Prompt: a set of symbols such as >>> to indicate that the interpreter is ready for user input

Script: a file containing code for an interpreter to execute

Interactive Mode:

Script Mode:

1

Page 2: CS 1110 Notes

Program StructureBug: an error in a program

Debugging: the process of fixing bugs in programs

Syntax: the structure of a program and the rules about that structure

Syntax Error:

Runtime Error (Exception): an error that doesn’t appear until a program has started running

Semantics:

Semantic Error: an error in what the program means; the program runs, but doesn’t do what you want it to

Natural Language: languages that people speak

Formal Language: languages that are designed for specific applications

ä natural languages are not explicitly developed by people but rather evolve naturally and have some structureimposed along the way

ä programming languages are formal languages that have been designed to express computations

Token:

Parse:

Print Statement:

Basic Program Components

ä input: get data from the keyboard, a file, or some other device

ä output: display data on the screen or send data to a file or other device

2

Page 3: CS 1110 Notes

ä math: perform basic mathematical operations like addition and multiplication

ä conditional execution: check for certain conditions and execute the appropriate code

ä repetition: perform some action repeatedly, usually with some variation

Variables and Types

Variables and TypesStatic Type-Checking: the process of verifying the type safety of a program based on analysis of a program’ssource code

Dynamic Type-Checking: the process of verifying the type safety of a program at runtime

Dynamically Typed: programming languages which include dynamic type-checking but not static type-checking

Strongly Typed: although not precisely defined, it typically means that typing errors are prevented at runtime andthe programming language does little implicit type conversion

Duck Typing: a style of dynamic typing in which an object’s methods and properties determine the valid semantics,rather than its inheritance from a particular class or implementation of a specific interface; a programming languagethat is both strongly typed and dynamically typed

Value: a basic unit of data

Type: a category of values

Variable: a name that refers to a value

State Diagram: a diagram showing the value each variable contains

Encode: to represent one type of value using another type of value by constructing a mapping between them

Token: a set of characters that are treated as a unit for purposes of parsing, such as the words in a naturallanguage

Mutable: an object that can be directly written to and changed

Immutable: an object that can NOT be directly written to and changed

ä immutable objects can be read and copied with changes to a new variable to effectively work very similarlyto mutable objects

ä the Python symbol for a comment is #

ä the Python line continuation character is \

• lines with the line continuation character can NOT contain comments

ä you can indent and break lines however you want between any sort of delimiter

• Python distinguishes between tabs and spaces

∗ you want to always use four spaces, never tabs

ä the website for Python documentation is: http://docs.python.org/3.2/index.html

3

Page 4: CS 1110 Notes

Python Built-In Typesbytearray int str

bytes float list

bool long tuple

set complex dict

frozenset

OperatorsOperator: a special symbol that represent a computation like addition or multiplication

Operand: the values the operator is applied to

Expression: a combination of values, variables, and operators; only one thing is necessary to be considered anexpression

Subexpression: an expression in parentheses that acts as a single operand in a larger expression

Statement: a unit of code that the Python interpreter can execute

Binary Operator: an operator that takes two operands

Prefix: writing a mathematical expression with the operators before the operands

Infix: writing a mathematical expression with the operators between the operands

Postfix: writing a mathematical expression with the operators after the operands

Floor Division: when both of the operands are integers, the result is also an integer; floor division truncates thefraction part

ä Python performs floor division

ä Python allows multiple assignment in a very intuitive way e.g. : x = y = 5

• this can also be done with boolean expressions

Arithmetic Operators Assignment Operators

= simple assignment

+ addition += add the RHS to the variable

- subtraction -= subtract the RHS from the variable

* multiplication *= multiple the variable by the RHS

/ division /= divide the variable by the RHS

% modulus %= assign the modulus of the variable and RHS to the variable

** exponentiation **= raise the variable to the power of the RHS

// floor division //= floor divide the variable by the RHS

4

Page 5: CS 1110 Notes

Logical Operators

and logical AND

or logical OR

not logical NOT

Membership Operators

in subset

not in not a subset

Identity Operators

is are the same object

is not are not the same object

Operator Precedence

** exponentiation

~, +, - complement, unary plus and minus

*, /, %, // multiple, divide, modulo, floor division

+, - addition and subtraction

>>, << right and left bitwise shift

& bitwise AND

^, | bitwise exclusive OR and regular OR

<=, <, >, >= comparison operators

==, != equality operators

=, %=, /=, //=, -=, +=, *=, **= assignment operators

is, is not identity operators

in, not in membership operators

not, or, and logical operators

Python Keywordsä in Python 3, exec is no longer a keyword

ä in Python 3, nonlocal is a new keyword

ä Python does NOT have a GOTO statement

ä Python does NOT have a switch statement

• this is in order to encourage making a polymorphic call instead

• anything a switch statement can do can be done with if -- elif -- elif -- else

5

Page 6: CS 1110 Notes

Python Keywordsand del from not while

as elif global or with

assert else if pass yield

break except import print

class exec in raise

continue finally is return

def for lambda try

Python Built-in Functions

Python 2.7.5 Built-in Functionsabs() divmod() input() open() staticmethod()

all() enumerate() int() ord() str()

any() eval() isinstance() pow() sum()

basestring() execfile() issubclass() print() super()

bin() file() iter() property() tuple()

bool() filter() len() range() type()

bytearray() float() list() raw_input() unichr()

callable() format() locals() reduce() unicode()

chr() frozenset() long() reload() vars()

classmethod() getattr() map() repr() xrange()

cmp() globals() max() reversed() zip()

compile() hasattr() memoryview() round() __import__()

complex() hash() min() set() apply()

delattr() help() next() setattr() buffer()

dict() hex() object() slice() coerce()

dir() id() oct() sorted() intern()

6

Page 7: CS 1110 Notes

Python 3.2.5 Built-in Functionsabs() dict() help() min() setattr()

all() dir() hex() next() slice()

any() divmod() id() object() sorted()

ascii() enumerate() input() oct() staticmethod()

bin() eval() int() open() str()

bool() exec() isinstance() ord() sum()

bytearray() filter() issubclass() pow() super()

bytes() float() iter() print() tuple()

callable() format() len() property() type()

chr() frozenset() list() range() vars()

classmethod() getattr() locals() repr() zip()

compile() globals() map() reversed() __import__()

complex() hasattr() max() round()

delattr() hash() memoryview() set()

Data Structures

StringsString: an immutable sequence of characters

ä Python treats single quotes ” exactly the same as double quotes “”

ä normal strings in Python are stored internally as 8-bit ASCII

ä indexes always start at zero

ä a negative sign makes indexes start from the right instead of the left

• when starting from the right, the indexes start at one instead of zero

ä for slices, the range selected starts at the first index and ends with the element before the second index

Create A String

<string name > = ’<string characters >’

Create A String (Alternative)

<string name > = "<string characters >"

Create A Raw String

7

Page 8: CS 1110 Notes

<string name > = r’<string characters >’

Create A Unicode String

<string name > = u’<string characters >’

Access An Element of a String

<string name >[<number of character index >]

Access A Set of Elements of a String

<string name >[<number of first character index >:<number of last index >]

String Formatting Symbols%c character

%s string conversion via str() prior to formatting

%i signed decimal integer

%d signed decimal integer

%u unsigned decimal integer

%o octal integer

%x hexadecimal integer (lowercase letters)

%X hexadecimal integer (uppercase letters)

%e exponential notation (with lowercase e)

%E exponential notation (with uppercase E)

%f floating point real number

%g the shorter of %f and %e

%G the shorter of %f and %E

8

Page 9: CS 1110 Notes

String Functionslen() returns the number of characters in the string

count(str) returns the number of times str occurs in string or the denoted substring

find()

index()

startswith()

endswith()

max() returns the maximum alphabetical character from the string

min() returns the minimum alphabetical character from the string

isalpha() returns True if the string has at least one character and all characters are alphabetic andFalse otherwise

isdigit() returns True if the string contains only digits and False otherwise

islower()

isupper()

capitalize() capitalizes the first letter of the string

title() returns a titlecased version of the string – all words begin with uppercase and the rest arelowercase

lower() converts all uppercase letters in the string to lowercase

upper() converts all lowercase letters in the string to uppercase

swapcase() inverts the case of all letters in the string

replace(” , ”) replaces all occurrences of the old substring with the new substring

split() splits the string according to the delimiter and returns a list of substrings

splitlines() splits the string at all newlines and returns a list of each line with the newline charactersremoved

TuplesTuple: an immutable sequence of multiple types of objects

ä the objects that a tuple contains must remain the same, but the contents of any given object can potentiallychange

• a simple string or number in a tuple can NOT be modified

• a string or a number inside a list inside a tuple can be freely modified

∗ the list itself must still exist in its original location in the tuple, even if what’s in the list changes

ä indexes always start at zero

ä a negative sign makes indexes start from the right instead of the left

9

Page 10: CS 1110 Notes

• when starting from the right, the indexes start at one instead of zero

ä for slices, the range selected starts at the first index and ends with the element before the second index

Create A Tuple

<tuple name > = (<tuple elements >)

Access An Element of a Tuple

<tuple name >[<number of element index >]

Access A Set of Elements of a Tuple

<tuple name >[<number of first element index >:<number of last index >]

Tuple Functionstuple() converts a list to a tuple

len() returns the number of elements in the tuple

count()

index()

max() returns the element from the tuple with the maximum value

min() returns the element from the tuple with the minimum value

ListsList: a mutable sequence of multiple types of objects

ä indexes always start at zero

ä a negative sign makes indexes start from the right instead of the left

• when starting from the right, the indexes start at one instead of zero

ä for slices, the range selected starts at the first index and ends with the element before the second index

Create A List

<list name > = [<list elements >]

Access An Element of a List

10

Page 11: CS 1110 Notes

<list name >[<number of element index >]

Access A Set of Elements of a List

<list name >[<number of first element index >:<number of last index >]

Access An Element of a List Inside Another List

<list name >[<number of sublist index >][<number of element index >]

List Functionslist() converts a tuple to a list

len() returns the number of elements in the list

count() returns the number of times the passed argument appears in the list

index() returns the index number of the passed argument

max() returns the element from the list with the maximum value

min() returns the element from the list with the minimum value

append() adds the passed argument to the end of the list

extend()

clear()

reverse() reverses the order of the elements of the list

remove(obj) removes the object obj from the list

pop() removes and returns the last element from the list

sort()

insert()

map()

DictionariesDictionary (Associative Array) (Hash Table) (Struct):

ä dictionary keys do NOT have to be strings

• IF a dictionary key is a string, it needs to be placed in quotes

• dictionary keys MUST be immutable

Create A Dictionary

<dictionary name > = <key name >: <value > , <key name >: <value > ,<key name >: <value >

11

Page 12: CS 1110 Notes

Access a Dictionary Key Value

<dictionary name >[<key name >]

Dictionary Functionscmp(dict1 , dict2)

len(dict) returns the length of the dictionary

str(dict)

dict.clear()

dict.copy() returns a shallow copy of the dictionary

dict.fromkeys()

dict.get(key , default=None)

dict.has_key(key)

dict.items() returns a list of the dictionary’s (key , value) tuple pairs

dict.keys() returns a list of the dictionary’s keys

dict.setdefault(key , default=None)

dict.update(dict2) adds dictionary dict2 key-value pairs to the dictionary

dict.values() returns a list of the dictionary’s values

ConditionalsIF Statement

if <boolean expression >:<statements >

IF-ELSE Statement

if <boolean expression >:<statements >

else:<statements >

IF-ELSEIF Statement

if <boolean expression >:<statements >

elif <boolean expression >:<statements >

elif <boolean expression >:<statements >

elif <boolean expression >:<statements >

12

Page 13: CS 1110 Notes

IF-ELSEIF-ELSE Statement

if <boolean expression >:<statements >

elif <boolean expression >:<statements >

elif <boolean expression >:<statements >

elif <boolean expression >:<statements >

else:<statements >

Loops

LoopsAccumulator: a variable used in a loop to accumulate a series of values, such as by concatenating them onto astring or adding them to a running sum

ä anything that can be done by a for loop can be done by a while loop

• NOT everything that can be done by a while loop can be done by a for loop

ä anything that can be done by a while loop can be done by recursion

ä anything that can be done by recursion can be done by a while loop

• loops tend to be easier and a better choice for simpler tasks

• recursion tends to be easier and a better choice for when the amount of repeating is less certain dueto complex algorithms and data structures

• iteration tends to be slightly faster than recursion, but usually not enough to bother considering

∗ when compiled, some types of recursion will be converted to iteration∗ recursion resides on the call stack, while iteration does not∗ iteration is less likely to create a stack overflow and various problems with handling large numbers

ä index, outerIndex, innerIndex, counter, and increment are useful variable names for use with loops

ä the pass statement is a null operation – it does nothing

• the pass statement is useful as a temporary syntactic placeholder before you get around to puttingsomething there

• the pass statement can be used anywhere, not just in loops

ä the break statement immediately exits a loop entirely

• this not only exits the current iteration through loop, but the loop in its entirety

ä the continue statement immediately exits a loop for the given iteration

• this only exits the current iteration through the loop, but the rest of the iterations will continue on normally

13

Page 14: CS 1110 Notes

For Loop

for <iterating variable > in <sequence of values >:<repetend >

For-Else Loop

for <iterating variable > in <sequence of values >:<repetend >

else:<statements >

While Loop

while <boolean expression >:<repetend >

While-Else Loop

while <boolean expression >:<repetend >

else:<statements >

Testing

ExceptionsCatching: handling an exception with a try statement

Throwing (Raising): handling an exception with an except statement

ä the finally block is always executed whether or not an exception is thrown, and is often used to close filesand do similar tidying up

Exceptions Block

try:<statements >

except <optional boolean expression >:<statements >

except <optional boolean expression >:<statements >

except <optional boolean expression >:<statements >

finally:<statements >

else:<statements >

14

Page 15: CS 1110 Notes

Custom Exceptions

class <>(Exception):def __init__(self , value):

self.value = valuedef __str__(self):

return repr(self.value)

Functions

FunctionsFunctional Programming: a style of programming where the majority of functions are pure

Pure Function: a function that does not modify any of the objects it receives as arguments; most pure functionsare fruitful

Fruitful Function: a function that returns a value

Void Function (Procedure): a function that does NOT return a value

Modifier: a function that changes one or more of the objects it receives as arguments; most modifiers are fruitless

First-Class Citizen: an entity that can be constructed at run-time, passed as a parameter, returned from a sub-routine, and assigned into a variable or data structure

Function Call: a statement that executes (calls) a function

Wrapper: a method that acts as a middleman between a caller and a helper method, often making the methodeasier or less error-prone to invoke

Helper: a method that is not invoked directly by a caller but is used by another method to perform part of anoperation

Encapsulation: wrapping a piece of code up in a function

Generalization: adding a parameter to a function

Docstring (Documentation String): a string at the beginning of a function that explains the interface

Invariant: a condition that should always be true during the execution of a program

Precondition: things that are supposed to be true before a function starts executing

Postcondition: things that a function is supposed to do and any side effects of the function

Namespace: a syntactic container providing a context for names so that the same name can reside in differentnamespaces without ambiguity

Naming Collision: when two or more names in a given namespace cannot be unambiguously resolved

Scope: the part of a program that can access a particular variable

Global Variable: a variable that is declared in the current module and can be accessed anywhere in the program

Local Variable: a variable that is declared inside a function and only exists inside the function, including parameters

Argument (Actual Parameter): a value provided to a function when the function is called, which can be assignedto the corresponding parameter in the function

Parameter (Formal Parameter): a name used inside a function to refer to the value which was passed to it as anargument

ä variables defined outside a function body have a global scope

15

Page 16: CS 1110 Notes

ä variables defined inside a function body have a local scope

ä a function without any arguments to return will return the value None

Create a Function

def <function name >(<function arguments >):’’’<docstring>’’’<function body >return <optional return expression >

Call a Function

<function name >(<function arguments >)

Functional Programming Conceptsä everything is a function

ä functions are first-class citizens

• functions can be passed other functions as arguments

• functions can return other functions as values

ä always use pure functions

ä make as many things immutable as possible

Functional Programming Pros and Consä greater abstraction and fewer lines of code than OOP

ä excellent for programming mathematical functions

ä easier to prove correctness of programs

ä much better and easier concurrency and multithreading

ä extremely difficult to do any I/O related tasks

ä completely different from OOP and procedural programming, so the learning curve can be steep

Proof By Induction1. prove the base case

2. prove the induction step

ä does proof for n hold for n+1?

16

Page 17: CS 1110 Notes

ä ASSUME that the induction hypothesis is true (the general n case)

ä both steps must hold for the proof to be valid

ä a common error is making an irrelevant base case or not having enough base cases

Optimizing Recursive AlgorithmsMemoization: an optimization technique used to speed up computer programs by having function calls avoidrepeating the calculation of results for previously processed inputs by caching results as they’re calculated

ä it may be necessary to cache not just the recursively calculated value, but also other parameters necessaryto calculate it

Tail Recursion: when the last action performed by a recursive function is a recursive call

ä for tail recursion to occur, it isn’t enough for a recursive call to just be in the last expression, it has to be thelast action performed

• the simple approach to calculating the Fibonacci series recursively is NOT tail recursive because thelast action performed is addition, not recursion f_n = fib(f_n-1) + fib(f_n-2)

ä tail recursion can easily be converted to a while loop to save memory overhead and prevent a stack overflow

Basic Recursion

def recursor(value):# handle base casesif <base case 1>:

return <base case 1 defined value >if <base case n>:

return <base case n defined value ># general recursive caseif <not at a base case >:

<statements >recursor(value)

return value

Memoization

def recursor(value):# check if value is in cacheif value in cache:

return cache[value]# handle base casesif <base case 1>:

return <base case 1 defined value >elif <base case n>:

return <base case n defined value >

17

Page 18: CS 1110 Notes

# general recursive caseelse <not at a base case >:

<statements >recursor(value)

# add value to cache if it isn’t already thereif value not in cache:

cache.append(value)return value

Classes and Objects

Object-Oriented Programming ConceptsEncapsulation:

Package: a collection of modules

Module:

Class: a user-defined type

Object (Class Object): an object that contains information about a user-defined type

Instance: an object that belongs to a class

Attribute: a variable defined inside a class definition but outside any method; one of the named values associatedwith an object

Method: a function that is defined inside a class definition and is invoked on instances of that class

Subject: the object that a method is invoked on

Object Diagram: a diagram that shows objects, their attributes, and the values of the attributes

Embedded (Embedded Object): an object that is stored as an attribute of another object

Shallow Copy: to copy the contents of an object, including any references to embedded objects; implemented bythe copy function in the copy module

Deep Copy: to copy the contents of an object as well as any embedded objects, and any objects embedded inthem, and so on; implemented by the deepcopy function in the copy module

Operator Overloading: changing the behavior of an operator so that it works with a user-defined type

Type-Based Dispatch: a programming pattern that checks the type of an operand and invokes different functionsfor different types

Polymorphic: pertaining to a function that can work with more than one type

Information Hiding: the principle that the interface provided by an object should not depend on its implementation,in particular the representation of its attributes

Encode: to represent on set of values using another set of values by constructing a mapping between them

Class Attribute: an attribute associated with a class object. Class attributes are defined inside a class definitionbut outside any method

Instance Attribute: an attribute associated with an instance of a class

Veneer: a method or function that provides a different interface to another function without doing much computation

Inheritance: the ability to define a new class that is a modified version of a previously defined class

Parent Class (Super Class): the class from which a child class inherits

Child Class (Sub Class): a new class created by inheriting from an existing class

18

Page 19: CS 1110 Notes

IS-A Relationship: the relationship between a child class and its parent class; are all X necessarily a Y?

HAS-A Relationship: the relationship between two classes where instances of one class contain references toinstances of the other

Multiplicity: a notation in a class diagram that shows, for a HAS-A relationship, how many references there are toinstances of another class

ä Python uses breadth first attribute search starting from the left

ä in Python, everything is public

• this is based on the philosophy of protections? to protect what from who?

ä attributes belonging to a given object should be passed in their normal form, e.g. int, string, etc.

• class attributes are accessible from any method in the class and are shared by all instances of the class

ä attributes a given object needs that belong to a different object should be given by passing the name of theobject owning the attribute and then using a getter method for the needed attribute

ä you should plan everything out as being separate objects and then afterwards see how inheritance can helpsimplify things

• don’t try to go from parent classes to child classes, instead, go from child classes to parent classes

Python OOP Syntax

Class Permissions<object name or self>.<fieldname> public accessible from both inside and outside

<object name or self>._<fieldname> protected like a public member, but shouldn’t be directlyaccessed from outside

<object name or self>.__<fieldname> private can’t be seen or accessed from outside

Overloading Relational Operators For Objectsdef __eq__(self , other): define equal to == relation for objects

def __nq__(self , other): define not equal to != relation for objects

def __ge__(self , other): define greater than or equal to >= relation for objects

def __gt__(self , other): define greater than > relation for objects

def __le__(self , other): define less than or equal to <= relation for objects

def __lt__(self , other): define less than to < relation for objects

19

Page 20: CS 1110 Notes

Define a Class

class <class name >(<optional parent1 class name > , <optional parent2 class name >):’’’<doc string>’’’<attribute definitions ># Python equivalent of a constructordef __init__(self , <arguments >):

self.<arguments > = <argument value ># Python equivalent of a toStringdef __str__(self , , <arguments >):

# getter methodsdef <method name >(self , <method arguments >):

<method statements ># setter methodsdef <method name >(self , <method arguments >):

<method statements ># other methodsdef <method name >(self , <method arguments >):

<method statements >

Create an Object Instance

<object name > = <class name >(<arguments >)

Call a Method of the Object’s Class

<object name >.<method name >(<arguments >)

Call a Method of the Object’s Parent Class

super.<method name >(<arguments >)

Directly Access an Attribute

<object name >.<attribute name >

Directly Access A Private Attribute

<object name >.<class name >.__<attribute name >

Import a Module

import <module name >

Create an Object Instance From a Module

import <module name >.<class name >(<arguments >)

20

Page 21: CS 1110 Notes

Abstract Data Types

Abstract Data TypesGeneric Data Structure: a kind of data structure that can contain data of any type

Abstract Data Type (ADT): a data type (usually a collection of objects) that is defined by a set of operations butthat can be implemented in a variety of ways

ä abstract data types simplify the task of specifying an algorithm if you can denote the operations you needwithout having to think at the same time about how the operations are performed

ä abstract data types provide a common high-level language for specifying and talking about algorithms

Interface: the set of operations that define an abstract data type

Implementation: code that satisfies the syntactic and semantic requirements of an interface

Client: a program (or the person who wrote the program) that uses an abstract data type

Provider: the code (or the person who wrote the program) that implements an abstract data type

Veneer: a class definition that implements an abstract data type with method definitions that are invocations ofother methods, sometimes with simple transformations

ä the veneer does no significant work, but it improves or standardizes the interface seen by the client

Linked ListsLinked List: a data structure that implements a collection using a sequence of linked nodes

Embedded Reference: a reference stored in an attribute of an object

Nodes: an element of a list, usually implemented as an object that contains a reference to another object of thesame type

Cargo: an item of data contained in a node

Link: an embedded reference used to link one object to another

Recursive Data Structure: a data structure with a recursive definition

Collection: multiple objects assembled into a single entity

The Fundamental Ambiguity Theorem: a variable that refers to a list node can treat the node as a single objector as the first in a list of nodes

Node Implementation

class Node:’’’an implementation of the node ADT’’’def __init__(self , cargo=None , next=None):

self.cargo = cargoself.next = next

def __str__(self):return str(self.cargo)

21

Page 22: CS 1110 Notes

def printBackward(self):if self.next is not None

tail = self.nexttail.printBackward()

print(self.cargo , end=’ ’)

Linked List Implementation

class LinkedList:’’’an implementation of the linked list ADT’’’def __init__(self):

self.length = 0self.head = None

def add_first(self , cargo):node = Node(cargo)node.next = self.headself.head = nodeself.length = self.length + 1

def printBackward(self):print(’[’ , end=’ ’)if self.head is not None:

self.head.printBackward()

StacksStack: a list that operates according to LIFO – Last In First Out

Stack Implementation

class Stack:’’’an implementation of the stack ADT’’’def __init__(self):

self.items = []def __str__(self):

return self.items

def is_empty(self):return (self.items == [])

def push(self , item):self.items.append(item)

def pop(self):return self.items.pop()

QueuesQueue: a list that operates according to FIFO – First In First Out

22

Page 23: CS 1110 Notes

Queueing Policy: the rules that determine which member of a queue is removed next

Linked Queue: a queue implemented using a linked list

ä by maintaining a reference to both the first and the last node, operations can be performed in constant timeinstead of linear time

Queue Implementation

class Queue:’’’an implementation of the queue ADT’’’def __init__(self):

self.length = 0self.head = Noneself.last = None

def is_empty(self):return self.length == 0

def insert(self , item):node = Node(cargo)if self.length == 0:

# if the list is empty, the new node is head and lastself.head = nodeself.last = node

else:# find the last nodelast = self.last# append the new nodelast.next = nodeself.last = node

self.lenth = self.length + 1def remove(self):

cargo = self.head.cargoself.head = self.head.nextself.length = self.length - 1if self.length == 0:

self.last = Nonereturn cargo

Priority QueuesPriority Queue: a queue where each member has a priority determined by external factors and the member withthe highest priority is the first to be removed

Priority Queue Implementation

class Queue:’’’an implementation of the priority queue ADT’’’def __init__(self):

23

Page 24: CS 1110 Notes

self.items = []

def is_empty(self):return self.items == []

def insert(self , item):self.items.append(item)

def remove(self):maximum = 0for index in range(1 , len(self.items)):

if self.items[index] > self.items[maximum]:maximum = index

item = self.items[maximum]del self.items[maximum]return item

def __gt__(self , other):’’’only necessary for queues of objects, not numbers or strings’’’return <boolean expression comparing the priority of self and other >

TreesTree: a set of nodes connected by edges that indicate the relationships among the nodes

ä trees are for hierarchical data

ä the path between a tree’s root and any other node is unique

Full Tree:

Complete Tree:

Subtree: a node and its descendants from a original tree

General Tree: a tree where each node can have an arbitrary number of children

n-ary Tree: a tree where each node has no more than n children

Binary Tree: a tree where each node has at most two children

Expression Tree:

Search Tree:

Decision Tree:

Expert System:

Game Tree: a general decision tree that represents the possible moves in any situation in a game

Parse Tree:

Grammar:

ä n = 2h−1(

number of nodes in a full binary tree)

ä h = log2 (n+1)(

height of a full binary tree with n nodes)

2-3 Tree: a general tree whose interior nodes must have either two or three children and whose leaves occur onthe same level

2-4 Tree (2-3-4 Tree): a general tree whose interior nodes must have two, three, or four children and whose leavesoccur on the same level

24

Page 25: CS 1110 Notes

ä a 2-3 tree is completely balanced

ä a 2-4 tree is completely balanced

B-Tree:

Red-Black Tree: a binary tree that is equivalent to a 2-4 tree

ä the root is black

ä every red node has a black parent

ä any children of a red node are black

ä every path from the root to a leaf contains the same number of black nodes

Root: a node in a tree with no parent; the top-most node in a tree

Leaf: a node in a tree with no children; a bottom-most node in a tree

Parent: the node that refers to a given node

Child: one of the nodes referred to by a node

Siblings: nodes with the same parent node

Ancestor: any node above a given node

Descendant: any node below a given node

Up: towards the root

Down: towards the leaves

Depth:

Height: the number of levels in a tree; equivalently, the number of nodes along the longest path between the rootand a leaf

Level: the set of nodes the same depth from the root

Path: a set of edges and nodes connecting a starting node and an ending node

Preorder Traversal (Depth-First Traversal): traversing a tree by visiting each node before its children

Inorder Traversal: traversing a tree by visiting the left child of each node, then the parent node, and then the rightchild of each node

Postorder Traversal: traversing a tree by visiting the children of each node before the node itself

Level-Order Traversal (Breadth-First Traversal): traversing a tree in the order of left, root, right; going from leftto right; a breadth-first search of the tree

Binary Tree Implementation

class BinaryTree:’’’an implementation of the binary tree ADT’’’def __init__(self . cargo , left=None , right=None):

self.cargo = cargoself.left = leftself.right = right

def __str__(self):return str(self.cargo)

25

Page 26: CS 1110 Notes

def total(tree):if tree is None:

return Nonereturn total(tree.left) + total(tree.right) + tree.cargo

def printTreePreOrder(tree):’’’prints the cargo of every node in the treeuses a preorder traversal’’’if tree is None:

return Noneprint(tree.cargo)printTreePreOrder(tree.left)printTreePreOrder(tree.right)

def printTreeInOrder(tree):’’’prints the cargo of every node in the treeuses a inorder traversal’’’if tree is None:

return NoneprintTreeInOrder(tree.left)print(tree.cargo)printTreeInOrder(tree.right)

def printTreePostOrder(tree):’’’prints the cargo of every node in the treeuses a postorder traversal’’’if tree is None:

return NoneprintTreePostOrder(tree.left)printTreePostOrder(tree.right)print(tree.cargo)

def printTreeLevelOrder(tree):’’’prints the cargo of every node in the treeuses a levelorder traversal’’’if tree is None:

return NoneifprintTreeLevelOrder(tree.left)printTreeLevelOrder(tree.right)print(tree.cargo)

HeapsHeap: a complete binary tree whose nodes contain comparable objects and are organized so that each node’sobject is no smaller (or alternatively no larger) than the objects in its descendants

Maxheap: a heap where the object in each node is greater than or equal to its descendant objects

Minheap: a heap where the object in each node is less than or equal to its descendant objects

Semiheap: a heap where the root node breaks the order

26

Page 27: CS 1110 Notes

Algorithms

Algorithm Strategies and ProblemsBrute Force (Guess and Check): an algorithm strategy that systematically calculates all of the possible answersto the problem and seeing which one is the best or satisfies the problem statement

Greedy Algorithm: an algorithm that uses the strategy of always picking the local optimal solution to generate theglobal optimal solution to a larger problem

Relaxation: the algorithm strategy of approximating a difficult problem with a simpler one and using its solution towork towards the solution to the original problem in an iterative fashion; alternatively, make a simple estimate ofthe solution to the original problem and iteratively improve the accuracy of the solution

Divide-And-Conquer (D&C): repeatedly reducing a hard problem to multiple simpler subproblems until the sub-problems are simple enough to solve and then combine the results from the subproblems in such a way to solvethe original problem; can be used with non-overlapping subproblems

Multiple And Surrender (Procrastinate And Surrender): an algorithm strategy for reluctant algorithms wherethe problem is repeatedly replaced by multiple simpler subproblems as long as possible until the problems are sosimple that they must be solved (unless you just completely stop making any sort of progress towards the problem)

Traveling Salesman Problem: given a list of locations and distances between each pair of locations, find theshortest possible route that visits each location exactly once and returns to the starting location

Dynamic Programming: an algorithm strategy only for problems with optimal substructure AND overlapping sub-problems where the problem divided into simpler subproblems which are solved and the subproblem solutions arecombined to get the solution to the original problem

Optimal Substructure: a problem whose solution can be obtained by combining the optimal solutions of thesubproblems

Overlapping Subproblems: a problem that when broken down into subproblems, the same subproblems cancome up multiple times and memoization of the solution can save time

Integer Programming: a mathematical optimization or feasibility problem where some or all of the variables arerestricted to be integers; integer programming is NP-hard

Linear Programming (LP): a technique for mathematical optimization of a linear objective function subject to linearequality and inequality constraints

Nonlinear Programming (NLP): techniques for mathematical optimization of an objective function subject to asystem of equality and inequality constraints where the objective function and or some or all of the constraints arenonlinear

Algorithm AnalysisP:

NP:

NP Easy: a problem that is at most as hard as NP, but not necessarily in NP

NP Equivalent: a problem exactly as difficult as the hardest problems in NP, but not necessarily in NP

NP Hard (Non-Deterministic Polynomial-Time Hard): a problem; a class of problems that are at least as hard asthe hardest NP problems

NP Complete (NP-C) (NPC):

27

Page 28: CS 1110 Notes

Satisfiability (The Satisfiability Problem) (SAT):

Turing Machine:

Constant Time: an operation whose runtime does not depend on the size of the data structure

Linear Time: an operation whose runtime is a linear function of the size of the data structure

ä f (n) = O(ϕ(n)) IF ∃ constants Mconstant | f (n)| ≤M |ϕ(n)|∀n > N

ä f (n) = o(ϕ(n)) IFf (n)ϕ(n)

→ o as n→ ∞

• f (n) = Ω(ϕ(n)) IF there exists constants N, A, and B such that A |ϕ(n)| ≤ | f (n)| ≤ B |ϕ(n)|∀n > N

• ∃= there exists ∀= for all

ä f (n)∼ cϕ(n)(

function asymptotic to)

ä only the largest term of each half of the rational expression is relevant

• take the largest term from the numerator and the denominator as a fraction and cancel things out to getthe big O behavior of a function

ä searching a list from start to end to find something (linear search) is on average O(n)

ä searching a binary search tree from the root to find something (binary search) is on average O(log2 n)

• in computer science logn is assumed to be log2 n because that’s what’s useful in computer science

Computing Requirements of Common Constructions

conditional O(1)

loop through n items O(n)

visit every element in a binary tree to depth n O()

visit 1 node at every depth to a depth n in a binary tree O(logn)

Searching Algorithms

Searching Algorithm ComparisonAlgorithm Best Average Worst

Linear Search O(1) n/2 = O(n) O(n)

Binary Search O(1) 0.5logn = O(logn) O(logn)

28

Page 29: CS 1110 Notes

Sequential Search1. check if the first element in the list is what you’re searching for

(a) IF yes, return it

(b) IF no, check the next element

2. repeat until you either find what you’re searching for or check the entire list

ä sequential search is your only real option for completely unstructured data

def SequentialSearch(list , value):’’’searches the unsorted list for the given value using a sequential search algorithm (AKA linear search)returns the index(s) containing the desired value’’’length = len(list)occurances = []for index in range(0 , length -1):

if list[index] == value:occurances.append(index)

return occurances

Binary Search1. check the median or middle element of the list

(a) IF yes, return it

(b) IF it come after the desired element in the list, get the median element of the first half of the list

(c) IF it comes before the desired element in the list, get the median element of the second half of the list

2. continue checking and getting the median of the relevant section of the list until the desired element is foundor there is nothing in between two elements you’ve already checked

ä binary search requires that the data is sorted in the relevant order

ä in practice, binary search is good, but not as good as one would expect because modern CPU’s are heavilyreliant on caching of nearby values, which generally isn’t helpful in a binary search, making it effectivelyslower than one would expect

def binarySearch(list , value):’’’searches the sorted list for the given value using a binary search algorithmreturns the index containing the desired value’’’startIndex = 0endIndex = len(list) - 1while startIndex <= endIndex:

median = startIndex + (endIndex - startIndex) // 2medianValue = list[median]if medianValue == value:

29

Page 30: CS 1110 Notes

print(’the location of the value is: ’ , median)return median

elif medianValue > value:endIndex = median - 1

elif medianValue < value:startIndex = median + 1

else:return None

def recusiveBinarySearch(list , value):’’’searches the sorted list for the given value using a recursive binary search algorithmreturns the index containing the desired value’’’length = len(list)median = length // 2medianValue = list[median]if medianValue == value:

print(’the location of the value is: ’ , median)return median

elif medianValue > value:recusiveBinarySearch(list[0:median] , value)

else:recusiveBinarySearch(list[median+1:length -1] , value)

Sorting AlgorithmsStable: equal keys aren’t reordered

Adaptable: speeds up to O(n) when data is nearly sorted or when there are few unique keys

Memory Usage: the additional space in memory required to perform the sort beyond storing the initial list of items

ä sorting is generally considered a solved problem in computer science

• the ideal sorting algorithm has the following properties:

∗ stable

∗ adaptable

∗ O(1) memory usage

∗ worst case O(n logn) comparisons

∗ worst case O(n) swaps

∗ minimum overhead

∗ maximum readability

• no algorithm has all of these properties, so the best choice depends on the application

ä in practice, you can usually just use a built-in sorting function for most programming languages

• the Python sorted() function uses Timsort, which is a hybrid of merge sort and insertion sort

30

Page 31: CS 1110 Notes

Sorting Algorithm ComparisonAlgorithm Best Average Worst Memory Stable

Timsort O(n) O(n logn) O(n logn) O(n) Yes

Smoothsort O(n) O(n logn) O(n logn) O(1) No

Merge Sort O(n logn) O(n logn) O(n logn) Depends, worst = n Yes

In-Place Merge Sort – – O(

n(logn)2)

O(1) Yes

Quicksort O(n logn) O(n logn) O(n2) average = logn , worst = n Depends

Heapsort O(n logn) O(n logn) O(n logn) O(1) No

Binary Tree Sort O(n) O(n logn) O(n logn) O(n) Yes

Shell Sort O(1) No

Insertion Sort O(n) O(n2) O

(n2) O(1) Yes

Selection Sort O(n2) O

(n2) O

(n2) O(1) No, unless O(n)

extra space

Bubble Sort O(n) O(n2) O

(n2) O(1) Yes

Lexicographical Sorting Algorithms

MSD Radix Sort O(

n · kd

)O(

n · kd

)O(

n+kd·2d)

LSD Radix Sort – O(

n · kd

)O(

n · kd

)O(n) Yes

Bucket Sort – O(n+ r) O(n+ r) O(n+ r) Yes

Bogo Sort1. randomly order the list

2. check if the list is order, element by element

(a) IF the list is sorted, THEN return the sorted list

(b) IF the list is not sorted, repeat the above process until it is

ä Bogosort is the canonical example of a comically bad algorithm intended to be as inefficient as possible

import random

def bogoSort(list):’’’takes a list and returns the list sorted from smallest to largest’’’# must shuffle the list first or it’s a bug if the list was pre-sortedlength = len(list)if length == 0 or length == 1:

return listrandom.shuffle(list)while not in_order(list):

random.shuffle(list)

31

Page 32: CS 1110 Notes

return list

def inOrder(list):last = list[0]for element in list[1:]:

if element < last:return False

last = elementreturn True

Bubble Sort1. from beginning to end, compare adjacent pairs of items

(a) IF in order, continue to the next element

ä this means the pair indexes shifts by one, so [1,2] becomes [2,3]

(b) IF out of order, swap

2. repeatedly go through the list until no swaps are required

ä bubble sort isn’t really used in practice

def bubbleSort(list):’’’takes a list and returns the list sorted from smallest to largest’’’length = len(list)if length == 0 or length == 1:

return list# keep going through the listfor round in range(length -1):

# increment through the listfor increment in range(length -1 - round):

# swap values if necessaryif list[increment] > list[increment+1]:

temp = list[increment]list[increment] = list[increment+1]list[increment+1] = temp

return list

Selection Sort1. append the smallest value of the given unsorted list to a new list

2. repeat until all of the values have been appended to the new sorted list

ä selection sort has bad big O performance, but is still useful because it minimizes the number of swaps, so ifthe cost of swaps is very high, it may be the best choice

32

Page 33: CS 1110 Notes

def selectionSort(list):’’’takes a list and returns the list sorted from smallest to largest’’’length = len(list)if length == 0 or length == 1:

return listelse:

for outerIndex in range(length -1):minIndex = outerIndexminValue = list[outerIndex]for innerIndex in range(outerIndex+1 , length):

value = list[innerIndex]if value < minValue:

minValue = valueminIndex = innerIndex

# swap values at minIndex and outerIndexlist[minIndex] = list[outerIndex]list[outerIndex] = minValue

return list

Insertion Sort1. create a new list to put the elements into in the correct order

2. insert the first element of the original list to the sorted list

3. take the next element of the original list and insert it into the correct spot in the new list

(a) increment through the sorted list, comparing the value to be inserted to the values already in the list

4. continue inserting elements from the original list into the sorted list until everything has been added to it

ä insertion sort has bad big O performance, but is still useful because it is adaptive and has low overhead

def insertionSort(list):’’’takes a list and returns the list sorted from smallest to largest’’’length = len(list)if length == 0 or length == 1:

return listelse:

# for every element in the unsorted portion of the listfor unsortedIndex in range(1 , length):

# check against every element in the sorted portion of the listfor insertionIndex in range(unsortedIndex , 0 , -1):

sortedElement = list[insertionIndex - 1]unsortedElement = list[insertionIndex]# if unsortedElement is in the correct spotif sortedElement <= unsortedElement:

break# swap elements as necessary

33

Page 34: CS 1110 Notes

temp = unsortedElementlist[insertionIndex] = sortedElementlist[insertionIndex - 1] = unsortedElement

return list

Shell Sortä shell sort is essentially an improved version of insertion sort

def shellSort(list):’’’takes a list and returns the list sorted from smallest to largest’’’length = len(list)if length == 0 or length == 1:

return listelse:

# for every element in the unsorted portion of the listfor unsortedIndex in range(1 , length):

# check against every element in the sorted portion of the listfor insertionIndex in range(unsortedIndex , 0 , -1):

sortedElement = list[insertionIndex - 1]unsortedElement = list[insertionIndex]# if unsortedElement is in the correct spotif sortedElement <= unsortedElement:

break# swap elements as necessarytemp = unsortedElementlist[insertionIndex] = sortedElementlist[insertionIndex - 1] = unsortedElement

return list

Merge Sort1. recursively divide a list in half

2. keep dividing until all of the pieces consist of either one or two elements

3. sort the lists of one or two elements each

4. recombine the lists into larger sorted lists

def mergeSort(list):’’’takes a list and returns a copy sorted from smallest to largest’’’# corner caseslength = len(list)if length == 0 or length == 1:

return listelse:

34

Page 35: CS 1110 Notes

middleIndex = length // 2# recursively break the lists down into single elementsleft = mergeSort(list[:middleIndex])right = mergeSort(list[middleIndex:])# call the merge method to merge the sorted left and right listsreturn merge(left , right)

def merge(left , right):’’’merges the two sorted lists into a sorted list’’’merged = []leftIndex = 0rightIndex = 0leftLength = len(left)rightLength = len(right)

# don’t exit the loop, just go until a return statement is reachedwhile True:

# exit when one of the lists has been completely gone through# also can handle if one or both of the lists is emptyif rightIndex >= rightLength:

return merged + left[leftIndex:]elif leftIndex >= leftLength:

return merged + right[rightIndex:]# add the next smallest element to the new listelif left[leftIndex] < right[rightIndex]:

merged.append(left[leftIndex])leftIndex = leftIndex + 1

else:merged.append(right[rightIndex])rightIndex = rightIndex + 1

Quick Sort1. select a random element to be a pivot

2. copy the other elements into two sublists

def quickSort(list):’’’takes a list and returns a copy sorted from smallest to largest’’’# corner caseslength = len(list)if length == 0 or length == 1:

return listelse:

pivot = list[-1]rest = list[:-1]left = []right = []

35

Page 36: CS 1110 Notes

for index in rest:if index < pivot:

left.append(index)else:

right.append(index)left.quickSort(left)right = quickSort(right)return left + [pivot] + right

def partition(list , startIndex , endIndex):

Bucket Sort1. must be given largest item in list

2. all list elements must fall in a predictable number of discrete values, e.g. ints, chars, floats with only onedecimal place, etc.

3. make new list from 0 to the largest value

4. iterate through the list and increment the second list accordingly to whatever value you read in the first list

ä bucket sort is a generalization of pigeonhole sort and counting sort

def bucket_sort(list):’’’ assuming list to be a list of integers’’’length = len(list)if length == 0 or length == 1:

return listbig = list[0] # starting to find the extreme valuessmall = list[0]for term in list: # to establish max range of possible values

if term > max:big = term

if term < min:small = term

freq = [ ] # to hold frequencies for valuesfor i in range(small , big + 1):

freq.append(0) # initialising freqs to be zerofor term in list:

freq[term - min] += 1 # incrementing freqsi = 0for loc in range(len(freq)): # run through freq list

count = freq[loc]# get frequency of occurrence to see how often to repeat valuefor more in range(count): # repeat the insertion of the value _count_ times

list[i] = loc + smalli += 1

return list

36

Page 37: CS 1110 Notes

LSD Radix Sort1. create a list of bucket queues

2. read sequence into queues according to right-most digit

3. empty the queues into a new sequence

4. read sequence into queues according to the digit one place to the left of the right-most digit

5. empty the queues into a new sequence

6. repeat steps 4 and 5 until all of the digits have been processed

ä IF not all of the numbers have the same number of digits, THEN the corresponding digit places must be filledin with zeros so that all of the items have the same number of digits

def radix_sort(my_list):’’’ assuming my_list to be a list of integers, and we’ll proceed base 10 for clarity

This could be vastly more efficient, but I’ve written the code to be highly transparent’’’if len(my_list) == 0 :

return my_listshifted_list = [ ] # to hold the values once shifted by the smallest valuebig = my_list[0] # starting to find the extreme valuessmall = my_list[0]for term in my_list : # to establish max range of possible values

if term > big :big = term

if term < small :small = term

spread = big - small # the max range of numbersfor value in my_list :

shifted_list.append(value - small) # so sorting a list whose smallest is zeroprint (shifted_list)base = 10radix = get_radix(spread , base)print("radix = " + str(radix))digitised = [] # to hold the digitised values of my_list relative to the basefor value in shifted_list :

digitised.append(long_get_digits(value , radix , base))print(digitised)buckets = [ ] # to hold the various queues of valuesfor count in range(base) : # one bucket for each potential value of a ’digit’

buckets.append(Queuer(count))for k in range(radix) :

for value in digitised :buckets[value[radix - k - 1]].insert(value)

for b in buckets : print(b)digitised = [ ] # empty and re-usefor q in buckets :

while not q.isEmpty() :

37

Page 38: CS 1110 Notes

digitised.append(q.leave())print(digitised)

my_list = [ ] # re-use this listfor digital in digitised :

my_list.append(reform_number(digital) + small) # so ’unshifting’ the values backreturn my_list

def get_radix(spread , base=10): # default base is 10’’’ assume spread > 0 and base > 1’’’n = 1 # to explore the least power of base to exceed spreadtemp = basewhile spread >= temp :

temp *= basen += 1 # to try the next power of base

return n

def get_digits(value , base=10):radix = get_radix(value , base)digits = [ ] # to hold the digits of spread relative to the value of basefor count in range(radix) :

digits.append(value % base)value = value // base

digits.reverse() # for human sanity!!return digits

def long_get_digits(value , radix , base=10):’’’ to fill in with leading zeros as needed’’’digits = get_digits(value , base)digits.reverse() # easy trick to prepend the right number of zerosn = len(digits)for count in range(radix - n) :

digits.append(0)digits.reverse()return digitsdef reform_number(digital , base=10):’’’ assuming digital is a list of digits to that base’’’radix = len(digital)temp_power = basetemp = digital[radix - 1]for k in range(1, radix) :

temp += digital[radix - k - 1] * temp_powertemp_power *= base

return temp

38

Page 39: CS 1110 Notes

MSD Radix Sort

GraphsGraph: a collection of vertices and edges

Subgraph: a portion of a graph that is itself a graph

Vertex (Node): discrete elements or units; the circles

Adjacent Vertices (Neighbors): both vertices in an undirected graph that are joined by an edge OR in a directedgraph, the ending vertex for an edge

Edge: the connections between vertices; the lines

Undirected Edge: an edge without a direction; both vertexes have the same relationship to each other

Directed Edge: an edge with a direction; there is a starting vertex and an ending vertex

Multiple Edge: when there are two or more edges connecting the same vertices in the same direction

Loop: an edge that starts and ends at the same vertex

In Degree: the number of edges ending at a given vertex

Out Degree: the number of edges starting at a given vertex

Weighted Graph: a graph that has values associated with each of the edges

Directed Graph (Digraph): a graph with directed edges

Bipartite Graph: a graph where the vertices can be divided into two groups such that every edge goes from avertex in one group to a vertex in the other group

Complete Graph: a graph with an edge connecting every pair of distinct vertices; there doesn’t have to be edgesconnecting a vertex to itself

Connected Graph: a graph that has a path between every pair of distinct vertices

Biconnected Graph: a graph where two paths that do not share edges or vertices exist between every pair ofvertices

Disconnected Graph: a graph that is NOT connected

Sparse Graph: a graph with relatively few edges

Dense Graph: a graph with many edges

ä typical graphs are sparse

ä n(n−1)

(maximum number of edges;

directed graph with n vertices

)

än(n−1)

2

(maximum number of edges;

undirected graph with n vertices

)

Graph Paths and CyclesPath: a sequence of edges connecting two vertices

Directed Path: a path in a directed graph, which must follow the directions of the edges

Simple Path: a path that does not pass through any vertex more than once

Length: the number of edges in a path

39

Page 40: CS 1110 Notes

Critical Path: the path with the greatest weight in a weighted, directed, acyclic graph

Diameter: for an unweighted graph, the maximum of all the shortest distances between pairs of vertices in thegraph

Cycle: a path that begins and ends at the same vertex

Acyclic: a graph that has no cycles

Simple Cycle: a cycle that passes through other vertices only once each

Hamiltonian Graph: a graph that contains a Hamiltonian cycle

Hamiltonian Path (Traceable Path): a path in a directed or undirected graph that visits each vertex exactly once

Hamiltonian Cycle (Hamiltonian Circuit): a Hamiltonian path that is a cycle

ä all Hamiltonian graphs are biconnected graphs

• not all biconnected graphs are Hamiltonian graphs

ä every platonic solid, considered as a graph, is Hamiltonian

ä every prism is Hamiltonian

ä a simple graph with n vertices (n≥ 3) is Hamiltonian IF every vertex has degree n/2 or greater

ä a graph with n vertices (n≥ 3) is Hamiltonian IF for every pair of non-adjacent vertices, the sum of theirdegrees is n or greater

Eulerian Graph: a graph that contains an Eulerian cycle

Eulerian Trail (Euler Walk): a path in an undirected graph that uses each edge exactly once

Eulerian Cycle (Eulerian Circuit) (Euler Tour): a cycle in an undirected graph that uses each edge exactly once

Topological Order:

Minimum Spanning Tree:

Graph Coloring: assigning a color to every vertex in a graph with the restriction that two vertices of the same colorcannot be adjacent

k-Colorable: a graph that can be colored in k or fewer colors

Shortest Path:

Max-Flow Min-Cut:

ä all graphs are trees

ä not all trees are graphs

ä a tree is a connected acyclic graph

Graph Algorithms

Topological Sort

Kruskal’s Algorithm

1. pick the edge with the lowest weight on the graph

40

Page 41: CS 1110 Notes

ä if two edges have the same weight, then the choice doesn’t matter

ä you are never allowed to pick edges that create a cycle

2. continuing picking the edge with the next lowest weight until a minimum spanning tree is achieved

ä finds a minimum spanning tree on a weighted graph

ä Kruskal’s algorithm is better than Prim’s algorithm for

Prim’s Algorithm

1. pick a random starting vertex

2. pick the edge from the starting vertex with the lowest weight

3. pick the edge from the connected vertexes with the lowest weight

ä never consider picking edges that create a cycle

4. continue until the tree spans all of the edges of the graph

ä finds a minimum spanning tree on a weighted graph

ä Prim’s algorithm is better than Kruskal’s algorithm for

Depth First Search (DFS)

ä accesses every vertex of a graph

Breadth First Search (BFS)

ä accesses every vertex of a graph

Dijkstra’s Algorithm

1. assign the starting vertex a distance of zero

2. assign all the other vertexes a tentative distance value of infinity

3. for the current node, consider all of its unvisited neighbors and calculate the [distance to the current vertex]plus the [distance from the current vertex to the neighbor]

4. IF this is less than the vertex’s current tentative distance (∞ or otherwise), THEN replace it with the newvalue

ä this is the relaxation part of Dijkstra’s algorithm

5. after the distances to all of the neighboring nodes have been updated, the current node is added to thevisited nodes list

6. the unvisited node with the smallest distance becomes the next current node

41

Page 42: CS 1110 Notes

7. continue until the destination vertex has been visited

ä finds the shortest/cheapest path on a weighted graph

• the weights must be non-negative

Bellman-Ford Algorithm

ä the Bellman-Ford algorithm is slower than Dijkstra’s algorithm, but can be used with negative edge weights

• IF there are negative cycles, THEN there is no shortest/cheapest path because any path can alwaysbe made shorter by another walk through the negative cycle

• the Bellman-Ford algorithm can report the existence of negative cycles

A* Search Algorithm

ä A* is essentially Dijkstra’s shortest path algorithm plus a heuristic to improve time performance

• A* is NOT guaranteed to find the optimal path (but hopefully a very good one), especially if the heuristichas low accuracy

Ford-Fulkerson Algorithm

Bipartite Graph Matching

Kosaraju-Sharir Algorithm

Hopcroft-Karp Algorithm

Dinitz’s Algorithm

File IO

File IOä it is generally advisable to read files line by line rather than all at once in case the input file is extremely large

ä it is a good practice to close a file as soon as you’re done with it

42

Page 43: CS 1110 Notes

File I/O Functions

raw_input() read one line from standard input and return it as a string (without thetrailing newline)

input() read one line from standard input and return it as an evaluated expression

print() converts the passed expressions to strings and then writes them tostandard output

open(<filename> , ’<r/w permission>’) opens the specified file and returns a pointer to it

<file name>.close() flushes any unwritten information from memory and closes the file object,after which no more writing can be done

<file name>.name returns the name of the file

<file name>.mode returns the access mode with which the file was opened

<file name>.closed returns true if the file is closed and false if it open

read() read the entire file or optionally, the specified number of characters or bytes

readline() read the next line

readlines() read the next

write(<string>) write the string to the file

writelines(<list of strings>) write the list of strings to the file

truncate() truncate the file at the current position or at the optionally specified size

Useful Packages

ä numpy

ä scipy

ä matplotlib

ä IPython

ä PyQt

ä Sage

ä Cython

ä MLabWrap

ä RPy

ä py2exe

ä py2app

ä wxPython

ä Tkinter

43

Page 44: CS 1110 Notes

Accessing The Internet

Open A Web Page In A New Window

import webbrowserwebbrowser.open_new(’<URL>’)

Open A Web Page In A New Tab

import webbrowserwebbrowser.open_new_tab(’<URL>’)

Perform A Google Search

import webbrowsergoogle = raw_input(’Google search: ’)# the user is prompted to enter what they want to Googlewebbrowser.open_new_tab(’https://www.google.com/search?btnG/1&q/%s’ % google)

GUIs

GUIsWidget: one of the elements that makes up a GUI, including buttons, menus, text entry fields, etc.

Option: a value that controls the appearance or function of a widget

Keyword Argument: an argument that indicates the parameter name as part of the function call

Callback: a function associated with a widget that is called when the user performs an action

Bound Method: a method associated with a particular instance

Event-Driven Programming: a style of programming in which a flow of execution is determined by user actions

Event: a user action, like a mouse click or key press, that causes a GUI to respond

Event Loop: an infinite loop that waits for user actions and responds

Item: a graphical element on a Canvas widget

Bounding Box: a rectangle that encloses a set of items, usually specified by two opposing corners

Pack: to arrange and display the elements of a GUI

Geometry Manager: a system for packing widgets

Binding: an association between a widget, an event, and an event handler. The event handler is called when theevent occurs in the widget

Tkinter GUIs

44

Page 45: CS 1110 Notes

Tkinter Widgets

Button creates buttons in an application

Canvas creates a rectangular area intended for drawing pictures and other complex objects

Checkbutton creates checkbox buttons in an application

Entry creates a textbox for the user to input a single-line string of text

Frame creates rectangular areas in the screen to organize the layout and to provide paddingfor other widgets

Label creates a display box for text and or images

Listbox creates a box of selectable lines of text

Menubutton creates a button to open a drop-down menu

Menu creates a pop-up, toplevel, or pull-down menu

Message creates a non-editable display box similar to Label, but with automatic line breakingand justification of the contents

Radiobuttion creates a multiple choice radio button

Scale creates a graphical sliding object to select a value for a variable

Scrollbar creates vertical and horizontal scrollbars

Text creates a display box with advanced text editing abilities

Toplevel creates a window to put other widgets in

Spinbox creates a box to select a value with direct input or clickable up and down arrows

PanedWindow creates a window pane inside a larger window

LabelFrame creates a container or spacer for other widgets with the properties of both a Frame anda Label

tkMessageBox creates a pop-up message box

wxPython GUIs

import wx

class TestFrame(wx.Frame):def __init__(self , parent , title):

wx.Frame.__init__(self , parent , wx.ID_ANY , title=title)text = wx.StaticText(self , label= "Hello World!")

app = wx.App(redirect=False)frame = TestFrame(None , "Hello World!")frame.Show()app.MainLoop()

Event-Driven Programming

45