4/25/011 cse 121/131 programming spring 2001 handout 9

23
4/25/01 1 CSE 121/131 Programming Spring 2001 Handout 9

Upload: stephanie-pierce

Post on 04-Jan-2016

220 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 4/25/011 CSE 121/131 Programming Spring 2001 Handout 9

4/25/01 1

CSE 121/131

Programming

Spring 2001

Handout 9

Page 2: 4/25/011 CSE 121/131 Programming Spring 2001 Handout 9

4/25/01 2

Graphs

Perhaps the most important object considered in CS.

What is a graph? (Informal)

1

23

45

Red circles denotenodes or vertices orpoints.

Blue lines denote edges. Edges connect pairs of nodes.

This graph has vertex set: {1,2,3,4,5}Has edge set: {(1,2), (2,5), (5,4), (1,5)}.

Page 3: 4/25/011 CSE 121/131 Programming Spring 2001 Handout 9

4/25/01 3

Graphs and puzzles

The Bridges of Konigsburg Park

R. Pregel

A

C

B

D

A

B

C

DQuestion: On a walk throughthe park can you walk overeach of the bridges exactly once?

Page 4: 4/25/011 CSE 121/131 Programming Spring 2001 Handout 9

4/25/01 4

More Puzzles...

Which of these figures can you draw w/oretracing steps or lifting pencil off paper?

Euler, a famous Swiss mathematician solved all ofthese problems and initiated the field of graphtheory in the process.

A walk through a graph that goes through everyedge exactly once (and returns) is called an Eulerian tour.

Page 5: 4/25/011 CSE 121/131 Programming Spring 2001 Handout 9

4/25/01 5

Knight’s moves

a b c d e f g h

12345678

a8

b8

b6

a6

c6

c7

c5

Question: Can a knight makea tour of the entire chessboard?

Question about graphs: Is therea tour of the graph that visits allthe vertices exactly once?(Hamiltonian cycle.)

Page 6: 4/25/011 CSE 121/131 Programming Spring 2001 Handout 9

4/25/01 6

Traveling Salesperson problem (TSP)Seattle

San Francisco

Los Angeles MiamiAtlantaDallas

Denver

Minneapolis

Chicago

New York

Boston

Philadelphia

$119

Given (one-way) airfare between each pair of cities, find least cost Hamiltonian tour.

Page 7: 4/25/011 CSE 121/131 Programming Spring 2001 Handout 9

4/25/01 7

Radio station frequencies

Nodes are radio stations.Edges show stations that interfere with each other.

Assign one of four frequencies to each of radiostations so that interfering stations get differentfrequencies.

Page 8: 4/25/011 CSE 121/131 Programming Spring 2001 Handout 9

4/25/01 8

Adjacency list representationWon’t work to have each node have a fixed setof fields as for binary trees…

Instead, can make each node have a list ofnodes it is joined to. Adjacency list representation.

1

23

40

1 4 2

0 2

1 0

0

Page 9: 4/25/011 CSE 121/131 Programming Spring 2001 Handout 9

4/25/01 9

Adjacency matrix representation

1

23

40

00001

00000

00011

00101

10110

At position (i,j) a 1 indicatesan edge between nodes i and jand a 0 indicates no edge.

Page 10: 4/25/011 CSE 121/131 Programming Spring 2001 Handout 9

4/25/01 10

Graph variations

Graphs we have seen --- undirected graphs.

When edges have directions --- directed graphs.(One-way streets in a city.)

When edges have costs/distances on them ---weighted graphs.

Trees are special kinds of graphs. What kind?

Page 11: 4/25/011 CSE 121/131 Programming Spring 2001 Handout 9

4/25/01 11

Basic definitions

Graph G = (V,E). V is a set of vertices. E is a setof pairs of vertices.

1

23

40

1 and 2 are adjacent.

1,2,0,4 is a path.

1,2,0 is a cycle.

Graph is not connected since thereis no way to get to 3 from 1.

{0,1,2,4} is a connected component.So is {3}.

A tree is a connected graph without cycles.

Page 12: 4/25/011 CSE 121/131 Programming Spring 2001 Handout 9

4/25/01 12

Binary TreesWe saw how binary trees were useful for implementingpriority queues. However, they arise in a number of other contexts as well.

Heaps are rather special because they satisfy the structure property. Because of this, it is possible toimplement them as arrays. For other applications itis impossible or very expensive to maintain the structure property. We then have to implement binarytrees as linked structures.

