# Topological Sort and Hashing

## Presentation on theme: "Topological Sort and Hashing"— Presentation transcript:

Topological Sort and Hashing
Presented by Phillip Nguyen Truong Nguyen

Topological Sort A topological sort of a directed graph is one in which if there is a path from v to w, v comes before w in the ordering The graph must be acyclic (no cycles exist) In a graph with no cycles, there must always be at least one vertex with 0 indegree, we will start at one of these vertices

Applications A topological sort is useful in something like a course prerequisite system, where you know what classes you need to take before you can take others Planning and scheduling. Building houses.

Graph Algorithms: Topological Sort
The topological sorting problem: given a directed, acyclic graph G = (V, E) , find a linear ordering of the vertices such that for all (v, w)  E, v precedes w in the ordering. B C A F D E

Graph Algorithms: Topological Sort
The topological sorting problem: given a directed, acyclic graph G = (V, E) , find a linear ordering of the vertices such that for all (v, w)  E, v precedes w in the ordering. B C A A B F C D E F D E

Graph Algorithms: Topological Sort
The topological sorting problem: given a directed, acyclic graph G = (V, E) , find a linear ordering of the vertices such that for all (v, w)  E, v precedes w in the ordering. Any linear ordering in which all the arrows go to the right. B C A A B F C D E F D E

Graph Algorithms: Topological Sort
The topological sorting problem: given a directed, acyclic graph G = (V, E) , find a linear ordering of the vertices such that for all (v, w)  E, v precedes w in the ordering. Any linear ordering in which all the arrows go to the right. B C A A B E C D F F D E This is not a topological ordering.

Graph Algorithms: Topological Sort
The topological sorting algorithm: Identify the subset of vertices that have no incoming edge. B C A F D E

Graph Algorithms: Topological Sort
The topological sorting algorithm: Identify the subset of vertices that have no incoming edge. (In general, this subset must be nonempty—why?) B C A F D E

Graph Algorithms: Topological Sort
The topological sorting algorithm: Identify the subset of vertices that have no incoming edge. (In general, this subset must be nonempty—because the graph is acyclic.) B C A F D E

Graph Algorithms: Topological Sort
The topological sorting algorithm: Identify the subset of vertices that have no incoming edge. Select one of them. B C A F D E

Graph Algorithms: Topological Sort
The topological sorting algorithm: Remove it, and its outgoing edges, and add it to the output. B C A F D E

Graph Algorithms: Topological Sort
The topological sorting algorithm: Again, identify the subset of vertices that have no incoming edge, . . . B C A F D E

Graph Algorithms: Topological Sort
The topological sorting algorithm: Again, identify the subset of vertices that have no incoming edge, select one of them, . . . B C A F D E

Graph Algorithms: Topological Sort
The topological sorting algorithm: Again, identify the subset of vertices that have no incoming edge, select one of them, remove it and any outgoing edges, and put it in the output. B C A F D E

Graph Algorithms: Topological Sort
The topological sorting algorithm: Again, identify the subset of vertices that have no incoming edge, select one of them, remove it and any outgoing edges, and put it in the output. B C A F D E

Graph Algorithms: Topological Sort
The topological sorting algorithm: Again, identify the subset of vertices that have no incoming edge, select one of them, remove it and any outgoing edges, and put it in the output. C A F B D E

Graph Algorithms: Topological Sort
The topological sorting algorithm: Again, identify the subset of vertices that have no incoming edge, select one of them, remove it and any outgoing edges, and put it in the output. C A F B D E

Graph Algorithms: Topological Sort
The topological sorting algorithm: Again, identify the subset of vertices that have no incoming edge, select one of them, remove it and any outgoing edges, and put it in the output. A F B C D E

Graph Algorithms: Topological Sort
The topological sorting algorithm: Again, identify the subset of vertices that have no incoming edge, select one of them, remove it and any outgoing edges, and put it in the output. A F B C D E

Graph Algorithms: Topological Sort
The topological sorting algorithm: Again, identify the subset of vertices that have no incoming edge, select one of them, remove it and any outgoing edges, and put it in the output. A F B C D E

Graph Algorithms: Topological Sort
The topological sorting algorithm: Again, identify the subset of vertices that have no incoming edge, select one of them, remove it and any outgoing edges, and put it in the output. A F B C D E

Graph Algorithms: Topological Sort
The topological sorting algorithm: Again, identify the subset of vertices that have no incoming edge, select one of them, remove it and any outgoing edges, and put it in the output. A F B C D E

Graph Algorithms: Topological Sort
The topological sorting algorithm: finished! B C A A F B C D E F D E

Graph Algorithms: Topological Sort
The topological sorting algorithm: Time bound? B C A A F B C D E F D E

Graph Algorithms: Topological Sort
The topological sorting algorithm: Time bound: Break down into total time to: Find vertices with no predecessors: ? Remove edges: ? Place vertices in output: ? B C A A F B C D E F D E

Graph Algorithms: Topological Sort
The topological sorting algorithm: Time bound: Break down into total time to: Find vertices with no predecessors: ? Remove edges: O(|E|) Place vertices in output: ? B C A A F B C D E F D E

Graph Algorithms: Topological Sort
The topological sorting algorithm: Time bound: Break down into total time to: Find vertices with no predecessors: ? Remove edges: O(|E|) Place vertices in output: O(|V|) B C A A F B C D E F D E

Graph Algorithms: Topological Sort
Find vertices with no predecessors: ? Assume an adjacency list representation: A B C D E F B D B C C A D E F E D E

Graph Algorithms: Topological Sort
The topological sorting algorithm: …and initialize and maintain for each vertex its no. of predecessors. A B C D E F B D 1 1 B 1 C C A 1 D E F 2 E 2 2 2 D E

Graph Algorithms: Topological Sort
Find vertices with no predecessors: ? Time for each vertex: O(|V|) B A B C D E F D B 1 C C A 1 D E F 2 E 2 D E

Graph Algorithms: Topological Sort
Find vertices with no predecessors: ? 2 Total time: O(|V| ) B A B C D E F D B 1 C C A 1 D E F 2 E 2 D E

Graph Algorithms: Topological Sort
The topological sorting algorithm: Time bound: Break down into total time to: Find vertices with no predecessors: O(|V| ) Remove edges: O(|E|) Place vertices in output: O(|V|) 2

Graph Algorithms: Topological Sort
The topological sorting algorithm: Time bound: Break down into total time to: Find vertices with no predecessors: O(|V| ) Remove edges: O(|E|) Place vertices in output: O(|V|) 2 2 Total: O(|V| + |E|)

Graph Algorithms: Topological Sort
The topological sorting algorithm: Time bound: Break down into total time to: Find vertices with no predecessors: O(|V| ) Remove edges: O(|E|) Place vertices in output: O(|V|) 2 2 Total: O(|V| + |E|) Too much!

Graph Algorithms: Topological Sort
The topological sorting algorithm: We need a faster way to do this step: Find vertices with no predecessors.

Graph Algorithms: Topological Sort
The topological sorting algorithm: Key idea: initialize and maintain a queue (or stack) holding pointers to the vertices with 0 predecessors A B C D E F B D 1 1 B 1 C C A 1 D E F 2 E 2 2 2 D E

Graph Algorithms: Topological Sort
The topological sorting algorithm: As each vertex is removed, update the predecessor counts, and for any vertex whose count has become zero, put it in the queue. A B C D E F B D 1 1 B 1 C C A 1 D E F 2 E 2 2 2 D E

Graph Algorithms: Topological Sort
The topological sorting algorithm: As each vertex is removed, update the predecessor counts, and for any vertex whose count has become zero, put it in the queue. No scan is required, so O(1). A B C D E F 1 B C C 1 D E F 1 E 1 2 2 D E Output: A

Graph Algorithms: Topological Sort
The topological sorting algorithm: As each vertex is removed, update the predecessor counts, and for any vertex whose count has become zero, put it in the queue. A B C D E F 1 B C C 1 D E 1 E 1 2 2 D E Output: A F

Graph Algorithms: Topological Sort
The topological sorting algorithm: As each vertex is removed, update the predecessor counts, and for any vertex whose count has become zero, put it in the queue. A B C D E F C D E 1 E 1 2 2 D E Output: A F B

Graph Algorithms: Topological Sort
The topological sorting algorithm: As each vertex is removed, update the predecessor counts, and for any vertex whose count has become zero, put it in the queue. A B C D E F E 1 1 D E Output: A F B C

Graph Algorithms: Topological Sort
The topological sorting algorithm: As each vertex is removed, update the predecessor counts, and for any vertex whose count has become zero, put it in the queue. A B C D E F E Output: A F B C D

Graph Algorithms: Topological Sort
The topological sorting algorithm: As each vertex is removed, update the predecessor counts, and for any vertex whose count has become zero, put it in the queue. Finished! A B C D E F Output: A F B C D E

Graph Algorithms: Topological Sort
The topological sorting algorithm: Time bound: Now the time for each part is Find vertices with no predecessors: O(|V|) Remove edges: O(|E|) Place vertices in output: O(|V|) Total: O(|V|+|E|) Linear in |V|+|E|. Much better!

Hashing The implementation of hash tables is called hashing.
Hashing is a technique used for performing insertions, deletions, and finds.

Binary Search Trees will give us a find operation or insert
operation in O(log n). There is a way to improve on this. We will create a table of size TableSize which we will index from 0 to TableSize - 1. Each element that we want to store has a “key” which is some portion of the data that we use to establish the order of the data items. We must have a function that can take a key as input and produce an integer in the range from 0 to TableSize - 1. This function is called a hash function.

Sample Hash functions If the key is an integer. One choice is to take h(key) = key % TableSize One of the things people do is to choose TableSize to be a prime number.

The hash function gives us an index into the array.
Open addressing In this method of implementing a hash table, we don’t have linked lists. Instead we just have an array of items to search. The hash function gives us an index into the array. In the event that collision occurs, there are two common strategies linear probing quadratic probing

Linear Probing Alice Ann Cynthia Jane Julia June Laura Sue Valerie
Vanessa