CSC231m Lecture 16 - PowerPoint PPT Presentation

1 / 45
About This Presentation
Title:

CSC231m Lecture 16

Description:

All of the preceding lists are implemented as arrays. ... Insertion Discussion. A New node is created, ... Linked Lists Insert. 43. Deletion Discussion ... – PowerPoint PPT presentation

Number of Views:31
Avg rating:3.0/5.0
Slides: 46
Provided by: markshutch
Category:
Tags: csc231m | lecture

less

Transcript and Presenter's Notes

Title: CSC231m Lecture 16


1
CSC-231m Lecture 16
  • Random Numbers
  • Timing Operations
  • Map-Based Games
  • Lists and List Operations

2
Todays Topics
  • Generating random numbers in MATLAB.
  • Timing operations.
  • Designing map-based games (finite state
    machines).
  • Types of lists.
  • Implementing lists
  • List operations.

3
Timing Operations
  • MATLAB has built-in timing functions so we can
    time operations
  • tic() starts the stopwatch timer.
  • toc() stops the timer, and returns the elapsed
    time in seconds.
  • Usage
  • tic
  • statements
  • iTime toc
  • disp ((int2str(iTime))

4
Random Numbers
  • First, we call the Random() method (function) of
    the Java Utility library
  • fNum java.util.Random
  • Then we normalize the range using the
    nextDouble() method of fNum object
  • fRand fNum.nextDouble (fMAX - fMIN) fMIN)
  • And finally we convert this to an appropriate
    integer, if desired
  • iNumber int32(fRand)
  • MATLAB also has a random() and rand().

5
More on Javas Random()
  • Random() can be called without an argument, in
    which case the seed is the milliseconds from
    the system clock.
  • Random() can also be called with a seed
  • Random (rnd), where rnd is a long.
  • Regardless, the number generated has a value
    between 0.0 and 1.0.

6
Other Random Stuff
  • betarndRandom numbers from the beta distributio
  • binorndRandom numbers from the binomial
    distribution
  • chi2rndRandom numbers from the chi-square (?2)
    distribution
  • evrndRandom matrices from the extreme value
    distribution
  • exprndGenerate random numbers from the
    exponential distribution
  • frndRandom numbers from the F distribution
  • gamrndRandom numbers from the gamma distributiong
  • eorndRandom numbers from the geometric
    distribution
  • gevrndRandom arrays from the generalized extreme
    value distribution
  • gprndRandom arrays from the generalized Pareto
    distribution
  • hygerndRandom numbers from the hypergeometric
    distribution
  • iwishrndGenerate inverse Wishart random matrix
  • lhsdesignGenerate a latin hypercube sample
  • lhsnormGenerate a latin hypercube sample with a
    normal distribution
  • lognrndRandom matrices from the lognormal
    distribution
  • mvnrndRandom matrices from the multivariate
    normal distribution
  • mvtrndRandom matrices from the multivariate t
    distribution
  • nbinrndRandom matrices from a negative binomial
    distributionnc
  • frndRandom matrices from the noncentral F
    distribution

7
Examples in Manual
  • SP-B, Guess-A-Number, uses
  • tic()
  • toc()
  • random numbers.

8
Map-Based Games
9
State Transition Table
  • In any given state (room) the player can move N,
    E, S, or W to another state.
  • If no state (room) exists in the chosen
    direction, the player stays in the current room.
  • Within each state, the player is advised of the
    valid directions he/she can go.

10
A Bit of Code
  • switch (cInput)
  • case n
  • iState aiMap(iState,1)
  • case e
  • iState aiMap(iState,2)
  • case s
  • iState aiMap(iState,3)
  • case w
  • iState aiMap(iState,4)
  • otherwise
  • disp (Invalid input cInput)
  • end

11
Goodies
  • A room can contain nothing, a goodie to pick up,
    or the goal you are seeking.
  • If the room contains nothing, you can drop a
    goodie.
  • An rule is that you can only carry four (an
    arbitrary number) items. If you already have four
    items, you must move to an empty room to drop
    an item, then move back to the other room to pick
    something up.

