Topological Sort and Shortest Path Algorithms, Study notes of Computer Science

Information on two important graph algorithms: topological sort and shortest path. The first algorithm constructs an ordering of vertices in a directed acyclic graph such that there is no path from a vertex with a larger index to a vertex with a smaller index. The second algorithm finds the shortest weighted path from a single distinguished vertex to every other vertex in a graph. Pseudocode for both algorithms and discusses their complexities.

Typology: Study notes

Pre 2010

Uploaded on 07/29/2009

koofers-user-f02
koofers-user-f02 🇺🇸

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
4/12/2004 CS216, Spring 2004 1
Topological Sort
Given a directed acyclic graph,
construct an ordering of the vertices
such that if there is a path from vito vj,
then vjappears after viin the ordering.
indegree of v: # of edges (u, v).
v3
v6
v1
v2
v4
v7
v5
v8
4/12/2004 CS216, Spring 2004 2
1
3
4
2
4/12/2004 CS216, Spring 2004 3
void Graph::toposort(){
Vertex v, w;
for (int counter=0; counter < NUM_VERTICES;
counter++){
v = findNewVertexOfDegreeZero();
if (v == NOT_A_VERTEX)
throw CycleFound();
v.topologicalNum = counter;
for each w adjacent to v
w.indegree--;
}
}
4/12/2004 CS216, Spring 2004 4
void Graph::topsort(){
Queue q(NUM_VERTICES);
int counter = 0;
Vertex v, w;
q.makeEmpty();
for each vertex v
if (v.indegree == 0)
q.enqueue(v);
while (!q.isEmpty()){
v = q.dequeue();
v.topologicalNum = ++counter;
for each w adjacent to v
if (--w.indegree == 0)
q.enqueue(w);
}
if (counter != NUM_VERTICES)
throw CycleFound();
}
intialize the
queue
get a vertex with
indegree 0
insert new
eligible
vertices
4/12/2004 CS216, Spring 2004 5
Shortest Path Algorithms
(“single-source” shortest path)
Given a graph G= (V, E) and a single
distinguished vertex s, find the shortest
weighted path from sto every other
vertex in G.
weighted path length of v1, v2, … , vN:
=
+
1
1
1,
c
N
i
ii , where ci,j is the cost of edge (vi, vj)
4/12/2004 CS216, Spring 2004 6
Unweighted Shortest Path
Special case of the weighted problem:
all weights are 1.
Solution: breadth-first search. Similar to
level-order traversal for trees.
v3
v6
v1
v2
v4v7
v5
v0
s
pf2

Partial preview of the text

Download Topological Sort and Shortest Path Algorithms and more Study notes Computer Science in PDF only on Docsity!

4/12/2004 CS216, Spring 2004 1

Topological Sort

• Given a directed acyclic graph,

construct an ordering of the vertices

such that if there is a path from v i to v j ,

then v j appears after v i in the ordering.

• indegree of v: # of edges (u, v).

v 3

v 1 v^6

v 2

v 4

v 7

v 5

v 8

4/12/2004 CS216, Spring 2004 2

4/12/2004 CS216, Spring 2004 3

void Graph::toposort(){

Vertex v, w;

for (int counter=0; counter < NUM_VERTICES;

counter++){

v = findNewVertexOfDegreeZero();

if (v == NOT_A_VERTEX)

throw CycleFound();

v.topologicalNum = counter;

for each w adjacent to v

w.indegree--;

4/12/2004 CS216, Spring 2004 4

void Graph::topsort(){ Queue q(NUM_VERTICES); int counter = 0; Vertex v, w;

q.makeEmpty(); for each vertex v if (v.indegree == 0) q.enqueue(v); while (!q.isEmpty()){ v = q.dequeue(); v.topologicalNum = ++counter; for each w adjacent to v if (--w.indegree == 0) q.enqueue(w); } if (counter != NUM_VERTICES) throw CycleFound(); }

intialize the

queue

get a vertex with

indegree 0

insert new

eligible

vertices

4/12/2004 CS216, Spring 2004 5

Shortest Path Algorithms

• (“single-source” shortest path)

• Given a graph G = (V, E) and a single

distinguished vertex s , find the shortest

weighted path from s to every other

vertex in G.

weighted path length of v 1 , v 2 , … , v N:

c , 1

N

i

i i , where^ c^ i,j is the cost of edge^ (v^ i , v^ j )

4/12/2004 CS216, Spring 2004 6

Unweighted Shortest Path

• Special case of the weighted problem:

all weights are 1.

• Solution: breadth-first search. Similar to

level-order traversal for trees.

v 3

v 6

v 1

v 2

v 4

v 7

v 5

v 0

s

4/12/2004 CS216, Spring 2004 7

void Graph::unweighted (Vertex s){

Queue q(NUM_VERTICES);

Vertex v, w;

q.enqueue(s);

s.dist = 0;

while (!q.isEmpty()){

v = q.dequeue();

for each w adjacent to v

if (w.dist == INFINITY){

w.dist = v.dist + 1;

w.path = v;

q.enqueue(w);

each edge examined

at most once – if adjacency

lists are used

each vertex enqueued

at most once

total running time: O( )

4/12/2004 CS216, Spring 2004 8

Weighted Shortest Path

• no negative weight edges.

• Dijkstra’s algorithm : uses similar ideas as the

unweighted case.

Greedy algorithms:

do what seems to be best at every decision

point.

S

“ known ”

s V - S

“ unknown ”

v

4/12/2004 CS216, Spring 2004 9

v 3

v 6

v 1

v 2 v 4

v 5

s v 0

v

v

v

v

v

v

Know Dist path

n

V

4/12/2004 CS216, Spring 2004 10

void Graph::dijkstra(Vertex s){

Vertex v,w;

s.dist = 0;

while (there exist unknown vertices, find the

one v with the smallest distance)

v.known = true;

for each w adjacent to v

if (!w.known)

if (v.dist + Cost_VW < w.dist){

decrease(w.dist to= v.dist + Cost_VW);

w.path = v;

4/12/2004 CS216, Spring 2004 11

Analysis

• How long does it take to find the

smallest unknown distance?

– simple scan using an array:

– binary heap:

• Total running time:

– simple scan:

– binary heap: