Data Structures in Java Session 24 Instructor: Bert Huang - - PowerPoint PPT Presentation

data structures in java
SMART_READER_LITE
LIVE PREVIEW

Data Structures in Java Session 24 Instructor: Bert Huang - - PowerPoint PPT Presentation

Data Structures in Java Session 24 Instructor: Bert Huang http://www.cs.columbia.edu/~bert/courses/3134 Announcements Homework 6 due Dec. 10, last day of class Final exam Thursday, Dec. 17 th , 4-7 PM, Hamilton 602 (this room) same


slide-1
SLIDE 1

Data Structures in Java

Session 24 Instructor: Bert Huang http://www.cs.columbia.edu/~bert/courses/3134

slide-2
SLIDE 2

Announcements

  • Homework 6 due Dec. 10, last day of class
  • Final exam Thursday, Dec. 17th, 4-7 PM,

Hamilton 602 (this room)

  • same format as midterm (open book/notes)
slide-3
SLIDE 3

Review

  • Note about hw4: rehashing order
  • Finish discussion of complexity
  • Polynomial Time Approximation Schemes
  • Graph Isomorphism
  • k-d trees
slide-4
SLIDE 4

Todayʼs Plan

  • A couple topics on data structures in

Artificial Intelligence:

  • Game trees
  • Graphical Models
  • Final Review (part 1)
slide-5
SLIDE 5

Artificial Intelligence

  • Sub-field of Computer Science concerned

with algorithms that behave intelligently

  • or if we're truly ambitious, optimally.
  • An AI program is commonly called an

agent

  • which makes decisions based on its

percepts

slide-6
SLIDE 6

A.I. in Games

  • AI still needs to simplify the environment

for its agents, so games are a nice starting point

  • Many board games are turn-based, so

we can take some time to compute a good decision at each turn

  • Deterministic turn-based games can be

represented as game trees

slide-7
SLIDE 7

Game Trees

  • The root node is the starting state of the game
  • Children correspond to possible moves
  • If 2-player, every other level is the computer's

turn

  • The other levels are the adversary's turns
  • In a simple game, we can consider/store the

whole tree, make decisions based on the subtrees

slide-8
SLIDE 8

Partial Tic-Tac-Toe Game Tree

X X X X O X O X O X O X O X O X X X O X X O X O X X O O X X O O X X X O O X X O X O X X X O O X

slide-9
SLIDE 9

Tree Strategy

  • Thinking about the game as a tree helps
  • rganize computational strategy
  • If adversary plays optimally, we can define

the optimal strategy via the minimax algorithm

  • Assume the adversary will play the optimal

move at the next level. Use that result to decide which move is optimal at current level.

slide-10
SLIDE 10

Result

Simple Tree

Our Turn

Win Lose Win Win Lose Win

slide-11
SLIDE 11

Result

Numerical Rewards

Our Turn

+100

  • 1

+1 +2

  • 1

+1

slide-12
SLIDE 12

Minimax Details

  • Depth first search (postorder) to find leaves;

propagate information up

  • Adversary also assume you will play optimally
  • Impossible to store full tree for most games,

use heuristic measures

  • e.g., Chess piece values, # controlled

squares

  • Cut off after a certain level
slide-13
SLIDE 13

Pruning

  • We can also ignore parts of the tree if we

see a subtree that can't possibly be better than one we saw earlier

  • This is called alpha-beta pruning
  • Figure from wikipedia article on alpha-beta pruning
slide-14
SLIDE 14

Search

  • Some puzzles can be thought of as

trees too

  • 15-puzzle, Rubik's Cube, Sudoku
  • Discrete moves move from current state

to children states

  • A.I. wants to find the solution state

efficiently

slide-15
SLIDE 15

8-puzzle