12
Yet Another Array
  • An array defines where stuff initially is placed.
  • Each state/room can contain one item, the goal,
    or nothing.

13
One More Array
  • Another array keeps track of what you have picked
    up.
  • If you drop something in an empty room, the
    goodie there changes from Nothing to whatever it
    is you drop.

14
Overall Operation
  • You are standing in the Kitchen.
  • Doors lead North and East.
  • The Backpack is here.
  • The Cat is not here.
  • You are currently carrying
  • Cycle Helmet
  • Windbreaker
  • Enter P to pick something up,
  • D to drop something,
  • M to move to another room,
  • or Q to quit the game

15
Scoring
  • You have to start with some number of points.
  • Each time you move to another room, you lose 10
    points.
  • Each time you drop something, you lose 25 points.
  • Each time you pick something up, you gain 50
    points.
  • When you enter the room containing the goal, you
    gain 100 points and the game is over.
  • All of these numbers are arbitrary.

16
In The Emacs Editor
17
The Game
18
Usually In UNIX
19
(No Transcript)
20
(No Transcript)
21
Types of Lists
  • Unsorted List
  • Sorted List
  • Linked List
  • Stack (LIFO Last In, First Out)
  • Queue (FIFO First In, First Out)

22
General List Operations
  • Determine the length of the list (number of
    items).
  • Check to see if list is empty.
  • Check to see if list is full.
  • Check to see if item is present in list.
  • Insert item into list.
  • Delete item from list.
  • Save list to file.
  • Print list.
  • Sort list (sorted lists only).

23
Unsorted Lists -- Operations
  • Length determine length of list.
  • Empty check for list length of 0.
  • Full check for list length of MAXITEMS.
  • Present traverse list looking for item.
  • Insert add item to end of list and increase
    length.
  • Delete move last item in list to place where
    item is to be deleted and decrease length.
  • Save write list to file.
  • Print print list to display.

24
Unsorted Lists Insert Delete
25
Sorted Lists -- Operations
  • Length determine length of list.
  • Empty check for list length of 0.
  • Full check for list length of MAXITEMS.
  • Present search for item in list using binary
    search.
  • Insert add item to proper place in list and
    increase length.
  • Delete delete item from list and shift other
    items down to fill in the hole, then decrease
    length.
  • Save write list to file.
  • Print print list to display.
  • Sort sort items in list.

26
Sorted Lists Insert Delete
27
Stacks -- Operations
  • Length determine length of list.
  • Empty check for list length of 0.
  • Full check for list length of MAXITEMS.
  • Present traverse list looking for item.
  • Insert (Push) add item to top of list and
    increase length.
  • Delete (Pop) delete item from top of list and
    decrease length.
  • Save write list to file.
  • Print print list to display.

28
Stacks Insert Delete
29
Queues -- Operations
  • Length determine length of list.
  • Empty check for list length of 0.
  • Full check for list length of MAXITEMS.
  • Present traverse list looking for item.
  • Insert add item to top of list and increase
    length.
  • Delete delete item from bottom of list and
    decrease length.
  • Save write list to file.
  • Print print list to display.

30
Queues Insert Delete
31
Searching
  • There are two basic searching algorithms
  • Selection or sequential search.
  • Works with any type of list.
  • Start at with first item in list, compare to test
    value, keep going until test value found or end
    of list reached.
  • Binary search.
  • Only works with sorted lists.
  • Start in middle, compare to test value, split
    search region in half, keep going until test
    value found or not.

32
Man vs. Computer
  • Man, unlike a computer, possesses omniscience.
  • A computer can only deal with 1s and 0s and
    cannot see.
  • Man can see the whole picture and act
    accordingly.
  • You know the 3? needs to go first, but the
    computer has to compare until it finds that card,
    then swaps it with the 10?.

