130 likes | 145 Views
This lecture explores different methods for representing graphs and provides information about the homework grading process. Topics covered include adjacency lists, adjacency matrices, and function representation. Homework must be turned in with a judge report for full credit.
E N D
Applied Algorithms Lecture #3 Data Structures
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.
Quiz • We have a 10 minute quiz today on the reading – Chapter 2.
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
a b c d 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…
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…
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
a b c d 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!
a b c d 0 b a d 0 c a 0 d a b 0 a b c d 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.
a b c d 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 [] }
c d a b 3 ? ? d 1 b ? c 1 0 ? 0 ? ? d ? a b c d 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 [] } int count [5] node child1 [5] node child2 [5] node child3 [5]
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.
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.