Download - 15.082 and 6.855J

Transcript
Page 1: 15.082 and 6.855J

1

15.082 and 6.855J

Topological Ordering

Page 2: 15.082 and 6.855J

2

Preliminary to Topological Sorting

LEMMA. If each node has at least one arc going out, then the first inadmissible arc of a depth first search determines a directed cycle.

COROLLARY 1. If G has no directed cycle, then there is a node in G with no arcs going. And there is at least one node in G with no arcs coming in.

COROLLARY 2. If G has no directed cycle, then one can relabel the nodes so that for each arc (i,j), i < j.

1 4 6 7 3

Page 3: 15.082 and 6.855J

3

Initialization

1

2

4

5 3

6

7

8

next 0

1 2 3 4 5 6 7 8

2 2 3 2 1 1 0 2

Node

Indegree

Determine the indegree of each node

LIST is the set of nodes with indegree of 0.

“Next” will be the label of nodes in the topological order.

LIST

7

Page 4: 15.082 and 6.855J

4

Select a node from LIST

next 0

1 2 3 4 5 7 8

2 2 3 2 1 0 2

Node

Indegree

Select a node from LIST and delete it.

LIST

7

next := next +1order(i) := next;

update indegrees

update LIST1

1

1

2

4

5 3

6

7

8

7

015

6

1

Page 5: 15.082 and 6.855J

5

Select a node from LIST

next 0

1 2 3 5 7 8

2 2 3 1 1 0 2

Node

Indegree

Select a node from LIST and delete it.

LIST

7

next := next +1order(i) := next;

update indegrees

update LIST1

1

1

2

4

5 3

6

7

8

7

0 5

5

2

1

6

0

4

210

2

4

6

Page 6: 15.082 and 6.855J

6

Select a node from LIST

next 0

1 2 3 5 7 8

2 2 3 1 1 0 2

Node

Indegree

Select a node from LIST and delete it.

LIST

7

next := next +1order(i) := next;

update indegrees

update LIST1

1

1

2

4

5 3

6

7

8

7

0 5

5

2

1

6

0

4

210

2

4

6

6

3

01

2

3

Page 7: 15.082 and 6.855J

7

Select a node from LIST

next 0

2 3 5 7 8

2 2 3 1 1 0 2

Node

Indegree

Select a node from LIST and delete it.

LIST

7

next := next +1order(i) := next;

update indegrees

update LIST1

1

1

2

4

5 3

6

7

8

7

0 5

5

2

1

6

0

4

210

2

4

6

6

3

0

2

2

1

1

4

0

1

3

4

Page 8: 15.082 and 6.855J

8

Select a node from LIST

next 0

2 3 5 7 8

2 2 3 1 1 0 2

Node

Indegree

Select a node from LIST and delete it.

LIST

7

next := next +1order(i) := next;

update indegrees

update LIST1

1

1

2

4

5 3

6

7

8

7

0 5

5

2

1

6

0

4

210

2

4

6

6

3

0

2

2

1

1

4

0

1

3

4

1

5

5

2 1

Page 9: 15.082 and 6.855J

9

Select a node from LIST

next 0

2 5 7 8

2 2 3 1 1 0 2

Node

Indegree

Select a node from LIST and delete it.

LIST

7

next := next +1order(i) := next;

update indegrees

update LIST1

1

1

2

4

5 3

6

7

8

7

0 5

5

2

1

6

0

4

210

2

46

6

3

0 2

2

1

1

4

0 1

3

4

1

5

5

1

4

3

2

6

01

6

8

Page 10: 15.082 and 6.855J

10

Select a node from LIST

next 0

2 5 7 8

2 2 3 1 1 0 2

Node

Indegree

Select a node from LIST and delete it.

LIST

next := next +1order(i) := next;

update indegrees

update LIST1

1

1

2

4

5 3

6

7

8

7

0

5

2

1

6

0

4

210

2

6

3

0

2

1

1

4

0

3

4

1

5

5

1

4

3

2

6

01

6

8

7

7

0

83

Page 11: 15.082 and 6.855J

11

Select a node from LIST

next 0

2 5 7 8

2 2 3 1 1 0 2

Node

Indegree

Select a node from LIST and delete it.

LIST

next := next +1order(i) := next;

update indegrees

update LIST1

1

1

2

4

5 3

6

7

8

7

0

5

2

1

6

0

4

210

2

6

3

0

2

1

1

4

0

3

4

1

5

5

1

4

3

2

6

01

6

8

7

7

0

3

8

8

List is empty.

The algorithm terminates with a topological order of the nodes

3