€¦  · web viewcomputer science iii. cop 3530 -- spring 1999. university of central florida....

337
Computer Science III COP 3530 -- Spring 1999 University of Central Florida Computer Science Department Charles E. Hughes

Upload: others

Post on 30-Jul-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Computer Science IIICOP 3530 -- Spring 1999

University of Central FloridaComputer Science Department

Charles E. Hughes

Page 2: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Basic InformationMeeting Times: TR 10:00 - 11:15, COM-117

Instructor: Charles E. Hughes, CSB-205, 823-2762

Email and Home Page: [email protected], http://www.cs.ucf.edu/~ceh/

Office Hours (Tentative): TR 1:15-2:30; MW 9:30-10:30.

Texts: Aho&Ullman, Foundations of Computer Science: C edition, Computer Science Press, 1995. References: Weiss, Data Structures and Algorithm Analysis in Java, Addison-Wesley, 1999.

Prerequisites: COP3503 (CS2), COT3100 (Discrete Structures.

Assignments: 5 small to moderate programming assignments; one reasonably large one; at least 5 non-programming assignments including a research paper.

Exams: Two quizzes, a midterm and a final.

Supporting Material:Web tutorials on Java (http://www.javasoft.com/docs/books/tutorial/index.html)

© C. E. Hughes, UCF Computer Science – 2 – COP 3530 Spring ‘99

Page 3: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Evaluation

Evaluation:This is an approximation, subject to restructuring based on increases or decreases in assignments or in complexity of assignments over what I am currently considering.Quizzes – 50 points each; Mid Term – 100 points; Final Exam – 150 pointsActually the weight of a quiz and its corresponding exam varies to your benefit.Assignments – 350 points; Available Points – 700A – 90%B – 80%-89%C – 70%-79%D – 60%-69%F – <60%

Important Dates (Quiz Dates are Subject to Change):Quiz#1-- February 4; MidTerm -- February 18; Spring Break -- Week of March 15; Withdraw Deadline -- February 26; Quiz#2 -- April 15; Final -- April 29, 10:00-12:50

© C. E. Hughes, UCF Computer Science – 3 – COP 3530 Spring ‘99

Page 4: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Topics

Topics 1 and 2 are intended to be reviews. If this is not so, you need to quickly read Chapters 2, 3 and 5 of Aho and Ullman

1. Iteration, induction and recursion: proving properties of programs

2. Analysis of the running time of programs: Big-Oh notation

3. Object oriented programming.

4. Parallel sorting algorithms: emphasis algorithms and their analyses

5. Tree Model: Special attention on BSTs, Dictionaries, Tries and POTs

6. List Model

7. Set Model

8. Relational Model

9. Graph Model

10. Patterns, Automata and Regular Expressions

11. Grammars and Parsing

12. Various discussions on parallel algorithms for problems posed in text

© C. E. Hughes, UCF Computer Science – 4 – COP 3530 Spring ‘99

Page 5: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

WEEK # 1 (1 day)1. Syllabus and the House Rules

Grading Policy2. The notion of an abstract data type (ADT) is that it consists of an encapsulated state and a set of behaviors that are invoked by messages

(operators, procedure and function calls.) The ADT must specify semantics of messages, but not their implementations. Examples: stacks, queues, priority queues, sets, bags

3. An abstract implementation is a data model specifying some abstract organization of data (an ADT’s state), e.g., a list, a binary tree, a binary search tree, a priority ordered tree, a graph, etc.

4. A data structure is the way we represent an abstract implementation. For example, a list can be represented by an array of data items or by a linked list of data items. Examples are linked lists, arrays and heaps

5. An algorithm is used to describe a method for implementing a behavior. Algorithms can be at various levels of abstraction, depending upon our needs.

6. Programming languages must provide ways for us to extend the available data models. Thus, we usually see arrays and records, though not always. Some languages directly support trees or lists and expect that you can do everything with them.

7. An undirected graph is a very useful data model. The data structure for a graph might be an adjacency list or an adjacency matrix. The adjacency matrix approach uses an N by N matrix of binary values, where there are N nodes. If nodes I and J are connected, then the I,J position and the J,I position contain a 1, else these positions contain a 0. This is a bit redundant, so we often find a way to represent by some sparse matrix or we use an adjacency list. There are definite tradeoffs in space and time.

8. The choice of a data structure and the corresponding algorithms depends on many things. Some of these are:SPACE EFFICIENCYTIME EFFICIENCYPERSONAL OR TEAM PREFERENCEEASE IN A GIVEN LANGUAGEREUSE (IT’S ALREADY BEEN DONE IN SOME ACCEPTABLE WAY)

9. Review of MergeSort analysis.10. Reading Assignment: Chapters 2, 3 and 5 of Aho and Ullman.11. Assignment #1: Problems 3.5.5; 3.8.1; 3.11.1, 3.11.3, 3.11.9, 3.11.10. Turn in on Thursday, January 21. You may apply the table on Page

151 to solve 3.11.3a-c, and 3.11.9. All others must be solved by providing complete proofs. Be complete and neat. This is individual work. If you have problems, see me.

© C. E. Hughes, UCF Computer Science – 5 – COP 3530 Spring ‘99

Page 6: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Abstract Data Types

Definition: (B. Meyer)... an abstract data type specification describes a class of data … not by an implementation, but by a list of services available on the data …, and the formal properties of those services.

• An ADT is a behavioral specification for objects

© C. E. Hughes, UCF Computer Science – 6 – COP 3530 Spring ‘99

Page 7: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Stacks as an Abstract Data Type

TYPESSTACK[X]

FUNCTIONSempty: STACK[X] BOOLEANnew: STACK[X]push: X STACK[X] STACK[X]pop: STACK[X] | STACK[X]top: STACK[X] | X

PRECONDITIONSpre pop (s: STACK[X]) = ( not empty (s) )pre top (s: STACK[X]) = ( not empty (s) )

AXIOMSFor all x: X, s: STACK[X]:

empty ( new () )not empty ( push ( x, s ) )top ( push ( x, s) ) = xpop ( push ( x, s) ) = s

© C. E. Hughes, UCF Computer Science – 7 – COP 3530 Spring ‘99

Page 8: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Data Models / Abstract Implementations

• An abstract implementation is a data model specifying some abstract organization of data (an ADT’s state), e.g., a list for stacks, a binary tree for expressions, a binary search tree for dictionaries, etc.

• A data structure is the way we represent an abstract implementation. For example, a list can be represented by an array of data items or by a linked list of data items.

• An algorithm is used to describe a method for implementing a behavior. Algorithms can be at various levels of abstraction, depending upon our needs.

© C. E. Hughes, UCF Computer Science – 8 – COP 3530 Spring ‘99

Page 9: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Choice of Data Structures & Algorithms

• A Data Structure is a concrete implementation of some Data Model. For instance, a tree is often implemented as a linked list, but can also be implemented by an array or a heap.

• The choice of a data structure and the corresponding algorithms depends on many things. Some of these are:

SPACE EFFICIENCY

TIME EFFICIENCY

PERSONAL OR TEAM PREFERENCE

EASE IN A GIVEN LANGUAGE

REUSE (IT’S ALREADY BEEN DONE IN SOME ACCEPTABLE WAY)

• Algorithms must be analyzed for correctness and complexity.

• The complexity of a correct algorithm is no better than the inherent complexity of the problem being modeled. Often, the problem’s inherent complexity – an intrinsic property – is less than that of our associated algorithm.

© C. E. Hughes, UCF Computer Science – 9 – COP 3530 Spring ‘99

Page 10: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Graph Data Models and Data Structures

A

B

F

E

G

C

D

67

5

7

7

3

711

614

5

3

Adjacency Matrix Representation Adjacency Lists RepresentationA B C D E F G

A 0 5 3 14 A ((C,5),(D,3),(E,14))B 0 5 7 6 B ((E,5),(F,7),(G,6))C 5 0 11 3 7 C ((A,5),(D,11),(E,3),(F,7))D 3 11 0 7 6 D ((A,3),(C,11),(E,7),(G,6))E 5 3 7 0 7 E ((B,5),(C,3),(D,7),(G,7))F 7 7 0 F ((B,7),(C,7))G 14 6 6 7 0 G ((A,14),(B,6),(D,6),(E,7))

© C. E. Hughes, UCF Computer Science – 10 – COP 3530 Spring ‘99

Page 11: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Timing Analysis of MergeSort Algorithm -- IntuititiveAssume that the merge of two lists of total size N/2 takes order N time.

T(1) = a

T(N) = 2 T(N/2) + b N + c

T(N) = 2 (2 T(N/22) + b N/2 + c) + b N + cT(N) = 22 T(N/22) + 2 b N + 3 c T(N) = 22 (2 T(N/23) + b N/22 + c) + 2b N + 3 cT(N) = 23 T(N/23) + 3 b N + 7 c

T(N) = 2k T(N/2k) + k b N + (2k–1) c

If we assume that N = 2k, for some k0, then for this k, N/2k is 1, and we hypothesize that T(N) = 2k a + k b 2k + (2k–1) c. To verify this hypothesis, we need to provide an inductive proof, showing that for k0,

© C. E. Hughes, UCF Computer Science – 11 – COP 3530 Spring ‘99

Page 12: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Timing Analysis of MergeSort Algorithm -- FormalAgain, assume that N = 2k, for some k0. We hypothesize that T(N) = 2k a + k b 2k + (2k–1) c. To verify this hypothesis, we need to provide an inductive proof, showing that for k0,

S(k): T(2k) = 2k a + k b 2k + (2k–1) c.

Basis – S(0):T(1) = a by definition and 20 a + 0 b 20 + (20–1) c = a

Inductive Hypothesis:Assume S(k), that is, T(2k) = 2k a + k b 2k + (2k–1) c, for some k≥0.

Inductive Step: Show S(k+1) , given S(k). T(2k+1) = 2 T(2k) + b 2k+1 + c by definition. Thus,T(2k+1) = 2 (2k a + k b 2k + (2k–1) c) + b 2k+1 + c by induction hypothesis. ThenT(2k+1) = 2k+1 a + k b 2k+1 + 2 (2k–1) c + b 2k+1 + c and, consequentlyT(2k+1) = 2k+1 a + (k+1) b 2k+1 + (2k+1–1) c , as we wished to show.

The terms with constants a and c are of order N. The term with constant b is of order N log2N and thus dominates. Hence a MergeSort takes O(N log2N) time, at worst.

© C. E. Hughes, UCF Computer Science – 12 – COP 3530 Spring ‘99

Page 13: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

More Solutions to RecurrencesInductive Equation T(n)

T(n) = T(n – 1) + bnk O(nk+1)T(n) = cT(n – 1) + bnk, for c > 1 O(cn)T(n) = cT(n/ d) + bnk, for c > dk O(nlogd c)T(n) = cT(n/ d) + bnk, for c < dk O(nk)T(n) = cT(n/ d) + bnk, for c = dk O(nk log

n)

3.11.2 We can attack a), b), and c) directly from the Table above, gettinga.) T(N) = T(N-1) + N2

b.) T(N) = T(N-1) +N2+3Nc.) T(N) = T(N-1) + N3/2

a.) O(N3) b.) O(N3) c.) O(N5/2)

but this table is of no help on parts d) and e). One approach to these is to solve them by writing out terms and recognizing patterns. Another approach is to look back at 3.11.1 and see that it gives us information on any recurrence relation of the form T(N) = T(N–1) + g(N), for N>1, and that’s the form given here with g(N)=log2(N) on part d) and g(N) = 2N on part e).

© C. E. Hughes, UCF Computer Science – 13 – COP 3530 Spring ‘99

Page 14: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

More Solutions to Recurrencesd.) T(N) = T(N-i) + +

log2(N j )

j 0

i 1 from 3.11.1, 1 I < N, N > 1. But then

T(N) = a +

log2 (N j )j 0

N 2 by substituting i = N-1

T(N) = a +

log2( j )j 2

N by renaming indices

We can continue the attack by noting that log is a monotonically increasing function, and so

T(N) a +

log2(N )j 2

N by substituting log2(N) for all log2(j)

= a + log2 (N ) 1

j 2

N by independence of term inside sum

a + N log2(N) by prior analysis of this sumThen T(N) is O(N log2(N) ) BUT IS IT TIGHT?

e.) T(N) = T(N-i) +

2N j

j 0

i 1 from 3.11.1, 1i<N, N>1. But then

T(N) = a +

2N j

j 0

N 2 by substituting i = N-1

T(N) = a +

2j

j 2

N by renaming indices

= a + 2N+1-4 by prior analysis of sum of powers of 2Then T(N) is O(2N )

© C. E. Hughes, UCF Computer Science – 14 – COP 3530 Spring ‘99

Page 15: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

WEEK # 2

1. OOP concepts and their implementations in Java.

2. Encapsulation, Polymorphism

3. Class hierarchies and reuse.Dynamic binding and polymorphism.

4. Abstract versus concrete classes. Notions of a common protocol.

5. Object handles versus pointers

6. Parameter passing (by value) in Java and its implications

7. Collections

© C. E. Hughes, UCF Computer Science – 15 – COP 3530 Spring ‘99

Page 16: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Java

Language of the Web.

Uses virtual machine to be non-machine specific.

Supports graphical interaction, component reuse and networking.

Encourages reactive programming

Almost pure object oriented with very rich set of base classes.

© C. E. Hughes, UCF Computer Science – 16 – COP 3530 Spring ‘99

Page 17: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

TypesPrimitive Types: int, long, short, byte, double, float, char, boolean

int anInteger;double aReal;char aCharacter;boolean aBool;

long (64 bits) has larger range than int (32 bits)int ranges to about 2 billion (around 9 digits)long range to about 8 quintillion (18 digits)

float (32 bits) has smaller range than double (64)float ranges to about 1038

double ranges to about 10308

Note: float has only seven digits of precisiondouble has just fifteenshort and byte are 16 and 8 bit ints

All Other Data are Instances of Classes -- User-Defined TypesA class instance is called an objectObjects are made in the image and likeness of their classes

© C. E. Hughes, UCF Computer Science – 17 – COP 3530 Spring ‘99

Page 18: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

AssignmentsAssignment Operator

Java and C use = rather than the symbol := used in Pascalint count = 0; // can initialize in declarationcount = count + 1; // will increment count by 1count += 5; // will increment by 5

ArithmeticAddition (+)Subtraction (-)Multiplication (*)Division (/)Modulo (%)

Java does integer division if both participants are intYou use “casts” to take care of other cases.double aDouble = 27.9;int quotient = (int) (aDouble / 3.1);

Constants in Java are objects, primitives or methods (functions) that are final (cannot be changed)final String PROFESSOR = "Charles E. Hughes ";final int NUMBER_OF_CHILDREN = 2;

© C. E. Hughes, UCF Computer Science – 18 – COP 3530 Spring ‘99

Page 19: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Input/OutputIn Pascal have

read(list); readln(list); write(list); writeln(list)

In Java, there is no easy correspondenceJava is not designed for keyboard operationJava is designed for file and GUI interaction

KB Input / Output in Javaimport java.io.* // needed to get access to I/O//Output

System.out.print(aString);System.out.println(aString);

//Input (one value per line / later we’ll do better)BufferedReader stdin = new BufferedReader

(new InputStreamReader(System.in));int anInt1 = Integer.parseInt(stdin.readLine()); // one wayint anInt2 = Integer.valueOf(stdin.readLine()).intValue(); // alternative waydouble aDouble = Double.valueOf(stdin.readLine()).doubleValue();

In above we wanted primitive int and double, but wrapper classes for eachInteger is a class, each of whose instances holds a single intDouble is a class, each of whose instances holds a single double

© C. E. Hughes, UCF Computer Science – 19 – COP 3530 Spring ‘99

Page 20: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

DecisionsRelational operators

== != > >= < <=

// if-thenif (cond1) { // must include parentheses

actions_when _ cond1_ true;} // braces needed only if multiple statements selected

// if-then-elseif (conditional_expression) {

actions_when_ conditional_expression_true;} else {

actions_when _ conditional_expression_ false;}

// if-then-elseif-elseif (cond1) {

actions_when _ cond1_ true;} else if (cond2) {

actions_when _ cond1_ false_and_cond2_true;} else {

actions_when _ cond1_ false_and_cond2_false;}

Logical Operators&& || !

© C. E. Hughes, UCF Computer Science – 20 – COP 3530 Spring ‘99

Page 21: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Applets and ApplicationsApplication is a stand-alone programApplet runs as a component of a browser

Application has full access to machine and networkApplet runs under tight security controls

Application must explicitly create a GUI interfaceApplet gets support from browser

Application must be available on machineApplet is accessed through normal web protocols

© C. E. Hughes, UCF Computer Science – 21 – COP 3530 Spring ‘99

Page 22: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Class AppletThe class Applet provides lots of services.

In particular, all instances of Applet know how to respond to the messages init() -- called when browser first encounters appletstart() -- called when browser visits page containing appletstop() -- called when browser leaves pagedestroy() -- called when browser decides applet is no longer relevantpaint(g) -- called when applet’s view is being uncovered, or originally shown

The default versions of these functions (provided by Applet) do nothing.

Applet inherits from Panel that provides a standard layout (FlowLayout)

Panel inherits from Container so it can add(aComponent) to its layoutButton, List, … are Components

Container inherits from Component which knows how to paint(g) its graphical appearance in Graphics context g

Hey -- an Applet is a Component and it has ComponentsThis makes sense if you think about it

java.lang.Object | +----java.awt.Component

| +----java.awt.Container

| +----java.awt.Panel

| +----java.applet.Applet

© C. E. Hughes, UCF Computer Science – 22 – COP 3530 Spring ‘99

Page 23: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Hello World Applet

// Hello Applet and HTMLimport java.applet.*;public class Hello extends Applet { // extends means inherits

public void paint(Graphics g) {g.drawRect(0, 0, 200, 48); // borderg.drawString("Hello from an applet.", 50, 32); // position in frame

}}

Vocabulary:import opens up a library so we can use its classesjava.applet.* all classes defined in the directory jdk??/java/applet/public accessible by clients of classclass template and factory for objects of new ADTextends adds or overrides services from a parent class (super type)Applet the class defining standard applet services and partsvoid returns nothingpaint called when Component image is originally drawn or must be repaired (uses clipping)Graphics the class associated with graphical contextsDrawRect/String a service of Graphics class which draws a rectangle/String

<HTML><HEAD><TITLE>Hello Applet</TITLE></HEAD><BODY><APPLET CODE = "Hello.class” WIDTH = 400 HEIGHT = 300></APPLET></BODY></HTML>

© C. E. Hughes, UCF Computer Science – 23 – COP 3530 Spring ‘99

Page 24: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Hello World Application

/** * The HelloWorldApp class implements an application that * simply displays "Hello World!" to the standard output. */

class HelloWorldApp {public static void main(String[] args) {

System.out.println("Hello World!"); //Display the string.}

}

Vocabulary:class template and factory for objects of new ADTpublic accessible by clients of class static associated with class, not instances of classvoid returns nothingmain start routine for any applicationString[] type of array of String objectsSystem a class with global objects, one of which is 'in', another is 'out'

Note:Main is automatically called when an application is started. The arguments are from command line.

© C. E. Hughes, UCF Computer Science – 24 – COP 3530 Spring ‘99

Page 25: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Primitives versus ObjectsIn Java, all actual parameters are passed by copy.

That is, the formal parameter receives a copy of the actual one. When the parameters are primitives, this limits modules to only returning a single value

-- the one returned as the function’s result.

Java is really a language of objects, not primitives. Each object is an instance of a class, Java’s mechanism for data abstraction.

ClassesA class is an abstract data type (ADT).A class (ADT) provides a new data type, along with services required to manipulate objects of this new type.A class encapsulates (hides) the details of how it implements the new data type and its services.A class exposes its name (for use as a new type), and the services that should be available to users of objects of this type. The exposed services are referred to as the class’s protocol.

ObjectsEach object is an instance of a class.The class specifies the protocol (services) and parts (data structures) of its objects.A class can be part of an inheritance hierarchy by which the class acquires some of its characteristics (parts and services) from its parent class.All classes are descendants of the base class called Object.

© C. E. Hughes, UCF Computer Science – 25 – COP 3530 Spring ‘99

Page 26: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Integer Class// Integer is child of Number, and so inherits its parts and services, if any

public class Integer extends Number { // public means it’s exposed for useprivate int value; // private means that it’s not known outside herepublic Integer(int value) { // used to construct new Integer objects

this.value = value;}public int intValue() { // public means that’s its exposed for use

return value;}… …

}

// using IntegerInteger myInt = new Integer(7); // can use constructor since it’s publicint myIntVal = myInt.intValue(); // can use intValue() since it’s public

int myIntVal = myInt.value; // ILLEGAL since value is private

© C. E. Hughes, UCF Computer Science – 26 – COP 3530 Spring ‘99

Page 27: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Fraction Class (not part of Java base classes)public class Fraction extends Number {

private int numerator, denominator; // hidden detailspublic Fraction(int num, int den) { // used to construct new Fraction objects

numerator = num;denominator = den;

}public int getNumerator() { // in protocol

return numerator ;}public int getDenominator() { // in protocol

return denominator ;}private void reduce() { } // internal utility service -- not in protocol

