18_Topological_sort.pptx

# If we use an adjacency matrix and the memory

• 145

This preview shows pages 54–71. Sign up to view the full content.

if we use an adjacency matrix and the memory requirements is (| V |) A 1 B 1 C 0 D 2 E 4 F 2 G 1 H 0 I 1 J 1 K 1 L 2

This preview has intentionally blurred sections. Sign up to view the full version.

Analysis What happens if at some step, all remaining vertices have an in-degree greater than zero? There must be at least one cycle within that sub-set of vertices Consequence: we now have an (| V | + | E |) algorithm for determining if a graph has a cycle A 1 B 1 C 0 D 2 E 4 F 2 G 1 H 0 I 1 J 1 K 1 L 2
Implementation Thus, to implement a topological sort: Allocate memory for and initialize an array of in-degrees Create a queue and initialize it with all vertices that have in-degree zero While the queue is not empty: Pop a vertex from the queue Decrement the in-degree of each neighbor Those neighbors whose in-degree was decremented to zero are pushed onto the queue

This preview has intentionally blurred sections. Sign up to view the full version.

Implementation We will use an array implementation of our queue Because we place each vertex into the queue exactly once We must never resize the array We do no t have to worry about the queue cycling Most importantly, however, because of the properties of a queue When we finish, the underlying array stores the topological sort
Implementation The operations with our queue Initialization Type array[vertex_size()]; int ihead = 0, itail = -1; Testing if empty: ihead == itail + 1 For push ++itail; array[itail] = next vertex ; For pop Type current_top = array[ihead]; ++ihead;

This preview has intentionally blurred sections. Sign up to view the full version.

Example With the previous example, we initialize: The array of in-degrees The queue A 1 B 1 C 0 D 2 E 4 F 2 G 1 H 0 I 1 J 1 K 1 L 2 The queue is empty Queue:
Example Stepping through the array, push all source vertices into the queue A 1 B 1 C 0 D 2 E 4 F 2 G 1 H 0 I 1 J 1 K 1 L 2 The queue is empty Queue:

This preview has intentionally blurred sections. Sign up to view the full version.

Example Stepping through the table, push all source vertices into the queue A 1 B 1 C 0 D 2 E 4 F 2 G 1 H 0 I 1 J 1 K 1 L 2 C H The queue is empty Queue:
Example Pop the front of the queue A 1 B 1 C 0 D 2 E 4 F 2 G 1 H 0 I 1 J 1 K 1 L 2 C H Queue:

This preview has intentionally blurred sections. Sign up to view the full version.

Example Pop the front of the queue C has one neighbor: D A 1 B 1 C 0 D 2 E 4 F 2 G 1 H 0 I 1 J 1 K 1 L 2 C H Queue:
Example Pop the front of the queue C has one neighbor: D Decrement its in-degree A 1 B 1 C 0 D 1 E 4 F 2 G 1 H 0 I 1 J 1 K 1 L 2 C H Queue:

This preview has intentionally blurred sections. Sign up to view the full version.

Example Pop the front of the queue A 1 B 1 C 0 D 1 E 4 F 2 G 1 H 0 I 1 J 1 K 1 L 2 C H Queue:
Example Pop the front of the queue H has two neighbors: D and I A 1 B 1 C 0 D 1 E 4 F 2 G 1 H 0 I 1 J 1 K 1 L 2 C H Queue:

This preview has intentionally blurred sections. Sign up to view the full version.

Example Pop the front of the queue H has two neighbors: D and I Decrement their in-degrees A 1 B 1 C 0 D 0 E 4 F 2 G 1 H 0 I 0 J 1 K 1 L 2 C H Queue:
Example Pop the front of the queue H has two neighbors: D and I Decrement their in-degrees Both are decremented to zero, so push them onto the queue A 1 B 1 C 0 D 0 E 4 F 2 G 1 H 0 I 0 J 1 K 1 L 2 C H Queue:

This preview has intentionally blurred sections. Sign up to view the full version.

Example Pop the front of the queue H has two neighbors: D and I Decrement their in-degrees Both are decremented to zero, so push them onto the queue A 1 B 1 C 0 D 0 E 4 F 2 G 1 H 0 I 0 J 1 K 1 L 2 C H D I Queue:
Example Pop the front of the queue A 1 B 1 C 0 D 0 E 4 F 2 G 1 H 0 I 0 J 1 K 1 L 2 C H D I Queue:

This preview has intentionally blurred sections. Sign up to view the full version.

This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern