2.2 topological sort 02

Krish_ver2 1,018 views 10 slides May 07, 2015
Slide 1
Slide 1 of 10
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10

About This Presentation

Data Structure-topological sort 02


Slide Content

TOPOLOGICAL SORT

Definition
A topological sort of a Directed Acylic Graph
(DAG) is a linear ordering of all its vertices such
that if there is a path from v
i
to v
k
then v
i
appears
before v
k
in the ordering.

Algorithm for finding a topological sort
Initialize TSArray to null
for (i=1; i<=numofvertices; i++)
v = find vertex of degree zero
if v is null
 Graph has a cycle. Exit
else
 add v to TSArray
 remove v and all its outgoing vertices
Print TSArray

Algorithm for enumerating all topological sorts
TopologicalSorts()
compute the indegree count array, Indegree[ ]
initialize TSArray[ ] to null
for each vertex v
if indegree[v] == zero
push Indegree[ ], TSArray[ ], v
while (stack not empty)
pop Indegree[ ], TSArray[ ], v
add v to TSArray
for each x adjacent to v, Indegree[x]--
for each vertex v
if indegree[v] == zero
push Indegree[ ], TSArray[ ], v

Find out the ordering
1 2
4
6 7
53

Solution
•1 2 5 4 3 7 6
•1 2 5 4 7 3 6
•Find out other possibilities.

How many squares can you create in this figure by connecting any 4
dots (the corners of a square must lie upon a grid dot?
TRIANGLES:
How many triangles are located in the image below?

There are 11 squares total; 5 small, 4 medium, and 2 large.
27 triangles. There are 16 one-cell triangles, 7 four-cell triangles, 3 nine-cell triangles, and
1 sixteen-cell triangle.

There are 11 squares total; 5 small, 4 medium, and 2 large.
27 triangles. There are 16 one-cell triangles, 7 four-cell triangles, 3 nine-cell triangles, and
1 sixteen-cell triangle.