… … }

// using Fraction

Fraction frac = new Fraction(2, 7);int num = frac.getNumerator(); // returns 2

© C. E. Hughes, UCF Computer Science – 27 – COP 3530 Spring ‘99

Page 28: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Complex Class (also not a base class)//Complex as Cartesianpublic class Complex extends Number {

private double real, imaginary; // hidden detailspublic Complex(double r, double i) { // construct new Complex object

real = r; imaginary = i; }public double getReal() { } // in protocolpublic double getImaginary() { } // in protocolpublic double getRadius() { } // in protocolpublic double getAngle() { } // in protocol

… … }//Complex as Polarpublic class Complex extends Number {

private radius, angle; // hidden detailspublic Complex(double r, double i) { // construct new Complex object

radius = ?? // need to compute from r and i.angle = ?? // trig needed here.

}// BAD NEWS – duplicate signature and a contract breakerpublic Complex(double r, double a) { // construct new Complex object

radius = r; angle = a; }public double getReal() { } // in protocolpublic double getImaginary() { } // in protocolpublic double getRadius() { } // in protocolpublic double getAngle() { } // in protocol

… … }

© C. E. Hughes, UCF Computer Science – 28 – COP 3530 Spring ‘99

Page 29: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Encapsulation and the Complex Class

Cartesian and Polar Representations

© C. E. Hughes, UCF Computer Science – 29 – COP 3530 Spring ‘99

Page 30: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Encapsulation and the Complex Class

Change from Cartesian coordinates ( x, y )CLASS Complex IS

variablesrealPartimagPart

methodssetRealImaginary(r,i)getReal()getImaginary()

to polar ( r, q ) and user need never know.CLASS Complex IS

variablesradiustheta

methodssetRealImaginary(r,i)getReal()getImaginary()

© C. E. Hughes, UCF Computer Science – 30 – COP 3530 Spring ‘99

Page 31: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Polymorphism – What it is

Different classes respond to the same message by performing the same action.

• All sorts of objects respond to add and print

However, a fraction’s add and print codes are different than those for a point in Cartesian space.

And both of those would differ from the add and print codes for a complex number.

© C. E. Hughes, UCF Computer Science – 31 – COP 3530 Spring ‘99

Page 32: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Dynamic Binding – What it is

The method associated with a message to an object is based on the object’s class.

The class of an object cannot always be determined at compile-time.

Dynamic binding means that we select the method at run-time.

Polymorphism gives rise to the need for dynamic binding.

Use of Dynamic BindingDisplay a UI component.Components may be containers of components. Each component of a container may be of a different type.

Moreover, the types of components may not be known until run time, and may vary during run time.If wait until run-time, can select correct display method based on component type.

© C. E. Hughes, UCF Computer Science – 32 – COP 3530 Spring ‘99

Page 33: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Object-Based versus Object-Oriented

Object-Based Provides

classesencapsulationbinding of operations to typespolymorphism

Lacksinheritancebenefits from dynamic binding

Object-OrientedProvides all that is Object-Based plusinheritance

extends in Javadynamic binding

Some OO languages support both static and dynamic bindingneed dynamic for effective use of collections of related objects

© C. E. Hughes, UCF Computer Science – 33 – COP 3530 Spring ‘99

Page 34: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Inheritance

Supports reuse

Helps to maintain consistency

In Java, we say a class extends another

It is important that a subclass not restrict its parent’s protocolthis can break ability to have collections of objects sharing a common base protocol

Inheritance as an “is-a” relation

Subclasses are “heirs” of their superclasses

Abstract vs Concrete ClassesAbstract classes are internal nodes in hierarchyConcrete classes are commonly leaf nodesAbstract classes exist for their heirsConcrete classes exist for their clients

Java uses single inheritanceaugmented with interfaces

© C. E. Hughes, UCF Computer Science – 34 – COP 3530 Spring ‘99

Page 35: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

A Collection Hierarchy

ObjectAbstractCollection

AbstractSetHashSetTreeSet implements SortedSet

AbstractMapHashMapTreeMap

AbstractListAbstractSequentialListArrayListVector

Stack // BARF!!!!!!!!

The Placement of Stack is an historical aberration.There are also classes Dictionary and its descendant HashTable which are reimplemented as Maps.

© C. E. Hughes, UCF Computer Science – 35 – COP 3530 Spring ‘99

Page 36: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Quality Characteristics of Hierarchies

Deep is better than wide

Extend, never restrict

When overriding, keep basic semantics the same

Collection and Exceptions Hierarchy are Examples of Good Hierarchies

© C. E. Hughes, UCF Computer Science – 36 – COP 3530 Spring ‘99

Page 37: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Inheritance Pluses and Minuses

PlusesReusability (design and code)Protocol Consistency (e.g., in Components)Software ICsRapid PrototypingFrameworks (e.g., model, view, controller)Information HidingLightweight Methods

MinusesSpeed (generally overrated)Premature optimization is deadlySize (if must carry many base classes)Message passing overhead (higher in untyped languages)Complexity (yo-yo problem)

© C. E. Hughes, UCF Computer Science – 37 – COP 3530 Spring ‘99

Page 38: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Protection in Java

Specifier Class Subclass Package WorldPrivate X

Protected X X* XPublic X X X X

Package X X

Parts should be private

Contract services (protocol) are public

Protected means services are for your heirs

Package is a "friends" relationship

© C. E. Hughes, UCF Computer Science – 38 – COP 3530 Spring ‘99

Page 39: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Object Handles versus PointersIn OO languages like Java, an object is always referenced by variables that contain handles to the object.

To get at the object, we send messages to it through one of its handles.

We never explicitly dereference the handle as we do with pointers.

To change what a variable references, we assign it to reference another object, either by copying an existing handle or by dynamically creating a new object and copying its handle.

When an object is passed to a method, a handle is replicated. This greatly affects the way we write code to achieve the effects of the many algorithms.

© C. E. Hughes, UCF Computer Science – 39 – COP 3530 Spring ‘99

Page 40: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

WEEK # 3

1. Java syntax for iteration, conditionals and arrays.

2. Tree Data model. Basic tree terminology. Tree traversal algorithms – breadth versus depth first; inorder, preorder and postorder. Binary trees. Binary tree traversal algorithms.

3. Expression trees. Evaluation. Code generation. Comments on optimizations that are based on tree transformations.

4. Discuss Tree data model use for abstract implementations of dictionaries. Show BST and Trie and review briefly.

5. Priority queue ADT, priority ordered tree (POT) abstract implementation, balanced POT abstract implementation, and heap data structure. Min and max heaps.

6. Heapify and Heap Sort based on Heapify and DeleteMin; Heap Sort in JavaAnalyze heap algorithms for insert, delete, heapify and heap sort

7. Assignment # 2: Do problem 5.9.10 on page 279 in text. Turn in on Tuesday, January 26.

8. Programming Assignment #1: Turn in on Tuesday, February 9. You must send the source code to me by e-mail, and hand in at the end of class a hard copy source, output listings and brief analysis. You must also provide an easy way for me to test your program, including giving me sample data sets. Write a Java, C++, C, or Pascal with Objects program that implements priority ordered queues whose data are Strings. Once you have this working so that you can sort Strings using Priority Ordered Queues, add some instrumenting code that allows you to measure the number of calls to swap for any interval of calls. To do so, add the services zeroSwaps and retnSwaps. The first of these sets the swap counter to 0; the second reports its current value. Each call to swap should increment this counter, and its initial value should be zero. Analyze the results you get for several tests of heapify and heap sort, providing a brief justification of the results you get. Be sure to compare these to the theoretical bounds.Note: I have most of your code here if you use Java or Pascal with Objects. The text has the C code.

© C. E. Hughes, UCF Computer Science – 40 – COP 3530 Spring ‘99

Page 41: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Iteration in Javawhile (conditional_expression)

statement;

do statement;

while (conditional_expression);

for ( init_expr; cond_expr; incr_expr )|statement;

for is very flexible, very useful, and very dangerousfor (index=0; index<MAX; ++index) s; //commonfor (index=0; index=MAX-1; ++index) s; //ERROR use ==, not =

Uses of For Iterationfor (int index=0; index<n; index++) s; // Obvious use with counting iterationsfor (Node current=list; current!=null; current=current.link) s; // Traversing a linked listfor (int count=0, p=list; p!=null; p=p.link, count++); // Counting nodes in a linked list

// above uses , operator to create multi-part expressions

Early Breaks and Continuesbreak; //immediately exit from a control structurecontinue; //immediately start next iterationbreak matches exitif in text

// exitif (condition) if ( condition ) break;

© C. E. Hughes, UCF Computer Science – 41 – COP 3530 Spring ‘99

Page 42: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Conditionals (other than if) in JavaConditional Operator

conditional_expression ? exp1 : exp2;Examples:

max = a > b ? a : b;System.out.println(a > b ? a : b);

Very compact, but can lead to overly cute code

Switchswitch (int_or_char_expression) {

case value1:statement_list1; [break;]

case value2:statement_list2; [break;]

…case valuek:

statement_listk; [break;] [default:

default statement_list;]}

© C. E. Hughes, UCF Computer Science – 42 – COP 3530 Spring ‘99

Page 43: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Arrays in JavaDeclaration

Type[ ] v; // just declarationType[ ] v = new Type[ size ]; // adds definitionType[ ] v = {c1, c2, c3, …, ck}; // size and values

Examples:int[ ] list = { 7, 3, 10, 4, 12 };// list is an object of type int[ ]// list[2] is an int; here it’s value is 10// list.length is 5; all array objects have length field

More About ArraysArrays are objects, so passing an array via its name passes a handle to the array -- we do not make a copy.

The array elements can now be changed, but the array handle cannot be changed.Arrays are 0-based indexed. Thus, an array list of size 3 has elements list[0], list[1], list[2]

for loops are often used to traverse arraysfor (int i=0; i<list.length; ++i) ++list[i]; // incrementfor (int i=0; i<list.length;) ++list[i++]; // too cute for (int i=-1; ++i<list.length;) ++list[i]; // too cute

© C. E. Hughes, UCF Computer Science – 43 – COP 3530 Spring ‘99

Page 44: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Multiple DimensionsCan have arrays of arrays

int[ ] [ ] a = { { 1, 2, 3 }, { 4, 5 } };// this has two rows // -- first has three columns// -- second has two columns

Access bya[0][2] is an int (value is 3)a[1] is an array of ints (value is { 4, 5 })a is an array of length 2

each element is an array of intsa.length is 2; a[0].length is 3; a[1].length is 2

HomogeneityAll elements of an array are of the same type -- or at least of the same super type.If we want to use String values in first row and integer values in second row,

we need to create an array of class Object, and then recast elements.An int is not an Object, so we need to wrap int inside Integer, recast the object as Integer,

and then extract the int value -- aargh!!!Alternatively, we can use Collections, e.g., ArrayList whose elements are themselves ArrayLists

An ArrayList is a class whose instances store elements in an indexed fashion.We can also use Vector which is similar, but a bit old in the Java world

© C. E. Hughes, UCF Computer Science – 44 – COP 3530 Spring ‘99

Page 45: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Tree Data Model – The Basic Parts

Graph ConceptsNode (point)

denoted by a pointLabeled Node

a value (or name) for each nodeEdge (branch)

denoted by a line between 2 nodesDirected Edge (arc)

an oriented line (from one node to another)orientation can be explicit or implicit

In and Out DegreesPath; Path Length; Cycle (cyclic, acyclic)Connected (path from any node to any other)

© C. E. Hughes, UCF Computer Science – 45 – COP 3530 Spring ‘99

Page 46: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Tree Definitions

Tree Definition # 1 (unoriented)Root (a designated node) with no parentEvery node, except the root, has an edge to its unique parentConnected in that all nodes have a unique path through parents to the root

Tree Definition # 2 (oriented from root to leaves)Root – only node with in-degree of 0Others have in-degree of 1 from their parentsConnected in that all have path from root

Tree Definition # 3Any single node r denotes a tree with r as its rootIf r is a new node and T1, …,Tn are trees with roots r1, …,rn, then T is a tree where T

has root r and r has an edge to each of r1, …,rn. Assumes that r and all nodes of T1,…,Tn are distinct.

© C. E. Hughes, UCF Computer Science – 46 – COP 3530 Spring ‘99

Page 47: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Tree Notation

RootParentChildAncestor (proper)Descendant (proper)SiblingLeaf (no children, out-degree of 0)Frontier (catenation of labels of leaves from left to right)Interior (at least one child, out-degree > 0)Subtree (a node, its proper descendants with arcs)Path – Unique heritage between node and rootDepth or level – Path length between node and rootHeight – longest path length between node and a leafHeight of tree is height of rootSome trees are Oriented

(parent-child or child-parent)Some trees are OrderedIf Ordered then have 1st, 2nd, etc. Child

lower numbered siblings are to left of higherIf Binary then Left and Right Child

Also left and right subtrees (which are themselves binary)On ordered, if a and y are siblings and x is to left of y, then all of x’s descendants are to left of all of y’s descendants

© C. E. Hughes, UCF Computer Science – 47 – COP 3530 Spring ‘99

Page 48: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Binary Tree Recursive Definition

A binary tree iseither empty // Note this is an extension to tree notionor consists of a node (root) with

data (of some appropriate type)a left child (which is also a binary tree)a right child (which is also a binary tree)

Above lends itself to recursive divide and conquer algorithms and inductive proofs of correctness and complexity.

Algorithm formif (isEmpty()) { easy case }else { process node, left child and right child in appropriate order }

Proof form (S(j) is property for binary trees with depth j)Basis: Show S(0). This is the statement of property for empty treeInductive hypothesis: Assume S(k) true, 0 k < NInductive step: Prove S(N). Induction depends on fact that children of root are binary trees with depth less than N.

© C. E. Hughes, UCF Computer Science – 48 – COP 3530 Spring ‘99

Page 49: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Data Structure for Trees – the Obvious

class Tree {private Object data;private Tree[] children;final static private int MaxChildren = 10; // or whatever

public Tree (Object data) { this.data = data;this.children = new Tree[MaxChildren];

}}

Given a node, we can access the i-th child in O(1) time.Space utilization is quite poor as lots of children are possible, but rare. Can use Vector with improve this.

This is usually a good structure for binary trees (ones for which every node has at most 2 children. In this case, we use fields left and right, rather than an array.Expression trees are commonly represented this way since unary operators are rare and binary are common.

CHALLENGE: For an expression with n binary operators and no unary operators, how many child pointers are NIL; how many are non-NIL?

© C. E. Hughes, UCF Computer Science – 49 – COP 3530 Spring ‘99

Page 50: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Data Structure for Trees – Less Obvious

An alternative data structure is

Leftmost-child – Right Sibling Pointers

class Tree {private Object data;private Tree leftChild;private Tree rightSibling;

}

This is just a reorientation of the tree.

© C. E. Hughes, UCF Computer Science – 50 – COP 3530 Spring ‘99

Page 51: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Data Structure for Trees – Unobvious

Another alternative data structure is

Parent Pointers

class Tree {private Object data;private Tree parent;

}

This changes the direction of reference so that a child points to its parent, but parents have no handle on their children. This is very space efficient since the only NIL pointer is in the root, and there is only one pointer per node. This data structure does not support many of the useful ways we traverse trees, but it is superb for certain applications, e.g., trees representing equivalence classes.

A fourth data structure is parent, firstChild and nextSibling parts.This hedges nearly all bets.

© C. E. Hughes, UCF Computer Science – 51 – COP 3530 Spring ‘99

Page 52: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Binary Tree

class BinaryTree {Object data;BinaryTree left = null;BinaryTree right = null;BinaryTree parent = null; // might not have this

BinaryTree (Object data, BinaryTree) { this.data = data; this.parent = parent;}

BinaryTree (Object data) { this.data = data; this.parent = null;}

}

© C. E. Hughes, UCF Computer Science – 52 – COP 3530 Spring ‘99

Page 53: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Common Tree Traversal Template

public void traverse ( ) {action0;Tree child = leftChild;if (child == null) return;child.traverse();action1;child = child.rightSibling;if (child == null) return;child.traverse();action2;child = child.rightSibling;…if (child == null) return;child.traverse();actionK;}

} // traverse

© C. E. Hughes, UCF Computer Science – 53 – COP 3530 Spring ‘99

Page 54: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Binary Expression Trees

Labels are operators or atomic operands

All operators are interior nodes. Common labels are +, –, *, / and unary –

All atomic operands are leaves

Inductive Definition

A single atomic operand is represented by a single node binary tree

If E and F are expressions represented by binary trees S and T, respectively, and op is a binary operator, then (E op F) is represented by the binary tree U consisting a new root labeled op with left child S and right child T.

If E is an expression represented by tree S and op is a prefix unary operator, then (op E) is represented by the binary tree T consisting a new root labeled op with empty left child and right child S.

Example UsesInterpreters and expression treesCompilers and expression treesOptimizing compilers and expression trees

© C. E. Hughes, UCF Computer Science – 54 – COP 3530 Spring ‘99

Page 55: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Preorder Tree Traversal (Print)

public void preorder ( ) {System.out.println(data + " ");Tree child = leftChild;while (child!=null) {

child.preorder();child = child.rightSibling;

}}Assume that we use this on an expression tree with the labels being operators or simple one-letter variable names. The expression (~A - B) * ( C / (D + E) ) is represented as

The preorder would print * – ~ A B / C + D E

© C. E. Hughes, UCF Computer Science – 55 – COP 3530 Spring ‘99

Page 56: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Preorder BinaryTree Traversal (Print)

public void preorder ( ) {System.out.println(data + " ");if (left!=null) left.preorder();if (right!=null) right.preorder();

}

Again, assume that we use this on an expression tree with the labels being operators or simple one-letter variable names. The expression (~A - B) * ( C / (D + E) ) is represented as

The preorder would print * – ~ A B / C + D E

© C. E. Hughes, UCF Computer Science – 56 – COP 3530 Spring ‘99

Page 57: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Postorder BinaryTree Traversal (Print)

public void postorder ( ) {if (left!=null) left.preorder();if (right!=null) right.preorder();System.out.println(data + " ");

}

The expression (~A - B) * ( C / (D + E) )

has postorder A ~ B – C D E + / *

Each of preorder and postorder provides an unambiguous way to denote expressions without parentheses. This is more compact than infix notation and can lead to very efficient expression evaluations.

© C. E. Hughes, UCF Computer Science – 57 – COP 3530 Spring ‘99

Page 58: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Postorder IntExpressionTree Traversal (Evaluation)class IntExpressionTree {

int data;char op; // op or blank if a valueBinaryTree left = null;BinaryTree right = null;…public int eval ( ) { // preorder peek helps; operation is performed postorder

if (op.equals(' ')) return data;else {

switch(op) {case '~' : return –right.eval(); break;case '+' : return left.eval() + right.eval(); break;case '-' : return left.eval() - right.eval(); break;case '*' : return left.eval() * right.eval(); break;case '/' : return left.eval() / right.eval(); break; } } } }

evaluates as –28.

© C. E. Hughes, UCF Computer Science – 58 – COP 3530 Spring ‘99

Page 59: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Inductive Proof of Evaluation

The text refers to “structural induction” as the process of proving a property of trees S(T) by:

BASIS: Show the property S(T) holds for any tree T with just one node.

INDUCTION: Assume T is a tree with a root r and children c1,…, ck, k1. Let T1, …,Tk be the subtrees rooted at c1, …,ck, respectively. The inductive step is to assume S(T1), …,S(Tk) are all true and prove S(T).

This really can be viewed as complete induction, where we are assuming the property for all tree with n or fewer nodes, and proving it for a tree with n+1 nodes.

© C. E. Hughes, UCF Computer Science – 59 – COP 3530 Spring ‘99

Page 60: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Computing the Height of a Tree