8 5 3 2 1 4 6 7 8 5 3 2 1 4 6 7 8 5 3 2 1 6 7 4 8 5 3 2 1 4 6 7 8 5 3 2 4 6 1 7 8 5 3 2 1 4 6 7 8 5 3 2 1 6 7 4 8 5 2 1 3 6 7 4 8 5 3 2 1 4 6 7

slide-16
SLIDE 16

Simple Idea

  • Breadth first search; level-order
  • Try every move from current state
  • Try 2 moves from current state
  • Try 3 moves from current state
  • ...
slide-17
SLIDE 17

Another Idea

  • Depth first search
  • Try a move
  • Try another move...
  • If we get stuck, backtrack
slide-18
SLIDE 18

Heuristic Search

  • The main problem is without any

knowledge, we are guessing arbitrarily

  • Instead, design a heuristic and choose

the next state to try according to heuristic

  • e.g., # of tiles in the correct location,

distance from maze goal

slide-19
SLIDE 19

Probabilistic Inference

  • Some of these decisions are too hard to

compute exactly, and often there is insufficient information to make an exact decision

  • Instead, model uncertainty via probability
  • An important application for graph theory

is using graphs to represent probabilistic independence

slide-20
SLIDE 20

Independent Coins

  • 1. Suppose I flip coin twice, what is the

probability of both flips landing heads?

  • 2. Compare to if we flip a coin, and if it

lands heads, we flip a second coin. What is the probability of two heads?

  • In Scenario 1, we can reason with less

computation by taking advantage of independence

slide-21
SLIDE 21

A Simple Bayesian Network

Subway runs Rain Cloudy Construction

slide-22
SLIDE 22

Inference Rules of Thumb

  • Trees and DAGs are easier to reason
  • We can use similar strategy to

Topological sort:

  • Only compute probability once all

incoming neighbors have been computed

  • Cyclic graphs are difficult; NP-hard in some

settings

slide-23
SLIDE 23

About the Final

  • Theory only (no programming)
  • Bring your book and notes
  • No electronic devices
  • Covers both halves of the semester,

mostly 2nd half.

slide-24
SLIDE 24

Course Topics

  • Lists, Stacks, Queues
  • General Trees
  • Binary Search Trees
  • AVL Trees
  • Splay Trees
  • Tries
  • Priority Queues

(heaps)

  • Hash Tables
  • Graphs
  • Topological Sort,

Shortest Paths, Spanning Tree

  • Disjoint Sets
  • Sorting Algorithms
  • Complexity Classes
  • kd-Trees
slide-25
SLIDE 25

Definitions

  • For N greater than some constant, we

have the following definitions:

  • There exists some constant c such that

cf(N) bounds T(N)

T(N) = O(f (N)) ← T(N) ≤ cf (N) T(N) = Ω(g(N)) ← T(N) ≥ cg(N) T(N) = Θ(h(N)) ← T(N) = O(h(N)) T(N) = Ω(h(N))

slide-26
SLIDE 26

Abstract Data Type: Lists

  • An ordered series of objects
  • Each object has a previous and next
  • Except first has no prev., last has no next
  • We can insert an object (at location k)
  • We can remove an object (at location k)
  • We can read an object from (location k)
slide-27
SLIDE 27

List Methods

  • Insert object (at index)
  • Delete by index
  • Get by index
slide-28
SLIDE 28

Stack Definition

  • Essentially a restricted List
  • Two (main) operations:
  • Push(AnyType x)
  • Pop()
  • Analogy – Cafeteria Trays, PEZ
slide-29
SLIDE 29

Stack Implementations

  • Linked List:
  • Push(x) <-> add(x) <-> add(x,0)
  • Pop() <-> remove(0)
  • Array:
  • Push(x) <-> Array[k] = x; k = k+1;
  • Pop() <-> k = k-1; return Array[k]
slide-30
SLIDE 30

Queue ADT

  • Stacks are Last In First Out
  • Queues are First In First Out, first-come

first-served

  • Operations: enqueue and dequeue
  • Analogy: standing in line, garden hose, etc
slide-31
SLIDE 31

