algorithms and data structures - uppsala universityalgorithms and data structures jonathancederberg...

Post on 01-Apr-2020

8 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Tutorial 4Algorithms and Data Structures

Jonathan Cederberg <jonathan.cederberg@it.uu.se>

Monday, October 11th, 2010

Example

DFS vs. BFS

Lastassignment

Outline

1 Example

2 DFS vs. BFS

3 Last assignment

AD’10 Dept. of Information Technology - 2 - Jonathan Cederberg | jonathan.cederberg@it.uu.se

Example

DFS vs. BFS

Lastassignment

Outline

1 Example

2 DFS vs. BFS

3 Last assignment

AD’10 Dept. of Information Technology - 3 - Jonathan Cederberg | jonathan.cederberg@it.uu.se

Example

DFS vs. BFS

Lastassignment

Problem(15p)

Assume that we a set of four digit numbers that we want tostore in a hash table:

1066 1789 1945 1600 1915 2005 1000

Consider two hash functions hashCode1(x) = x mod 10 andhashCode2(x) = x−(x mod 1000)

1000 . Assume numbers arrive fromleft to right.a) Draw the resulting hash table if we use hashCode1 and

linear probing to resolve collisions.b) Draw the resulting hash table if we use hashCode2 and

chaining to resolve collisions.

c) With the additional knowledge that the input numbers areall years, which of the two hashfunctions would be thebetter choice for arbitrary input?

AD’10 Dept. of Information Technology - 4 - Jonathan Cederberg | jonathan.cederberg@it.uu.se

Example

DFS vs. BFS

Lastassignment

Problem(15p)

Assume that we a set of four digit numbers that we want tostore in a hash table:

1066 1789 1945 1600 1915 2005 1000

Consider two hash functions hashCode1(x) = x mod 10 andhashCode2(x) = x−(x mod 1000)

1000 . Assume numbers arrive fromleft to right.a) Draw the resulting hash table if we use hashCode1 and

linear probing to resolve collisions.b) Draw the resulting hash table if we use hashCode2 and

chaining to resolve collisions.c) With the additional knowledge that the input numbers are

all years, which of the two hashfunctions would be thebetter choice for arbitrary input?

AD’10 Dept. of Information Technology - 4 - Jonathan Cederberg | jonathan.cederberg@it.uu.se

Example

DFS vs. BFS

Lastassignment

Outline

1 Example

2 DFS vs. BFS

3 Last assignment

AD’10 Dept. of Information Technology - 5 - Jonathan Cederberg | jonathan.cederberg@it.uu.se

Example

DFS vs. BFS

Lastassignment

Graphs - what are they good for?Provide a good representation of...

Data structures (linked lists, tables, trees)The internetDNAA road netA sewer systemThe circulation of the blood in the body. . .

Google uses graphs!

AD’10 Dept. of Information Technology - 6 - Jonathan Cederberg | jonathan.cederberg@it.uu.se

Example

DFS vs. BFS

Lastassignment

And then?With the representation, we can isolate interesting properties ofthe graph, and thus discover interesting properties of theunderlying system.Example: A road system is abstracted as a directed graph inthe natural way. What information would computing thestrongly connected components of this graph give us?

AD’10 Dept. of Information Technology - 7 - Jonathan Cederberg | jonathan.cederberg@it.uu.se

Example

DFS vs. BFS

Lastassignment

AD’10 Dept. of Information Technology - 8 - Jonathan Cederberg | jonathan.cederberg@it.uu.se

Example

DFS vs. BFS

Lastassignment

Whether there is a path from every junction to all otherjunctions.

AD’10 Dept. of Information Technology - 9 - Jonathan Cederberg | jonathan.cederberg@it.uu.se

Example

DFS vs. BFS

Lastassignment

Whether there is a path from every junction to all otherjunctions.

AD’10 Dept. of Information Technology - 9 - Jonathan Cederberg | jonathan.cederberg@it.uu.se

Example

DFS vs. BFS

Lastassignment

Fundamentals: DFS and BFSWe look a vertex at a time, examining it and discoveringits neighbours (moving the frontier).The difference lies in how when the neighbours areexamined!

AD’10 Dept. of Information Technology - 10 - Jonathan Cederberg | jonathan.cederberg@it.uu.se

Example

DFS vs. BFS

Lastassignment

DFSWhen examining a node, just tag it as discovered andexamine 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

Example

DFS vs. BFS

Lastassignment

BFSWhen examining a node, put all not yet discoveredneighbours 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

Example

DFS vs. BFS

Lastassignment

The essenceDFS uses LIFO, BFS uses FIFO.This means they are essentially the same, except that DFSuses a stack and BFS uses a queue.

Note that this glosses over a lot of important points, read thebook for more detailed explanation.

AD’10 Dept. of Information Technology - 13 - Jonathan Cederberg | jonathan.cederberg@it.uu.se

Example

DFS vs. BFS

Lastassignment

Outline

1 Example

2 DFS vs. BFS

3 Last assignment

AD’10 Dept. of Information Technology - 14 - Jonathan Cederberg | jonathan.cederberg@it.uu.se

Example

DFS vs. BFS

Lastassignment

Implementation of graphsImplement some representation of directed graphs. Youare advised to use adjacency list representation if you donot definitely prefer to use adjacency matrix. (4 pts)

Implement the possibility of doing a BFS search on yourgraph, starting at a user-specified vertex. The programshould print (to standard out) the order in which thevertices are discovered. (3 pts)Implement the possibility of doing a DFS search on yourgraph, starting at a user-specified vertex. The programshould print (to standard out) the order in which thevertices are discovered. (3 pts)

Deadline: 27/10

AD’10 Dept. of Information Technology - 15 - Jonathan Cederberg | jonathan.cederberg@it.uu.se

Example

DFS vs. BFS

Lastassignment

Implementation of graphsImplement some representation of directed graphs. Youare advised to use adjacency list representation if you donot definitely prefer to use adjacency matrix. (4 pts)Implement the possibility of doing a BFS search on yourgraph, starting at a user-specified vertex. The programshould print (to standard out) the order in which thevertices are discovered. (3 pts)

Implement the possibility of doing a DFS search on yourgraph, starting at a user-specified vertex. The programshould print (to standard out) the order in which thevertices are discovered. (3 pts)

Deadline: 27/10

AD’10 Dept. of Information Technology - 15 - Jonathan Cederberg | jonathan.cederberg@it.uu.se

Example

DFS vs. BFS

Lastassignment

Implementation of graphsImplement some representation of directed graphs. Youare advised to use adjacency list representation if you donot definitely prefer to use adjacency matrix. (4 pts)Implement the possibility of doing a BFS search on yourgraph, starting at a user-specified vertex. The programshould print (to standard out) the order in which thevertices are discovered. (3 pts)Implement the possibility of doing a DFS search on yourgraph, starting at a user-specified vertex. The programshould print (to standard out) the order in which thevertices are discovered. (3 pts)

Deadline: 27/10

AD’10 Dept. of Information Technology - 15 - Jonathan Cederberg | jonathan.cederberg@it.uu.se

top related