class Tree {private Object data;private Tree leftChild;private Tree rightSibling;

public int computeHT( ) {int height = 0;Tree child = leftChild;while (child != null) {

height = Math.max(height, child.computeHT( )+1);child = child.rightSibling;

}return height;

} // computeHT

Inductive Proof of Correctness:

S(t): when the computeHT message is sent to a non-null tree, t, it returns the height of the tree.

BASIS: If t has just one node then t has no children. The while test in computeHT fails on the first try and hence the correct value of 0 is returned.

INDUCTIVE HYPOTHESIS: Assume for arbitrary non-null tree t with children t1, … tk, that S(ti) for all 1ik.

© C. E. Hughes, UCF Computer Science – 60 – COP 3530 Spring ‘99

Page 61: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

More on the Height of a TreeINDUCTIVE STEP: Suppose t is a tree with children t1, … tk, k1. Then t has at least 1 child and, by definition, the height of t is one more than the maximum of the heights of its children. By the inductive hypothesis, computeHT will correctly compute the heights of all of t's children. To see that the correct value is returned for t we need to show that the while loop sets height to one more than the maximum of the heights of t’s children. This requires an embedded inductive proof.

S'(i): After the while loop has completed i iterations, the value of height is 1 more than the largest of the heights of the first i children of t.

BASIS: S'(1). Height is 0 prior to the first execution of the loop. Thus the new value of height is max(0,child.computeHT( )+1), where child is the first child. Clearly this is one more than the height of the first child.

INDUCTIVE HYPOTHESIS: Assume S'(n), n1.

INDUCTIVE STEP: Show S(n+1). If the height of the n+1 -st child is less than or equal to any of the previous, then its height+1 will be less or equal to the value of height and the iteration will make no changes, as required. If, on the other hand, the n+1 -st child has a height greater than any of the preceding children than its height+1 will exceed the current value of height and the max statement will set height to this new value, as required.

RETURNING TO THE STRUCTURAL INDUCTION: Since the while condition and the statement that assigns each successive rightSibling to child ensures that all subtrees are inspected, we can call upon the inner induction to assure that the value of height will be correct for t when we exit the while. The remaining statement just returns the value of height as computed.

© C. E. Hughes, UCF Computer Science – 61 – COP 3530 Spring ‘99

Page 62: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Inorder and Binary Expression Trees

public void inorder ( ) {System.out.println("(");if (left!=null) left.preorder();System.out.println(data);if (right!=null) right.preorder();System.out.println(")");

}

The expression (~A - B) * ( C / (D + E) )

is printed as ( ( ( ~ ( A ) ) – ( B ) ) * ( ( ( C ) / ( ( D ) + ( E ) ) ) ) )The correct fully parenthesized version.

© C. E. Hughes, UCF Computer Science – 62 – COP 3530 Spring ‘99

Page 63: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Code Generation from an Expression Tree (binary ops)class ExpressionTree {

private String label; // use no label as a null tree (leaf has two null children)int height;private ExpressionTree left;private ExpressionTree right;public int computeHT( ) {

if (label == null) height = -1;else height = 1 + Math.max(left.computeHT( ), right.computeHT( )));return height;

} // computeHT

private void genCode(register : integer) {if (codeGenerator.isOperator(label) {

left.genCode(register);right.genCode(rightChild.height);codeGenerator.genOp(label, rightChild.height, register);

} else codeGenerator.genLoad(label, register);} // genCode

public void generateCode() {computeHT(); // set heightsgenCode(height); // start with register height

}

© C. E. Hughes, UCF Computer Science – 63 – COP 3530 Spring ‘99

Page 64: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Code Generator Support Routines

public boolean isOperator(String s){return (s.equal("+")) || (s.equal("-")) || (s.equal("*")) || (s.equal("/"));

}

public void genOp(String s, int r1, int r2) {// generate assembly language opcodesswitch ( s.charAt(0) ) {

case '+' : System.out.print("ADD ");case '-' : System.out.print("SUB ");case '*' : System.out.print("MUL ");case '/' : System.out.print("DIV ");

}// follow opcode by operandsSystem.out.println ("R" + r1 + ", R" + r2);

} // genOp

public void genLoad(String s, int r) {System.out.println ("LOAD " + c + " R" + r);

} // genLoad

© C. E. Hughes, UCF Computer Science – 64 – COP 3530 Spring ‘99

Page 65: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

An Example of Code Generator

+

– B

/

A #7

C

*

D#0

1) LOAD #0, R32) LOAD A, R03) SUB R0, R34) LOAD B, R05) SUB R0, R36) LOAD C, R27) LOAD D, R18) LOAD #7, R09) ADD R0, R110) DIV R1, R211) MULT R2, R3

© C. E. Hughes, UCF Computer Science – 65 – COP 3530 Spring ‘99

Page 66: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Register Usage Optimization

The method computeHT is very closely related to an algorithms used to make efficient use of registers when generating code.

Consider if we have a commutative operator like +. We can compute

exp1 + exp2

as originally specified, or as

exp2 + exp1

The choice can effect the registers needed to complete the computation. If we exceed the number of actual registers, then we have register spill. This is a situation in which we must copy register contents to memory, and then restore these values later.

CHALLENGE: When is to your benefit to take advantage of commutativity? In other words what criteria should be used to reduce register consumption.

© C. E. Hughes, UCF Computer Science – 66 – COP 3530 Spring ‘99

Page 67: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Dictionary ADT

Dictionary: An ADT that maintains a collection of comparable (totally ordered) elements and provides three services:

void insert(Element x) – inserts element x into Dictionary.

void delete(Element x) – deletes element x from Dictionary.

boolean lookup(Element x) – returns true or false depending on whether or not element x is in the Dictionary.

There are several abstract implementations that are useful for trees. Two of these are the Binary Search Tree (BST) and Trie, both based on the tree data model.

BST – A binary tree whose nodes contain words, such thatThe word at each node of the tree is lexically greater than the words at all nodes in its left subtree and lexically less than the words at all nodes in its right subtree.

Trie – A tree representing a set of words, such thatThe root of the tree has a null label.Other nodes are labeled with a letter and a Boolean flag.The set of words represented are those formed by concatenating the labels of all nodes in some path starting at a child of the root and continuing from child to child, stopping at some node whose flag is “true”.

© C. E. Hughes, UCF Computer Science – 67 – COP 3530 Spring ‘99

Page 68: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Dictionaries as BSTs

Consider how we would represent the collection of words in the sentence

"this is the time for tiny things"

BST (here are 2)

this

tiny

timethingsis

the

for

this tiny

time

things

is

thefor

© C. E. Hughes, UCF Computer Science – 68 – COP 3530 Spring ‘99

Page 69: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Dictionaries as Tries

Consider again how we would represent the collection of words in the sentence

"this is the time for tiny things"

Trie (example is a bit of a cheat – in general we need end of word markers.)

© C. E. Hughes, UCF Computer Science – 69 – COP 3530 Spring ‘99

Page 70: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Integer Priority Queue in Java// Heap Implementation of Priority Queuepublic class IntPriorityQueue {

final static private int DefaultCapacity = 32;private int[] heap;private int capacity;private int size = 0;

// Constructorspublic IntPriorityQueue() { this(DefaultCapacity); }public IntPriorityQueue(int capacity) { this.capacity = capacity; heap = new int[capacity]; }

// Priority Queue Basic Servicespublic void removeAll() { size = 0; }

public boolean isEmpty() { return size == 0; }

public boolean isFull() { return size ==capacity; }

public void insert(int value) { if (!isFull()) { heap[size] = value; bubbleUp(size++); }

}

public int deleteMax() {if (!isEmpty()) { int result = heap[0]; swap(0,--size); bubbleDown(0); return result; }

else return Integer.MIN_VALUE;}

© C. E. Hughes, UCF Computer Science – 70 – COP 3530 Spring ‘99

Page 71: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Priority Queue -- Heap Sort// BubbleUp/Down Algorithmsprivate void swap(int i, int j) {

int temp = heap[i]; heap[i] = heap[j]; heap[j] = temp;}private void bubbleUp(int i) {

int parent = (int)((i-1) / 2);if (i>0) if (heap[i]>heap[parent]) { swap(i, parent); bubbleUp(parent);}

}private void bubbleDown(int i) {

int child = 2*i+1;if (child < size-1) if (heap[child+1] > heap[child]) child++;if (child < size) if (heap[i] < heap[child]) { swap(i, child); bubbleDown(child); }

}

// Heap Sortprivate void heapify(int[] a, int n) {

heap = new int[a.length];for (int i = 0; i<n; i++) heap[i] = a[i];size = n;for (int i = (int)((n-1)/2); i>=0; i--) bubbleDown(i);

}

public void heapSort(int[] a, int n) {removeAll();heapify(a, n);int i = n-1;while (! isEmpty()) a[i--] = deleteMax();

}} // end IntProrityQueue

© C. E. Hughes, UCF Computer Science – 71 – COP 3530 Spring ‘99

Page 72: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Heap Sort AnalysisNaive:

Each BubbleDown could require log2N moves, so Heapify takes N log2N

Intelligent:The lower 1/2 of all nodes are never bubbled down.The second 1/4 of all nodes can only bubble down 1 place.The second 1/8 can only go down 2 places.Only one element can bubble down log2N - 1 places.

Analysis:Can show time is

N (1/4 + 2/8 + 3/16 + 4/32 + 5/64 + … + k/2k+1 + …)= N/2 (1/2 + 2/4 + 3/8 + 4/16 + 5/32 + … + k/2k + …)

One way to find closed form is1/2 + 2/4 + 3/8 + 4/16 + 5/32 + … + k/2k + …

= 1/2 + 1/4 + 1/8 + 1/16 + 1/32 + … + 1/2k + …+ 1/4 + 1/8 + 1/16 + 1/32 + … + 1/2k + …+ + 1/8 + 1/16 + 1/32 + … + 1/2k + …+ …

= 1 + 1/2 + 1/4 + 1/8 + 1/16 + 1/32 + … + 1/2k + … = 2so N/2 (1/2 + 2/4 + 3/8 + 4/16 + 5/32 + … + k/2k + …) = N/2 (2)= N

Another way isS = 1/2 + 2/4 + 3/8 + 4/16 + 5/32 + … + k/2k + …S/2 = 1/4 + 2/8 + 3/16 + 4/32 + 5/64 + … + k/2k+1 + …S - S/2 = 1/2 + 1/4 + 1/8 + 1/16 + 1/32 + … + 1/2k + … = 1So S/2 = 1, S = 2, and the sum is once again N/2 (2) = N

© C. E. Hughes, UCF Computer Science – 72 – COP 3530 Spring ‘99

Page 73: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Implementing Priority Queues by Heaps in Pascal with Objectsunit IntPQs;(* Simple case of Integer Priority Queues *)interfaceconst

MaxHeap = 32;type

IntPQueue = objectprocedure Init;function isEmpty : Boolean;function isFull : Boolean;procedure Insert(value : Integer);function DeleteMax : Integer;

privateheap : Array[1..MaxHeap] of Integer;size : Integer;procedure swap(i, j : Integer);procedure bubbleUp(i : Integer);procedure bubbleDown(i : integer);

end;

© C. E. Hughes, UCF Computer Science – 73 – COP 3530 Spring ‘99

Page 74: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Implementing Priority Queues in Pascal with Objects - IIimplementation

procedure IntPQueue.Init;begin

size := 0end;

function IntPQueue.isEmpty : Boolean;begin

isEmpty := size = 0end;

function IntPQueue.isFull : Boolean;begin

isFull := size = MaxHeapend;

procedure IntPQueue.Insert(value : Integer);begin

if not isFull then beginsize := size+1; heap[size] := value; bubbleUp(size)

endend;

function IntPQueue.DeleteMax : Integer;begin

if not isEmpty then beginDeleteMax := heap[1]; swap(1,size); size := size-1; bubbleDown(1)

endend;

© C. E. Hughes, UCF Computer Science – 74 – COP 3530 Spring ‘99

Page 75: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Implementing Priority Queues in Pascal with Objects - IIIprocedure IntPQueue.swap(i, j : Integer);var temp : Integer;begin

temp := heap[i]; heap[i] := heap[j]; heap[j] := tempend;

procedure IntPQueue.bubbleUp(i : Integer);begin

if (i>1) and (heap[i]>heap[i div 2]) then beginswap(i, i div 2);bubbleUp(i div 2)

endend;

procedure IntPQueue.bubbleDown(i : integer);var child : Integer;begin

child := 2*i;if child < size then if heap[child+1] > heap[child] then

child := child + 1;if child <= size then if heap[i] < heap[child] then begin

swap(i, child);bubbleDown(child)

endend;

end.

© C. E. Hughes, UCF Computer Science – 75 – COP 3530 Spring ‘99

Page 76: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

WEEK # 4

1. Parallel Sorting – architectures, algorithms, correctness and analysis for some simple approaches to sorting on parallel machines.2. Collection abstraction3. List data model. Implementing lists by linked list data structures: singly linked versus doubly linked; circular.

Implementing lists by array data structures: use of sentinel to reduce constant factor on linear search; binary search of sorted listcomma separated listwords, sentences, paragraphs, documents as listslength of list (counts repetitions)head (element), tail (list), sublist, empty list, prefix, suffix; element position, first, last, follows, precedesoperations: insert, delete, lookup, sort, merge (two lists to one), spilt (one to two), concatenate, first, last, head, tail, retrieve ith, length, isEmpty, isNotEmptysublist versus subsequence

4. Common Abstract Data Types that can be associated with List data modelStack – clear, isEmpty, isFull, push, popQueue – clear, isEmpty, isFull, enqueue, dequeueDeque – clear, isEmpty, isFull, addFront, removeFront, addRear, removeRear

array and linked list implementations5. Collections of heterogeneous elements in a single stack or queue

Polymorphism in actionNo need to alter clients’ code whether implemented as arrays or linked lists

Encapsulation in action6. Longest Common Subsequence Problem – dynamic programming

Compare naive approach (exponential) versus dynamic programming (quadratic)7. Character strings

Fixed length, linked list, paged allocation, null markersSimple analysis to study trade-offs on characters per cell used in paged method

8. Programming Assignment #2: Turn in on Tuesday, February 23. Write a Java, C++, C, or Pascal with Objects program that reads in two lines of text, computes and prints the LCS of these two documents. (See Section 6.9 in text for algorithms and C code segments.) Actually, your program must have a pure recursive implementation and a dynamic programming implementation, both instrumented to determine complexity. You must send the source code to me by e-mail, and hand in at the end of class a hard copy source, output listings and brief analysis, including the means you used to measure the complexities of running each algorithm. You must also provide an easy way for me to test your program, including giving me sample data sets. Be sure to compare each pair of results to the theoretical bounds.

© C. E. Hughes, UCF Computer Science – 76 – COP 3530 Spring ‘99

Page 77: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Classification of Algorithms

Classification of Algorithms

Divide and Conquer – Searching, Sorting, Multiplication, Parsing

Greedy

Scheduling, cost optimizing while spanning a circuit, bin packing

Dynamic Programming

Divide and conquer to its extreme

© C. E. Hughes, UCF Computer Science – 77 – COP 3530 Spring ‘99

Page 78: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Divide and ConquerThe problem is either small and easy to solve, or it can be naturally broken up into several sub-problems, each of which can be solved by the exact same approach as was applied to the original problem.

When the sub-problems have been solved, it is possible to merge the solutions into one that is correct for the larger original problem.

Thus, we look for three characteristics:(a) Easy to split into sub-problems;(b) Sub-problems are simpler instances of original;(c) Sub-problem results can be easily combined to solve the original problem.

D&C -- Algorithmic Formalgorithm p (s);

if (small (s)) then return easy_attack (s);else {

[ s1, s2 , ... , sk ] = divide (s);return (combine ( p(s1), p(s2), …, p(sk) ) )

}

In some cases, we can divide and immediately reject one sub-problem, pursuing only the other. BST search is such an example.

© C. E. Hughes, UCF Computer Science – 78 – COP 3530 Spring ‘99

Page 79: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

GreedyWant to Max or Min some objective function.

Solution must satisfy some feasibility constraint.

Any solution satisfying constraints is feasible.

A feasible solution that maxes or mins the objective is optimal.

Greedy solutions are often sub-optimal, but are always feasible solutions.

For example, First Fit BinPacking never overfills a trunk, so it always returns a feasible solution. Its solutions are, however, not guaranteed to be optimal.

Greedy -- Algorithmic Formsolution = { };for ( int i = 1 ; i <= numberOfChoices; i++) {

x = select (a); // where select is simpleif (feasible (solution x))

solution = solution x;}return solution;

© C. E. Hughes, UCF Computer Science – 79 – COP 3530 Spring ‘99

Page 80: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Dynamic ProgrammingBased on "Principal of Optimality"

Technique requires that a problem have multiple stages at which decisions are made. We talk about stage i, and indicate that the system is in state i.

The Principal of Optimality says that the choice that is made at this stage is dependent only on state i and not on the policy that was used to make decisions at stages 1 to i-1.

Stated differently – the solution from stage i to n (the last stage) must be optimal in order for 1 to n to be optimal.

© C. E. Hughes, UCF Computer Science – 80 – COP 3530 Spring ‘99

Page 81: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

A Model of Cooperation: Bucket Brigades

…P1 P2 P3 PN

Commonly Called a Systolic Array

• N Processors, Labeled P1 to PN

• Processor Pi is connected to Pi+1, i<N

© C. E. Hughes, UCF Computer Science – 81 – COP 3530 Spring ‘99

Page 82: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Characteristics of Architecture

Fixed Connection Network

• Processors Labeled P1, P2, … , PN

• Each Processor knows its Unique ID

• Local Control

• Local Memory

• Fixed Bi-directional Connections

• SynchronousGlobal Clock Signals Next Phase

© C. E. Hughes, UCF Computer Science – 82 – COP 3530 Spring ‘99

Page 83: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Operations at Each Phase

Each Time the Global Clock Ticks

• Receive Input from Neighbors

• Inspect Local Memory

• Perform Computation

• Generate Output for Neighbors

• Update Local Memory

© C. E. Hughes, UCF Computer Science – 83 – COP 3530 Spring ‘99

Page 84: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

A Sort Algorithm

Odd-Even Transposition on Linear Array

…P1 P2 P3 PN

• The Array is X[1 : N]

• Pi's Local Variable X is X[i]

• Pi's have Local Variables Step and Y

• Step is Initialized to Zero (0) at all Pi

• Compares and Exchanges are DoneAlternately at Odd/Even - Even/Odd Pairs

© C. E. Hughes, UCF Computer Science – 84 – COP 3530 Spring ‘99

Page 85: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Odd-Even Transposition

Algorithmic Description of Parallel Bubble Sort

• At Each Clock Tick and For Each Pi do

Step := Step+1;

if parity(i) = parity(Step) & i < N then

Read from Pi+1 to Y;

X := min(X,Y)

else if i > 1 then

Read from Pi-1 to Y;

X := max(X,Y);

© C. E. Hughes, UCF Computer Science – 85 – COP 3530 Spring ‘99

Page 86: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Example of Parallel Bubble Sort

Sort 4 Numbers 7, 2, 3, 1 on an Array of 4 Processors

1 732

2 317

7 132

2 371

Case of 4, 3, 2, 1 Takes 4 Steps

© C. E. Hughes, UCF Computer Science – 86 – COP 3530 Spring ‘99

Page 87: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Measuring Benefits

How Do We Measure What We Have Gained?

• Let T1(N) be the Best Sequential Algorithm

• Let TP(N) be the Time for Parallel Algorithm (P processors)

• The Speedup SP(N) is T1(N)/TP(N)

• The Cost CP(N) is PTP(N), assuming P processors

• The Work WP(N) is the summation of the number of steps taken by each of the processors. It is often, but not always, the same as Cost.

• The Cost Efficiency CE P(N) (often called efficiency Ep(N)) isSP(N)/P = C1(N) / CP(N) = T1(N) / (PTP(N))

• The Work Efficiency WE P(N) isW1 (N) / WP (N) = T1 (N) / WP (N)

© C. E. Hughes, UCF Computer Science – 87 – COP 3530 Spring ‘99

Page 88: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Napkin Analysis of Parallel Bubble

How'd We Do ? - Well, Not Great !

• T1(N) = N lg N Optimal Sequential

• TN(N) = N Parallel Bubble

• SN(N) = lg N Speedup

• CN(N) = WN(N) = N2 Cost and Work

• EN(N) = lg N / N Cost and Work Efficiency

But Good Relative to Sequential Bubble

SN(N) = N2/N = N ; EN(N) = SN(N) /N = 1 !

© C. E. Hughes, UCF Computer Science – 88 – COP 3530 Spring ‘99

Page 89: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Non-Scalability of Odd-Even Sort

Assume we start with 1 processor sorting 64 values, and then try to scale up by doubling number of values (N), each time we double number of processors (P) in a ring. The cost of the parallel sort requires each processor to sort its share of values (N/P), then do P swaps and merges. Since P processors are busy, the cost is N lg N/P. After the local sort, sets are exchanged, merged, and parts thrown away. The merge costs N/P on each of P processors, for a Cost of N, and P-1 such merges occur, for a total cost of N(P-1). Efficiency is thenE = N lg N / (N lg N/P + N(P-1)) = lg N / (P - 1 + lg N - lgP)First 2 columns double N as P doubles. Second three try to increase N to keep efficiency when P doubles.

N P E N P E64 1 1.0000 64 1 1.0000128 2 1.0000 4096 2 1.0000256 4 0.8889 16777216 4 0.9600512 8 0.6923 2.81475E+14 8 0.9231

1024 16 0.4762 7.92282E+28 16 0.89722048 32 0.2973 6.2771E+57 32 0.88074096 64 0.1739 3.9402E+115 64 0.87078192 128 0.0977 1.5525E+231 128 0.8649

© C. E. Hughes, UCF Computer Science – 89 – COP 3530 Spring ‘99

Page 90: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

ADTs -- Collection Abstraction in Javaclass AbstractCollection extends Object {

public boolean isEmpty( ) { return size( ) == 0; }public abstract int size( );public abstract boolean add(Object o);public abstract boolean remove(Object o);public abstract boolean removeAll() {

Iterator e = iterator();if (o==null) while (e.hasNext()) if (e.next()==null) { e.remove(); return true; }else while (e.hasNext()) if (o.equals(e.next())) { e.remove(); return true; }return false;

}public boolean contains(Object o) {

Iterator e = iterator();if (o==null) while (e.hasNext()) if (e.next()==null) return true;else while (e.hasNext()) if (o.equals(e.next())) return true;return false;

}public abstract Iterator iterator( );

}