10?
A?
6?
7?
3?
4?
33
Sorting
  • There are many sorting algorithms, some faster
    than others
  • Sequential sort list is traversed to find
    minimum value, that value is moved to first place
    in list, list length is shortened by one, process
    repeated until list is in order.
  • Bubble sort first item in list is compared to
    next item in list. If next item is smaller, items
    are swapped. This continues until the list is
    sorted. It gets its name from smaller values
    bubbling to the top of the list.
  • Quick sort
  • Merge sort

34
List Implementation Arrays
  • All of the preceding lists are implemented as
    arrays.
  • In most languages, an array is declared and
    allocated for a specific size. A special variable
    keeps track of how many cells in the array are
    used.
  • In MATLAB, as a list is expanded or contracted,
    memory is allocated and deallocated
    automatically, so the array is always as
    large/small as it needs to be.

35
MATLAB Implementation
  • function aiList aiInsert (aiList)
  • aiInsert Inserts a value in a non-full list
  • Precondition List is not full.
  • iNum 0
  • disp ('Enter a number to insert into the list
    ')
  • iNum input ('')
  • aiList aiList iNum
  • return
  • end function aiInsert()

36
Implementation, Continued
  • function aiList aiDelete (aiList)
  • aiDelete Deletes a value from a non-empty list
  • iLen 0
  • bStatus 0
  • disp ('Enter number to delete ')
  • iNum input ('')
  • bStatus bIsPresent (aiList, iNum)
  • disp (bStatus)
  • if (bStatus -1)
  • aiList (bStatus)
  • else
  • disp ('Cannot delete value, not found in
    list.')
  • end
  • return
  • end function aiDelete()

37
Implementation, Concluded
  • function bStatus bIsPresent (aiList, iNum)
  • bIsPresent Determines if a value is present in
    the list
  • iLen 0
  • iIndex 1
  • bStatus -1
  • iLen length (aiList)
  • while ((bStatus -1) (iIndex lt iLen))
  • if (aiList(iIndex) iNum)
  • bStatus iIndex
  • end
  • iIndex iIndex 1
  • end
  • return
  • end function bIsPresent()

38
Linked Lists
  • A more efficient method of implementing a list is
    to create a linked list.
  • Each node of the list contains a value and a
    pointer (address of) the next node.
  • Nodes are added or deleted as needed.
  • The list must always be accessed from the
    beginning node.
  • MATLAB does not support pointers, so are limited
    to array-based lists.

39
Linked Lists -- Operations
  • Length determine length of list.
  • Empty check for list length of 0.
  • Full check for list length of MAXITEMS.
  • Present traverse list looking for item.
  • Insert add item to end of list and increase
    length.
  • Delete move last item in list to place where
    item is to be deleted and decrease length.
  • Save write list to file.
  • Print print list to display.

40
Linked List Nodes
  • A Head pointer points to the first node in the
    list.
  • A Current pointer points to the current node in
    the traversal of the list.
  • A Previous pointer points to the previous node in
    the traversal of the list.
  • The last node in the list has a nullified pointer.

41
Insertion Discussion
  • A New node is created, with a null pointer.
  • The Current pointer points to the node after
    where the New node is to be inserted.
  • The Previous pointer points to the node before
    where the New node is to be inserted.
  • The New node is assigned to the Current node. The
    node the Previous pointer is pointing to is
    reassigned to the New node.
  • The Current and Previous pointers can be
    nullified and the New node is in its proper
    location.

42
Linked Lists Insert
43
Deletion Discussion
  • The Current pointer points to the node to be
    deleted.
  • The Previous pointer points to the node before
    the node to be deleted.
  • The node the Previous pointer points to is
    reassigned to the node after the node to be
    deleted (the node pointed to by the node the
    Current pointer is pointing to).
  • The node to be deleted is deallocated.
  • The Current pointer is nullified.

44
Linked Lists Delete
45
Todays Lessons
  • You should be able to get a random number.
  • You should be able to time some operation, for
    some unknown reason.
  • You could design a map-based game, a command-line
    interface adventure game.
  • You should understand lists and operations on
    various types of lists.
  • You should understand why we can sort better and
    than a computer can.
Write a Comment
User Comments (0)
About PowerShow.com