0% found this document useful (0 votes)
6 views

Algorithms

Uploaded by

vabb22ise
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Algorithms

Uploaded by

vabb22ise
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Warshall's and Floyd's Algorithms

Warshall's Algorithm
ALGORITHM Warshall(A[l..n, l..n])
//Implements Warshall's algorithm for computing the transitive closure
//Input: The adjacency matrix A of a digraph with n vertices
//Output: The transitive closure of the digraph
R(O) <- A
fork <- 1 to n do
fori <-Hondo
for j <- 1 to n do
return R(n)
time efficiency is only in O(n3).

Floyd's Algorithm for the All-Pairs Shortest-Paths Problem

ALGORITHM Floyd(W[l..n, l..n])


//Implements Floyd's algorithm for the all-pairs shortest -paths problem
//Input: The weight matrix W of a graph with no negative-length cycle
//Output: The distance matrix of the shortest paths' lengths
D <-- W /lis not necessary if W can be overwritten
for k <-- 1 to n do
for i <-- 1 to n do
for j <-- 1 to n do
D[i. j] <-- min{D[i, j], D[i, k] + D[k, j])

return D
time efficiency is only in O(n3).

The Knapsack Problem

ALGORITHM MFKnapsack(i, j)
//Implements the memory function method for the knapsack problem
//Input: A nonnegative integer i indicating the number of the first
II items being considered and a nonnegative integer j indicating
II the knapsack's capacity
//Output: The value of an optimal feasible subset of the first i items
//Note: Uses as global variables input arrays Weights[l .. n ], Values[l .. n ],
//and table V[O .. n, O .. W] whose entries are initialized with -l's except for
//row 0 and column 0 initialized with O's
ifV[i,j]<O
if j < Weights[i]
value<-- MFKnapsack(i- 1, j)
else
value<-- max(MFKnapsack(i- 1, j),
V[i, j] <--value
return V[i, j]

The time efficiency and space efficiency of this algorithm are both in O (n W).

Prim's Algorithm
ALGORITHM Prim(G)
//Prim's algorithm for constructing a minimum spanning tree
//Input: A weighted connected graph G = (V, E)
//Output: Et, the set of edges composing a minimum spanning tree of G
Vt <- { v0] //the set of tree vertices can be initialized with any vertex
Et <- 0
for i <- 1 to IV I - 1 do
find a minimum-weight edge e* = (v*, u') among all the edges (v, u)
such that vis in Vr and u is in V - Vr
Vt <- Vt U {u*]
Et <- Et U {e*]
return Et

If a graph is represented by its adjacency lists and the priority queue is implemented as a
min-heap, the running time of the algorithm is in 0(|E I log |VI).

Kruskal's Algorithm

ALGORITHM Kruskal(G)
// Kruskal's algorithm for constructing a minimum spanning tree
// Input: A weighted connected graph G = (V, E)
// Output: Er, the set of edges composing a minimum spanning tree of G
sort E in nondecreasing order of the edge weights w(e)
Er <- {} // initialize the set of tree edges
ecounter <- 0 // initialize the count of edges in Er
k <- 0 // initialize the index for processed edges
while ecounter < |V| - 1 do
k <- k + 1
Let (u, v) be the k-th edge in sorted order by weight
if adding (u, v) to Er does not form a cycle then
Er <- Er U {(u, v)}
ecounter <- ecounter + 1
return Er

The time complexity of Kruskal's Algorithm is O(ElogE), where E is the number of edges in the
graph. This complexity is because the algorithm uses a priority queue with a time complexity of
O(logE). However, the space complexity of the algorithm is O(E), which is relatively high.

Dijkstra's Algorithm

ALGORITHM Dijkstra(G, s)
//Dijkstra's algorithm for single-source shortest paths
//Input: A weighted connected graph G = (V, E) with nonnegative weights
II and its vertex s
//Output: The length d" of a shortest path from s to v
II and its penultimate vertex p" for every vertex v in V
lnitialize(Q) //initialize vertex priority queue to empty
for every vertex v in V do
dv +- oo; Pv +-- null
Insert(Q, v, d") //initialize vertex priority in the priority queue
d, <-- 0; Decrease(Q, s, d,) //update priority of s with d,
Vr <-- 0
fori <-Oto IVI-1do
u* <-- DeleteMin(Q) //delete the minimum priority element
Vr <-- Vr U {u*)
for every vertex u in V - Vr that is adjacent to u* do
ifdu*+w(u*, u) <du
du +-- du* + w(u*, u); p11 +-- u*
Decrease(Q, u, d,)

The time efficiency of Dijkstra's algorithm depends on the data structures used for implementing
the priority queue and for representing an input graph it-self. For the reasons explained in the
analysis of Prim's algorithm in Section 9.1, it is in O(| V |^2) for graphs represented by their
weight matrix and the priority queue implemented as an unordered array. For graphs represented
by their adjacency lists and the priority queue implemented as a min-heap, it is in O(IE|log lVI).

Huffman Trees
Huffman's Algorithm
Step 1 Initialize n one-node trees and label them with the characters of the
alphabet. Record the frequency of each character in its tree's root to
indicate the tree's weight. (More generally, the weight of a tree will be
equal to the sum of the frequencies in the tree's leaves.)

Step 2 Repeat the following operation until a single tree is obtained. Find two
trees with the smallest weight (ties can be broken arbitrarily, but see
Problem 2 in the exercises). Make them the left and right subtree of
a new tree and record the sum of their weights in the root of the new
tree as its weight.
A tree constructed by the above algorithm is called a Huffman tree. It
defines-in the manner described-a Huffman code.

Time complexity is O (n logn).

Topological Sorting

Algorithm TopoSource(G(V, E))


make empty Container of vertices, C
make empty List of vertices, S
for each vertex v in V do
incounter(v) ← in-deg(v)
if incounter(v) == 0 then C.add(v)
while C is not empty do
u ← C.remove()
S.add(u)
for each edge (u, w) do // edge is out from u to w
incounter(w) ← incounter(w)-1
if incounter(w) == 0 then C.add(w)
if L.size() == V.size() then return S
else return "G has a cycle"

The time complexity of performing the topological sort is O(V+E), where V is the number of
vertices in the graph.

You might also like