This is actually more abstract than an ADT since we cannot even define the semantics for the operation add, because we don't know if duplicates are allowed.

This could be the root of a subtree of collection-like classes in a well-constructed class hierarchy.

© C. E. Hughes, UCF Computer Science – 90 – COP 3530 Spring ‘99

Page 91: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Linked List of Object Values in Java -- NOT Java's LinkedList// A ListNode class might be declared bypublic class ListNode {

private Object data ;private ListNode next;public ListNode(Object value) {data = value; next = null;}public Object getData() { return data; }public void setData(Object value) {data = value;}public ListNode getNext() { return next; }public void setNext(ListNode node) {next = node;}

}

We can declare and initialize the list headListNode listHead = null; // actually all object handles start with null

Dynamic Creation of ObjectsTo create a new nodeListNode node = new ListNode(anObject); //

To add a new node to front of listnode.setNext(listHead);listHead = node;

To remove first element in listif (listHead != null)

listHead = listHead.getNext();

© C. E. Hughes, UCF Computer Science – 91 – COP 3530 Spring ‘99

Page 92: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Traversing Linked List in Java// Traversal -- print list in Java

public void printList(ListNode current) {if (current != null) {

System.out.println(current.getData().toString()); // all objects understand "toString()"printList(current.getNext());

}}

// to start this, we just call with head of listprintList(listHead);

Traversal -- search list in Javapublic void searchList(ListNode current, Object target) {

if (current == null)System.out.println("The value " + target.toString() + " is not in the list.");

else if (current.data.equals(target)) // == means is same object -- we are less picky System.out.println("The value " + target.toString() + " is in the list.");

elsesearchList(current.getNext(), target.toString());

}

// to start this, we just call with head of list and target, e.g.,searchList(listHead, anObject);

© C. E. Hughes, UCF Computer Science – 92 – COP 3530 Spring ‘99

Page 93: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Removing Item from Linked List in JavaTraversal -- remove in Java -- Challenge!!!

public boolean removeFromList (ListNode current, Object target) {if (current == null) then

return false;else if (current.getData().equals(target)) {

// oh no, what to do here????? -- we need the previous ListNodereturn true; }

elsereturn removeFromList(current.getNext(), target);

}

// to start this, we just call with head of list and target, e.g.,if (removeFromList(listHead, anObject)) print(“Got it!!”);

Traversal -- remove in Java -- Challenge!!!public boolean removeFromList (ListNode current, ListNode old, Object target) {

if (current == null)return false;

else if (current.getData().equals(target)) {if (old == null) listHead = current.getNext();else old.setNext(current.getNext());return true; }

elsereturn removeFromList(current.getNext(), current, target);

}

// to start this, we just call with head of list, null and target, e.g.,if (removeFromList(listHead, null, anObject)) print(“Got it!!”);

© C. E. Hughes, UCF Computer Science – 93 – COP 3530 Spring ‘99

Page 94: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

ADTs -- Stack, Queue, PriorityQueue Protocols in Java FormStack ADT

public Stack( )public boolean isEmpty( )public int size( )public boolean add(Object data)public boolean remove( )public Object peek( )public Iterator iterator( )

Queue ADTpublic Queue( )public boolean isEmpty( )public int size( )public void add(Object data)public Object remove( )public Object peek( )public Iterator iterator( )

Priority Queue (max) ADTpublic PriorityQueue( )public boolean isEmpty( )public int size( )public void add(Comparable data) // Comparables are objects that have a natural (total) orderpublic Comparable deleteMax( )public Comparable peekMax( )public Iterator iterator( )

© C. E. Hughes, UCF Computer Science – 94 – COP 3530 Spring ‘99

Page 95: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

ADTs -- Bag Protocol in Java Form

Bag ADTpublic boolean isEmpty( )public int size( )public boolean add(Object data)public boolean remove(Object data)public boolean removeAllInstances(Object data)public int countInstancesOf(Object data)public boolean contains(Object data)public Iterator iterator( )

© C. E. Hughes, UCF Computer Science – 95 – COP 3530 Spring ‘99

Page 96: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Set and List Model Protocols in Java

class AbstractSet extends AbstractCollection implements Setpublic boolean isEmpty( )public int size( )public boolean add(Object data)public boolean remove(Object data)public boolean contains(Object data)public Iterator iterator( )

class AbstractList extends AbstractCollection implements Listpublic boolean isEmpty( )public int size( )public boolean add(Object data)public void add(int index, Object element) public boolean remove(Object data)public boolean remove(int index)public boolean contains(Object data)public Object get(int index)public int indexOf(Object data)public Iterator iterator( )

© C. E. Hughes, UCF Computer Science – 96 – COP 3530 Spring ‘99

Page 97: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Simple Linked List Implementation of Queue Linked new to old, point to newest

Insert is O(1); Remove is O(N)

Linked old to new, point to oldest

Insert is O(N); Remove is O(1)

Linked new to old, newest and oldest pointers

Insert is O(1); Remove is still O(N) !

Linked old to new, newest and oldest pointers

Insert is O(1); Remove is O(1)

The fourth case is interesting since it’s optimal. The basic idea is that with old to new, we are matching the operations since we increase on the new, so previous new points to newest. In the case of remove, we need to set the end to the one that the current oldest points at. That’s easy. The only potential land mines are the extreme conditions – empty and overflow.

© C. E. Hughes, UCF Computer Science – 97 – COP 3530 Spring ‘99

Page 98: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Circular Linked List Implementation of Queue Circularly Linked new to old, point to newest

Insert is O(1) !; Remove is O(N) !

Circularly Linked new to old, point to oldest

Insert is O(1); Remove is O(N) !

Circularly Linked old to new, point to newest

Insert is O(1); Remove is O(1)

Circularly Linked old to new, point to oldest

Insert is O(1); Remove is O(1)

© C. E. Hughes, UCF Computer Science – 98 – COP 3530 Spring ‘99

Page 99: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

The Actual Linked List Entry in Java is Doubly Linkedprivate static class Entry { // embedded in LinkedList class

Object element;Entry next;Entry previous;

Entry(Object element, Entry next, Entry previous) {this.element = element;this.next = next;this.previous = previous;

}}

Here's Use of This in Java's Linked List Classpublic boolean remove(Object o) {

if (o==null) {for (Entry e = header.next; e != header; e = e.next) {

if (e.element==null) { remove(e); return true; }}

} else {for (Entry e = header.next; e != header; e = e.next) {

if (o.equals(e.element)) { remove(e); return true; }}

}return false;

}

© C. E. Hughes, UCF Computer Science – 99 – COP 3530 Spring ‘99

Page 100: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Circular Array Representation of Dequepublic class Deque {

final private int MaxDeque = 10;private int left=0, right=0, size=0, capacity; private Object contents[ ];

public Deque() { this(MaxDeque); }public Deque(int max) { capacity = max; contents = new Object[max]; }

public boolean isEmpty() { return size == 0; }public boolean isFull() { return size>=capacity; }

public boolean addLeft(Object item) {if (!isFull()) {

contents[left] = item;left = (left-1) % capacity; size++;return true;

} else return false;

}

public boolean addRight(Object item) {if (!isFull()) {

right = (right+1) % capacity; size++;contents[right] = item;return true;

} else return false;

}

© C. E. Hughes, UCF Computer Science – 100 – COP 3530 Spring ‘99

Page 101: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Implementation of Deque Remove Operationspublic Object removeRight() {

if (!isEmpty()) {Object result = contents[right];right = (right-1) % capacity; size--;return result;

} else return null;

}

public Object removeLeft() {if (!isEmpty()) {

left = (left+1) % capacity; size--;return contents[left];

} else return null;

}} // End of Deque

Testing Overflow and UnderflowIn Deque class we kept track of size, using it to detect overflow and underflow.Consider how we might detect these situations using only the values of left and right.Clearly, when we start, the Deque is empty. So can we use left==right as condition for underflow?But, if we were to do capacity addRight’s, then we would also get left == right!!!Without knowing the size, we cannot differentiate overflow from underflow.

© C. E. Hughes, UCF Computer Science – 101 – COP 3530 Spring ‘99

Page 102: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Longest Common Subsequence

A sublist refers to a subsequence that was contiguous in the original list. The LCS problem is not about sublists, it's about subsequences, which are sequences formed by crossing out zero of more of the items in the original list. What remains is not necessarily a contiguous piece of the original list.

The longest common subsequence of two lists is not necessarily unique.

An obvious approach to finding subsequences is a recursive one that essentially tries everything, taking no advantage of any information gleaned from other inspections of the lists.

Dynamic programming approach uses fact that we can build optimal solutions from ones that are optimal on subproblems. When we are working with pairs of lists, this is often done by building a table, where the i,j position in the table relates to the i-th element in one list and the j-th element in the other list.

© C. E. Hughes, UCF Computer Science – 102 – COP 3530 Spring ‘99

Page 103: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

LCS Table Building

for (j=0; j<=n; j++)lcsTable[0][j] = 0;

for (i = 1; i<=m; i++) {lcsTable[i][0] = 0;for (j=1; j<=n; j++)

if (a[i] != b[j])lcsTable[i][j] = Math.max(lcsTable[i-1][j], lcstable[i][j-1]);

elselcsTable[i][j] = lcsTable[i-1][j-1] + 1;

}

© C. E. Hughes, UCF Computer Science – 103 – COP 3530 Spring ‘99

Page 104: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

LCS Table Template

c 6 0a 5 0b 4 0a 3 0b 2 0c 1 0

0 0 0 0 0 0 0 0 00 1 2 3 4 5 6 7

a b c a b b a

Strings are:a b c a b b a

c b a b a c

© C. E. Hughes, UCF Computer Science – 104 – COP 3530 Spring ‘99

Page 105: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

LCS Table Filled and Followed

c 6 0 1 2 3 3 3 3 4a 5 0 1 2 2 3 3 3 4b 4 0 1 2 2 2 3 3 3a 3 0 1 1 1 2 2 2 3b 2 0 0 1 1 1 2 2 2c 1 0 0 0 1 1 1 1 1

0 0 0 0 0 0 0 0 00 1 2 3 4 5 6 7

a b c a b b a

Strings are:a b c a b b a

c b a b a c

LCS is c b b a

© C. E. Hughes, UCF Computer Science – 105 – COP 3530 Spring ‘99

Page 106: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

LCS Table Building

c 6 0 1 2 3 3 3 3 4a 5 0 1 2 2 3 3 3 4b 4 0 1 2 2 2 3 3 3a 3 0 1 1 1 2 2 2 3b 2 0 0 1 1 1 2 2 2c 1 0 0 0 1 1 1 1 1

0 0 0 0 0 0 0 0 00 1 2 3 4 5 6 7

a b c a b b a

Strings are:a b c a b b a

c b a b a c

LCS is b a b a

© C. E. Hughes, UCF Computer Science – 106 – COP 3530 Spring ‘99

Page 107: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

A Better Quick Sort/** * Sorts the specified sub-array of integers into ascending order. */private static void sort1(int x[], int off, int len) {

// Insertion sort on smallest arraysif (len < 7) { for (int i=off; i<len+off; i++)

for (int j=i; j>off && x[j-1]>x[j]; j--) swap(x, j, j-1);

return;}

// Choose a partition element, vint m = off + len/2; // Small arrays, middle elementif (len > 7) { int l = off; int n = off + len - 1; if (len > 40) { // Big arrays, pseudomedian of 9

int s = len/8;l = med3(x, l, l+s, l+2*s);m = med3(x, m-s, m, m+s);n = med3(x, n-2*s, n-s, n);

} m = med3(x, l, m, n); // Mid-size, med of 3}int v = x[m];

© C. E. Hughes, UCF Computer Science – 107 – COP 3530 Spring ‘99

Page 108: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

A Better Quick Sort - 2// Establish Invariant: v* (<v)* (>v)* v*int a = off, b = a, c = off + len - 1, d = c;while(true) { while (b <= c && x[b] <= v) {

if (x[b] == v) swap(x, a++, b);b++;

} while (c >= b && x[c] >= v) {

if (x[c] == v) swap(x, c, d--);c--;

} if (b > c)

break; swap(x, b++, c--);}

// Swap partition elements back to middleint s, n = off + len;s = Math.min(a-off, b-a ); vecswap(x, off, b-s, s);s = Math.min(d-c, n-d-1); vecswap(x, b, n-s, s);

// Recursively sort non-partition-elementsif ((s = b-a) > 1) sort1(x, off, s);if ((s = d-c) > 1) sort1(x, n-s, s);

}

© C. E. Hughes, UCF Computer Science – 108 – COP 3530 Spring ‘99

Page 109: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

A Better Quick Sort - 3 /** * Swaps x[a] with x[b]. */ private static void swap(int x[], int a, int b) {

int t = x[a];x[a] = x[b];x[b] = t;

}

/** * Swaps x[a .. (a+n-1)] with x[b .. (b+n-1)]. */ private static void vecswap(int x[], int a, int b, int n) {

for (int i=0; i<n; i++, a++, b++) swap(x, a, b);

}

/** * Returns the index of the median of the three indexed integers. */ private static int med3(int x[], int a, int b, int c) {

return (x[a] < x[b] ?(x[b] < x[c] ? b : x[a] < x[c] ? c : a) :(x[b] > x[c] ? b : x[a] > x[c] ? c : a));

}

© C. E. Hughes, UCF Computer Science – 109 – COP 3530 Spring ‘99

Page 110: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

WEEK # 5

1. Review for Quiz # 1 to be given on Thursday, February 4.

2. Do several more inductive proofs.

3. Discuss in a detailed review BPOT and Heap.Do some analyses again.

4. Review LCS.

5. Go over assignment # 1, diagnostic test and sample quiz.

6. Quiz # 1

© C. E. Hughes, UCF Computer Science – 110 – COP 3530 Spring ‘99

Page 111: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Quiz#1 Topics and Promises COP3530, Spring 1999Topics:1. Abstract data type (ADT) consists of an encapsulated state and a set of behaviors.

An abstract implementation is a data model specifying some abstract organization of data (an ADT’s state), e.g., a list, a binary tree, a binary search tree, set, etc.A data structure is the way we represent an abstract implementation. For example a list can be represented by an array of data items or by a linked list of data items. In Java, a behavior is invoked by a message. Methods are used to provide the behaviors associated with messages.

2. Order analysis –solving recurrence equations3. OOP concepts and their implementations in Java. Classes, encapsulation, hierarchies, polymorphism, static and dynamic binding.4. Tree data model – terminology, various data structures, traversals, binary trees.

Tree data model use for abstract implementations of dictionaries. Binary Search Tree and Trie abstract implementations. priority queue ADT, priority ordered tree (POT) abstract implementation, balanced POT abstract implementation, and heap data structure. heapify, heapSort, bubbleUp and bubbleDown – code and analysis. Expression trees – height, evaluation, code generation

5. Parallel Sorting – array architecture, even/odd exchange algorithm, analysis.6. List data model. Sublists versus subsequences. operations: insert, delete, lookup, sort, merge (two lists to one), split (one to two),

concatenate, first, last, head, tail, retrieve ith, length, isEmpty, isNotEmpty7. Abstract Data Types based on List data model

Stack – clear, isEmpty, isFull, push, popQueue– clear, isEmpty, isFull, enqueue, dequeueDeque – clear, isEmpty, isFull, addLeft, addRight, removeLeft, removeRight

array and linked list implementations8. Longest Common Subsequence Problem – dynamic programming

Compare naive approach (exponential) versus dynamic programming (quadratic)

© C. E. Hughes, UCF Computer Science – 111 – COP 3530 Spring ‘99

Page 112: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Promises:

1. Short questions/answers on ADT versus abstract implementation versus data structure plus algorithm.

2. OOP question, perhaps on dynamic binding or inheritance or encapsulation.

3. A recurrence equation to solve using inductive reasoning and then a short inductive proof to verify your assertion

4. Even-Odd parallel sorting question.

5. Several algorithmic questions about trees, tries, BSTs, heaps

6. Several algorithmic questions about lists, stacks, queues, deques.

7. An LCS question

8. I will not have you write any long code sequences

9. The assignments, diagnostic test and example questions are good models to use when you prepare for this quiz.

© C. E. Hughes, UCF Computer Science – 112 – COP 3530 Spring ‘99

Page 113: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Key for Assignment # 1 -- 13.5.5 Suppose f(n) is O(g(n)), show that max(f(n),g(n)) is O(g(n)).

By definition of big –Oh notation, c0>0, n0>0 such that, nn0, f(n)c0g(n)To show that max(f(n),g(n)) is O(g(n)), we must demonstrate the existence of c1>0, n1>0, such thatnn1, max(f(n),g(n))c1g(n)Define c1 = max(1,c0) and n1 = n0. and let n be an arbitrary integer n1.

max(f(n),g(n)) is either f(n) or g(n). But, f(n)c0g(n)c1g(n), and g(n)1g(n)c1g(n), since c1 = max(1,c0).

Thus, we have shown that max(f(n),g(n)) is O(g(n)), as was desired.

3.8.1 Show that We can break this summation into two parts, the first containing only the term i, and the second containing n(n+1)/2. The first summation is well known to be n(n+1)/2. Since the second term is independent of the summation index, its value is just n times the term, which is n2(n+1)/2. Adding and expanding these terms we get (n3+ n2+n)/2 as was desired.

© C. E. Hughes, UCF Computer Science – 113 – COP 3530 Spring ‘99

Page 114: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Key for Assignment # 1 – 23.11.1 Let T(N) = T(N–1) + g(N), for N>1. Prove by induction that, if 1 i < N, then

T(N) = T(N-i) +

g(N j )j 0

i 1

We use the notation S(i), 1 i < N, to denote the above assertion.Basis: S(1) –

T(N) = T(N-1) + g(N) by definition

= T(N-1) +

g(N j )j 0

0 by definition of summation

= T(N-i) +

g(N j )j 0

i 1 for i=1

IH: Assume S(i) for some i, 1 i < N, that is, T(N) = T(N-i) +

g(N j )j 0

i 1

IS: Prove S(i+1), 1 i < N, given S(i) and definition of T(N).

T(N) = T(N-i) +

g(N j )j 0

i 1 by induction hypothesis, and

T(N-i) = T(N-i-1) + g(N-i) by the definition of T. Combining these,

T(N) = T(N-i-1) + g(N-i) +

g(N j )j 0

i 1

= T(N-(i+1)) +

g(N j )j 0

i

and this is just S(i+1), as was desired.

© C. E. Hughes, UCF Computer Science – 114 – COP 3530 Spring ‘99

Page 115: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Key for Assignment # 1 – 33.11.3 For all of these c=1, d=2.

a.) g(n) = n2. k=2, so c=1 < 4 = dk, and thus line 4 of table applies. O(nk) = O(n2)b.) g(n) = 2n. k=1, so c=1 < 2 = dk, and thus line 4 of table applies. O(nk) = O(n)c.) g(n) = 10. k=0, so c=1 = 1 = dk, and thus line 5 of table applies. O(nk lg n) = O(lg n)d.) g(n) = n lg n. Assume n is a power of 2.

T(2k) = 2k (k) + 2k-1 (k-1) + … + 2 (1) + 1 (0) + a = a +

S = = k 2k + (k-1) 2k-1 + (k-2) 2k-2 + … + 1 21

2S = = k 2k+1 + (k-1) 2k + (k-2) 2k-1 + … + 1 22 2S – S = k 2k+1 - 2k - 2k-1 - … - 1 21 = k 2k+1 - 2k+1 - + 2 = (k-1) 2k+1 + 2Thus, T(2k) = a + (k-1) 2k+1 + 2 The above still involves some magic, or shall we say unsubstantiated reasoning. We can now either prove that the expansion is correct, or prove the result is correct. We will do the latter.S(i) is the statement that T(2i) = a+ (i-1) 2i+1 + 2Basis: S(0). a + (k-1) 2k+1 + 2 = a+ (-1) 21 + 2 = a+ 2 – 2 = a = T(1)IH: Assume S[i] for all i<k, for some k>0. That is, T(2i) = a + (i-1) 2i+1 + 2 for all i<k.IS: Prove S[k].

T(2k) = T(2k-1) + k 2k, by definition of T.= a + (k-2) 2k + 2 + k 2k, by inductive hypothesis.= a + (2k-2) 2k + 2, by regrouping.= a + (k – 1) 2k+1 + 2, by refactoring.

But, this is S(k), which is what we desired.The consequence of this is that T(n) = a + (lg n – 1) n + 2 = O(n lg n), and this is a tight bound.

e.) g(n) = 2n.T(n) = 2n + 2n/2 + 2n/4 … + 21 + aClearly T(n) > 2n, so O(2n) is a lower bound.We claim that T(n) is less than a + 2n+1, thus showing O(2n) is an upper bound.This combination shows that O(2n) is a tight bound.Now to deal with our claim, we let S(i) be the statement that T(2i) < a + .Basis: S(0). a + 21 > a = T(1)IH: Assume S[i] for all i<k, for some k>0. That is, T(2i) < a + for all i<k.IS: Prove S[k]. Let n=2k. By definition, T(n) = T(n/2)+2n. Note, T(n/2) is T(2k-1).