One of the most basic things we want to do witha binary tree is to traverse it, i.e., to systematicallyvisit all its nodes.

Page 13: 4/25/011 CSE 121/131 Programming Spring 2001 Handout 9

4/25/01 13

Binary Tree Traversals

There are many ways of traversing a binary tree.Three of the most popular ones are

• Postorder: left, right, root.• Inorder: left, root, right.• Preorder: root, left, right.

We already saw that for expression trees: inorder traversals give infix expressions. postorder traversals give postfix expressions.Traversals arise in a lot of other situations as well.Since a binary tree is put together recursively fromsmaller trees, recursive algorithms are natural here.Alternatively we can use a stack data structure.

Page 14: 4/25/011 CSE 121/131 Programming Spring 2001 Handout 9

4/25/01 14

Iterator for postorder traversal Each iterator maintains a stack explicitly to keep track of the state.

In stack, keep track of nodes and number of times they have been popped from stack.

If a node is popped for the first time, push it back, increment “pop-counter” and visit left side. If popped for the second time, push it back, increment “pop-counter” and visit right side.

If popped for the third time, visit node itself.

Page 15: 4/25/011 CSE 121/131 Programming Spring 2001 Handout 9

4/25/01 15

Applications of Tree Traversals

v

x y

w

Computing heights and depths of nodes.

depth(v) = depth(w) + 1

ht(v) = max(ht(x), ht(y)) + 1

Use appropriate traversals to compute.

Page 16: 4/25/011 CSE 121/131 Programming Spring 2001 Handout 9

4/25/01 16

Breadth-first search:

• Start at some node, s. Put s in queue Q.• While Q is non-empty

• dequeue a node, • mark it,• put its unmarked neighbors in Q.

Assume graph is given by adjacency list.

Graph traversal

Applications?

Page 17: 4/25/011 CSE 121/131 Programming Spring 2001 Handout 9

4/25/01 17

What was this course about?

Problem solving with software Correctness of software Efficiency of software

What is problem solving? Analyzing the task, designing and refining a software solution.

What is correctness? Software is designed with a goal in mind. Does it meet this goal?

How do we choose between different correct solutions? Important criterion --- Efficiency

Page 18: 4/25/011 CSE 121/131 Programming Spring 2001 Handout 9

4/25/01 18

Correctness

Specification: captures the requirements (“logic”)

Implementation: realizes a spec (“computation”)

Conceptual tools (divide to conquer!):

Abstraction levels (implementations in terms of more basic specifications)

Modularization (horizontal and vertical)

Page 19: 4/25/011 CSE 121/131 Programming Spring 2001 Handout 9

4/25/01 19

Efficiency

Determined empirically (measurements) analytically (eg. running time analysis) complexity

Conceptual tools for complexity

Models of computation

Asymptotic comparisons

Worst-case

Amortized

Page 20: 4/25/011 CSE 121/131 Programming Spring 2001 Handout 9

4/25/01 20

Data structures

Abstract data types

Provides modularity for correctness

Critical efficiency (since they are used repeatedly)

We covered:

Lists (mutable and immutable) Stacks Queues Ranked sequences

Priority queuesBinary heaps

DictionariesSearch treesHash tables

Page 21: 4/25/011 CSE 121/131 Programming Spring 2001 Handout 9

4/25/01 21

Techniques

(Tricks of the trade…)

Linked lists, double-linked lists, end “pointer”

Array doubling

Circular arrays

Balancing binary trees (AVL)

Binary heaps as arrays

ETC.!

Page 22: 4/25/011 CSE 121/131 Programming Spring 2001 Handout 9

4/25/01 22

Java

An imperative language (most computation is in sequences of statements)

An object-oriented language (almost everything is an object!)

Small core language

Practical usefulness through JDK (Java Development Kit) classes etc.

Exploits particularly well the concepts of interface, abstract class, inheritance (with much method overriding)

GUIs • Applets

Page 23: 4/25/011 CSE 121/131 Programming Spring 2001 Handout 9

4/25/01 23

Elements of software engineering Making use of the JDK.

Specification, abstraction, modularization

Design patterns

Threads

We didn’t talk about these, but they are important (check out CSE 350): Tools for software design, eg. UML (Unified Modeling

Language) Software life cycle methodologies, software process

models. Verification, testing, formal methods. COTS vs. SE standards vs. open source