Sei sulla pagina 1di 3

C-Routine For Topological Sort

void Topsort (Graph G) { Queue Q ; int counter = 0; Vertex V, W ; Q = CreateQueue (NumVertex); Makeempty (Q); for each vertex V if (indegree [V] = = 0) Enqueue (V, Q); while (! IsEmpty (Q)) { V = Dequeue (Q); TopNum [V] = + + counter; for each W adjacent to V if (--Indegree [W] = = 0) Enqueue (W, Q); } if (counter ! = NumVertex) Error (" Graph has a cycle"); DisposeQueue (Q); /* Free the Memory */ } Note : Enqueue (V, Q) implies to insert a vertex V into the queue Q. Dequeue (Q) implies to delete a vertex from the queue Q. TopNum [V] indicates an array to place the topological numbering.

Shortest path algorithm


The Shortest path algorithm determines the minimum cost of the path from source to every other vertex. The cost of the path V1, V2, --VN is . This is referred as weighted path length. The

unweighted path length is merely the number of the edges on the path, namely N - 1. Two types of shortest path problems, exist namely, 1. The single source shortest path problem 2. The all pairs shortest path problem The single source shortest path algorithm finds the minimum cost from single source vertex to all other vertices. Dijkstra's algorithm is used to solve this problem which follows the greedy technique. All pairs shortest path problem finds the shortest distance from each vertex to all other vertices. To solve this problem dynamic programming technique known as floyd's algorithm is used. These algorithms are applicable to both directed and undirected weighted graphs provided that they do not contain a cycle of negative length. Single Source Shortest Path Given an input graph G = (V, E) and a distinguished vertex S, find the shortest path from S to every other vertex in G. This problem could be applied to both weighted and unweighted graph. 2 .Dijkstra's Algorithm The general method to solve the single source shortest path problem is known as Dijkstra's algorithm. This is applied to the weighted graph G.
Dijkstra's algorithm is the prime example of Greedy technique, which generally solve a problem in stages by doing what appears to be the best thing at each stage. This algorithm proceeds in stages, just like the unweighted shortest path algorithm. At each stage, it selects a vertex v, which has the smallest dv among all the unknown vertices, and declares that as the shortest path from S to V and mark it to be known. We should set dw = dv + Cvw, if the new value for dw would be an improvement.

ROUTINE FOR ALGORITHM Void Dijkstra (Graph G, Table T) { int i ; vertex V, W; Read Graph (G, T) /* Read graph from adjacency list */ /* Table Initialization */ for (i = 0; i < Numvertex; i++) { T [i]. known = False; T [i]. Dist = Infinity; T [i]. path = NotA vertex; } T [start]. dist = 0; for ( ; ;) { V = Smallest unknown distance vertex; if (V = = Not A vertex) break ; T[V]. known = True; for each W adjacent to V if ( ! T[W]. known) { T [W]. Dist = Min [T[W]. Dist, T[V]. Dist + CVW] T[W]. path = V; } } }

Potrebbero piacerti anche