But then by IH, T(n) < a + 2n+2n. = a + 2n+1. But this was what we needed to prove.

© C. E. Hughes, UCF Computer Science – 115 – COP 3530 Spring ‘99

Page 116: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Key for Assignment # 1 – 43.11.9

a.) c=3, d=2, k=2, so c=3 < 4 = dk, and thus line 4 of table applies. O(nk) = O(n2)b.) c=10, d=3, k=2, so c=10 > 9 = dk, and thus line 3 of table applies. O(nlog d c) = O(nlog 3 10) n2.1

c.) c=16, d=4, k=2, so c=16 = 16 = dk, and thus line 5 of table applies. O(nk lg n) = O(n2lg n)

3.11.10 One easy approach is to restate the definition of T on inputs of powers of 2.

T(20) = 1, T(2k) = 32kT(2k-1), for k>0.So, for k>1,

T(2k) = 32k32k-1T(2k-2) = 32k+2k-1T(2k-2)

= 32k+2k-1+…+21T(20) = 32k+1-2 by prior analysis of sums of powers of 2For N = 2k, can restate as T(N) = 32n–2We must now provide an inductive proof of the above.

S[i] is the statement that T(2i) = 32i+1-2

Basis: S[0] is the statement that T(20) = T(1) = 320+1-2 = 30 = 1.But, T(20) = 1 by definition, and so S[0] is true.

IH: Assume S[i] for all i<k, for some k>0. That is, T(2i) = 32i+1-2 for all i<k.IS: Prove S[k].

T(2k) = 32kT(2k-1), by definition of T.

= 32k32k-1+1-2, by IH

= 32k+2k-2, by simple algebra

= 32k+1-2, by more simple algebraBut, this is exactly S[k], and the hypothesis is verified.

© C. E. Hughes, UCF Computer Science – 116 – COP 3530 Spring ‘99

Page 117: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

COP 3530 Spring 1999 Diagnostic Exam#1 Key Name:_____________________

1. The algorithmic (programming) techniques of recursion and iteration can be related to the mathematical proof technique of induction in a manner that allows inductive proofs of correctness and run-time complexity. Show this relationship by proving that the first of the following two code segments correctly computes N2, for N0, and that the second has run time complexity 2N–1, N1. In this latter case, we base complexity on the number of recursive calls made.function sq(N : integer) : integer;begin

if N<=0 then sq := 0else sq := 2*N – 1 + sq(N-1)

end; { sq }HINT: Prove S(N): sq(N) = N2, N0Basis: S(0): sq(0) = 0 by definition of function and fact that 0 <= 0. But, 02 = 0 and thus sq(0) = 02 IH: Assume, for some N>0, that S(k), for all k <N. IS: Show S(N). sq(N) = 2*N – 1 + sq(N – 1) by definition of function and fact that N>0.But, sq(N-1) = (N – 1)2 by inductive hypothesis. Hence sq(N)= 2* N – 1 + N2 – 2*N + 1 = N2

procedure Move (n:integer; X, Y, Z:char);begin

if n = 1 then writeln('Move ', X, ' to ', Y)else begin

Move (n-1, X, Z, Y); writeln('Move ', X, ' to ', Y);Move (n-1, Z, Y, X)

endend; { Move }HINT: Prove S(N): T(N) = 2N–1, N1, where T(1) = 1; T(N) = 2*T(N–1) + 1, N>1. Basis: S(1): T(1) = 1 by definition of function and fact that 1 = 1. For this case, there is only the initial call to Move, with no subsequent recursive calls.IH: Assume, for some N>1, that S(k), for all k <N. IS: Show S(N). T(N) = 2*T(N–1) + 1 by definition of function and fact that N>1. For this case we recursively call Move twice, each time with its first parameter set to N–1.But, T(N–1) = 2N-1–1 by inductive hypothesis. Hence T(N)= 2*(2N-1–1) + 1 = 2N – 2 + 1 = 2N – 1

© C. E. Hughes, UCF Computer Science – 117 – COP 3530 Spring ‘99

Page 118: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

2. The following algorithm will print out a fully parenthesized version of the contents of a binary expression tree, given that it is called with a pointer to the root node.type NodePtr = ^Node;

Node = recordnodeLabel : char;height : integer;leftChild, rightChild : NodePtr;

end;procedure fullParens(tree : NodePtr);begin

if tree <> NIL then with tree^ do beginwrite( ‘ ( ’ );fullParens(leftChild);write(nodeLabel, ‘ ’);fullParens(rightChild);write( ‘ ) ’ );

endend; (* fullParens *)

a.) First, given the expression (~A – B) * C / (D + E), show the binary tree that represents this expression. (Note: ~ is a unary operator with higher precedence than binary operators. Hint: The operand of a unary operator is typically recorded in the right subtree of the operator node.

b.) Now, show what would be printed if we called fullParens with this tree.( ( ( ( ~ ( A ) ) – ( B ) ) * ( C ) ) / ( ( D ) + ( E ) ) )

© C. E. Hughes, UCF Computer Science – 118 – COP 3530 Spring ‘99

Page 119: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

c.) Write a function computeHT which, when given a pointer to the root node of a binary expression tree, sets the value of the height field of each node to reflect its height in the tree. Note: Leaf nodes have height 0.function computeHT(tree : NodePtr) : integer;begin

if ( tree = nil ) computeHT := -1else begin

tree.height = 1+max(computeHT(tree.left), computeHT(tree.right));computeHT = tree.height

endend; (* computeHT *)

3. Fill in the following table by placing X’s in the appropriate columns (one per row):Order of Execution O(1) O(log2N) O(N) O(Nlog2N) O(N2) O(2N)Worst case for Bubble Sort XBest case for Bubble Sort XWorst case for a Quick Sort XAverage case for Merge Sort XWorst case for Towers of Hanoi XWorst case for delete from heap XBest case for insert into heap XAverage case for Heapify X4. Assuming that T(1) = 1 and k0, use the following table to solve the recurrence equations in a.)-d.).

Inductive Equation T(n)T(n) = T(n – 1) + bnk O(nk+1)T(n) = cT(n – 1) + bnk, for c > 1 O(cn)T(n) = cT(n/ d) + bnk, for c > dk O(nlogd c)T(n) = cT(n/ d) + bnk, for c < dk O(nk)T(n) = cT(n/ d) + bnk, for c = dk O(nk log n)

a.) T(n) = 2 T(n/2) + 56n10 c < dk, O(n10) by line 4

b.) T(n) = 2 T(n–1) + 56n10 c>1, O(2n) by line 2c.) T(n) = T(n/2) + 12 c=dk, O(log n) by line 5d.) T(n) = T(n–1) + 12 c=1, O(n) by line 1

© C. E. Hughes, UCF Computer Science – 119 – COP 3530 Spring ‘99

Page 120: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

5. Explain why, when we represent a queue in a linked list data structure, that we are better off linking a queue's elements from oldest to newest, rather than newest to oldest. You can either discuss this in terms of having an oldest and newest pointer or in terms of having a circularly linked list with just a newest pointer. Describe the problems with newest to oldest linking and indicate how oldest to newest addresses these problems. Use pictures to make your discussion clear and easy to follow.Consider circular with pointer to newest, and all internal nodes pointing from newer toward olderInsertion is O(1). We can do by the following really ugly method!!!

if (newest == null) { newest = newNode; newest.next = newest; }else {

saveData = newest.data; saveNext = newest.next; newest.data = newNode.data; newNode.data = saveData; newest.next = newNode; newNode.next = saveNext; }

Deletion is O(N): We must find the oldest, but the only path to it is through list. We can do by if (newest == null) return null;else {

old = newest;while (old.next.next != newest) old = old.next; oldest = old.next;if (oldest == newest) newest = null;else old.next = newest; return oldest; }

Consider circular with pointer to newest, and all internal nodes pointing from older toward newerInsertion is O(1). We can do by

if (newest == null) newNode.next = newNode;else { newNode.next = newest.next; newest.next = newNode; }newest = newNode;

Deletion is O(1): We can do by if (newest == null) return null;else {

oldest = newest.next; if (oldest == newest) newest = null;else newest.next = oldest.next; return oldest; }

© C. E. Hughes, UCF Computer Science – 120 – COP 3530 Spring ‘99

Page 121: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Key for Assignment # 2 -- 15.9.10A BST can be used as a abstract implementation of a priority queue. Show how the operation insert and deletemax can

be implemented using a BST with left-child – right-child data structure. What is the running time of these operations a) in the worst case and b) on the average?

insert can be done using the BST insert. This is O(N) at worst and O(lg2n) on average. Note BST are close to balanced on random data.

deletemax is nastier. No direct service of a BST works. We need to build a new service, taking advantage of the data structure as best we can. First we have to find the max. Well, it's the value associated with the "rightmost" node. So, the first phase moves down the right children, until a node is encountered that has no right child. That's the desired node. Once it's found, we must delete it. This may not throw away the node's left subtree, if that's not empty. Fortunately, all nodes in this left subtree (call its root lefty) have values greater than the parent (p) of the one we wish to delete. That's because these nodes are part p's right subtree. Assuming such a p exists, we just set lefty to be p's right child. Of course, if p doesn't exist, then lefty is the root of the new tree, after removal of the max. Also, if lefty doesn't exist, then we just remove the max node, being careful to set the tree to null, if there is no parent node. The search is O(N) in the worst case, and O(lg2n) on average. The tree repair takes O(1) time, so it does not contribute to the complexity.

© C. E. Hughes, UCF Computer Science – 121 – COP 3530 Spring ‘99

Page 122: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

COP 3530 – CS3 Spring 1999 Quiz # 1 Sample Questions Name:____Sample Quiz______1. Consider an ADT, KeySortedCollection (KSC) defined by the following protocol (family of services)

void init() – initializes the KSC to an empty statevoid put(Key k, Element x) – adds a new element x, with sort key k, to the KSC. Duplicates are allowed.Element keyGet(Key k) – returns an element having sort key k, if one is in the KSC. Returns null on failure.Element indexGet(int pos) – returns the element at the pos-th position, based on a low to high ordering by keys, from the KSC. Returns null if there are fewer than pos elements in the KSC.This ADT may be used for a variety of purposes, including as the basis for printing a list of elements in ascending order, according to some key. Several abstract implementations (data models) seem appropriate candidates for representing such an ADT. Moreover, each such abstract implementation might need to be evaluated in terms of a specific data structure.

10 a.) Fill in the order of complexities in terms of N, the number of elements being stored, of each of the last three services provided for the KSC ADT, given the following twp approaches to implementation. In both cases, assume that individual keys can be compared in constant time and that you are concerned with expected, not worst case performance.i.) The state of the KSC is represented as a Binary Search Tree (BST).ii.) The state of the KSC is represented by a Sorted List (SL).Assume a simple array data structure in ii.

BST SL

PutKeyGetIndexGet

6 b.) Compare BST to SL, specifying under what circumstances each is better than the other.3 2. Describe dynamic binding, distinguishing it from static binding.6 3. Analyzing the complexity of algorithms often requires that you solve a recurrence equation. For instance, an algorithm involving recursion

might yield a time T(n), for n>1, defined recursively by T(n) = 3 * T(n/2), with the boundary condition that T(1) = 1.

Assuming that n is a power of 2, show that T(n) is 3log2 n. You must use induction to prove that this equality holds for all n = 2k, where k is any whole number.Hint: Inductively prove the statement S(k) : T(2k) = 3k, for all k0.

© C. E. Hughes, UCF Computer Science – 122 – COP 3530 Spring ‘99

Page 123: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

4.5 a.) Apply the even-odd parallel algorithm presented in class for sorting the 6 elements in the following linear array of 6 processors. Show the

results of each of the 5 passes that it takes to complete this ascending (low to high) sort.

7 9 2 11 4 3 Initial Contents

After Pass 1

After Pass 2

After Pass 3

After Pass 4

After Pass 5

1 b.) Briefly state the limitations that we place on interprocessor communications.5. Consider an ordered binary tree data model. For each node, we use the term level to refer to the length of the unique path from the root to

this node. The root is at level 0, its direct descendants are at level 1, etc. A Heap is a linear (one-dimensional array) data structure that represents binary tree relations by the simple mechanism that the i-th possible node at level j is stored at position 2j+i-1 (assuming 0-based indexing.)

3 a.) Describe the simple arithmetic relations that exist to compute the indices of the left and right children from the index of their parent node, p, and the parent’s index from that of one of its children, c.LeftChild( p ) = RightChild( p ) = Parent( c ) =

4 b.) Consider representing a BST using a Heap style data structure. What benefits and drawbacks does this have over the standard linked list data structure? Consider timing and memory usage. Be explicit.

© C. E. Hughes, UCF Computer Science – 123 – COP 3530 Spring ‘99

Page 124: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

COP 3530 – CS3 Spring 1999 Quiz # 1 Sample Questions Name:____Sample Quiz______1. Consider an ADT, KeySortedCollection (KSC) defined by the following protocol (family of services)

void init() – initializes the KSC to an empty statevoid put(Key k, Element x) – adds a new element x, with sort key k, to the KSC. Duplicates are allowed.Element keyGet(Key k) – returns an element having sort key k, if one is in the KSC. Returns null on failure.Element indexGet(int pos) – returns the element at the pos-th position, based on a low to high ordering by keys, from the KSC. Returns null if there are fewer than pos elements in the KSC.This ADT may be used for a variety of purposes, including as the basis for printing a list of elements in ascending order, according to some key. Several abstract implementations (data models) seem appropriate candidates for representing such an ADT. Moreover, each such abstract implementation might need to be evaluated in terms of a specific data structure.

10 a.) Fill in the order of complexities in terms of N, the number of elements being stored, of each of the last three services provided for the KSC ADT, given the following twp approaches to implementation. In both cases, assume that individual keys can be compared in constant time and that you are concerned with expected, not worst case performance.i.) The state of the KSC is represented as a Binary Search Tree (BST).ii.) The state of the KSC is represented by a Sorted List (SL).Assume a simple array data structure in ii.

BST SL

put O(log2N) O(N)keyGet O(log2N) O(log2N)

indexGet O(N) O(1)6 b.) Compare BST to SL, specifying under what circumstances each is better than the other.

BST is faster building up a list since inserts (puts) can be done in constant time once log time is expended finding a right place. In contrast, after finding the correct place in log time, SL must then push everything down to make room for the new item. Lookups (keyGets) of both kinds are just log time operations. SL shines on indexed gets, which it does in constant time, as opposed to the linear time required by a BST. BST is better for a table whose contents constantly change, whereas SL is better if indexed gets dominate puts.

3 2. Describe dynamic binding, distinguishing it from static binding.Binding refers to the process of associating a procedure or function name with the body of code that should be run to provide the desired service. Static binding means that this association is done at compile time. Dynamic binding means that this association is deferred until execution time when the precise class (type) of service required can more easily be determined.

© C. E. Hughes, UCF Computer Science – 124 – COP 3530 Spring ‘99

Page 125: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

6 3. Analyzing the complexity of algorithms often requires that you solve a recurrence equation. For instance, an algorithm involving recursion might yield a time T(n), for n>1, defined recursively by T(n) = 3 * T(n/2), with the boundary condition that T(1) = 1.

Assuming that n is a power of 2, show that T(n) is 3log2 n. You must use induction to prove that this equality holds for all n = 2k, where k is any whole number.Hint: Inductively prove the statement S(k) : T(2k) = 3k, for all k0.Basis: Show S(0). By definition T(20) = T(1) = 1. But 1 = 30, and hence T(20) = 30. Inductive Hypothesis: Assume S(k) is true for some k0. That is assume T(2k) = 3k.Inductive Step: Show S(k+1), given S(k). By definition T(2k+1) = 3 T(2k+1/2) = 3 T(2k) .But, since T(2k) = 3k , by inductive hypothesis, then T(2k+1) = 3k+1

4.5 a.) Apply the even-odd parallel algorithm presented in class for sorting the 6 elements in the following linear array of 6 processors. Show the

results of each of the 5 passes that it takes to complete this ascending (low to high) sort.

7 9 2 11 4 3 Initial Contents

7 9 2 11 3 4 After Pass 1

7 2 9 3 11 4 After Pass 2

2 7 3 9 4 11 After Pass 3

2 3 7 4 9 11 After Pass 4

2 3 4 7 9 11 After Pass 5

1 b.) Briefly state the limitations that we place on interprocessor communications.Each processor is restricted to communicate with its immediate neighbors, only.

© C. E. Hughes, UCF Computer Science – 125 – COP 3530 Spring ‘99

Page 126: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

5. Consider an ordered binary tree data model. For each node, we use the term level to refer to the length of the unique path from the root to this node. The root is at level 0, its direct descendants are at level 1, etc. A Heap is a linear (one-dimensional array) data structure that represents binary tree relations by the simple mechanism that the i-th possible node at level j is stored at position 2j+i-1 (assuming 0-based indexing.)

3 a.) Describe the simple arithmetic relations that exist to compute the indices of the left and right children from the index of their parent node, p, and the parent’s index from that of one of its children, c.LeftChild( p ) = 2 p + 1RightChild( p ) = 2 p + 2Parent( c ) = (c-1) div 2 (* or (int) (c-1)/2 *)

4 b.) Consider representing a BST using a Heap style data structure. What benefits and drawbacks does this have over the standard linked list data structure? Consider timing and memory usage. Be explicit.Memory Usage: The linked list data structure has the great advantage that memory is usually allocated dynamically, and so there is no wasted space for nodes that have yet to be entered into the tree. In contrast an array is a static structure , and so all its space is preallocated, even if not needed. Moreover, an array’s memory usage is efficient for dense trees, like balanced priority ordered trees; it is terrible for sparse trees. Arrays do, however, win one on the size of each node since they do not use space for left-right pointers.Time: There really is no way to compare this for standard BST operations since computing a child takes a shift, and perhaps an increment, for arrays and an indirect reference for a linked list. Each takes about the same amount of time. The array does win if we need to find parents from children, or list all the leaves, etc.

© C. E. Hughes, UCF Computer Science – 126 – COP 3530 Spring ‘99

Page 127: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

WEEKS # 6 & 71. Set data model: set notation; empty set; lists versus sets; bags (multisets) versus lists; Infinite sets – (N, Z, R, C);

union, intersection and difference; complement as a special case of difference; basic algebraic laws of commutation, association, distribution and idempotence; empty set as identity for union, universe as identity for intersection.Proof techniques for sets: Venn Diagrams; transformations; subset (containment) relations; equivalence by mutual inclusion.Power sets; size of power set.

2. Implementing sets as lists: unsorted lists and union, intersection, difference – O(mn);sorted list and union, intersection, difference – O(m+n); factoring in the cost of sorting O(m log2m + n log2n).

3. Characteristic Vectors: Boolean arrays; Packed bit vectors.Sets and efficient update algorithms for spreadsheets.Pascal Set of enumeration type declaration; size limitations on basis set and why.

4. Hashing – Hashing function, perfect hash (example from old Basic), collisions.Collision detection and handling:Alphabetic chaining – a simple bucket techniqueSelection of hash functions – case study of symbol tableBuckets and linked lists – O(N/B), linear if B is O(N); rehashing on overflowLinear collision handling – can also be linear; circular list; secondary collisions; why lookup works; use of increment other than 1 (relatively prime increment); naive analysis of linear collisionVirtual hash functions – avoiding wasted comparisons of keys

5. Relations and functionsNotations: list of tuples, binary relations, infix notation, graphsCartesian product: A B = {(a,b) | a A, b B }Partial functions and relations; total functions and many-one relations; 1-1 into (injection), 1-1 onto (bijection)Functions as data – finite set of pairs with a relation for which no simple functional pattern exists. Operator tables, parse tables, and lots of other associations fall into this category. Implement as linked list, vector (R[a] = b if a R b) or hash table.Binary relation – a single domain element can be related to more than one range element. Implementation must account for this. Lookup usually returns all values that a key might map onto. An example of a binary relation might be one that maps spreadsheet cells to spreadsheet cells, where a R b, means a change to a’s value necessitates reevaluation of b. Can do with R[a] = S, where b S whenever a R b. Can also use a linked notation indexed by domain elements (a heads a list of all b, such that a R b), a linked list of pairs (a,b) whenever a R b or a hash linked list of pairs (a,b), indexed by the domain element a.

6. Properties of relations: Reflexive, symmetric, transitive, anti-symmetricPartial and total orders; Topological sorts – closure of partial orderingEquivalence relations and their importance in CS – mention minimization; Partitions and Union/Find problem – child/parent trees, intelligent union, path compression.

© C. E. Hughes, UCF Computer Science – 127 – COP 3530 Spring ‘99

Page 128: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

WEEKS # 6 & 7

7. Exam#1 is on Thursday, February 18.

8. Programming Assignment # 3: Turn in on Thursday, March 11.Write a program that supports the input, output and manipulation of polynomials, each term of which is over a single variable, having a rational coefficient and an integer exponent. Your program starts by reading in two polynomials in the form of lists of (coefficient numerator, coefficient denominator, exponent) triples. A single polynomial is defined by one triple of numbers for each non-zero term. The last such term in a polynomial is followed by a sentinel term consisting of only a zero coefficient numerator. Thus, the following input

