Download presentation

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.

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

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

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

9
**Graph Algorithms: Topological Sort**

The topological sorting algorithm: Identify the subset of vertices that have no incoming edge. B C A 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?) B C A 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.) B C A 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. B C A F D E

13
**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

14
**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

15
**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

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

25
**Graph Algorithms: Topological Sort**

The topological sorting algorithm: finished! B C A A F B C D E F D E

26
**Graph Algorithms: Topological Sort**

The topological sorting algorithm: Time bound? B C A A F B C D E 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: ? B C A A F B C D E 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: ? B C A A F B C D E 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|) B C A A F B C D E F D E

30
**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

31
**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

32
**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

33
**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

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

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 2 Total: O(|V| + |E|) 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 D E F B D 1 1 B 1 C C A 1 D E F 2 E 2 2 2 D E

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 D E F B D 1 1 B 1 C C A 1 D E F 2 E 2 2 2 D E

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

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. A B C D E F 1 B C C 1 D E 1 E 1 2 2 D E 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. A B C D E F C D E 1 E 1 2 2 D E 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. A B C D E F E 1 1 D E 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. A B C D E F E 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! A B C D E F 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
**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

51
**Linear Probing Alice Ann Cynthia Jane Julia June Laura Sue Valerie**

Vanessa

52
**Instead of using linear probing, we can adopt a different **

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
keys: 89, 18, 49, 58, 69 Empty table After 89 After 18 After 49 After 58 After 69 49 49 49 1 2 58 58 3 69 4 5 6 7 8 18 18 18 18 9 89 89 89 89 89 Quadratic Probing

54
THE END

Similar presentations

Presentation is loading. Please wait....

OK

Spring 2015 Lecture 11: Minimum Spanning Trees

Spring 2015 Lecture 11: Minimum Spanning Trees

© 2018 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on ministry of corporate affairs new delhi Ppt on health tourism in india Ppt on pre-ignition piston Ppt on bmc remedy action Seminar ppt on brain computer interface Ppt on question tags worksheet Ppt on non-biodegradable waste picture Ppt on bank lending 2016 Ppt on fibonacci numbers and the golden Download ppt on crop production and management