Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 410 Applied Algorithms Applied Algorithms Lecture #3 Data Structures.

Similar presentations


Presentation on theme: "CS 410 Applied Algorithms Applied Algorithms Lecture #3 Data Structures."— Presentation transcript:

1 CS 410 Applied Algorithms Applied Algorithms Lecture #3 Data Structures

2 CS 410 Applied Algorithms 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 CS 410 Applied Algorithms Quiz We have a 10 minute quiz today on the reading – Chapter 2.

4 CS 410 Applied Algorithms 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 CS 410 Applied Algorithms 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… ab cd

6 CS 410 Applied Algorithms 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 CS 410 Applied Algorithms 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 g[i][j]. 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 CS 410 Applied Algorithms Adjacency matrix representation: A simple example: Uses O(|V| 2 ) 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! ab cd

9 CS 410 Applied Algorithms Adjacency list representation A simple example: Uses O(|V|+|E|) 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. ab cd a b c d bcd 0 a a 0 ab 0 d 0

10 CS 410 Applied Algorithms Function representation Best when we have directed graphs. I.e. edges have an orientation. A simple example: list graph(node x) { if (node==a) return [b,c,d] else if (node==b) return [d] else if (node==c) return [] else if (node==d) return [] else return [] } ab cd

11 CS 410 Applied Algorithms 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 (node==a) return [b,c,d] else if (node==b) return [d] else if (node==c) return [] else if (node==d) return [] else return [] } ab cd 3 1 0 0 b d ? ? c ? ? ? d ? 1 ? a b c d int count [5] node child1 [5]node child2 [5] node child3 [5]

12 CS 410 Applied Algorithms 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 CS 410 Applied Algorithms Today’s 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.


Download ppt "CS 410 Applied Algorithms Applied Algorithms Lecture #3 Data Structures."

Similar presentations


Ads by Google