-1 1 4 5 3 2 2 1 0 0-3 1 6 -7 1 -1 2 4 4 1 1 0 0

would specify two polynomials, - 1 X4 + 5/3 X2 + 2 and - 3 X6 + 1/2 X4 + 1 - 7 X-1. The input for the second of these was specified in a non-standard form, but you will need to store the polynomial’s terms in canonical order with reduced coefficients, as I have done. You must store the terms of each polynomial as a linked list, with no zero terms.

Once both polynomials, p1 and p2, are read in, your program must print the polynomials, and then compute and print the sum (p1 + p2), difference (p1 – p2), and product (p1 * p2), followed by the first derivative wrt the single variable X of each polynomial (d p1/dX and d p2/dX). Turn in a disk with source, plus a hard copy listing and output achieved by running your program with the above input.

Note, for the sample input above, the output is

- 1 X4 + 5/3 X2 + 2

- 3 X6 + 1/2 X4 + 1 - 7 X-1

- 3 X6 - 1/2 X4 + 5/3 X2 + 3 - 7 X-1

+ 3 X6 - 3/2 X4 + 5/3 X2 + 1 + 7 X-1

+ 3 X10 - 11/2 X8 - 31/6 X6 + 5/3 X2 + 2 + 7 X3 - 35/3 X - 14 X-1

- 4 X3 + 10/3 X

- 18 X5 + 2/1 X3 + 7 X-2

© C. E. Hughes, UCF Computer Science – 128 – COP 3530 Spring ‘99

Page 129: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Exam#1 Topics and Promises Added to Quiz#1 COP3530, Spring 1999Topics:

9. Set data model: Implementing sets as lists: sorted vs unsorted lists.Characteristic Vectors: Boolean arrays; Packed bit vectors.Sets and efficient update algorithms for spreadsheets.Pascal Set of enumeration type declaration; size limitations on basis set and why.

10. Hashing – Hashing function, perfect hash (example from old Basic), collisions.Collisions detection and handling; Alphabetic chaining – simple bucket technique

11. Selection of hash functions; Buckets and linked lists; Linear collision handling; secondary collisions; Virtual hash functions – avoiding wasted key comparisons

12. Multi-sets/Bags – maps as functions; bags as relations

13. Relations and functions; Partial and total orders; Equivalence relations and their importance in CS – mention minimization; Partitions and Union/Find problem; Topological sorts – closure of partial ordering; Processor scheduling; Acyclic dependency graphs

Promises:

10. A list and a set question

11. Hashing, especially collision detection and handling

12. Union/Find problem

© C. E. Hughes, UCF Computer Science – 129 – COP 3530 Spring ‘99

Page 130: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

WEEKS # 8 & 9

1. Algebra of Relations Union Intersection– DifferenceC (R) Selection B1,…Bn (R) ProjectionR Ai=Bj S Join R S Natural Join

2. Examples of Each Relational Operator – Use of trees to describe queries

3. Complexity Analyses of Implementing Relational OperatorsNaive versus intelligent versus specialized

4. Algebraic Properties of Relational OperatorsReordering and changing operations to reduce costs

5. Other comments on data bases – Views and Object-Oriented Data Bases

6. Assignment#3: Problems 8.6.2, 8.6.3, 8.7.3, 8.9.1, 8.9.3, 8.9.6, 8.9.7. Turn in on Thursday, March 25.

© C. E. Hughes, UCF Computer Science – 130 – COP 3530 Spring ‘99

Page 131: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Relational Operators

Union Set Union

Intersection Set Intersection

– Difference Set Difference

C (R) Selection Select All Rows Having Property CB1,…Bn (R) Projection Keep Only Columns B1,…BnR Ai=Bj S Join Merge/Keep Rows Where Ai in R is Same as Bj in SR S Natural Join Join Using the Single Common Attribute of R, S

© C. E. Hughes, UCF Computer Science – 131 – COP 3530 Spring ‘99

Page 132: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Examples of Relational Operators

© C. E. Hughes, UCF Computer Science – 132 – COP 3530 Spring ‘99

Page 133: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

EMPLOYEESNAME IDSmith, Mary 027Arco, Max 145Simmons, Richard 037Gonzalez, Rafael 111Jones, Atticus 621Torey, Phyllis 006Casper, Leona 427

EMPLOYEES union SHAREHOLDERSNAME IDArco, Max 145Blackman, Tonya 088Casper, Leona 427Gonzalez, Rafael 111Jones, Atticus 621Kolomotov, Karyl 077Pham, Carole 777Simmons, Richard 037Smith, Mary 027Ting, Xin 099Torey, Phyllis 006Torres, Alejandro 174

SHAREHOLDERSNAME IDSimmons, Richard 037Pham, Carole 777Torres, Alejandro 174Blackman, Tonya 088Ting, Xin 099Gonzalez, Rafael 111Smith, Mary 027Kolomotov, Karyl 077

EMPLOYEES intersect SHAREHOLDERSNAME IDGonzalez, Rafael 111Simmons, Richard 037Smith, Mary 027

EMPLOYEES minus SHAREHOLDERSNAME IDArco, Max 145Blackman, Tonya 088Casper, Leona 427Jones, Atticus 621Torey, Phyllis 006

© C. E. Hughes, UCF Computer Science – 133 – COP 3530 Spring ‘99

Page 134: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:
Page 135: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Examples of Relational Operators

© C. E. Hughes, UCF Computer Science – 135 – COP 3530 Spring ‘99

Page 136: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

EMPLOYED_BYNAME FIRMSmith, M. RCAArco, M. MitreSim, R. AppleGarcia, R. MitreJones, A. TCBYTorey, P. RCACarr, L. RCA

EMPLOYED_BY join HEALTH_PLANSNAME FIRM HMOSmith, M. RCA PPCSmith, M. RCA AVMEDSmith, M. RCA HumanaArco, M. Mitre HumanaSims, R. Apple KaiserGarcia,R. Mitre HumanaJones, A. TCBY PPCTorey, P. RCA PPCTorey, P. RCA AVMEDTorey, P. RCA HumanaCarr, L. RCA PPCCarr, L. RCA AVMEDCarr, L. RCA Humana

HEALTH_PLANSFIRM HMORCA PPCRCA AVMEDRCA HumanaMitre HumanaTCBY PPCApple Kaiser

FIRM=Mitre (EMPLOYED_BY)NAME FIRMArco, M. MitreGarcia, R. Mitre

HMO (HEALTH_PLANS)HMOPPCAVMEDHumanaKaiser

© C. E. Hughes, UCF Computer Science – 136 – COP 3530 Spring ‘99

Page 137: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

© C. E. Hughes, UCF Computer Science – 137 – COP 3530 Spring ‘99

Page 138: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Complexity of Relational Operators

MaxSz MinSz Naive Pre-Sort;Post-Sort

Indexed

R S t = n+m max(n,m) nmn log n + m log m;t log t

t=n+m

R S min(n,m) 0 nmn log n + m log m;t log t

t=n+m

R – S n 0 nmn log n + m log m;t log t †

t=n+m

C (R) n 0 nno gain;

no gainkLucky!

– (R) nn, usual

1, raren^2

nlog n;

nlog nn

R S nm 0 nmno gain;k+t log t ††sort-join

k+n or k+mindex-join

Assumes |R| = n, |S| = m, t = n+m, and |Result| = k† An extra field is initially added to each result tuple to identify the relation from which this tuple came.†† (a,k) in R becomes (k,a,R) and (k,b) in S is (k,b,S).When doing a sequence of operations, it is critical to keep the size of intermediary results as small as possible. Reordering and deferring operations can be very helpful. In arithmetic A*B+A*C = A*(B+C), but second expression is usually faster than first.

Page 139: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Algebraic Laws for Relational Operators

Laws for Join

Limited Associativity((R A=B S) C=D T) (R A=B (S C=D T))provided A is an attribute of R, B and C are different attributes of S, and D is an attribute of T.

Laws for Selection

Selection Pushing below Joins(C (R S)) (C (R) S)provided all attributes of C are in R(C (R S)) (R C (S))provided all attributes of C are in SSelection Splitting(C and D(R)) (C (D (R))Selection Commutivity(C (D (R)) (D (C (R))

Page 140: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Algebraic Laws for Relational Operators -- Continued

Laws for Projection

Projection Pushing below Unions(L (R S)) (L (R) L (S))Limited Projection Pushing below Joins(L (R A=B S)) (L (M (R) A=B N (S)))where1) M is attributes of L from R followed by A, if not in L,2) N is attributes of L from S followed by B, if not in LProjection IdentityL (R) R, when L is all attributes of R

Page 141: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Query on a Relational DatabaseRELATIONS1. CSG (COURSE-STUDENTID-GRADE)2. SNAP (STUDENTID-NAME-ADDRESS-PHONE)3. CDH (COURSE-DAY-HOUR)4. CR (COURSE-ROOM)QUERY

“Where is C. Brown 9AM on Mondays?”An Approach( ( ( ( SG SNAP ) CDH ) CR )Gets Tuples

(C, S, G, N, A, P, D, H, R)Can Select

NAME = “C. Brown”and (DAY=“M”) and (HOUR=“9AM”)

and Project ROOM

Page 142: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Query Tree

Leaf nodes in the tree are relations.

Interior nodes are relational operators.

Query can be interpreted from leaves towards root, with intermediate results generated at each node.

The final Join is enormous, even though we require just one item in the result.

Page 143: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Optimization by Pushing Selection

Push Selection below Join

Push in both directions, but remove slection on right branch, since CR has no Name, Day or Hour field.

This makes final Join trivial, since there will now be only one tuple coming up the left branch.

Page 144: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Optimization by Splitting Selection

Split Selection to prepare for sending only relevant parts of the Selection down each branch on the next Join.

Could Split again, but that won’t help

Page 145: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Push Selections on Separate Paths

Push Selections down only those paths that involve selected attributes

Day / Hour apply to only CDH.

Name applies to only SNAP, so keep on Pushing

Page 146: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Push Name Selections to SNAP

Push Name Selection down toward SNAP since CSG does not involve Selected attributes

Can’t Push Selections any farther down.

Page 147: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Push Projection Down

Must Push Projection on attributes of Join as well as that in the original Projection.

Join attribute is Course, so we project to Course alone on left, since Room is not an attribute on left. Pushing a Projection of Room and Course to right is useless.

Page 148: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Push Projection Down Farther

Push Projection down middle Join.

Course Projection can restrict size on both sides.

Page 149: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Push Projection Down to BottomPush Projection down as far as possible.

Just need the attributes that play a role in Join and are kept after Projection.

Page 150: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Relax on Some ProjectionsThere’s little to be gained by Projecting out attributes that disappear in next step. We relax by not removing the Grade attribute since it disappears at next Join.

This may or may not avoid some wasted effort.

Page 151: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Linda Basics

Provides coordination and communication, not computation.

Language featuresa) Global Memory-model. The model is based on tuple-space.b) Provides facilities for process creation and communication.c) Two type of tuples:

process tuples (active) data tuples (passive)d) process tuples can consume, generate and read other tuples.e) process tuple that is done turns into a data (passive) tuple.

Tuple: is a series of typed fields.E.g. ("astring", 10.35, 5, "somestring")

Template - matches tuple -- may include entries that specify type of the fieldE.g. (?ƒ, ?i, 5).

Linda Operationsout (t) : add a new tuple to the tuple space.

Caller would continue to execution.in (s) : Look for a tuple "t" that matches a template "s". If none, wait

for one. If there is only one, copy the values of "t" to the formal parameters of "s" and take "t" out of the tuple space. If more tan one exists, LINDA picks one arbitrarily.

rd (s) : same as "in" but the tuple is left in tuple space.inp (),rdp() : non-blocking, predicate versions of in ( ), rd ( ).

If there is no matching, return 0. Otherwise, return 1.eval (t) : Same as "out (t)" semantically, except that "t" is evaluated

after (rather than before) it is put in tuple space.E.g. eval (7, "astring", ƒ (7)); out (7, "astring", ƒ (7))Separate processes evaluate .

Page 152: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Linda Examples

Implementing a Binary semaphoreLinda provides access to a common data tuple.

wait (sem) in ("sem")signal (sem) out ("sem")

Initialization codeout ("sem")

Barrier Synchronizationn-processes arrive or converge at a point, then go on . . . . .

InitializationOut ("barriers", n)

On reaching the barriers every processin ("barrier", ?in) read what is in the barrierout ("barrier", n - 1) decrement and throw back into tuple spacerd ("barrier", 0) wait to read a 0 in the pool

Page 153: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

More Linda Examples

Position access data structures("A", 5, 4, 35.4) // Throw in a matrix A, and an index of 5 and 4 and value of 35.4.

In case of a sparse data structure we can use rdp () to find if there is a data in the position.

PrimesOne process exists for each number within the limit.

real main ( ){ int i;

for (i = 2, i < LIMIT, i + +)eval ("Primes", i, isPrime (i)) ;

for (i = 2, i < LIMIT, i + +) /* print the primes in order */rd ("Primes", ?i, ?ok);if (ok) print i;

}

isPrime (int me){ int i, limit, ok;

limit = sqrt (me) + 1for (i = 1, i < limit, i + + ) { /* wait for primes up to me to be evaluated*/

rd ("primes", i, ?ok)if (ok && (me % i = = 0)) return (0); }

return (1)}

Page 154: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Dynamic Views on Data Bases

View - Dynamically Derived RelationRelation that is formed from base relations

Actual Form – Change as base relations changePotential Form – Reevaluate as needed

Views often appear in a distributed data base, where users in a network have different access needs. There are potentially large costs associated with keeping such views current. This seems to argue for potential form, where we await access requests. But, access request can then result in a large delay while view is brought up to date. There is no one answer to this problem. Compromises are often needed, e.g., using a history of changes that is compressed when operations cancel each other. Additionally, views can be materialized (made actual) or left potential based on a history of usage. Such a technique, called a virtual method from an analogous technique in memory management, was the basis of one Ph.D. dissertation at UCF.

Page 155: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Modern Data BasesCharacteristics of Modern Data Base

Large volume of persistent dataEfficient access and manipulation of dataTransaction-oriented (commit on success)Access control (different levels of access)Concurrent access / LockingDistributed access and storageComplex elements (not just numbers / strings)Entity-specific operations and interpretations

Relational Data ModelEntities are recordsRecords are tuples of attributesAttributes are represented by fieldsFields are primitive (numbers / strings)Common access / manipulation operations

OO Data ModelEntities are objectsObjects specify attributes and methodsAttributes are represented by subobjectsSubobjects are themselves objectsMethods define access / manipulationClasses abstract attributes and methods

Page 156: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Advantages / Problems of OODBs

The Potential Up SideFields are not limited to primitivesEncapsulation supports access restrictionsEncapsulation supports evolution of schemaPolymorphism specializes operationsSpecialization can improve efficiencyConstraints can easily be supported by methodsSchemas, constraints and operations are reused (by inheritance hierarchy)Spatial relations are easy to supportEach entity has a unique OO pointer (OOP)OO nature integrates with OO languages

The Potential Down Side

Need to have many, many distinct pointersEfficiency may be compromised since we cannot use old tricksEncapsulation can interfere with sharing of very active attributes

Page 157: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

WEEK # 101. Basics Terminology Associated with Graphs

nodes/arcs (binary relation), unique node name, head/tail of arc, predecessors/successors, in-degree and out-degree of nodes and graph, node/arc labels, paths (list of successor nodes), path length, cycles, simple cycle (rep at start/end only), simple cycle derived from non-simple one, cyclic/acyclic graph, acyclic paths, edges, undirected graphs (symmetric relation), adjacent nodes/neighbors, degree of nodes and graph, binary tree as undirected graph of degree 3 with distinguished node (root), paths in undirected graphs, simple cycle of length 3 or more is cycle notion for undirected graph.

2. Examples of GraphsTask precedence graph – must be acyclic; Computer networks – often cyclic, e.g., token ring.Control flow graphs and intraprocedural data flow analysis – usually cyclic.Calling graphs, interprocedural analysis , direct/indirect recursion evidenced by cycles.Undirected graphs – highway and airline maps.

3. Data StructuresAdjacency lists – common with sparse graphsAdjacency matrix – common with dense graphsTime

Operation Dense Graph Sparse GraphLook up arc Matrix EitherSuccessor Either ListsPredecessor Matrix Either

Space (assume 32 bit words)Matrix – n2/32 words – one bit per cellLists – n + 2a – n list headers, a list elements, where a arcs and data + link = 2 wordsList is space efficient if n + 2a < n2/32, which, if n is much smaller than 2a, simplifies to a < n2/64. Of course if 2a is much smaller than n, then just use lists except when N<32, but then there is no great gain since we still need 32 bits per word, so the comparison is really against n, not n2/32.

Matrix is symmetric for undirected graph – can use triangular mapping techniques.Can extend both methods for labeled graphs

4. Connected Components of Undirected GraphsDivide graph into maximal connected subgraphs (connected components).A graph with a single connected component is said to be connected.Connected components as equivalence classes.Partition ADT to compute connected components.

5. Spanning Trees and Greedy Algorithms – Kruskal’s and Prim’s Algorithms

Page 158: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

6. Programming Assignment#4: Write a C++ or Java program to implement the ADT (class) Partition:Turn in on Thursday, April 8.public Partition(int n)– Constructs the Partition having n partitions, designated by the integers 0…n-1.public void union(int element1, int element2) – Unions the partition containing element1 with that containing element2. Reduces the number of partitions by one, provided the elements are not already in the same partition..public int find(int element) – Returns the “representative element” in the partition containing element.public String toString() – Provides a printable report depicting current partitions, total number of calls to find, and total and average number of elements inspected to perform the find service.public void resetStats() – Resets number of finds and number elements inspected during finds.Test your class by using the following Java main program or an equivalent C++ main.package partition;import java.io.*;import java.util.*;class PartitionTest extends Object {

public static void main(String[] args) throws IOException {final int SZ = 32;Random r = new Random(168886542);Partition p = new Partition(SZ);System.out.println(p.toString()); System.out.println();for (int i = 0; i < SZ-1; i++) {

int rep1, rep2;do {

int el1 = Math.abs(r.nextInt())%SZ; int el2 = Math.abs(r.nextInt())%SZ;rep1 = p.find(el1); rep2 = p.find(el2);

} while (rep1 == rep2);p.union(rep1,rep2); if ((i % 8) == 0) { System.out.println(p.toString ()); System.out.println(); }

}System.out.println(p.toString ()); System.out.println();p.resetStats(); for (int i = 0; i < 10000; i++) p.find(Math.abs(r.nextInt())%SZ);System.out.println(p.toString ()); System.out.println(); System.out.println("**** Press Enter ****"); System.in.read();

}}

Page 159: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

6. Programming Assignment#4 Continued: Create three versions of this program, using the tree data structure we discussed in class. First use the totally naive union algorithm that ignores depths, then use one that minimizes the depth of the unioned tree, then use path compression during find operations. Because I'm a good guy, I already did the first part for you in Java. The text has C versions of the partition code (with intelligent unioning) under the names find/merge. Your versions must be based on class definitions for Partition and any ADTs it needs. This is true, whether you use Java or C++.

Now, run your implementations on partitions of size (SZ) 16, 32, 128, 1024. Report the average you got from the 10000 random finds exercise of the structure. Analyze these results, comparing them to the theoretical expectations.

Turn in a printout containing your report, listings of the three implementations of Partition. Send me a zipped copy of the report and all the files (including source) necessary to test your program.

7. Assignment#4: Problems 9.3.5, 9.4.2, 9.5.2, 9.5.4, 9.6.1, 9.6.2, 9.6.3, 9.6.4, 9.7.4, 9.8.2, 9.9.1, 9.9.2. Turn in Thurs., April 15.

Page 160: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Partition Assignment Test Programpackage partition;import java.io.*;import java.util.*;class PartitionTest extends Object {

public static void main(String[] args) throws IOException {final int SZ = 32;Random r = new Random(168886542);Partition p = new Partition(SZ);System.out.println(p.toString()); System.out.println();for (int i = 0; i < SZ-1; i++) {

int rep1, rep2;do {

int el1 = Math.abs(r.nextInt())%SZ; int el2 = Math.abs(r.nextInt())%SZ;rep1 = p.find(el1); rep2 = p.find(el2);

} while (rep1 == rep2);p.union(rep1,rep2); if ((i % 8) == 0) { System.out.println(p.toString ()); System.out.println(); }

}System.out.println(p.toString ()); System.out.println();p.resetStats(); for (int i = 0; i < 10000; i++) p.find(Math.abs(r.nextInt())%SZ);System.out.println(p.toString ()); System.out.println(); System.out.println("**** Press Enter ****"); System.in.read();

}}

Page 161: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Partition Class #1package partition;import java.io.*;

