Docsity

Topological Sort Algorithm and Implementation - Prof. Chung-Chih Li, Study notes of Information Technology

Toggle sidebar
bg1
1
12/3/2007 ITK 279 1
Topological Sort
(
topological order
)
Let G = (V, E) be a directed graph
with |V| = n.
1. { v
1
, v
2
,.... v
n
} = V, and
2. for every iand jwith 1 ≤i < j ≤n, (v
j
, v
i
) ∉E.
17 6 50 3 42
A Topological Sort is a sequence v
1
, v
2
,.... v
n
, such that
1
7
6
5
0
3
4
2
There are more than
one such order in this
graph!!
12/3/2007 ITK 279 2
If the graph is not acyclic, then there is no
topological sort for the graph.
2 4
3 5
7
1
6
9
0
8
Not an acyclic Graph
No way to arrange vertices in
this cycle without pointing
backwards
12/3/2007 ITK 279 3
Algorithm for finding Topological Sort
Principle: vertex with in-degree 0 should be listed first.
Algorithm:
Repeat the following steps until no more vertex left:
1. Find a vertex vwith in-degree 0;
Input G=(V,E)
2. Remove vfrom V and update E
3. Process v
If can’t find such vand V is not empty
then stop the algorithm; this graph is not acyclic
217 2 7
1
12/3/2007 ITK 279 4
Topological Sort (
topological order
) of an acyclic graph
17 6 50 3 42
1
7
6
0
3
4
5
2
12/3/2007 ITK 279 5
2 4
3 5
7
1
6
9
0
8
the graph is not acyclic
Fail to find a topological sort (
topological order
)
1 2 8
12/3/2007 ITK 279 6
2 4
3 5
7
1
6
9
0
8
This graph is acyclic
Topological Sort (
topological order
) of an acyclic graph
0285437961
pf3

Partial preview of the text

Download Topological Sort Algorithm and Implementation - Prof. Chung-Chih Li and more Study notes Information Technology in PDF only on Docsity!

12/3/2007 ITK 279 1

Topological Sort

(topological order)

Let G = (V, E) be a directed graph

with |V| = n.

  1. { v 1 , v 2 ,.... vn } = V, and
  2. for every i and j with 1 ≤ i < j ≤ n, (vj , vi) ∉ E.

A Topological Sort is a sequence v 1 , v 2 ,.... vn, such that

There are more than

one such order in this

graph!!

12/3/2007 ITK 279 2

If the graph is not acyclic, then there is no

topological sort for the graph.

Not an acyclic Graph

No way to arrange vertices in

this cycle without pointing

backwards

12/3/2007 ITK 279 3

Algorithm for finding Topological Sort

Principle: vertex with in-degree 0 should be listed first.

Algorithm:

Repeat the following steps until no more vertex left:

1. Find a vertex v with in-degree 0;

Input G=(V,E)

2. Remove v from V and update E

3. Process v

If can’t find such v and V is not empty

then stop the algorithm; this graph is not acyclic

12/3/2007 ITK 279 4

Topological Sort (topological order) of an acyclic graph

12/3/2007 ITK 279 5

the graph is not acyclic

Fail to find a topological sort (topological order)

12/3/2007 ITK 279 6

This graph is acyclic

Topological Sort (topological order) of an acyclic graph

12/3/2007 ITK 279 7

Topological Sort in C++

// return an array where the first entry is the size of g // or 0 if g is not acyclic; int * graph::topsort(const Graph & g) { int *topo; Graph tempG = g; topo = new int[g.size()+1]; topo[0] = g.size(); for (int i=1; i<=topo[0]; i++) { int v = findVertexwithZeroIn(tempG); if (v == -1) { // g is not acyclic delete [] topo; topo = new int[1]; topo[0] = 0; return topo; } topo[i] = v; remove_v(tempG, v); } return topo; }

12/3/2007 ITK 279 8

Edsger Wybe Dijkstra 1930-

Dijkstra Algorithm:

To find the shortest path between two vertices in a weighted graph.

  • Algorithm design
  • Programming languages
  • Operating systems
  • Distributed processing
  • Formal specification and verification

1972 Turing Award

12/3/2007 ITK 279 9

1. Back Tracking....

2. Greedy Algorithms: Algorithms that make decisions

based on the current information; and once a decision is

made, the decision will not be revised

3. Divide and Conquer....

4. Dynamic Programming.....

Common Algorithm Techniques

Problem: Minimized the number of coins for change.

In real life, we can use a greedy algorithm to obtain the

minimum number of coins. But in general, we need dynamic

programming to do the job

12/3/2007 ITK 279 10

Greedy Algorithms  Dynamic Programming

Problem: Minimized the number of coins for 66 ¢

Greedy method Greedy method Dynamic method

12/3/2007 ITK 279 11

Unweighted Graphs

Weighted Graphs

Starting Vertex

w Final Vertex

Starting Vertex

w (^) Final Vertex

Both are using the

greedy algorithm.

Finding the shortest path between two vertices

12/3/2007 ITK 279 12

Starting Vertex

Final Vertex

2 ,1^2 ,

Construct a shortest path Map

Starting Vertex

n Final Vertex

x w-1,y

w,x