Applied Algorithms - PowerPoint PPT Presentation

About This Presentation
Title:

Applied Algorithms

Description:

... prepared to answer questions in class next Friday from the reading. ... Those who volunteer to discuss their program get class participation points. ... – PowerPoint PPT presentation

Number of Views:14
Avg rating:3.0/5.0
Slides: 14
Provided by: she46
Learn more at: http://web.cecs.pdx.edu
Category:

less

Transcript and Presenter's Notes

Title: Applied Algorithms


1
Applied Algorithms
  • Lecture 3
  • Data Structures

2
Homework Grading Notes
  • You must turn in your homework in class each
    Friday.
  • Give me a paper copy in class. Email is not
    convenient for me.
  • You must turn in the report from the judge.
  • Homework without a Judge report can get a maximum
    of 85.
  • Always turn in a page or two of output. Try and
    make the input for the output you turn in be
    non-standard. Test the gotchas!
  • Include some sort of high level description of
    the strategy your program uses to solve the
    problem.

3
Quiz
  • We have a 10 minute quiz today on the reading
    Chapter 2.

4
Representing Graphs
  • Graphs are ubiquitous, they appear in many
    problems.
  • Best to have some canned strategies for thinking
    about graphs.
  • Separate in your mind the logical descriptions of
    graphs, form the physical representations
  • Functions
  • Arrays
  • Matrices
  • Pointers

5
Graphs
  • A graph is a pair (V,E) where
  • V is a set of vertices
  • E is a set of edges u,v, where u,v are distinct
    vertices from V.
  • For example
  • G (a,b,c,d, a,b, a,c, a,d, b,d)
  • Examples computer networks, street layout, etc

6
Variations
  • There are many variations on this theme. For
    example, in some cases we may want to allow
  • Self loops v,v
  • Multiple edges between two vertices
    (multigraphs)
  • Labels attached to vertices by a function V ? A
  • Labels attached to edges by a function E ? B
  • Hyperedges that connect multiple vertices
    (hypergraphs)
  • etc

7
Representing graphs
  • Function Define a function that when applied to
    vertex v, returns a set of children (or a set of
    parents). Each child is a node where (v,c) is in
    the set of edges.
  • Adjacency list for each vertex v, we store a
    linked list of the vertices u that it connects to
    by a single edge.
  • Adjacency matrix a two dimensional array
    gij. An entry of 1 means that there is an
    edge between vertices i and j.
  • Pointers actually construct a heap object where
    edges are implemented by pointers

8
Adjacency matrix representation
  • A simple example
  • Uses O(V2) space, much of which will be wasted
    if the graph is sparse (i.e., relatively few
    edges).
  • Easily adapted to store information about each
    edge in the entries of the matrix.
  • Alternatively, if all we need is 0/1, then a
    single bit will do!

9
Adjacency list representation
  • A simple example
  • Uses O(VE) space, good for sparse graphs,
    more expensive for dense case (i.e., many edges).
  • Easily adapted to store information about each
    edge in each part of the linked lists.
  • Testing to see if there is an edge (u,v) is not
    O(1) we must search the adjacency list of u for
    v.

10
Function representation
  • Best when we have directed graphs. I.e. edges
    have an orientation.
  • A simple example
  • list graph(node x)
  • if (nodea) return b,c,d
  • else if (nodeb) return d
  • else if (nodec) return
  • else if (noded) return
  • else return

11
Arrays
  • When a graph has fixed in-degree (or out degree)
    the function representation has an especially
    nice implementation as a set of parallel arrays.
  • list graph(node x)
  • if (nodea) return b,c,d
  • else if (nodeb) return d
  • else if (nodec) return
  • else if (noded) return
  • else return

int count 5
node child1 5
node child2 5
node child3 5
12
In Class Problems
  • Jolly Jumper 2.8.1
  • Page 42 of the text
  • We will write this together as a class
  • Hartals 2.8.3
  • Page 45 of the text
  • Keeping in mind the rules we discussed in class,
    break into teams of two, and solve the problem.
  • Pick a different partner than you used last time.
  • Use pair programming. One person drives the
    other observes.

13
Todays Assignments
  • Read for next time
  • Chapter 3 of the text. pp 56-77
  • Be prepared to answer questions in class next
    Friday from the reading.
  • Programming assignment
  • 2.8.2 Poker Hands
  • Page 43-44
  • Write a solution
  • Submit your solution (until you get it right)
  • Hand in both your program, and the judge output.
  • Those who volunteer to discuss their program get
    class participation points. Email me solutions
    before noon on Friday, April 22.
Write a Comment
User Comments (0)
About PowerShow.com