public class Partition {class ParentTree {

ParentTree parent = null;int element = 0;

public ParentTree(int element) {this.element = element;

}}

static int inspected = 0, finds = 0;

ParentTree[] partitions = null;

public Partition(int size) {partitions = new ParentTree[size];for (int i=0; i<size; i++) partitions[i] = new ParentTree(i);

}

public void resetStats() {inspected = 0; finds = 0;

}

Page 162: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Partition Class #2private int doFind(int element) {

inspected++;if (partitions[element].parent == null) return element;else return doFind(partitions[element].parent.element);

}

public int find(int element) {finds++;return doFind(element);

}

public void union(int el1, int el2) {int root1 = find(el1); int root2 = find(el2);if (root1 != root2) partitions[root2].parent = partitions[root1];

}

Page 163: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Partition Class #3public String toString() {

boolean[] processed = new boolean[partitions.length+1];for (int i=0; i<=partitions.length; i++) processed[i] = false;int start = 0;String trace = "Finds = " + finds + "; Calls = " + inspected + "\n";int averagePathLength = 0;// rint rounds the result, choosing the closest integer; simple recasting truncates rather than roundsif (finds>0) averagePathLength = (int)Math.rint((double)inspected/finds);trace += "Average path length per Find = " + averagePathLength + "\n";while (start < partitions.length) {

while (processed[start]) start++;if (start < partitions.length) {

trace += start;int current = start; boolean done = false;while (!done) {

processed[current] = true;ParentTree parent = partitions[current].parent;trace += ((parent == null) ?

"::" :("->" +(new Integer(parent.element).toString())));

if (parent != null) current = parent.element;done = (parent == null) || processed[current];

}trace += " ";

}}return trace;

}

Page 164: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Flow GraphsA flow graph G = (N, E, s) refers to a directed graph (N, E) and an initial node s in N, where there is a path from s to every node of G. Nodes can be statements or basic blocks (single entry, single exit). Commonly, they are the latter.Program SquareRoot;var L, N, K, M : integer; C : boolean;begin

read(L); (* start of block B1 *)N := 0; K := 0; M := 1; (* end of block B1 *)loop

K := K + M; (* start of block B2 *)C := K > L;if C then break; (* end of block B2 *)N := N + 1; (* start of block B3 *)M := M + 2 (* end of block B3 *)

end loop;write(N) (* all of block B4 *)

end. (* SquareRoot *)

1

2

3 4

Page 165: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

A More Complex Flow Graph

1

2

3

4

5

8

10

9

76

Page 166: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Partitions and Connected Components

import java.util.*;……Partition connectedComponents(int n, Collection edges) {

p = new Partition( n );Iterator edgeIterator = edges.iterator();while (edgeIterator.hasNext()) {

edge = (Edge) edgeIterator.next();p.union(edge.node1, edge.node2);

}return p;

}

Assume m is max of n, the number of nodes, and e, number of edges, then this takes O(m*f), where f is cost of a Find operation (the basis for Union). Text uses log2(n) find, so cost is O(m log2n). Your assignment actually improves this to almost O(m), so that’s a clue that there may be a direct O(m) algorithm – there is one!

Note: I'm assuming an available Edge and Partition class. Imports may be needed!

Page 167: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Greedy – Basics

Want to Max or Min some objective function. Solution must satisfy some feasibility constraint.

Any solution satisfying constraints is feasible.

A feasible solution that maxes or mins the objective is optimal.

Greedy solutions are often suboptimal, but are always feasible solutions.

For example, our First Fit never overfills a trunk, so it always return a feasible solution. Its solutions are, however, not guaranteed to be optimal.

General Form of Greedy Algorithm:

solution := {};

FOR i:=1 to NumberOfChoices DOX := Select (A); (* where Select is simple *)IF Feasible (Solution X) THEN

Solution := Solution X

Page 168: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

RETURN Solution

Page 169: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Spanning Trees

Assume that G = (V, E), where G is an undirected graph, V is the set of vertices (nodes), and E is the set of edges.

A spanning tree of G is a subgraph which is a tree that encompasses all nodes in the original graph. Such a tree will commonly include just a subset of the original edges. Here, by tree, we mean a graph with no simple cycles. We ignore the normal designation of a root and we do not order nodes.

If G is a single connected component, then there is always a spanning tree.

Adding weights to edges gives us the minimum spanning tree problem, where we wish to span with edges whose sum is minimum among all spanning trees.

Page 170: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Spanning TreesConsider four nodes, fully connected as below,

The spanning trees are:

Page 171: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Min Spanning Tree–Kruskal's Algorithm

Weights could be distances, costs, signal degradation, …Feasible – There are no simple cycles at every stage

import java.util.*;……List kruskalMinSpan (int n, List edges) {

p = new Partition( n );spanningEdges = new List();sort(edges); // sorted low to high by costIterator edgeIterator = edges.iterator();while (edgeIterator.hasNext()) {

edge = (Edge) edgeIterator.next();int p1 = p.find(edge.node1); int p2 = p.find(edge.node2);if (p1 != p2) {

p.union(edge.node1, edge.node2);spanningEdges.add(edge);

}}return spanningEdges;

}

Page 172: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Kruskal’s Algorithm in Action1 2

34

5

6

10

50

15

35

4045

2555

20

30

Edge Cost Graph

(1,2) 10 1 2

(3,6) 15

1 26 3

(4,6) 20

1 26 34

(2,6) 25

1 26 34

(1,4) 30, Reject

(3,5) 35

1 26 34 5

(2,5), (1,5), (2,3) 40, 45, 50, Reject

Page 173: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

WEEK # 11

1. Spanning Trees and Greedy AlgorithmsCorrectness Analysis of Kruskal’s AlgorithmPrim’s Algorithms – Comparison to Kruskal’s

2. Depth First SearchBasic algorithm for Depth First Search TreeClassification of arcs in depth first search tree T created from graph G

Tree arcs – arcs u v in G, such that dfs(v) is called by dfs(u)Forward arcs – arcs u v in G, such that v is a descendant but not child of u in TBackward arcs – arcs u v in G, such that v is an ancestor of u in T (can have u=v)Cross arcs – arcs u v in G, where v is neither an ancestor nor descendant of u in T

cross arcs are always right to leftDepth First Search Forest – Note why DFS Tree is sufficient in program flow graphsRunning Time of DFS AlgorithmPostorder, rPostorder numberings during DFSRelation between Postorder number of a and v, when u v in G

if u v is a tree or forward arc then v follows u in T and so v<u in postorderif u v is a cross arc then v is to left of u in T and so v<u in postorderif u v is a backward arc and vu then v precedes u in T and so v>u in postorderif u v is a backward arc and v=u then v and u are the same and v=u in postorder

in general, if u v is a backward arc then vu in postorderFrom above relation, we can easily find the backward arcs as those arcs u v where u, the tail, has postorder number equal to or less than that of v, the head.

3. Algorithm Based on Depth First SearchFinding cycles in a directed graphTopological sortReachability problemConnected components

Page 174: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Min Spanning Tree–Prim’s Algorithm1 2

34

5

6

10

50

15

35

4045

2555

20

30

Weights could be distances, costs, signal degradation, …Feasible – Edges form a tree (acyclic), A, at every stageOptimization– At each point choose edge (u,v) so (u,v) is minimum weight edge allowing A (u,v) to be a tree Edge Cost Tree

(1,2) 10 1 2

(2,6) 25

1 26

(3,6) 15

1 26 3

(6,4) 20

1 26 34

(1,4) Reject

(3,5) 35

1 26 34 5

Page 175: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Min Spanning Tree–Prim's Algorithm

Weights could be distances, costs, signal degradation, …

Feasible – There are no simple cycles at every stage.

Greedy – We grab the closest node to one of the ones that has already been included.

There are lots of ways to implement Prim’s algorithm.

We will study an O(N2) way.

Other implementations are O(MlgN), where M = max (|E|, N)

Page 176: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Min Spanning Tree–Prim's Algorithm

program PrimMinSpan;var N, j, k : Integer;

Adjacency : AdjacencyMatrix;V : set of 1..MaxNodes;Dist, Source: Array [1..MaxNodes]

begin(* Assume N nodes, labeled 1 to N *)GetGraph(N, Adjacency);Dist := Adjacency[1];V := [2..N]; Source[1] := 0; { Root has no source }for j in V do

Source[j] := 1; { Distances are from root }while V <> [ ] do begin

k := index in V with smallest value in Dist;V := V – [k];for j in V do

if Dist[j] > Adjacency[k,j] then beginDist[j] := Adjacency[k,j]; Source[j] := k

end;end;

end.

Page 177: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Applying Prim’s Algorithm1 2

34

5

6

10

50

15

35

4045

2555

20

30

Node Dist/Source Cost Tree1 [0/0,10/1,/1,30/1,45/1,/1]2 [0/0,10/1,50/2,30/1,40/2,25/2] 10 1 2

6 [0/0,10/1,15/6,20/6,40/2,25/2] 25

1 26

3 [0/0,10/1,15/6,20/6,35/3,25/2] 15

1 26 3

4 [0/0,10/1,15/6,20/6,35/3,25/2] 20

1 26 34

5 [0/0,10/1,15/6,20/6,35/3,25/2] 35

1 26 34 5

Page 178: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Categorizing Arcs in DFS Tree

Page 179: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

10

9

7

6

5

2

8

1

43 cross

back

back

forward

Page 180: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Depth First Search

procedure dfs(u : NODE);var p : LIST;

v : NODE;begin

G[u].mark := VISITED;p := G[u].adjacency;while p<>NIL do begin

v := p^.nodeName;if G[v].mark = UNVISITED then dfs(v);p := p^.next

endend; (* dfs *)

procedure dfsForest(G : GRAPH);var u : NODE;begin

for u:=1 to n doG[u].mark := UNVISITED;

for u := 1 to n doif G[u].mark = UNVISITED the dfs(u)

end; (* dfsForest *)

Page 181: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Postorder Numbering in DFS

procedure dfs(u : NODE);var p : LIST;

v : NODE;begin

G[u].mark := VISITED;p := G[u].adjacency;while p<>NIL do begin

v := p^.nodeName;if G[v].mark = UNVISITED then dfs(v);p := p^.next

end;k := k+1;G[u].postorder := k

end; (* dfs *)

procedure dfsForest(G : GRAPH);var u : NODE;begin

k := 0;for u:=1 to n do G[u].mark := UNVISITED;for u := 1 to n do if G[u].mark = UNVISITED then dfs(u)

end; (* dfsForest *)

Page 182: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Testing for Cycles

function TestAcyclic(G : GRAPH) : Boolean;var answer : Boolean;

p : LIST;u, v : NODE;

beginanswer := TRUE;dfsForest(G);for u:=1 to n do begin

p := G[u].adjacency;while p<>NIL do begin

v := p^.nodeName;if G[u].postorder <= G[v].postorder then

answer := FALSE;p := p^.next

endend;TestAcyclic := answer

end; (* TestAcyclic *)

Page 183: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

WEEK # 12

1. Dynamic Programming RevisitedThe Principle of OptimalityRecall its use on LCS algorithm

2. Reflexive Transitive Closure of a Directed (or Undirected) GraphUse of a Boolean Adjacency Matrix to represent edgesAnalysis – correctness and O(N3) timeComparison to reflexive transitive closure by depth first search

3. Shortest Path ProblemWeights on arcsThe Weary Traveler or Shortest Path Problem

4. Lousy Weary AlgorithmUse of an Adjacency Matrix to represent weighted arcs

Infinite weights for no direct connectionTerribly exponential approach

5. Floyd's Algorithm – All PathsAgain, use of an Adjacency Matrix to represent weighted arcsUse Principle of OptimalityJust a variant of Warshall’s Algorithm

6. Dijkstra's AlgorithmUses adjacency lists and a POT

Page 184: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Dynamic Planning (Programming)

Based on "Principal of Optimality"

Technique requires that a problem have multiple stages at which decisions are made. We talk about stage i, and indicate that the system is in state i.

The Principal of Optimality says that the choice that is made at this stage is dependent only on state i and not on the policy that was used to make decisions at stages 1 to i-1.

Stated differently – the solution from stage i to n (the last stage) must be optimal in order for 1 to n to be optimal.

Page 185: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Reflexive Transitive Closure

The Problem:Given a graph, G, determine for which pairs of nodes, (A,B), there is a path between A and B.

A

B

F

E

G

C

D

Array representation – 1 is True; 0 is FalseA B C D E F G

A 1 0 1 1 0 0 1B 0 1 0 0 0 0 0C 0 0 1 0 1 1 0D 0 0 0 1 1 0 1E 0 1 0 0 1 0 0F 0 1 0 0 0 1 0

Page 186: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

G 0 1 0 0 1 0 1

Page 187: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Warshall’s “Can’t Get There from Here”

public void warshallsAlgorithm() {//for each pivot try all pairs of nodesfor (int pivot = 0; pivot < N; pivot++)

for (int v = 0; v < N; v++)for (int w = 0; w < N; w++)

if (v != w)connectedMatrix[v][w] = connectedMatrix[v][w] ||

(connectedMatrix[v][pivot] && connectedMatrix[pivot][w]);}

Analysis easily shows that this is O(N3).In comparison N DFS’s take O(NM), where M is max of N and number of arcs.In the very worst case, DFS is O(N3).

Page 188: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Weary Traveler – Shortest PathThe Problem:

Given a graph (a dag), G, with weighted arcs, and two nodes, A and B, determine the minimum weight path from A to B.Greedy fails here: Get 3 + 6 + 6 = 15; but can get 5 + 3 + 5 = 13

A

B

F

E

G

C

D

Source

Sink

67

5

7

7

3

711

614

5

3

Greedy fails here: Get 3 + 6 + 6 = 15; but can get 5 + 3 + 5 = 13Array representation

A B C D E F GA 0 5 3 14B 0 C 0 3 7 D 11 0 7 6E 5 0 F 7 0 G 6 7 0

Page 189: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Lousy Weary Traveler Solutionconst INFINITY = 9999;

FirstCity = 'A';LastCity = 'G';

type City = FirstCity .. LastCity;var Dist : array[City] of array[City] of Word;

procedure Weary ( Source, Sink : City) : Word;var cost, c : Word;

intermediary : City;begin

if Source = Sink then cost := 0else begin

cost := INFINITY;for intermediary := FirstCity to LastCity do

if (Dist[Source, intermediary] < INFINITY) and(Source <> intermediary) thencost := min(cost,

Dist[Source,intermediary]+Weary(intermediary,Sink));end;Weary := cost

end; (*Weary*)ORbegin

if Source = Sink then cost := 0else begin

cost := INFINITY;for intermediary := FirstCity to LastCity do

if (Dist[intermediary, Sink] < INFINITY) and(Sink <> intermediary) thencost := min(cost,

Dist[intermediary,Sink]+Weary(Source,intermediary));end;Weary := cost

end; (*Weary*)

Page 190: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Analysis of Lousy Weary Algorithm

We would like to determine the number of times we execute the loop body in the procedure Weary. That is the dominant factor.

Consider the first call. The number of cities = N. Thus we will execute the loop body exactly N times, plus the times associated with each recursive call. At worst, the source is connected to all other nodes. So there can be up to N-1 calls to Weary. Each one of them will do N iterations, plus of course the work done in their recursive calls. But now, the maximum number of node connections is only N-2, since there cannot be cycles, and therefore the source is unreachable.

Looking at a timing function, where K is the number of nodes that can be directly connected to the current source.T(K) = N + (K) * T( K - 1)T(0) = NWe would start at T(N-1), since the first source can be connected to at most N-1 other nodes. Clearly, if we ignore the N+ part, we have K!, or (N-1)! for the problem at hand. In fact a careful analysis shows this is even worse.

Page 191: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Floyd’s All Shortest Paths Algorithm

final int INFINITY = Integer.MAX_VALUE; // choose value not used in weights

private boolean connected(int v, int w) {return adjacencyMatrix[v][w] != INFINITY)

}

public void floydsAlgorithm() {for (int pivot = 0; pivot < N; pivot++)

for (int v = 0; v < N; v++)for (int w = 0; w < N; w++) {

if (connected(v,pivot) && connected (pivot,w)) {int tempDistance = adjacencyMatrix[v][pivot] +

adjacencyMatrix[pivot][w];if (tempDistance < adjacencyMatrix[v][w])

adjacencyMatrix[v][w] = tempDistance;}

}Analysis again shows that this is O(N3).

Is there a way to get close to the potential gain we saw in DFS versus Warshall’s.

Page 192: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Adjacency Lists for Shortest Path

A

B

F

E

G

C

D

Source

Sink

67

5

7

7

3

711

614

5

3

Graph Representation Using Adjacency ListsGRAPH dist (to A) toPOT adjacencyA 0 1 ((C,5),(D,3),(G,14))B 2 ()C 3 ((E,3),(F,7))D 4 (C,11),(E,7),(G,6))E 5 ((B,5))F 6 ((B,7))G 7 ((B,6),(E,7))POT node1 A2 B3 C4 D5 E6 F7 G last

Page 193: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Dijkstra’s Shortest Paths Algorithmconst FirstCity = 'A'; LastCity = 'G';type City = FirstCity .. LastCity;

POTCity = 1..(ord(LastCity)-ord(FirstCity))+1;Graph = array[City] of (* see above *)POT = array[POTCity] of City;

var G : Graph; POTCities: POT; last: POTCity;procedure swap(a,b : POTCity);var temp : City;begin

temp := POTCities[b];POTCities[b] := POTCities[a]; POTCities[a] := temp;G[POTCities[a]].toPOT := a; G[POTCities[b]].toPOT := b

end;procedure Dijkstra;var u, v : City; p : List;begin

Initialize;while last>1 do begin

v := POTCities[1]; (* pick this one to settle *)swap(1, last); last := last-1; bubbleDown(1); (* dist is priority *)p := G[v].adjacency;while p<>NIL do begin

u := p^.name;G[u].dist := min(G[u].dist,G[v].dist+p^.label);bubbleUp(G[u].toPOT);p := p^.next

endend

end; (* Dijkstra *)

Page 194: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Analysis of Dijkstra’s Algorithm

This is a Greedy algorithm in that it always does the best it can at each step.

To run it for one node takes O(M log2 N) where M is max of N and number of arcs. This is just a log factor away from the time it takes to look at the graph. To get “All Paths”, we need to run this N times, getting O(N M log2 N). If M is close to N2, then this runs worse than Floyd’s algorithm. If M is small then this is better. There is an N2 implementation of Dijkstra’s algorithm which can be used to create a competitive N3 all path algorithm, but it’s more complicated than Floyd’s.

Page 195: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Greedy N2 Shortest Paths Algorithmconst FirstCity = 'A'; LastCity = 'G';type City = FirstCity .. LastCity;var Dist: array[City] of array[City] of Word;procedure Greedy;var u, v : City; shortest : Word;

short: array[City] of Word; (* from source *)settled, unsettled : Set of City;

begin(* initialize the shortest paths from FirstCity *)for u:=FirstCity to LastCity do

short[u] := Dist[FirstCity,u];short[FirstCity] := 0;(* initially only FirstCity is a settled node *)settled := [FirstCity];unsettled := [succ(FirstCity) .. LastCity];(* iterate until all nodes are settled *)while unsettled <> [ ] do begin

(* greedily pick next one to settle *)shortest := INFINITY; (* a global const *)for v in unsettled do

if short[v] <= shortest thenbegin u := v; shortest := short[v] end;

settled := settled + [u]; unsettled := unsettled - [u];(* fix the current shortest paths from FirstCity *)for v in unsettled do

short[v] := min(short[v],short[u]+Dist[u,v])end

end; (* Greedy *)This is N2. Why use <= in greedy part? What would happen to complexity if short were kept as a heap?

Page 196: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

WEEK # 131. Scanning and Parsing

Grammars - Simple notion of Context Free GrammarsExtending grammars with star notationArithmetic expression grammarsRecursive descent and grammars – avoidance of left recursionA simple parser for arithmetic expressionsAdding semantics to parsersScanning for tokens – Wirth’s lexical analyzer for Pascal

2. Programming Assignment # 5: I have written a Pascal program to implement a simple assignment language. The language has 26 variables a, b, c, …, z, all initialized to zero. Each assignment statement appears on its own line and is of the form var := expression; , where expression’s operands are variables and integer constants, its operators are binary +,–,* and / and it may use parentheses ( ) to override normal precedence. The evaluator is based on a recursive descent strategy using the following grammatical description:<program> <statement-list><statement-list> <statement> <statement-list> | !<statement> LETTER := <expression> ;<expression> <term> { + <term> | – <term> }*<term> <factor> { * <factor> | / <factor> }*<factor> ( <expression> ) | LETTER | NUMBERThe input to your program is just a sequence of assignments, one per line, terminated by a line containing just the symbol !. The output is an echo of each line of input, followed by the value assigned to the statement’s variable. Additionally, the program dumps a list of all variables and their current values at the end of all input (when the ! is encountered.)

Your job is to rewrite this in either C++ or Java and to expand it to a more competent program. Here are your additions: i.) Add comments, especially those showing the relation of code to grammarii.) Change error messages to reflect actual errors (e.g., missing right parenthesis)iii.) Recover properly from errors -- I don’t resync too well

Turn in on Tuesday, April 27.

Page 197: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Grammar for Assignment Language

<program> <statement-list>

<statement-list> <statement> <statement-list>

| !