Queue Implementation

  • Linked List
  • add(x,0) to enqueue, remove(N-1) to dequeue
  • Array List wonʼt work well!
  • add(x,0) is expensive
  • Solution: use a circular array
slide-32
SLIDE 32

Circular Array

  • Donʼt shift after removing from array list
  • Keep track of start and end of queue
  • When run out of space, wrap around;

modular arithmetic

  • When array is full, increase size using

list tactic

slide-33
SLIDE 33

Tree Implementation

  • Many possible implementations
  • One approach: each node stores a list of

children

  • public class TreeNode<T> {

T Data; Collection<TreeNode<T>> myChildren; }

slide-34
SLIDE 34

Tree Traversals

  • Suppose we want to print all nodes in a tree
  • What order should we visit the nodes?
  • Preorder - read the parent before its children
  • Postorder - read the parent after its children
slide-35
SLIDE 35

Preorder vs. Postorder

  • // parent before children

preorder(node x) print(x) for child : myChildren preorder(child)

  • // parent after children

postorder(node x) for child : myChildren postorder(child) print(x)

slide-36
SLIDE 36

Binary Trees

  • Nodes can only have two children:
  • left child and right child
  • Simplifies implementation and logic
  • public class BinaryNode<T> {

T element; BinaryNode<T> left; BinaryNode<T> right; }

  • Provides new inorder traversal
slide-37
SLIDE 37

Inorder Traversal

  • Read left child, then parent, then right child
  • Essentially scans whole tree from left to right
  • inorder(node x)

inorder(x.left) print(x) inorder(x.right)

slide-38
SLIDE 38

Search (Tree) ADT

  • ADT that allows insertion, removal, and

searching by key

  • A key is a value that can be compared
  • In Java, we use the Comparable interface
  • Comparison must obey transitive property
  • Search ADT doesnʼt use any index
slide-39
SLIDE 39

Binary Search Tree

  • Binary Search Tree Property:

Keys in left subtree are less than root. Keys in right subtree are greater than root.

  • BST property holds for all subtrees of a BST

10 8 15 3 12 9

slide-40
SLIDE 40

Inserting into a BST

  • Compare new value to current node, if greater, insert

into right subtree, if lesser, insert into left subtree

  • insert(x, Node t)

if (t == null) return new Node(x) if (x > t.key), then t.right = insert(x, t.right) if (x < t.key), then t.left = insert(x, t.left) return t

10 8 15 3 12 9

slide-41
SLIDE 41

Searching a BST

  • findMin(t) // return left-most node

if (t.left == null) return t.key else return findMin(t.left)

  • search(x,t) // similar to insert

if (t == null) return false if (x == t.key) return true if (x > t.key), then return search(x, t.right) if (x < t.key), then return search(x, t.left)

slide-42
SLIDE 42

Deleting from a BST

  • Removing a leaf is easy,

removing a node with

  • ne child is also easy
  • Nodes with no

grandchildren are easy

  • What about nodes with

grandchildren?

10 8 15 3 12 9

slide-43
SLIDE 43

A Removal Strategy

  • First, find node to be removed, t
  • Replace with the smallest node

from the right subtree

  • a = findMin(t.right);

t.key = a.key;

  • Then delete original smallest

node in right subtree remove(a.key, t.right)

10 8 15 3 12 9 root

slide-44
SLIDE 44

AVL Trees

  • Motivation: want height of tree to be

close to log N

  • AVL Tree Property:

For each node, all keys in its left subtree are less than the nodeʼs and all keys in its right subtree are greater. Furthermore, the height of the left and right subtrees differ by at most 1

slide-45
SLIDE 45

AVL Tree Visual

+

  • +
slide-46
SLIDE 46

Tree Rotations

  • To balance the tree after an insertion

violates the AVL property,

  • rearrange the tree; make a new node

the root.

  • This rearrangement is called a

rotation.

  • There are 2 types of rotations.
