Tutorial 4 Algorithms and Data Structures Jonathan Cederberg <jonathan.cederberg@it.uu.se> Monday, October 11 th, 2010
Outline Example 1 Example 2 3 AD 10 Dept. of Information Technology - 2 - Jonathan Cederberg jonathan.cederberg@it.uu.se
Outline Example 1 Example 2 3 AD 10 Dept. of Information Technology - 3 - Jonathan Cederberg jonathan.cederberg@it.uu.se
Problem (15p) Assume that we a set of four digit numbers that we want to store in a hash table: 1066 1789 1945 1600 1915 2005 1000 Consider two hash functions hashcode 1 (x) = x mod 10 and x (x mod 1000) hashcode 2 (x) = 1000. Assume numbers arrive from left to right. a) Draw the resulting hash table if we use hashcode 1 and linear probing to resolve collisions. b) Draw the resulting hash table if we use hashcode 2 and chaining to resolve collisions. AD 10 Dept. of Information Technology - 4 - Jonathan Cederberg jonathan.cederberg@it.uu.se
Problem (15p) Assume that we a set of four digit numbers that we want to store in a hash table: 1066 1789 1945 1600 1915 2005 1000 Consider two hash functions hashcode 1 (x) = x mod 10 and x (x mod 1000) hashcode 2 (x) = 1000. Assume numbers arrive from left to right. a) Draw the resulting hash table if we use hashcode 1 and linear probing to resolve collisions. b) Draw the resulting hash table if we use hashcode 2 and chaining to resolve collisions. c) With the additional knowledge that the input numbers are all years, which of the two hashfunctions would be the better choice for arbitrary input? AD 10 Dept. of Information Technology - 4 - Jonathan Cederberg jonathan.cederberg@it.uu.se
Outline Example 1 Example 2 3 AD 10 Dept. of Information Technology - 5 - Jonathan Cederberg jonathan.cederberg@it.uu.se
Graphs - what are they good for? Provide a good representation of... Data structures (linked lists, tables, trees) The internet DNA A road net A sewer system The circulation of the blood in the body... Google uses graphs! AD 10 Dept. of Information Technology - 6 - Jonathan Cederberg jonathan.cederberg@it.uu.se
And then? With the representation, we can isolate interesting properties of the graph, and thus discover interesting properties of the underlying system. Example: A road system is abstracted as a directed graph in the natural way. What information would computing the strongly connected components of this graph give us? AD 10 Dept. of Information Technology - 7 - Jonathan Cederberg jonathan.cederberg@it.uu.se
AD 10 Dept. of Information Technology - 8 - Jonathan Cederberg jonathan.cederberg@it.uu.se
AD 10 Dept. of Information Technology - 9 - Jonathan Cederberg jonathan.cederberg@it.uu.se
Whether there is a path from every junction to all other junctions. AD 10 Dept. of Information Technology - 9 - Jonathan Cederberg jonathan.cederberg@it.uu.se
Fundamentals: DFS and BFS We look a vertex at a time, examining it and discovering its neighbours (moving the frontier). The difference lies in how when the neighbours are examined! AD 10 Dept. of Information Technology - 10 - Jonathan Cederberg jonathan.cederberg@it.uu.se
DFS When examining a node, just tag it as discovered and examine all not yet discovered neighbours first. So we examine the nodes using a LIFO policy. AD 10 Dept. of Information Technology - 11 - Jonathan Cederberg jonathan.cederberg@it.uu.se
BFS When examining a node, put all not yet discovered neighbours on hold and let them wait for their turn. So we examine the nodes using a FIFO policy. AD 10 Dept. of Information Technology - 12 - Jonathan Cederberg jonathan.cederberg@it.uu.se
The essence DFS uses LIFO, BFS uses FIFO. This means they are essentially the same, except that DFS uses a stack and BFS uses a queue. Note that this glosses over a lot of important points, read the book for more detailed explanation. AD 10 Dept. of Information Technology - 13 - Jonathan Cederberg jonathan.cederberg@it.uu.se
Outline Example 1 Example 2 3 AD 10 Dept. of Information Technology - 14 - Jonathan Cederberg jonathan.cederberg@it.uu.se
Implementation of graphs Implement some representation of directed graphs. You are advised to use adjacency list representation if you do not definitely prefer to use adjacency matrix. (4 pts) AD 10 Dept. of Information Technology - 15 - Jonathan Cederberg jonathan.cederberg@it.uu.se
Implementation of graphs Implement some representation of directed graphs. You are advised to use adjacency list representation if you do not definitely prefer to use adjacency matrix. (4 pts) Implement the possibility of doing a BFS search on your graph, starting at a user-specified vertex. The program should print (to standard out) the order in which the vertices are discovered. (3 pts) AD 10 Dept. of Information Technology - 15 - Jonathan Cederberg jonathan.cederberg@it.uu.se
Implementation of graphs Implement some representation of directed graphs. You are advised to use adjacency list representation if you do not definitely prefer to use adjacency matrix. (4 pts) Implement the possibility of doing a BFS search on your graph, starting at a user-specified vertex. The program should print (to standard out) the order in which the vertices are discovered. (3 pts) Implement the possibility of doing a DFS search on your graph, starting at a user-specified vertex. The program should print (to standard out) the order in which the vertices are discovered. (3 pts) Deadline: 27/10 AD 10 Dept. of Information Technology - 15 - Jonathan Cederberg jonathan.cederberg@it.uu.se