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

19
Tutorial 4 Algorithms and Data Structures Jonathan Cederberg <[email protected]> Monday, October 11 th , 2010

Upload: others

Post on 01-Apr-2020

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Algorithms and Data Structures - Uppsala UniversityAlgorithms and Data Structures JonathanCederberg Monday,October11th,2010. Example DFS vs. BFS

Tutorial 4Algorithms and Data Structures

Jonathan Cederberg <[email protected]>

Monday, October 11th, 2010

Page 2: Algorithms and Data Structures - Uppsala UniversityAlgorithms and Data Structures JonathanCederberg Monday,October11th,2010. Example DFS vs. BFS

Example

DFS vs. BFS

Lastassignment

Outline

1 Example

2 DFS vs. BFS

3 Last assignment

AD’10 Dept. of Information Technology - 2 - Jonathan Cederberg | [email protected]

Page 3: Algorithms and Data Structures - Uppsala UniversityAlgorithms and Data Structures JonathanCederberg Monday,October11th,2010. Example DFS vs. BFS

Example

DFS vs. BFS

Lastassignment

Outline

1 Example

2 DFS vs. BFS

3 Last assignment

AD’10 Dept. of Information Technology - 3 - Jonathan Cederberg | [email protected]

Page 4: Algorithms and Data Structures - Uppsala UniversityAlgorithms and Data Structures JonathanCederberg Monday,October11th,2010. Example DFS vs. BFS

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 | [email protected]

Page 5: Algorithms and Data Structures - Uppsala UniversityAlgorithms and Data Structures JonathanCederberg Monday,October11th,2010. Example DFS vs. BFS

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 | [email protected]

Page 6: Algorithms and Data Structures - Uppsala UniversityAlgorithms and Data Structures JonathanCederberg Monday,October11th,2010. Example DFS vs. BFS

Example

DFS vs. BFS

Lastassignment

Outline

1 Example

2 DFS vs. BFS

3 Last assignment

AD’10 Dept. of Information Technology - 5 - Jonathan Cederberg | [email protected]

Page 7: Algorithms and Data Structures - Uppsala UniversityAlgorithms and Data Structures JonathanCederberg Monday,October11th,2010. Example DFS vs. BFS

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 | [email protected]

Page 8: Algorithms and Data Structures - Uppsala UniversityAlgorithms and Data Structures JonathanCederberg Monday,October11th,2010. Example DFS vs. BFS

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 | [email protected]

Page 9: Algorithms and Data Structures - Uppsala UniversityAlgorithms and Data Structures JonathanCederberg Monday,October11th,2010. Example DFS vs. BFS

Example

DFS vs. BFS

Lastassignment

AD’10 Dept. of Information Technology - 8 - Jonathan Cederberg | [email protected]

Page 10: Algorithms and Data Structures - Uppsala UniversityAlgorithms and Data Structures JonathanCederberg Monday,October11th,2010. Example DFS vs. BFS

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 | [email protected]

Page 11: Algorithms and Data Structures - Uppsala UniversityAlgorithms and Data Structures JonathanCederberg Monday,October11th,2010. Example DFS vs. BFS

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 | [email protected]

Page 12: Algorithms and Data Structures - Uppsala UniversityAlgorithms and Data Structures JonathanCederberg Monday,October11th,2010. Example DFS vs. BFS

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 | [email protected]

Page 13: Algorithms and Data Structures - Uppsala UniversityAlgorithms and Data Structures JonathanCederberg Monday,October11th,2010. Example DFS vs. BFS

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 | [email protected]

Page 14: Algorithms and Data Structures - Uppsala UniversityAlgorithms and Data Structures JonathanCederberg Monday,October11th,2010. Example DFS vs. BFS

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 | [email protected]

Page 15: Algorithms and Data Structures - Uppsala UniversityAlgorithms and Data Structures JonathanCederberg Monday,October11th,2010. Example DFS vs. BFS

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 | [email protected]

Page 16: Algorithms and Data Structures - Uppsala UniversityAlgorithms and Data Structures JonathanCederberg Monday,October11th,2010. Example DFS vs. BFS

Example

DFS vs. BFS

Lastassignment

Outline

1 Example

2 DFS vs. BFS

3 Last assignment

AD’10 Dept. of Information Technology - 14 - Jonathan Cederberg | [email protected]

Page 17: Algorithms and Data Structures - Uppsala UniversityAlgorithms and Data Structures JonathanCederberg Monday,October11th,2010. Example DFS vs. BFS

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 | [email protected]

Page 18: Algorithms and Data Structures - Uppsala UniversityAlgorithms and Data Structures JonathanCederberg Monday,October11th,2010. Example DFS vs. BFS

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 | [email protected]

Page 19: Algorithms and Data Structures - Uppsala UniversityAlgorithms and Data Structures JonathanCederberg Monday,October11th,2010. Example DFS vs. BFS

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 | [email protected]