<statement> LETTER := <expression> ;

<expression> <term> { + <term> | – <term> }*

<term> <factor> { * <factor> | / <factor> }*

<factor> ( <expression> )

| LETTER

| NUMBER

Page 198: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Lexical Analyzer for Calculatorunit Lexical;

interface

type Tokens = ( UNKNOWN, EOS, EQUALS, ADDOP, MULOP, LPAREN, RPAREN, SEMICOLON, LETTER, NUMBER, BANG, EOD );

LexStream = object public Token : record typ : Tokens; val : integer end; procedure Open(fileName : string); procedure Next; procedure NextLine; private fileStream : Text; dataStream : String;

Page 199: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

position : Integer; procedure scan; end; (* LexStream *)

Page 200: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Lexical Analyzer for Calculator – 2implementationuses WinCRT;

const NULL = chr(0);

function asInt(c : Char):integer;begin asInt := ord(c) - ord('0')end; (* asInt *)

procedure LexStream.Open(fileName : String);begin Assign(fileStream, fileName); Reset(fileStream); if IOResult = 0 then Token.typ := EOS else Token.typ := EOD; Nextend; (* Open *)

procedure LexStream.Nextline;begin with Token do if typ <> EOD then begin typ := EOS; Next endend; (* Nextline *)

procedure LexStream.scan;begin

Page 201: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

if not (Token.typ in [EOS,EOD]) then repeat inc(position) until dataStream[position] <> ' 'end; (* scan *)

Page 202: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Lexical Analyzer for Calculator – 3procedure LexStream.Next;begin scan; with Token do begin if (dataStream[position] = NULL) or (typ=EOS) then if EOF(fileStream) then typ := EOD else begin ReadLn(fileStream, dataStream); WriteLn(dataStream); dataStream := dataStream + NULL; position := 0; typ := UNKNOWN; scan end; (* if *)

Page 203: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Lexical Analyzer for Calculator – 4if typ <> EOD then begin val := ord(dataStream[position]); case dataStream[position] of NULL : typ := EOS; '+','-' : typ := ADDOP; '*','/' : typ := MULOP; '(' : typ := LPAREN; ')' : typ := RPAREN; ';' : typ := SEMICOLON; '!' : typ := BANG; 'A'..'Z', 'a'..'z': begin typ := LETTER; val := ord(UpCase(dataStream[position])) end; ':' : if dataStream[position+1] = '=' then begin typ := EQUALS; inc(position) end else typ := UNKNOWN; '0'..'9': begin typ := NUMBER; val := asInt(dataStream[position]); while dataStream[position+1] in ['0'..'9'] do begin position := position+1; val := val*10 + asInt(dataStream[position]) end end; else typ := UNKNOWN end; (* case *)

Page 204: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

end (* if *) end (* with Token *)end; (* Next *)end. (* Lexical *)

Page 205: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Syntax and Semantics for Calculatorprogram Calculator;uses lexical, WinCRT;var stream : LexStream; register : array['A'..'Z'] of Integer; r : Char;procedure Error;begin WriteLn('OOPS!'); stream.NextLineend; (* Error *)

function expression : Integer;var x, y : Integer; op : Char;function term : Integer;var x, y : Integer; op : Char;function factor : Integer;var x, y : Integer; op : Char;begin with stream, Token do begin case typ of NUMBER : factor := val; LETTER : factor := register[chr(val)]; LPAREN : begin Next; factor := expression; if typ<>RPAREN then Error end

Page 206: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

end; Next endend; (* factor *)

Page 207: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Syntax / Semantics for Calculator – 2begin (* term *) with stream, Token do begin x := factor; while typ = MULOP do begin op := chr(val); Next; y := factor; if op = '*' then x := x*y else if y<>0 then x := x div y else Error end end; term := xend; (* term *)

begin (* expression *) with stream, Token do begin x := term; while typ = ADDOP do begin op := chr(val); Next; y := term; if op = '+' then x := x+y else x := x-y end end; expression := xend; (* expression *)

Page 208: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Syntax / Semantics for Calculator – 3

procedure statement;var r : Char; x : Integer;begin with stream, Token do begin r := chr(val); Next; if typ = EQUALS then begin Next; register[r] := expression; WriteLn('===> ', r,' = ', register[r]); if typ = SEMICOLON then NextLine else Error end else Error endend; (* statement *)

procedure stmtList;begin with stream, Token do while typ=LETTER do begin Write(‘Press Enter for Results of this statement’); ReadLn; statement; endend; (* stmtList *)

Page 209: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Syntax / Semantics for Calculator – 4

begin (* Calculator *) for r:='A' to 'Z' do register[r] := 0; with stream, Token do begin Open('lex.dat'); stmtList; if typ<>BANG then Error; NextLine; if typ<>EOD then Error; WriteLn; WriteLn('Register# Value Register# Value'); r:='A'; while r<'Z' do begin Write(r:5, register[r]:13); inc(r); WriteLn(r:11, register[r]:13); inc(r) end endend. (* Calculator *)

Page 210: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

WEEK # 14

1. Finite State AutomataLexical analysis and text pattern matchingDeterministic versus non-deterministicThe subset constructionRegular expressionsGREP in UNIX

2. Review for Quiz#2

3. Quiz -- Thursday, April 15

Page 211: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Quiz#2 Topics and Promises COP3530, Spring 1999Topics:1. Relational Data Model

Relational database is a collection of tables, called relationsEach row is called a tuple and is an instance of the relationEach column is a labeled attribute; Set of attribute names is scheme of the relationEach relation has a key, which is a set of attributes that uniquely identifies each rowData Structures for Relations:

BST – sorted by key; Hash, as function of key; List (linked or array) of tuplesUsing keys to navigate among relationsRelational algebra – The operators: Union Intersection – DifferenceC (R) Selection B1,…Bn (R) ProjectionR Ai=Bj S Join R S Natural JoinComplexity Analyses of Implementing Relational OperatorsQuery optimization – Algebraic Properties of Relational OperatorsViews – dynamically derived relationsObject-Oriented Data Bases – object as fields; polymorphismTuple space – adding tuples based on no pre-determined scheme

2. Amortized Union / Find on equivalence relationsParent pointers leading up to a canonical element of the equivalence classConstant time Union algorithm; Linear time Find algorithmConstant time Union algorithm using weights; lg time Find algorithmConstant time Union algorithm; lg* time path compression Find

3. Graph data model – terminology, representation schemesUnion/Find partitioning to compute connected components.Spanning Trees and Greedy Algorithms – Kruskal’s and Prim’s AlgorithmsBasic algorithm for Depth First Search TreeClassification of arcs in depth first search tree T created from graph G

Tree arcs ,Forward arcs, Backward arcs, Cross arcsDepth First Search Forest – Postorder numberings during DFSAlgorithms Based on Depth First Search

Finding cycles in a directed graph; Topological sort; Reachability problem;Connected components; Reflexive transitive closure

Page 212: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

4. Connectivity Problems in GraphsReflexive Transitive Closure by Warshall’s Algorithm

Use Principle of Optimality – intermediate (pivot) nodesUse a Boolean Adjacency Matrix – O(N3) timeComparison to reflexive transitive closure by depth first search

All Shortest Paths Problem by Floyd’s AlgorithmUse Principle of Optimality – intermediate (pivot) nodesUse an Adjacency Matrix to represent weighted arcs – O(N3) time

Shortest Path Problem by Dijkstra’s AlgorithmGreedy based on closest unsettled nodeUse Adjacency Lists for weighted arcs and POT for unsettled – O(M lg N) timeO(N M lg N) time extension for all shortest pathsOr use Adjacency Matrix for arcs and List for unsettled – O(N2) time

Promises:

1. I will give you a set of relations and ask you questions about the results of operations (union, intersection, difference, projection, selection and join.) There will be no proofs.

2. I will ask you to apply rules for query optimization – you do not need to memorize the rules, but you must understand them. There will be no proofs.

3. I will have you demonstrate changes to trees used in union/find algorithms. You will not need to write out any algorithms.

4. I will ask you to apply various algorithms to graphs. I will also ask you questions that test your knowledge of the complexities and sources of complexities in the various graph algorithms that we have discussed. Be sure to know union/find based connected components; Kruskal’s; Prim’s; DFS; DFS based acyclic, sort, reachability, connected components, reflexive-transitive closure; Warshall’s; Floyd’s; and Dijkstra’s (both).

Page 213: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

COP 3530 – CS3 Fall 1993 Quiz # 2 Name:__Sample Quiz_________

15 1. Consider the following trees being used to represent equivalence classes (partitions) over the set {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}. Show the resulting combination of the first two trees if we do a union(2,3). Now show the final tree the results after we do a union(6,16). In each case, assume that the union starts with two finds, each of which uses path compression and that the unions use tree heights to minimize path lengths.

4

6

2

8

5

1

3

7

9

14

11

13

16

12

15

10

4

6 28

5

1 37

9

14 4 6

28

5

1 37

9

14

11

1316

12

15

10

Define the function lg* N (also called log2*(N)).

lg* N is the least integer k such that k repeated lg application produced a value 1That is, lg lg lg … lg N 1, where there are k lg’s in sequence

What is the value of lg* 216 ? 4 since lg 216 = 16, lg 16 = 4, lg 4 =2, lg 2 =1.

How does lg* N relate to the management of partition trees by the above algorithms?

This is the expected depth of a tree when it contains N elements.

Page 214: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

10 2. The following table shows algorithmic costs for naive approaches (ones not involving sorts or indices) to relational operations. Fill in the columns associated with the use of indices. You may assume constant time index lookup via a hash table. Assume |R| = n, |S| = m, t = n+m, and |Result| = k

Naive IndexedR S nm n+m = tR – S nm nC (R) n k– (R) n^2 nR S nm k+n or k+m

10 3. An undirected graph can be checked to see if it’s connected by using a union/find algorithm, similar to Kruskal’s tree spanning algorithm (see #7), or by employing depth first search. In words, describe how the depth first search algorithm solves this problem. You may assume a graph G with N nodes and E edges.

This algorithms marks all nodes as unvisited. It then selects one node that serves as the basis for finding all those connected to this one. If that’s the set of all nodes, then the graph is connected. The chosen node is marked visited. Each of its unvisited nodes are treated in the same manner. Each node is ignored after it has been marked visited. This approach will eventually visit all nodes that are connected to the first. Thus, the graph is connected if all nodes are marked as visited after the depth first search completes.

What is the time complexity of the depth first search algorithm?O(max(N,E))What is the time complexity of the union/find algorithm?O(max(N,E) lg* N)Which is preferable and why?The depth first search algorithm always performs better.

15 4. Consider the following relations DIRECTORS, BORROWERS and LOCATIONS. I have specified several tables, each unfilled and each labelled with a single operation that is used to define its tuples. Fill in these tables. I have allowed for at least as many entries (generally more) as are needed.

DIRECTORS

Page 215: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

NAME BANKArco, M. CENTRUSTArco, M. SUNGarcia, R. BARNETTJones, A. BARNETTSim, R. BARNETTSmith, M. SUNTorey, P. CENTRUST

BORROWERSNAME BANKArco, M. SUNGarcia, R. BARNETTJones, A. SUNTorey, P. CENTRUSTTrent, C. CENTRUST

LOCATIONSBANK STATEBARNETT FLCENTRUST FLCENTRUST NCSUN FLSUN GA

DIRECTORS BORROWERSNAME BANKArco, M. CENTRUSTArco, M. SUNGarcia, R. BARNETTJones, A. BARNETTJones, A. SUNSim, R. BARNETTSmith, M. SUNTorey, P. CENTRUSTTrent, C. CENTRUST

Page 216: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

DIRECTORS BORROWERSNAME BANKArco, M. SUNGarcia, R. BARNETTTorey, P. CENTRUST

BANK=SUN (BORROWERS)NAME BANKArco, M. SUNJones, A. SUN

STATE (LOCATIONS)STATEFLGANC

DIRECTORS LOCATIONS NAME BANK STATEArco, M. CENTRUST FLArco, M. CENTRUST NCArco, M. SUN FLArco, M. SUN GAGarcia, R. BARNETT FLJones, A. BARNETT FLSim, R. BARNETT FLSmith, M. SUN FLSmith, M. SUN GATorey, P. CENTRUST FLTorey, P. CENTRUST NC

Page 217: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:
Page 218: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

10 5. Assume we wish to issue the following query :NAME (STATE=FL((DIRECTORS BORROWERS) LOCATIONS)))Present the tree associated with this query expression.

Now show how the following algebraic rules may be applied to optimize the query by pushing the projection and selection operators as low as possible. Present the new expression and its corresponding tree

Selection Pushing below Joins(C (R S)) (C (R) S) , provided all attributes of C are in R(C (R S)) (R C (S)) , provided all attributes of C are in SProjection Pushing below Unions(L (R S)) (L (R) L (S))Limited Projection Pushing below Joins(L (R A=B S)) (L (M (R) A=B N (S))) , where1) M consists of attributes of L from R followed by attribute A, if it is not in L,2) N consists of attributes of L from S followed by attribute B, if it is not in L.Projection IdentityL (R) R , when L is all the attributes of R

© C. E. Hughes, UCF Computer Science – 218 – COP 3530 Spring ‘99

Page 219: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

NAME ((DIRECTORS BORROWERS) BANK (STATE=FL(LOCATIONS)))

10 6. The two implementations of Dijkstra’s shortest paths algorithm have incomparable complexity, even though each is based on the same greedy approach involving selected and unselected nodes.

a.) What is the greedy basis for selecting a next node in Dijkstra’s algorithm?Chose the unselected node whose currently computed shortest distance from source is minimum among all unselected nodes.

b.) One implementation leads to a complexity of m lg n, where n is the number of nodes in the graph and m is the maximum of the number of nodes and edges. What operation(s) lead to the lg n term?The heap maintenance operation (bubbleDown and BubbleUp) needed to ensure the minimum is at the heap’s top costs lg n each iterationWhat operation(s) lead to the m term?Picking the next node and going through its adjacency list introduces a term that is bounded by the worst of n and the number of edges.

c.) The other implementation leads to a complexity of n2, where n is the number of nodes in the graph. Why does this approach use a linear list rather than some fancier one to support its selection of the best node? Be explicit.Because the heap maintenance required in this algorithm forces a complete reheapify each pass. Heapify costs O(n) time, with a constant greater than that of the simple linear search that we are trying to avoid.

d.) Under which circumstances is the n2 implementation preferable to the m lg n one?When the graph is totally connected,then m is n2, and so the m lg n technique degenerates to n2 lg n, a definite loser to n2.

© C. E. Hughes, UCF Computer Science – 219 – COP 3530 Spring ‘99

Page 220: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

10 7. Consider the following implementation of Kruskal’s algorithm.List kruskalMinSpan (int n, List edges) {

p = new Partition( n );spanningEdges = new List();sort(edges); // sorted low to high by costIterator edgeIterator = edges.iterator();while (edgeIterator.hasNext()) {

edge = (Edge) edgeIterator.next(); int p1 = p.find(edge.node1); int p2 = p.find(edge.node2);if (p1 != p2) {

p.union(edge.node1, edge.node2);spanningEdges.add(edge);

}}return spanningEdges;

}

What are the complexities of the bolded operations, assuming N nodes and E edges?sort(edges) O(E lg N) or O(E lg E)p.find(edge.node1) O(lg* N)p.union(edge.node1, edge.node2) O(1) or O(lg* N), if there is no memoryspanningEdges.add(edge); O(1)What is the overall complexity of this algorithm?N + O(E lg N )+ O(E lg* N) = O(N + E lg N) = O(max(N,E) lg N)

© C. E. Hughes, UCF Computer Science – 220 – COP 3530 Spring ‘99

Page 221: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

8. Floyd’s “All Shortest Path Algorithm” is presented below. program Floyd;uses GraphSupport; { + any other units you need }var Distance : AdjMatrix; N, source, intermediate, destination : Integer;begin

GetGraph(N, Distance); { Assume graph is represented by adjacency matrix }for pivot := 1 to N do

for source := 1 to N dofor destination := 1 to N do

Distance[source,destination] := min(Distance[source,destination], (Distance[source, pivot] + Distance[pivot,destination]){ Answer is in the adjacency matrix, Distance }

end. { Floyd }

2 Assuming n nodes and e edges, what is the overall complexity of this algorithm? n3

2 Is this a Greedy or a Dynamic Programming algorithm? Dynamic

An alternative algorithm by Dijkstra, when run n times, once for each source node, leads to a complexity of n m lg n, where m is the maximum of n and e.

2 What operation(s) lead to the lg n term? BubbleUp and BubbleDown

2 What operation(s) lead to the m term?Selecting all nodes and their associated edges from adjacency list

2 What property of a graph makes this algorithm preferable to Floyd’s? Be explicit.sparse in the sense that m lg n < n2 or max(n,e) < n2 / lg n or e < n2 / lg n

© C. E. Hughes, UCF Computer Science – 221 – COP 3530 Spring ‘99

Page 222: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

WEEK # 15

1. Finite State AutomataRegular expressionsLexical analysis and text pattern matchingDeterministic versus non-deterministicThe subset constructionGREP in UNIX

2. Review for Final Exam

3. AVL Trees

4. Final Exam -- Thursday, April 29

© C. E. Hughes, UCF Computer Science – 222 – COP 3530 Spring ‘99

Page 223: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Finite State Automata

A finite state automaton A is a 5-tuple (Q, , , s, F), whereQ is a finite set of states, is a finite set of symbols, called the alphabet is the next state mapping from Q to Qs is a member of Q, called the start stateF, a subset of Q, is the set of final states

The language accepted by A, denoted L(A), is the set{ w | *(s,w) F }

where * is the reflexive-transitive closure of . That is,*(q,) = q, where is the string of length 0.*(q,aw) = *( (q,a), w)

We usually express as a labeled graph (called a transition diagram), where there is one node for each state and there is an arc labeled a from node q to node r, just in case (q,a) = r. Moreover, we don’t explicitly list the alphabet or the states since they can be deduced by looking at the graph. Finally, we use a graphic notation to indicate the start state by drawing a tailless arc into it, and we similarly show the final states by drawing their borders with a double circle.

Example: An FSA to accept odd parity strings over {0,1}.

EVEN ODD1

1

0 0

This takes one flip-flop to implement

© C. E. Hughes, UCF Computer Science – 223 – COP 3530 Spring ‘99

Page 224: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Regular Expressions

A regular expression is an algebraic method of describing languages. This notation is equivalent in descriptive power to the family of finite state automata. A regular expression (RE) over some alphabet is:

0. Ø. This denotes the empty set { }.

1. . This denotes the set containing just the string of length zero {}.

2. a, where a is in . This denotes the set {a}.

3. (A + B), where A and B are both RE’s. This denotes the union of the sets denoted by A and B.

4. (A B), where A and B are both RE’s. This denotes the Cartesian concatenation of the sets denoted by A and B. That is, if x is in A and y is in B, then xy is in A B.

5. (A)*, where A is an RE. This denotes the Kleene star concatenation of A. A* = + A + AA + AAA + …

6. Nothing else is a regular expression.

Parentheses can be omitted where no confusion arises.

The odd parity language can be expressed by the regular expression

(0* 1 0* 1)* 0* 1 0*

© C. E. Hughes, UCF Computer Science – 224 – COP 3530 Spring ‘99

Page 225: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Regular Expressions as PatternsRegular expressions are commonly used to specify patterns. This is common in the specification of tokens for lexical analyzers and in search strings used for finding a matching pattern in some data file.

Consider the shorthand that

Digit = 0+1+2+3+4+5+6+7+8+9Alpha = A + B + … + Z

then an integer number in Pascal is (almost) denoted by

(Digit)* Digit

an identifier is (almost)

Alpha (Alpha+Digit)*

In the UNIX operating system, there is a command called grep. This weird name stands for

(in general) match the regular expression and print matches

grep ‘[Cc].*[Ee].*[Hh]’ file

would match any place where a C (or c) is followed by zero or more anything’s (the dot is wild), an E (or e), zero or more anything’s, and an H (or h). This leads to a non-deterministic FSA represented

C

c,CE

e,EH

h,H

© C. E. Hughes, UCF Computer Science – 225 – COP 3530 Spring ‘99

Page 226: €¦  · Web viewComputer Science III. COP 3530 -- Spring 1999. University of Central Florida. Computer Science Department. Charles E. Hughes. Basic Information. Meeting Times:

Non-Deterministic Automata

There is a technique called the “subset construction” that converts non-deterministic automata to deterministic ones. Note that in non-deterministic, we accept if any path leads to a final state.

Example: A non-deterministic automaton that accepts any string of 0’s and 1’s that includes a 01 is easy to construct as

00

11

This can be redone as a deterministic automaton by recalling in the state of the new automaton all states that might have been reached in the original.

0 1

Our example did not grow the number of states. In general, however, the deterministic version can have 2N states, where the non-deterministic one had N states. grep has several versions. One keeps the non-deterministic automaton and is a slow interpreter. This is good for small file searches. Another converts the non-deterministic fsa to a deterministic one. This cost must be amortized over a long file search.

© C. E. Hughes, UCF Computer Science – 226 – COP 3530 Spring ‘99