If we use an adjacency matrix and the memory

Info icon 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
Image of page 54

Info icon 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
Image of page 55
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
Image of page 56

Info icon 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
Image of page 57
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;
Image of page 58

Info icon 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:
Image of page 59
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:
Image of page 60

Info icon 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:
Image of page 61
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:
Image of page 62

Info icon 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:
Image of page 63
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:
Image of page 64

Info icon 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:
Image of page 65
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:
Image of page 66

Info icon 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:
Image of page 67
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:
Image of page 68

Info icon 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:
Image of page 69
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:
Image of page 70

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

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern