Presentation is loading. Please wait.

Presentation is loading. Please wait.

Topological Sort and Hashing Presented by Phillip Nguyen Truong Nguyen.

Similar presentations


Presentation on theme: "Topological Sort and Hashing Presented by Phillip Nguyen Truong Nguyen."— Presentation transcript:

1 Topological Sort and Hashing Presented by Phillip Nguyen Truong Nguyen

2 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

3 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.

4

5 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. A B C F D E

6 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. A B C F D EAD E F BC

7 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. A B C F D EAD E F BC Any linear ordering in which all the arrows go to the right.

8 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. A B C F D FAD E E BC Any linear ordering in which all the arrows go to the right. This is not a topological ordering.

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

10 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?) A B C F D E

11 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.) A B C F D E

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

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

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

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

16 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 B C F D E

17 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 B C F D E

18 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. AB C F D E

19 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. AB C F D E

20 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. ABCF D E

21 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. ABCF D E

22 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. ABCFD E

23 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. ABCFD E

24 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. ABCFDE

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

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

27 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: ? ABCFDE A B C F D E

28 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: ? ABCFDE A B C F D E

29 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|) ABCFDE A B C F D E

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

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

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

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

34 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

35 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 Total: O(|V| + |E|) 2

36 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 Total: O(|V| + |E|) 2 Too much!

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

38 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 F D E ABCDEFABCDEF B D E E D C 0 0 1 2 1 2 0 1 0 2 2 1

39 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 F D E ABCDEFABCDEF B D E E D C 0 0 1 2 1 2 0 1 0 2 2 1

40 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. B C D E ABCDEFABCDEF E E D C 0 0 1 1 2 0 0 0 1 2 1 F Output: A No scan is required, so O(1).

41 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. B C D E ABCDEFABCDEF E E D C 0 1 1 2 0 0 0 1 2 1 Output: A F

42 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. C D E ABCDEFABCDEF E E D 1 0 2 0 0 0 1 2 0 Output: A F B

43 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. D E ABCDEFABCDEF E 0 1 0 0 0 0 1 0 Output: A F B C

44 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. E ABCDEFABCDEF 0 0 0 0 0 0 0 Output: A F B C D

45 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! ABCDEFABCDEF 0 0 0 0 0 0 Output: A F B C D E

46 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!

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

48 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.

49 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.

50 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

51 Linear Probing Ann JaneJulia Cynthia Alice June Laura Sue ValerieVanessa

52 Quadratic probing Instead of using linear probing, we can adopt a different strategy called quadratic probing. If a collision occurs at a location h(x), then we try h(x) + 1,then h(x) + 4, h(x) + 9,... (modulo the size of the table). The major result about quadratic probing is: Theorem. If quadratic probing is used and the table size is prime, then a new element can always be inserted if the table is at least half empty.

53 Empty table After 89 After 18 After 49 After 58 After 69 0 1 2 3 4 5 6 7 8 9 89 18 49 58 69 keys: 89, 18, 49, 58, 69 Quadratic Probing

54 THE END


Download ppt "Topological Sort and Hashing Presented by Phillip Nguyen Truong Nguyen."

Similar presentations


Ads by Google