slide-47
SLIDE 47

AVL Tree Visual: Before insert

b a 3 1 2

slide-48
SLIDE 48

AVL Tree Visual: After insert

b a 3 1 2

slide-49
SLIDE 49

AVL Tree Visual: Single Rotation

b a 3 1 2

slide-50
SLIDE 50

AVL Tree Single Rotation

  • Works when new node is added to
  • uter subtree (left-left or right-right)
  • What about inner subtrees? (left-right or

right-left)

slide-51
SLIDE 51

AVL Tree Visual: Before Insert 2

b a 1 c 2 3 4

slide-52
SLIDE 52

AVL Tree Visual: After Insert 2

b a 1 c 3 2 4

slide-53
SLIDE 53

AVL Tree Visual: Double Rotation

b a 1 c 3 2 4

slide-54
SLIDE 54

AVL Tree Visual: Double Rotation

b a 1 c 2 4 3

slide-55
SLIDE 55

Splay Trees

  • Like AVL trees, use the standard binary

search tree property

  • After any operation on a node, make

that node the new root of the tree

  • Make the node the root by repeating
  • ne of two moves that make the tree

more spread out

slide-56
SLIDE 56

Easy cases

  • If node is root, do nothing
  • If node is child of root, do single AVL

rotation

  • Otherwise, node has a grandparent,

and there are two cases

slide-57
SLIDE 57

Case 1: zig-zag

  • Use when the node is the right child of a

left child (or left-right)

  • Double rotate, just like AVL tree

a b c w x y z a b c w x y z

slide-58
SLIDE 58

Case 2: zig-zig

  • We canʼt use the single-rotation

strategy like AVL trees

  • Instead we use a different process, and

weʼll compare this to single-rotation

slide-59
SLIDE 59

Case 2: zig-zig

  • Use when node is the right-right child (or left-left)
  • Reverse the order of grandparent->parent->node
  • Make it node->parent->grandparent

a b c y w x z a b c y w x z

slide-60
SLIDE 60

Priority Queues

  • New abstract data type Priority Queue
  • Insert: add node with key
  • deleteMin: delete the node with

smallest key

  • findMin: access the node with

smallest key

  • (increase/decrease priority)
slide-61
SLIDE 61

Heap Implementation

  • Binary tree with special

properties

  • Heap Structure Property:

all nodes are full*

  • Heap Order Property:

any node is smaller than its children

A B C

A < B A < C C ? B

slide-62
SLIDE 62

Array Implementation

  • A full tree is regular: we can store in an array
  • Root at A[1]
  • Rootʼs children at A[2], A[3]
  • Node i has children at 2i and (2i+1)
  • Parent at floor(i/2)
  • No links necessary, so much faster (but only

constant speedup)

slide-63
SLIDE 63

Insert

  • To insert key X, create a hole in

bottom level

  • Percolate up
  • Is holeʼs parent is less than X
  • If so, put X in hole, heap
  • rder satisfied
  • If not, swap hole and parent

and repeat

slide-64
SLIDE 64

DeleteMin

  • Save root node, and delete,

creating a hole

  • Take the last element in the heap X
  • Percolate down:
  • is X is less than holeʼs children?
  • if so, weʼre done
  • if not, swap hole and smallest

child and repeat

slide-65
SLIDE 65

buildHeap

  • Start at deepest non-leaf node
  • in array, this is node N/2
  • percolateDown on all nodes in

reverse level-order

  • for i = N/2 to 1

percolateDown(i)

slide-66
SLIDE 66

Heap Operations

  • Insert – O(log N)
  • deleteMin – O(log N)
  • change key – O(log N)
  • buildHeap – O(N)
slide-67
SLIDE 67

Reading

  • pre-midterm: Weiss Ch. 2, 3, 4, 6
  • post-midterm: Weiss Ch. 5, 7, 8, 9, 12.6
  • See schedule on class website for

specific sections (i.e., which to skip)